refactor camunda client in apih
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / onap / so / apihandler / common / ResponseHandlerTest.java
index 2095a91..e6f7a36 100644 (file)
 package org.onap.so.apihandler.common;
 
 
-import static org.hamcrest.Matchers.hasProperty;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.startsWith;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.ProtocolVersion;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.message.BasicHttpResponse;
-import org.apache.http.message.BasicStatusLine;
+import java.io.IOException;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
-import org.onap.so.apihandlerinfra.exceptions.ApiException;
-import org.onap.so.apihandlerinfra.exceptions.ValidateException;
-
-/**
- * This class implements test methods of CamundaResoponseHandler.
- * 
- *
- */
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Spy;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
+import org.springframework.http.ResponseEntity;
+
+@RunWith(MockitoJUnitRunner.class)
 public class ResponseHandlerTest {
 
+    @Spy
+    @InjectMocks
+    private ResponseHandler responseHandler;
+
     @Rule
     public ExpectedException thrown = ExpectedException.none();
 
     @Test
-    public void tesParseCamundaResponse() throws ApiException {
-        // String body
-        // ="{\"links\":[{\"method\":\"GET\",\"href\":\"http://localhost:9080/engine-rest/process-instance/2047c658-37ae-11e5-9505-7a1020524153\",\"rel\":\"self\"}],\"id\":\"2047c658-37ae-11e5-9505-7a1020524153\",\"definitionId\":\"dummy:10:73298961-37ad-11e5-9505-7a1020524153\",\"businessKey\":null,\"caseInstanceId\":null,\"ended\":true,\"suspended\":false}";
-
-        String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
-                + "\"message\": \"Successfully started the process\"}";
-
-        HttpResponse response = createResponse(200, body, "application/json");
-
-        ResponseHandler respHandler = new ResponseHandler(response, 1);
-
-        int status = respHandler.getStatus();
-        assertEquals(status, HttpStatus.SC_ACCEPTED);
-        assertEquals(respHandler.getResponse().getMessage(), "Successfully started the process");
-
-    }
-
-    @Test
-    public void tesParseCamundaResponseForCamundaTaskType() throws ApiException {
-        String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
-                + "\"message\": \"Successfully started the process\"}";
-
-        HttpResponse response = createResponse(200, body, "application/json");
-
-        ResponseHandler respHandler = new ResponseHandler(response, 2);
-
-        int status = respHandler.getStatus();
-        assertEquals(status, HttpStatus.SC_ACCEPTED);
-        assertEquals(respHandler.getResponseBody(), body);
-
-    }
-
-    @Test
-    public void tesParseBpelResponse() throws ApiException {
-        String body = "<test:service-response xmlns:test=\"http://org.onap/so/test\">"
-                + "<test:request-id>req5</test:request-id>" + "<test:request-action>test</test:request-action>"
-                + "<test:source>test</test:source>" + "<test:ack-final-indicator>n</test:ack-final-indicator>"
-                + "</test:service-response>";
-
-        HttpResponse response = createResponse(200, body, "text/xml");
-
-        ResponseHandler respHandler = new ResponseHandler(response, 0);
-
-        int status = respHandler.getStatus();
-        assertEquals(status, HttpStatus.SC_ACCEPTED);
-        assertTrue(respHandler.getResponseBody() != null);
-    }
-
-    @Test
-    public void tesMappingErrorResponse() throws ApiException {
-        thrown.expect(ValidateException.class);
-        thrown.expectMessage(startsWith("Cannot parse Camunda Response"));
-        thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
-        thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
-
-        HttpResponse response = createResponse(HttpStatus.SC_NOT_FOUND, "<html>error</html>", "text/html");
-        ResponseHandler respHandler = new ResponseHandler(response, 1);
-
-        int status = respHandler.getStatus();
-
-        assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, status);
-
+    public void acceptedResponseTest() throws IOException, BPMNFailureException {
+        ResponseEntity<String> camundaResponse = ResponseEntity.noContent().build();
+        thrown.expect(BPMNFailureException.class);
+        thrown.expectMessage("Request Failed due to BPEL error with HTTP Status = 204");
+        responseHandler.acceptedResponse(camundaResponse);
     }
 
     @Test
-    public void tesGenricErrorResponse() throws ApiException {
-
-        String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 500,"
-                + "\"message\": \"Something went wrong\"}";
-
-        HttpResponse response = createResponse(500, body, "application/json");
-        ResponseHandler respHandler = new ResponseHandler(response, 1);
-        int status = respHandler.getStatus();
-        assertEquals(status, HttpStatus.SC_BAD_GATEWAY);
-        assertEquals(respHandler.getResponse().getMessage(), "Something went wrong");
-    }
+    public void acceptedOrNoContentResponseTest() throws IOException, BPMNFailureException {
+        ResponseEntity<String> camundaResponse = ResponseEntity.badRequest().build();
+        thrown.expect(BPMNFailureException.class);
+        thrown.expectMessage("Request Failed due to BPEL error with HTTP Status = 400");
+        responseHandler.acceptedOrNoContentResponse(camundaResponse);
 
-    private HttpResponse createResponse(int respStatus, String respBody, String contentType) {
-        HttpResponse response =
-                new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
-        response.setStatusCode(respStatus);
-        try {
-            response.setEntity(new StringEntity(respBody));
-            response.setHeader("Content-Type", contentType);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return response;
     }
 
 }