Replaced all tabs with spaces in java and pom.xml
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / onap / so / apihandler / common / ResponseHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.apihandler.common;
24
25
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;
42
43 /**
44  * This class implements test methods of CamundaResoponseHandler.
45  * 
46  *
47  */
48 public class ResponseHandlerTest {
49
50     @Rule
51     public ExpectedException thrown = ExpectedException.none();
52
53     @Test
54     public void tesParseCamundaResponse() throws ApiException {
55         // String body
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}";
57
58         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
59                 + "\"message\": \"Successfully started the process\"}";
60
61         HttpResponse response = createResponse(200, body, "application/json");
62
63         ResponseHandler respHandler = new ResponseHandler(response, 1);
64
65         int status = respHandler.getStatus();
66         assertEquals(status, HttpStatus.SC_ACCEPTED);
67         assertEquals(respHandler.getResponse().getMessage(), "Successfully started the process");
68
69     }
70
71     @Test
72     public void tesParseCamundaResponseForCamundaTaskType() throws ApiException {
73         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
74                 + "\"message\": \"Successfully started the process\"}";
75
76         HttpResponse response = createResponse(200, body, "application/json");
77
78         ResponseHandler respHandler = new ResponseHandler(response, 2);
79
80         int status = respHandler.getStatus();
81         assertEquals(status, HttpStatus.SC_ACCEPTED);
82         assertEquals(respHandler.getResponseBody(), body);
83
84     }
85
86     @Test
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>";
92
93         HttpResponse response = createResponse(200, body, "text/xml");
94
95         ResponseHandler respHandler = new ResponseHandler(response, 0);
96
97         int status = respHandler.getStatus();
98         assertEquals(status, HttpStatus.SC_ACCEPTED);
99         assertTrue(respHandler.getResponseBody() != null);
100     }
101
102     @Test
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)));
108
109         HttpResponse response = createResponse(HttpStatus.SC_NOT_FOUND, "<html>error</html>", "text/html");
110         ResponseHandler respHandler = new ResponseHandler(response, 1);
111
112         int status = respHandler.getStatus();
113
114         assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, status);
115
116     }
117
118     @Test
119     public void tesGenricErrorResponse() throws ApiException {
120
121         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 500,"
122                 + "\"message\": \"Something went wrong\"}";
123
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");
129     }
130
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);
135         try {
136             response.setEntity(new StringEntity(respBody));
137             response.setHeader("Content-Type", contentType);
138         } catch (Exception e) {
139             e.printStackTrace();
140         }
141         return response;
142     }
143
144 }