2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.apihandler.common;
26 import static org.hamcrest.Matchers.hasProperty;
27 import static org.hamcrest.Matchers.is;
28 import static org.hamcrest.Matchers.startsWith;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertTrue;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.HttpStatus;
33 import org.apache.http.ProtocolVersion;
34 import org.apache.http.entity.StringEntity;
35 import org.apache.http.message.BasicHttpResponse;
36 import org.apache.http.message.BasicStatusLine;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.onap.so.apihandlerinfra.exceptions.ApiException;
41 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
44 * This class implements test methods of CamundaResoponseHandler.
48 public class ResponseHandlerTest {
51 public ExpectedException thrown = ExpectedException.none();
54 public void tesParseCamundaResponse() throws ApiException {
56 // ="{\"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}";
58 String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
59 + "\"message\": \"Successfully started the process\"}";
61 HttpResponse response = createResponse(200, body, "application/json");
63 ResponseHandler respHandler = new ResponseHandler(response, 1);
65 int status = respHandler.getStatus();
66 assertEquals(status, HttpStatus.SC_ACCEPTED);
67 assertEquals(respHandler.getResponse().getMessage(), "Successfully started the process");
72 public void tesParseCamundaResponseForCamundaTaskType() throws ApiException {
73 String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
74 + "\"message\": \"Successfully started the process\"}";
76 HttpResponse response = createResponse(200, body, "application/json");
78 ResponseHandler respHandler = new ResponseHandler(response, 2);
80 int status = respHandler.getStatus();
81 assertEquals(status, HttpStatus.SC_ACCEPTED);
82 assertEquals(respHandler.getResponseBody(), body);
87 public void tesParseBpelResponse() throws ApiException {
88 String body = "<test:service-response xmlns:test=\"http://org.onap/so/test\">"
89 + "<test:request-id>req5</test:request-id>" + "<test:request-action>test</test:request-action>"
90 + "<test:source>test</test:source>" + "<test:ack-final-indicator>n</test:ack-final-indicator>"
91 + "</test:service-response>";
93 HttpResponse response = createResponse(200, body, "text/xml");
95 ResponseHandler respHandler = new ResponseHandler(response, 0);
97 int status = respHandler.getStatus();
98 assertEquals(status, HttpStatus.SC_ACCEPTED);
99 assertTrue(respHandler.getResponseBody() != null);
103 public void tesMappingErrorResponse() throws ApiException {
104 thrown.expect(ValidateException.class);
105 thrown.expectMessage(startsWith("Cannot parse Camunda Response"));
106 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
107 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
109 HttpResponse response = createResponse(HttpStatus.SC_NOT_FOUND, "<html>error</html>", "text/html");
110 ResponseHandler respHandler = new ResponseHandler(response, 1);
112 int status = respHandler.getStatus();
114 assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, status);
119 public void tesGenricErrorResponse() throws ApiException {
121 String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 500,"
122 + "\"message\": \"Something went wrong\"}";
124 HttpResponse response = createResponse(500, body, "application/json");
125 ResponseHandler respHandler = new ResponseHandler(response, 1);
126 int status = respHandler.getStatus();
127 assertEquals(status, HttpStatus.SC_BAD_GATEWAY);
128 assertEquals(respHandler.getResponse().getMessage(), "Something went wrong");
131 private HttpResponse createResponse(int respStatus, String respBody, String contentType) {
132 HttpResponse response =
133 new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
134 response.setStatusCode(respStatus);
136 response.setEntity(new StringEntity(respBody));
137 response.setHeader("Content-Type", contentType);
138 } catch (Exception e) {