Skip to content

Commit

Permalink
[Rahul] | BAH-3583 | Fix. Incorrect Alpha Numeric String Parsing (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahu1ramesh committed Feb 20, 2024
1 parent a38a4a1 commit c60fdac
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
5 changes: 5 additions & 0 deletions openerp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
<build>
<finalName>openerp-client</finalName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import freemarker.template.Template;
import org.apache.commons.lang3.math.NumberUtils;
import org.bahmni.openerp.web.OpenERPException;
import org.bahmni.openerp.web.config.FreeMarkerConfig;
import org.bahmni.openerp.web.request.OpenERPRequest;
Expand Down Expand Up @@ -53,10 +54,21 @@ private static HashMap<String, Object> getParameters(OpenERPRequest openERPReque
}

private static Object parseParameterValue(String value) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(value, Object.class);
} catch (Exception e) {
if (value.startsWith("{") && value.endsWith("}")) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(value, Object.class);
} catch (Exception e) {
return value;
}
} else {
if(NumberUtils.isCreatable(value)){
try {
return NumberUtils.createNumber(value);
} catch (Exception e) {
return value;
}
}
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.bahmni.openerp.web.request.builder;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bahmni.openerp.web.request.OpenERPRequest;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -227,6 +223,54 @@ public void shouldEscapeHindiCharactersWhenRESTRequestIsCreated() throws Excepti
comparingStringWithoutSpaces(requestXml, expected);
}

@Test
public void shouldReturnStringEvenWhenRESTRequestStartsWithANumber() throws Exception {

String name = "6 - DENTAL - Surgical Extraction of tooth including LA - 1200 - D";
String expected = "{\"name\":\"6 - DENTAL - Surgical Extraction of tooth including LA - 1200 - D\"}";

Parameter parameter = new Parameter("name", name, "string");
ArrayList<Parameter> parameters = new ArrayList<Parameter>();
parameters.add(parameter);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);

String requestXml = RequestBuilder.buildNewRestRequest(request);

comparingStringWithoutSpaces(requestXml, expected);
}

@Test
public void shouldReturnNumberWhenRESTRequestIsANumber() throws Exception {

String number = "62380";
String expected = "{\"number\":62380}";

Parameter parameter = new Parameter("number", number, "string");
ArrayList<Parameter> parameters = new ArrayList<Parameter>();
parameters.add(parameter);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);

String requestXml = RequestBuilder.buildNewRestRequest(request);

comparingStringWithoutSpaces(requestXml, expected);
}

@Test
public void shouldReturnNumberWhenRESTRequestIsADecimal() throws Exception {

String number = "62380.9";
String expected = "{\"number\":62380.9}";

Parameter parameter = new Parameter("number", number, "string");
ArrayList<Parameter> parameters = new ArrayList<Parameter>();
parameters.add(parameter);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);

String requestXml = RequestBuilder.buildNewRestRequest(request);

comparingStringWithoutSpaces(requestXml, expected);
}

private void comparingStringWithoutSpaces(String requestXml, String expected) {
assertEquals(expected.replaceAll("\\s{2,}", ""), requestXml.replaceAll("\\s{2,}", "").trim());
}
Expand Down

0 comments on commit c60fdac

Please sign in to comment.