0d4778b5b68f8f43d224a05239dc62d1c6cc4fbe
[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
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;
43
44 /**
45  * This class implements test methods of CamundaResoponseHandler.
46  * 
47  *
48  */
49 public class ResponseHandlerTest{
50
51     @Rule
52     public ExpectedException thrown = ExpectedException.none();
53
54     @Test
55     public void tesParseCamundaResponse () throws ApiException {
56         // String body
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}";
58
59         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
60                       + "\"message\": \"Successfully started the process\"}";
61
62         HttpResponse response = createResponse (200, body, "application/json");
63
64         ResponseHandler respHandler = new ResponseHandler (response, 1);
65         
66         int status = respHandler.getStatus ();
67         assertEquals (status, HttpStatus.SC_ACCEPTED);
68         assertEquals (respHandler.getResponse ().getMessage (), "Successfully started the process");
69
70     }
71     
72     @Test
73     public void tesParseCamundaResponseForCamundaTaskType () throws ApiException {
74        String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
75                       + "\"message\": \"Successfully started the process\"}";
76
77         HttpResponse response = createResponse (200, body, "application/json");
78
79         ResponseHandler respHandler = new ResponseHandler (response, 2);
80         
81         int status = respHandler.getStatus ();
82         assertEquals (status, HttpStatus.SC_ACCEPTED);
83         assertEquals (respHandler.getResponseBody(), body);
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>"
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>";
94
95         HttpResponse response = createResponse (200, body, "text/xml");
96
97         ResponseHandler respHandler = new ResponseHandler (response, 0);
98
99         int status = respHandler.getStatus ();
100         assertEquals (status, HttpStatus.SC_ACCEPTED);
101         assertTrue (respHandler.getResponseBody () != null);
102     }
103
104     @Test
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)));
110         
111         HttpResponse response = createResponse (HttpStatus.SC_NOT_FOUND, "<html>error</html>", "text/html");
112         ResponseHandler respHandler = new ResponseHandler (response, 1);
113
114         int status = respHandler.getStatus ();
115
116         assertEquals (HttpStatus.SC_NOT_IMPLEMENTED, status);
117
118     }
119
120     @Test
121     public void tesGenricErrorResponse () throws ApiException {
122
123         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 500,"
124                       + "\"message\": \"Something went wrong\"}";
125
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");
131     }
132
133     private HttpResponse createResponse (int respStatus, String respBody, String contentType) {
134         HttpResponse response = new BasicHttpResponse (new BasicStatusLine (new ProtocolVersion ("HTTP", 1, 1),
135                                                                             respStatus,
136                                                                             ""));
137         response.setStatusCode (respStatus);
138         try {
139             response.setEntity (new StringEntity (respBody));
140             response.setHeader ("Content-Type", contentType);
141         } catch (Exception e) {
142             e.printStackTrace ();
143         }
144         return response;
145     }
146
147 }