Skip to content

Commit

Permalink
sanitize whitespaces from productName
Browse files Browse the repository at this point in the history
  • Loading branch information
hilpitome committed Feb 27, 2024
1 parent 157a72a commit 2742a06
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.opensrp.repository.ProductCatalogueRepository;
import org.opensrp.repository.postgres.mapper.custom.CustomProductCatalogueMapper;
import org.opensrp.search.ProductCatalogueSearchBean;
import org.opensrp.util.Utils;
import org.smartregister.domain.ProductCatalogue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -225,7 +226,7 @@ private org.opensrp.domain.postgres.ProductCatalogue convert(ProductCatalogue pr
org.opensrp.domain.postgres.ProductCatalogue pgProductCatalogue = new org.opensrp.domain.postgres.ProductCatalogue();
pgProductCatalogue.setUniqueId(primaryKey);
pgProductCatalogue.setJson(productCatalogue);
pgProductCatalogue.setProductName(productCatalogue.getProductName());
pgProductCatalogue.setProductName(Utils.replaceConsecutiveSpaces(productCatalogue.getProductName()));
pgProductCatalogue.setServerVersion(productCatalogue.getServerVersion());

return pgProductCatalogue;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/opensrp/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public static void closeCloseable(Closeable closeable) {
logger.error(e.getMessage(), e);
}
}

public static String replaceConsecutiveSpaces(String input) {
// Use regular expression to replace consecutive spaces with a single space
return input.trim().replaceAll("\\s+", " ");
}

public static class DatabaseConnectionParams {

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/opensrp/util/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,19 @@ public void testGetXlsToJsonForInvalidXls() throws IOException, JSONException {
Utils.getXlsToJson(path);
}

@Test
public void testReplaceConsecutiveSpaces(){
// Test string with spaces in between words
String inputString = "Medicines (PPOS)";
String modifiedString = Utils.replaceConsecutiveSpaces(inputString);
assertEquals("Medicines (PPOS)", modifiedString);
// Test string with space after words
inputString = "Medicines (PPOS) ";
modifiedString = Utils.replaceConsecutiveSpaces(inputString);
assertEquals("Medicines (PPOS)", modifiedString);
// Test string with space before words
inputString = "Medicines (PPOS)";
modifiedString = Utils.replaceConsecutiveSpaces(inputString);
assertEquals("Medicines (PPOS)", modifiedString);
}
}

0 comments on commit 2742a06

Please sign in to comment.