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 2a65f2d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
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.lang.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.isNumber(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 2a65f2d

Please sign in to comment.