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;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.HttpStatus;
34 import org.apache.http.ProtocolVersion;
35 import org.apache.http.entity.StringEntity;
36 import org.apache.http.message.BasicHttpResponse;
37 import org.apache.http.message.BasicStatusLine;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.onap.so.apihandlerinfra.exceptions.ApiException;
42 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
45 * This class implements test methods of CamundaResoponseHandler.
49 public class ResponseHandlerTest{
52 public ExpectedException thrown = ExpectedException.none();
55 public void tesParseCamundaResponse () throws ApiException {
57 // ="{\"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}";
59 String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
60 + "\"message\": \"Successfully started the process\"}";
62 HttpResponse response = createResponse (200, body, "application/json");
64 ResponseHandler respHandler = new ResponseHandler (response, 1);
66 int status = respHandler.getStatus ();
67 assertEquals (status, HttpStatus.SC_ACCEPTED);
68 assertEquals (respHandler.getResponse ().getMessage (), "Successfully started the process");
73 public void tesParseCamundaResponseForCamundaTaskType () throws ApiException {
74 String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
75 + "\"message\": \"Successfully started the process\"}";
77 HttpResponse response = createResponse (200, body, "application/json");
79 ResponseHandler respHandler = new ResponseHandler (response, 2);
81 int status = respHandler.getStatus ();
82 assertEquals (status, HttpStatus.SC_ACCEPTED);
83 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>"
90 + "<test:request-action>test</test:request-action>"
91 + "<test:source>test</test:source>"
92 + "<test:ack-final-indicator>n</test:ack-final-indicator>"
93 + "</test:service-response>";
95 HttpResponse response = createResponse (200, body, "text/xml");
97 ResponseHandler respHandler = new ResponseHandler (response, 0);
99 int status = respHandler.getStatus ();
100 assertEquals (status, HttpStatus.SC_ACCEPTED);
101 assertTrue (respHandler.getResponseBody () != null);
105 public void tesMappingErrorResponse () throws ApiException {
106 thrown.expect(ValidateException.class);
107 thrown.expectMessage(startsWith("Cannot parse Camunda Response"));
108 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
109 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
111 HttpResponse response = createResponse (HttpStatus.SC_NOT_FOUND, "<html>error</html>", "text/html");
112 ResponseHandler respHandler = new ResponseHandler (response, 1);
114 int status = respHandler.getStatus ();
116 assertEquals (HttpStatus.SC_NOT_IMPLEMENTED, status);
121 public void tesGenricErrorResponse () throws ApiException {
123 String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 500,"
124 + "\"message\": \"Something went wrong\"}";
126 HttpResponse response = createResponse (500, body, "application/json");
127 ResponseHandler respHandler = new ResponseHandler (response, 1);
128 int status = respHandler.getStatus ();
129 assertEquals (status, HttpStatus.SC_BAD_GATEWAY);
130 assertEquals (respHandler.getResponse ().getMessage (), "Something went wrong");
133 private HttpResponse createResponse (int respStatus, String respBody, String contentType) {
134 HttpResponse response = new BasicHttpResponse (new BasicStatusLine (new ProtocolVersion ("HTTP", 1, 1),
137 response.setStatusCode (respStatus);
139 response.setEntity (new StringEntity (respBody));
140 response.setHeader ("Content-Type", contentType);
141 } catch (Exception e) {
142 e.printStackTrace ();