Skip to content

Commit

Permalink
[Parvathy, Rahul] | BAH-3410 | Add. Json Serialized REST Payload
Browse files Browse the repository at this point in the history
Co-authored-by: Parvathy Babu <[email protected]>
  • Loading branch information
rahu1ramesh and parvathy00 committed Dec 15, 2023
1 parent a5221a2 commit 547b431
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ public OdooRESTClient(OpenERPProperties openERPProperties) {

@Override
public Object execute(OpenERPRequest openERPRequest, String URL) {
String requestBody = "";
List<Parameter> parameters = openERPRequest.getParameters();
for (Parameter parameter : parameters) {
if (parameter.getName().equals("category") && parameter.getValue().equals("create.sale.order")) {
requestBody = RequestBuilder.buildNewJSONObject(openERPRequest, UUID.randomUUID().toString());
return restClient.post(URL, requestBody);
}
}
requestBody = RequestBuilder.buildNewRestRequest(openERPRequest, UUID.randomUUID().toString());
String requestBody = RequestBuilder.buildNewRestRequest(openERPRequest);
return restClient.post(URL, requestBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ public RestClient(String baseURL, String username, String password, int connecti
private void login() {
if (accessToken == null) {
OpenERPRequest openERPRequest = new OpenERPRequest("res.users", "login", Arrays.asList(new Parameter("username", username), new Parameter("password", password)));
String requestBody = RequestBuilder.buildNewRestRequest(openERPRequest, UUID.randomUUID().toString());
String requestBody = RequestBuilder.buildNewRestRequest(openERPRequest);
WebClient client = getWebClient(baseURL);
HttpHeaders headers = getHttpHeaders();
Consumer<HttpHeaders> consumer = httpHeaders -> httpHeaders.addAll(headers);
try{
String response = client.post().uri("api/odoo-login").headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).timeout(Duration.ofMillis(connectionTimeout)).block();
logger.debug("\n-----------------------------------------------------Login Initiated-----------------------------------------------------\n* Request : {}\n* Response : {}\n-----------------------------------------------------End of Login-----------------------------------------------------", requestBody, response);
if (response == null) {
throw new OpenERPException("Login failed");
}
Expand All @@ -65,6 +66,7 @@ public String post(String URL, String requestBody) {
headers.set(HttpHeaders.AUTHORIZATION, accessToken);
Consumer<HttpHeaders> consumer = httpHeaders -> httpHeaders.addAll(headers);
String response = client.post().uri(URL).headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).timeout(Duration.ofMillis(connectionTimeout)).block();
logger.debug("\n-----------------------------------------------------{} Initiated-----------------------------------------------------\n* Request : {}\n* Response : {}\n-----------------------------------------------------End of {}-----------------------------------------------------", URL, requestBody, response, URL);
if (response == null) {
throw new OpenERPException(String.format("Could not post to %s", URL));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,18 @@ private static HashMap<String, Object> getXMLContext(OpenERPRequest openERPReque
return context;
}

public static String buildNewRestRequest(OpenERPRequest openERPRequest, String id) {
public static String buildNewRestRequest(OpenERPRequest openERPRequest) {
try {
HashMap<String, Object> context = getRestContext(openERPRequest, id);
return buildRequest("rest_template.ftl", context);
} catch (Exception e) {
throw new OpenERPException(e);
}
}

private static HashMap<String, Object> getRestContext(OpenERPRequest openERPRequest, String id) {
HashMap<String, Object> context = new HashMap<>();
context.put("parametersList", openERPRequest.getParameters());
context.put("id", id);
return context;
}

private static String buildRequest(String templateName, HashMap<String, Object> context){
try {
Template template= FreeMarkerConfig.getConfiguration().getTemplate(templateName);
StringWriter writer = new StringWriter();
template.process(context, writer);
return writer.toString();
} catch (Exception e) {
throw new OpenERPException(e);
}
}
public static String buildNewJSONObject(OpenERPRequest openERPRequest, String id) {
try {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("id", id);

Map<String, Object> data = getParameters(openERPRequest);
Map<String, Object> params = new HashMap<>();
params.put("data", data);
requestBody.put("params", params);

HashMap<String, Object> requestBody = getParameters(openERPRequest);
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(requestBody);
return jsonString;
return mapper.writeValueAsString(requestBody);
} catch (Exception e) {
throw new OpenERPException(e);
}
}

private static Map<String, Object> getParameters(OpenERPRequest openERPRequest) {
Map<String, Object> parameters = new HashMap<>();
private static HashMap<String, Object> getParameters(OpenERPRequest openERPRequest) {
HashMap<String, Object> parameters = new HashMap<>();
for (Parameter parameter : openERPRequest.getParameters()) {
parameters.put(parameter.getName(), parseParameterValue(parameter.getValue()));
}
Expand All @@ -95,4 +61,15 @@ private static Object parseParameterValue(String value) {
return value;
}
}

private static String buildRequest(String templateName, HashMap<String, Object> context){
try {
Template template= FreeMarkerConfig.getConfiguration().getTemplate(templateName);
StringWriter writer = new StringWriter();
template.process(context, writer);
return writer.toString();
} catch (Exception e) {
throw new OpenERPException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void shouldCreateNewRESTCustomerRequestWithPatientDataPopulated() throws
String patientName = "Ramu";
String patientId = "13466";
String village = "Ganiyari";
String expected = "{\"id\": \"3a0ee6b3-4e3a-425d-a2ef-806ccb0d1744\",\"params\": {\"data\": {\"name\": \"Ramu\",\"ref\": \"13466\",\"village\": \"Ganiyari\"}}\n}";
String expected = "{\"ref\":13466,\"name\":\"Ramu\",\"village\":\"Ganiyari\"}";

Parameter name = new Parameter("name", patientName, "string");
Parameter ref = new Parameter("ref", patientId, "string");
Expand All @@ -92,7 +92,7 @@ public void shouldCreateNewRESTCustomerRequestWithPatientDataPopulated() throws
parameters.add(ref);
parameters.add(villageParam);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);
String requestXml = RequestBuilder.buildNewRestRequest(request, id);
String requestXml = RequestBuilder.buildNewRestRequest(request);

comparingStringWithoutSpaces(requestXml, expected);
}
Expand Down Expand Up @@ -150,13 +150,13 @@ public void shouldEscapeSpecialCharactersWhenXMLRequestIsCreated() throws Except
public void shouldEscapeSpecialCharactersWhenRESTRequestIsCreated() throws Exception {
String income = "<=10000";
String id = "f1cfccea-6f5e-4d6b-9759-f55e92cd4f8f";
String expected = "{\"id\": \"f1cfccea-6f5e-4d6b-9759-f55e92cd4f8f\",\"params\": {\"data\": {\"income\": \"<=10000\"}}\n}";
String expected = "{\"income\":\"<=10000\"}";

Parameter parameter = new Parameter("income", income, "string");
ArrayList<Parameter> parameters = new ArrayList<Parameter>();
parameters.add(parameter);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);
String requestXml = RequestBuilder.buildNewRestRequest(request, id);
String requestXml = RequestBuilder.buildNewRestRequest(request);

comparingStringWithoutSpaces(requestXml, expected);
}
Expand Down Expand Up @@ -215,85 +215,19 @@ public void shouldEscapeHindiCharactersWhenRESTRequestIsCreated() throws Excepti

String name = "कृपा";
String id = "6397d716-5ec8-47c6-b5c8-dd49e3e09235";
String expected = "{\"id\": \"6397d716-5ec8-47c6-b5c8-dd49e3e09235\",\"params\": {\"data\": {\"name\": \"कृपा\"}}\n}";
String expected = "{\"name\":\"कृपा\"}";

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, id);
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());
}

@Test
public void shouldBuildNewJsonObject() throws Exception {

List<Parameter> parameters = Arrays.asList(
new Parameter("category", "create.sale.order", "string"),
new Parameter("customer_id", "GAN203012", "string"),
new Parameter("encounter_id", "8bb8cdbb-82f1-4aac-b610-24498bc2cf70", "string"),
new Parameter("feed_uri", "http://openmrs:8080/openmrs/ws/atomfeed/encounter/recent", "string"),
new Parameter("last_read_entry_id", "tag:atomfeed.ict4h.org:6aa022bf-395f-45b6-867b-7a5df9918d41", "string"),
new Parameter("feed_uri_for_last_read_entry", "http://openmrs:8080/openmrs/ws/atomfeed/encounter/6", "string"),
new Parameter("locationName", "General Ward", "string"),
new Parameter("orders", "{\"id\":\"8bb8cdbb-82f1-4aac-b610-24498bc2cf70\",\"openERPOrders\":[{\"orderId\":\"304ddc83-c3dd-4754-bef8-913b8bf68de8\",\"previousOrderId\":null,\"encounterId\":\"8bb8cdbb-82f1-4aac-b610-24498bc2cf70\",\"productId\":\"014bd216-72bd-44ae-b25a-270ed10bb2da\",\"productName\":\"Monocyte\",\"quantity\":1.0,\"quantityUnits\":\"Unit(s)\",\"action\":\"NEW\",\"visitId\":\"fcbe0bca-2638-4281-8f29-a21d4ca15fe6\",\"visitType\":\"OPD\",\"type\":\"Lab Order\",\"description\":null,\"voided\":false,\"locationName\":null,\"providerName\":\"Super Man\",\"dispensed\":\"false\",\"conceptName\":null}]}", "string")
);

OpenERPRequest openERPRequest = new OpenERPRequest("res.partner", "execute", parameters);

String mockUuid = UUID.randomUUID().toString();
String result = RequestBuilder.buildNewJSONObject(openERPRequest, mockUuid);

String expectedJson = "{\n" +
" \"id\": \"" + mockUuid + "\",\n" +
" \"params\": {\n" +
" \"data\": {\n" +
" \"category\": \"create.sale.order\",\n" +
" \"customer_id\": \"GAN203012\",\n" +
" \"encounter_id\": \"8bb8cdbb-82f1-4aac-b610-24498bc2cf70\",\n" +
" \"feed_uri\": \"http://openmrs:8080/openmrs/ws/atomfeed/encounter/recent\",\n" +
" \"last_read_entry_id\": \"tag:atomfeed.ict4h.org:6aa022bf-395f-45b6-867b-7a5df9918d41\",\n" +
" \"feed_uri_for_last_read_entry\": \"http://openmrs:8080/openmrs/ws/atomfeed/encounter/6\",\n" +
" \"locationName\": \"General Ward\",\n" +
" \"orders\": {\n" +
" \"id\": \"8bb8cdbb-82f1-4aac-b610-24498bc2cf70\",\n" +
" \"openERPOrders\": [\n" +
" {\n" +
" \"orderId\": \"304ddc83-c3dd-4754-bef8-913b8bf68de8\",\n" +
" \"previousOrderId\": null,\n" +
" \"encounterId\": \"8bb8cdbb-82f1-4aac-b610-24498bc2cf70\",\n" +
" \"productId\": \"014bd216-72bd-44ae-b25a-270ed10bb2da\",\n" +
" \"productName\": \"Monocyte\",\n" +
" \"quantity\": 1.0,\n" +
" \"quantityUnits\": \"Unit(s)\",\n" +
" \"action\": \"NEW\",\n" +
" \"visitId\": \"fcbe0bca-2638-4281-8f29-a21d4ca15fe6\",\n" +
" \"visitType\": \"OPD\",\n" +
" \"type\": \"Lab Order\",\n" +
" \"description\": null,\n" +
" \"voided\": false,\n" +
" \"locationName\": null,\n" +
" \"providerName\": \"Super Man\",\n" +
" \"dispensed\": \"false\",\n" +
" \"conceptName\": null\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
"}";

ObjectMapper objectMapper = new ObjectMapper();
JsonNode expectedJsonNode = objectMapper.readTree(expectedJson);
JsonNode actualJsonNode = objectMapper.readTree(result);

assertEquals(expectedJsonNode, actualJsonNode);
}

}

0 comments on commit 547b431

Please sign in to comment.