diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java index ce6df101..47e3897f 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java @@ -30,15 +30,7 @@ public OdooRESTClient(OpenERPProperties openERPProperties) { @Override public Object execute(OpenERPRequest openERPRequest, String URL) { - String requestBody = ""; - List 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); } } diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java index ead3aafb..b3af40b6 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java @@ -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 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"); } @@ -65,6 +66,7 @@ public String post(String URL, String requestBody) { headers.set(HttpHeaders.AUTHORIZATION, accessToken); Consumer 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)); } diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java index b16138db..21d75080 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java @@ -35,52 +35,18 @@ private static HashMap getXMLContext(OpenERPRequest openERPReque return context; } - public static String buildNewRestRequest(OpenERPRequest openERPRequest, String id) { + public static String buildNewRestRequest(OpenERPRequest openERPRequest) { try { - HashMap context = getRestContext(openERPRequest, id); - return buildRequest("rest_template.ftl", context); - } catch (Exception e) { - throw new OpenERPException(e); - } - } - - private static HashMap getRestContext(OpenERPRequest openERPRequest, String id) { - HashMap context = new HashMap<>(); - context.put("parametersList", openERPRequest.getParameters()); - context.put("id", id); - return context; - } - - private static String buildRequest(String templateName, HashMap 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 requestBody = new HashMap<>(); - requestBody.put("id", id); - - Map data = getParameters(openERPRequest); - Map params = new HashMap<>(); - params.put("data", data); - requestBody.put("params", params); - + HashMap 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 getParameters(OpenERPRequest openERPRequest) { - Map parameters = new HashMap<>(); + private static HashMap getParameters(OpenERPRequest openERPRequest) { + HashMap parameters = new HashMap<>(); for (Parameter parameter : openERPRequest.getParameters()) { parameters.put(parameter.getName(), parseParameterValue(parameter.getValue())); } @@ -95,4 +61,15 @@ private static Object parseParameterValue(String value) { return value; } } + + private static String buildRequest(String templateName, HashMap 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); + } + } } \ No newline at end of file diff --git a/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java b/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java index ed214f2b..b591ea4b 100644 --- a/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java +++ b/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java @@ -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"); @@ -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); } @@ -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 parameters = new ArrayList(); 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); } @@ -215,14 +215,14 @@ 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 parameters = new ArrayList(); 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); } @@ -230,70 +230,4 @@ public void shouldEscapeHindiCharactersWhenRESTRequestIsCreated() throws Excepti private void comparingStringWithoutSpaces(String requestXml, String expected) { assertEquals(expected.replaceAll("\\s{2,}", ""), requestXml.replaceAll("\\s{2,}", "").trim()); } - - @Test - public void shouldBuildNewJsonObject() throws Exception { - - List 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); - } - } \ No newline at end of file