Change the header to SO
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / openecomp / mso / camunda / tests / 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.openecomp.mso.camunda.tests;
22
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.IOException;
28
29 import org.apache.http.HttpResponse;
30 import org.apache.http.HttpStatus;
31 import org.apache.http.ProtocolVersion;
32 import org.apache.http.entity.StringEntity;
33 import org.apache.http.message.BasicHttpResponse;
34 import org.apache.http.message.BasicStatusLine;
35 import org.codehaus.jackson.JsonGenerationException;
36 import org.codehaus.jackson.map.JsonMappingException;
37 import org.junit.Test;
38
39 import org.openecomp.mso.apihandler.common.ResponseHandler;
40
41 /**
42  * This class implements test methods of CamundaResoponseHandler.
43  * 
44  *
45  */
46 public class ResponseHandlerTest {
47
48     @Test
49     public void tesParseCamundaResponse () throws JsonGenerationException, JsonMappingException, IOException {
50         // String body
51         // ="{\"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}";
52
53         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
54                       + "\"message\": \"Successfully started the process\"}";
55
56         HttpResponse response = createResponse (200, body, "application/json");
57
58         ResponseHandler respHandler = new ResponseHandler (response, 1);
59
60         int status = respHandler.getStatus ();
61         assertEquals (status, HttpStatus.SC_ACCEPTED);
62         assertEquals (respHandler.getResponse ().getMessage (), "Successfully started the process");
63
64     }
65
66     @Test
67     public void tesParseBpelResponse () throws JsonGenerationException, JsonMappingException, IOException {
68         String body = "<layer3activate:service-response xmlns:layer3activate=\"http://org.openecomp/mso/request/layer3serviceactivate/schema/v1\""
69                       + "xmlns:reqtype=\"http://org.openecomp/mso/request/types/v1\""
70                       + "xmlns:aetgt=\"http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd\""
71                       + "xmlns:types=\"http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd\">"
72                       + "<reqtype:request-id>req5</reqtype:request-id>"
73                       + "<reqtype:request-action>Layer3ServiceActivateRequest</reqtype:request-action>"
74                       + "<reqtype:source>OMX</reqtype:source>"
75                       + "<reqtype:ack-final-indicator>n</reqtype:ack-final-indicator>"
76                       + "</layer3activate:service-response>";
77
78         HttpResponse response = createResponse (200, body, "text/xml");
79
80         ResponseHandler respHandler = new ResponseHandler (response, 0);
81
82         int status = respHandler.getStatus ();
83         assertEquals (status, HttpStatus.SC_ACCEPTED);
84         assertTrue (respHandler.getResponseBody () != null);
85     }
86
87     @Test
88     public void tes404ErrorResponse () throws JsonGenerationException, JsonMappingException, IOException {
89
90         
91         HttpResponse response = createResponse (HttpStatus.SC_NOT_FOUND, "<html>error</html>", "text/html");
92         ResponseHandler respHandler = new ResponseHandler (response, 1);
93
94         int status = respHandler.getStatus ();
95
96         assertEquals (HttpStatus.SC_NOT_IMPLEMENTED, status);
97
98     }
99
100     @Test
101     public void tesGenricErrorResponse () throws JsonGenerationException, JsonMappingException, IOException {
102
103         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 500,"
104                       + "\"message\": \"Something went wrong\"}";
105
106         HttpResponse response = createResponse (500, body, "application/json");
107
108         ResponseHandler respHandler = new ResponseHandler (response, 1);
109
110         int status = respHandler.getStatus ();
111         assertEquals (HttpStatus.SC_BAD_GATEWAY, status);
112         assertEquals (respHandler.getResponse ().getMessage (), "Something went wrong");
113         System.out.println (respHandler.getResponseBody ());
114
115     }
116
117     private HttpResponse createResponse (int respStatus, String respBody, String contentType) {
118         HttpResponse response = new BasicHttpResponse (new BasicStatusLine (new ProtocolVersion ("HTTP", 1, 1),
119                                                                             respStatus,
120                                                                             ""));
121         response.setStatusCode (respStatus);
122         try {
123             response.setEntity (new StringEntity (respBody));
124             response.setHeader ("Content-Type", contentType);
125         } catch (Exception e) {
126             e.printStackTrace ();
127         }
128         return response;
129     }
130
131 }