2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.apihandler.common;
24 import static org.hamcrest.Matchers.hasProperty;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.startsWith;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
30 import java.io.IOException;
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.apihandler.common.ErrorNumbers;
42 import org.onap.so.apihandler.common.ResponseHandler;
44 import com.fasterxml.jackson.core.JsonGenerationException;
45 import com.fasterxml.jackson.databind.JsonMappingException;
46 import org.onap.so.apihandlerinfra.exceptions.ApiException;
47 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
48 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
51 * This class implements test methods of CamundaResoponseHandler.
55 public class ResponseHandlerTest{
58 public ExpectedException thrown = ExpectedException.none();
61 public void tesParseCamundaResponse () throws ApiException {
63 // ="{\"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}";
65 String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
66 + "\"message\": \"Successfully started the process\"}";
68 HttpResponse response = createResponse (200, body, "application/json");
70 ResponseHandler respHandler = new ResponseHandler (response, 1);
72 int status = respHandler.getStatus ();
73 assertEquals (status, HttpStatus.SC_ACCEPTED);
74 assertEquals (respHandler.getResponse ().getMessage (), "Successfully started the process");
79 public void tesParseBpelResponse () throws ApiException{
80 String body = "<test:service-response xmlns:test=\"http://org.onap/so/test\">"
81 + "<test:request-id>req5</test:request-id>"
82 + "<test:request-action>test</test:request-action>"
83 + "<test:source>test</test:source>"
84 + "<test:ack-final-indicator>n</test:ack-final-indicator>"
85 + "</test:service-response>";
87 HttpResponse response = createResponse (200, body, "text/xml");
89 ResponseHandler respHandler = new ResponseHandler (response, 0);
91 int status = respHandler.getStatus ();
92 assertEquals (status, HttpStatus.SC_ACCEPTED);
93 assertTrue (respHandler.getResponseBody () != null);
97 public void tesMappingErrorResponse () throws ApiException {
98 thrown.expect(ValidateException.class);
99 thrown.expectMessage(startsWith("JSON Object Mapping Request"));
100 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
101 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
103 HttpResponse response = createResponse (HttpStatus.SC_NOT_FOUND, "<html>error</html>", "text/html");
104 ResponseHandler respHandler = new ResponseHandler (response, 1);
106 int status = respHandler.getStatus ();
108 assertEquals (HttpStatus.SC_NOT_IMPLEMENTED, status);
113 public void tesGenricErrorResponse () throws ApiException {
115 String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 500,"
116 + "\"message\": \"Something went wrong\"}";
118 HttpResponse response = createResponse (500, body, "application/json");
119 ResponseHandler respHandler = new ResponseHandler (response, 1);
120 int status = respHandler.getStatus ();
121 assertEquals (status, HttpStatus.SC_BAD_GATEWAY);
122 assertEquals (respHandler.getResponse ().getMessage (), "Something went wrong");
125 private HttpResponse createResponse (int respStatus, String respBody, String contentType) {
126 HttpResponse response = new BasicHttpResponse (new BasicStatusLine (new ProtocolVersion ("HTTP", 1, 1),
129 response.setStatusCode (respStatus);
131 response.setEntity (new StringEntity (respBody));
132 response.setHeader ("Content-Type", contentType);
133 } catch (Exception e) {
134 e.printStackTrace ();