Repository: authorjapps/zerocode
Pull request #203 (2019-02-06 14:30:00)

Fixed issue in mocking xml response of soap endpoint


Patch:
diff --git a/.gitignore b/.gitignore
index 5f7e48b8..302c16c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,4 +21,6 @@ hs_err_pid*
 #for eclipse
 .classpath
 .project
-.settings
\ No newline at end of file
+.settings
+
+*/target
\ No newline at end of file
diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/mocker/RestEndPointMocker.java b/core/src/main/java/org/jsmart/zerocode/core/engine/mocker/RestEndPointMocker.java
index 3d094210..dd5788e5 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/engine/mocker/RestEndPointMocker.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/engine/mocker/RestEndPointMocker.java
@@ -1,5 +1,6 @@
 package org.jsmart.zerocode.core.engine.mocker;
 
+import com.fasterxml.jackson.databind.JsonNode;
 import com.github.tomakehurst.wiremock.WireMockServer;
 import com.github.tomakehurst.wiremock.client.MappingBuilder;
 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
@@ -33,7 +34,10 @@ public class RestEndPointMocker {
         restartWireMock(mockPort);
 
         mockSteps.getMocks().forEach(mockStep -> {
-            String jsonBodyRequest = mockStep.getResponse().get("body").toString();
+            JsonNode jsonNodeResponse = mockStep.getResponse();
+            JsonNode jsonNodeBody = jsonNodeResponse.get("body");
+            String jsonBodyRequest = (jsonNodeBody != null)?jsonNodeBody.toString():jsonNodeResponse.get("xmlBody").asText();
+
 
             if("GET".equals(mockStep.getOperation())){
                 LOGGER.info("*****WireMock- Mocking the GET endpoint");
@@ -113,40 +117,44 @@ public class RestEndPointMocker {
         return responseBuilder;
     }
 
+    public static int createWithLocalMock(String endPointJsonApi) {
+        if(StringUtils.isNotEmpty(endPointJsonApi)){
+            // read this json into virtuoso.
+        }
+
+        return 200;
+    }
+
+    public static WireMockServer getWireMockServer() {
+        return wireMockServer;
+    }
 
     /*
      * This is working code, whenever you put the virtuoso dependency here, you can uncomment this block.
      */
     public static int createWithVirtuosoMock(String endPointJsonApi) {
-//        if(StringUtils.isNotEmpty(endPointJsonApi)){
-//            ApiSpec apiSpec = SimulatorJsonUtils.deserialize(endPointJsonApi);
-//            apiSpec.getApis().stream()
-//                    .forEach(api -> {
-//                        int status = aVirtuosoRestMocker()
-//                                .url(api.getUrl())
-//                                .operation(api.getOperation())
-//                                .willReturn(
-//                                        aResponse()
-//                                                .status(api.getResponse().getStatus())
-//                                                .body(api.getResponse().getBody())
-//                                                .build()
-//                                );
-//
-//                        if(200 != status){
-//                            logbuilder.info("Mocking virtuoso end point failed. Status: " + status);
-//                            throw new RuntimeException("Mocking virtuoso end point failed. Status: " + status + ". Check tunnel etc.");
-//                        }
-//                    });
-//        }
+    //        if(StringUtils.isNotEmpty(endPointJsonApi)){
+    //            ApiSpec apiSpec = SimulatorJsonUtils.deserialize(endPointJsonApi);
+    //            apiSpec.getApis().stream()
+    //                    .forEach(api -> {
+    //                        int status = aVirtuosoRestMocker()
+    //                                .url(api.getUrl())
+    //                                .operation(api.getOperation())
+    //                                .willReturn(
+    //                                        aResponse()
+    //                                                .status(api.getResponse().getStatus())
+    //                                                .body(api.getResponse().getBody())
+    //                                                .build()
+    //                                );
+    //
+    //                        if(200 != status){
+    //                            logbuilder.info("Mocking virtuoso end point failed. Status: " + status);
+    //                            throw new RuntimeException("Mocking virtuoso end point failed. Status: " + status + ". Check tunnel etc.");
+    //                        }
+    //                    });
+    //        }
 
         return 200;
     }
 
-    public static int createWithLocalMock(String endPointJsonApi) {
-        if(StringUtils.isNotEmpty(endPointJsonApi)){
-            // read this json into virtuoso.
-        }
-
-        return 200;
-    }
 }
\ No newline at end of file


Test patch:
diff --git a/core/src/test/java/org/jsmart/zerocode/core/engine/mocker/RestEndPointMockerTest.java b/core/src/test/java/org/jsmart/zerocode/core/engine/mocker/RestEndPointMockerTest.java
index 478f1417..0a439203 100644
--- a/core/src/test/java/org/jsmart/zerocode/core/engine/mocker/RestEndPointMockerTest.java
+++ b/core/src/test/java/org/jsmart/zerocode/core/engine/mocker/RestEndPointMockerTest.java
@@ -7,7 +7,6 @@ import com.github.tomakehurst.wiremock.client.WireMock;
 import com.github.tomakehurst.wiremock.junit.WireMockRule;
 import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
@@ -20,7 +19,6 @@ import org.jsmart.zerocode.core.di.main.ApplicationMainModule;
 import org.jsmart.zerocode.core.domain.MockStep;
 import org.jsmart.zerocode.core.domain.MockSteps;
 import org.jsmart.zerocode.core.domain.ScenarioSpec;
-import org.jsmart.zerocode.core.httpclient.BasicHttpClient;
 import org.jsmart.zerocode.core.utils.SmartUtils;
 import org.jukito.JukitoRunner;
 import org.jukito.TestModule;
@@ -48,6 +46,8 @@ import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.jsmart.zerocode.core.engine.mocker.RestEndPointMocker.createWithWireMock;
+import static org.jsmart.zerocode.core.engine.mocker.RestEndPointMocker.getWireMockServer;
 
 @RunWith(JukitoRunner.class)
 public class RestEndPointMockerTest {
@@ -80,7 +80,7 @@ public class RestEndPointMockerTest {
 
     @Test
     public void willDeserializeA_VanilaFlow() throws Exception {
-        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/01_wire_mock_end_point_will_respond_to_get_call.json");
+        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/wiremock_end_point_json_body.json");
         ScenarioSpec flowDeserialized = objectMapper.readValue(jsonDocumentAsString, ScenarioSpec.class);
 
         assertThat(flowDeserialized, notNullValue());
@@ -113,7 +113,7 @@ public class RestEndPointMockerTest {
         // WireMock wireMock = new WireMock(9073);
         // WireMock.configureFor(9073);
 
-        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/01_wire_mock_end_point_will_respond_to_get_call.json");
+        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/wiremock_end_point_json_body.json");
         ScenarioSpec flowDeserialized = objectMapper.readValue(jsonDocumentAsString, ScenarioSpec.class);
         MockSteps mockSteps = smartUtils.getMapper().readValue(flowDeserialized.getSteps().get(0).getRequest().toString(), MockSteps.class);
 
@@ -144,7 +144,7 @@ public class RestEndPointMockerTest {
 
         WireMock.configureFor(9073);
 
-        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/01_wire_mock_end_point_will_respond_to_get_call.json");
+        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/wiremock_end_point_json_body.json");
         ScenarioSpec flowDeserialized = objectMapper.readValue(jsonDocumentAsString, ScenarioSpec.class);
         MockSteps mockSteps = smartUtils.getMapper().readValue(flowDeserialized.getSteps().get(0).getRequest().toString(), MockSteps.class);
 
@@ -174,12 +174,75 @@ public class RestEndPointMockerTest {
 
     }
 
+    @Test
+    public void willMockRequest_jsonBody() throws Exception{
+
+        int WIRE_MOCK_TEST_PORT = 9077;
+
+        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/wiremock_end_point_json_body.json");
+        ScenarioSpec flowDeserialized = objectMapper.readValue(jsonDocumentAsString, ScenarioSpec.class);
+        MockSteps mockSteps = smartUtils.getMapper().readValue(flowDeserialized.getSteps().get(0).getRequest().toString(), MockSteps.class);
+
+        final MockStep mockPost = mockSteps.getMocks().get(1);
+        final String reqBody = mockPost.getRequest().get("body").toString(); //"{ \"id\" : \"p002\" }";
+        String respBody = mockPost.getResponse().get("body").toString();
+
+        createWithWireMock(mockSteps, WIRE_MOCK_TEST_PORT);
+
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        HttpPost request = new HttpPost("http://localhost:" + WIRE_MOCK_TEST_PORT + mockPost.getUrl());
+        request.addHeader("Content-Type", "application/json");
+        StringEntity entity = new StringEntity(reqBody);
+        request.setEntity(entity);
+        HttpResponse response = httpClient.execute(request);
+
+        final String responseBodyActual = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
+        System.out.println("### response: \n" + responseBodyActual);
+
+        assertThat(response.getStatusLine().getStatusCode(), is(201));
+        JSONAssert.assertEquals(respBody, responseBodyActual, true);
+
+        getWireMockServer().stop();
+    }
+    // --------------------------------------------------------------
+    // ISSUE-202 - https://github.com/authorjapps/zerocode/issues/202
+    // - xmlBody for SOAP mocking
+    // - Fixed by - arunvelusamyd
+    // --------------------------------------------------------------
+    @Test
+    public void willMockRequest_xmlBody() throws Exception{
+        int WIRE_MOCK_TEST_PORT = 9077;
+
+        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/wiremock_end_point_soap_xml_body.json");
+        ScenarioSpec flowDeserialized = objectMapper.readValue(jsonDocumentAsString, ScenarioSpec.class);
+        MockSteps mockSteps = smartUtils.getMapper().readValue(flowDeserialized.getSteps().get(0).getRequest().toString(), MockSteps.class);
+
+        final MockStep mockPost = mockSteps.getMocks().get(0);
+        //final String reqBody = mockPost.getRequest().get("body").toString();
+        String mockedXmlBody = mockPost.getResponse().get("xmlBody").toString();
+
+        createWithWireMock(mockSteps, WIRE_MOCK_TEST_PORT);
+
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        HttpPost request = new HttpPost("http://localhost:" + WIRE_MOCK_TEST_PORT + mockPost.getUrl());
+        HttpResponse response = httpClient.execute(request);
+
+        final String responseBodyActual = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
+        System.out.println("### response: \n" + responseBodyActual);
+
+        assertThat(response.getStatusLine().getStatusCode(), is(200));
+        assertThat(responseBodyActual, containsString("<SOAP-ENV:Envelope xmlns:SOAP-ENV"));
+        assertThat(responseBodyActual, containsString("<ns2:description>It is a tablet computers designed, developed and marketed by Apple.</ns2:description>"));
+
+        getWireMockServer().stop();
+    }
+
     @Test
     public void willMockAGetRequestWith_headers() throws Exception{
 
         WireMock.configureFor(9073);
 
-        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/01_wire_mock_end_point_will_respond_to_get_call.json");
+        String jsonDocumentAsString = smartUtils.getJsonDocumentAsString("wiremock_integration/wiremock_end_point_json_body.json");
         ScenarioSpec flowDeserialized = objectMapper.readValue(jsonDocumentAsString, ScenarioSpec.class);
         MockSteps mockSteps = smartUtils.getMapper().readValue(flowDeserialized.getSteps().get(0).getRequest().toString(), MockSteps.class);
 
diff --git a/core/src/test/resources/wiremock_integration/01_wire_mock_end_point_will_respond_to_get_call.json b/core/src/test/resources/wiremock_integration/wiremock_end_point_json_body.json
similarity index 100%
rename from core/src/test/resources/wiremock_integration/01_wire_mock_end_point_will_respond_to_get_call.json
rename to core/src/test/resources/wiremock_integration/wiremock_end_point_json_body.json
diff --git a/core/src/test/resources/wiremock_integration/wiremock_end_point_soap_xml_body.json b/core/src/test/resources/wiremock_integration/wiremock_end_point_soap_xml_body.json
new file mode 100644
index 00000000..9031d95b
--- /dev/null
+++ b/core/src/test/resources/wiremock_integration/wiremock_end_point_soap_xml_body.json
@@ -0,0 +1,28 @@
+{
+    "scenarioName": "soap xmlBody mocking",
+    "steps": [
+        {
+            "name": "mock_soap_xml_response",
+            "url": "/$MOCK",
+            "operation": "$USE.WIREMOCK",
+            "request": {
+                "mocks": [
+                    {
+                        "name": "mocking_SOAP_to_get_device_details",
+                        "operation": "POST",
+                        "url": "/ws",
+                        "request": {
+                        },
+                        "response": {
+                            "status": 200,
+                            "xmlBody": "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:GetDeviceDetailsResponse xmlns:ns2=\"http://flexy.com/device\"><ns2:DeviceDetails><ns2:id>1</ns2:id><ns2:name>iPad</ns2:name><ns2:description>It is a tablet computers designed, developed and marketed by Apple.</ns2:description></ns2:DeviceDetails></ns2:GetDeviceDetailsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>"
+                        }
+                    }
+                ]
+            },
+            "assertions": {
+                "status": 200
+            }
+        }
+    ]
+}