Containerization feature of SO
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.apihandler.common;
22
23
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;
29
30 import java.io.IOException;
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.apihandler.common.ErrorNumbers;
42 import org.onap.so.apihandler.common.ResponseHandler;
43
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;
49
50 /**
51  * This class implements test methods of CamundaResoponseHandler.
52  * 
53  *
54  */
55 public class ResponseHandlerTest{
56
57     @Rule
58     public ExpectedException thrown = ExpectedException.none();
59
60     @Test
61     public void tesParseCamundaResponse () throws ApiException {
62         // String body
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}";
64
65         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
66                       + "\"message\": \"Successfully started the process\"}";
67
68         HttpResponse response = createResponse (200, body, "application/json");
69
70         ResponseHandler respHandler = new ResponseHandler (response, 1);
71
72         int status = respHandler.getStatus ();
73         assertEquals (status, HttpStatus.SC_ACCEPTED);
74         assertEquals (respHandler.getResponse ().getMessage (), "Successfully started the process");
75
76     }
77
78     @Test
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>";
86
87         HttpResponse response = createResponse (200, body, "text/xml");
88
89         ResponseHandler respHandler = new ResponseHandler (response, 0);
90
91         int status = respHandler.getStatus ();
92         assertEquals (status, HttpStatus.SC_ACCEPTED);
93         assertTrue (respHandler.getResponseBody () != null);
94     }
95
96     @Test
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)));
102         
103         HttpResponse response = createResponse (HttpStatus.SC_NOT_FOUND, "<html>error</html>", "text/html");
104         ResponseHandler respHandler = new ResponseHandler (response, 1);
105
106         int status = respHandler.getStatus ();
107
108         assertEquals (HttpStatus.SC_NOT_IMPLEMENTED, status);
109
110     }
111
112     @Test
113     public void tesGenricErrorResponse () throws ApiException {
114
115         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 500,"
116                       + "\"message\": \"Something went wrong\"}";
117
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");
123     }
124
125     private HttpResponse createResponse (int respStatus, String respBody, String contentType) {
126         HttpResponse response = new BasicHttpResponse (new BasicStatusLine (new ProtocolVersion ("HTTP", 1, 1),
127                                                                             respStatus,
128                                                                             ""));
129         response.setStatusCode (respStatus);
130         try {
131             response.setEntity (new StringEntity (respBody));
132             response.setHeader ("Content-Type", contentType);
133         } catch (Exception e) {
134             e.printStackTrace ();
135         }
136         return response;
137     }
138
139 }