d4d563a88bb8dd54a4610059b4e4321eec7f69cb
[so.git] /
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 public class ResponseHandlerTest {
45
46     @Test
47     public void tesParseCamundaResponse() throws JsonGenerationException, JsonMappingException, IOException {
48         // String body
49         // ="{\"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}";
50
51         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 200,"
52                 + "\"message\": \"Successfully started the process\"}";
53
54         HttpResponse response = createResponse(200, body, "application/json");
55
56         ResponseHandler respHandler = new ResponseHandler(response, 1);
57
58         int status = respHandler.getStatus();
59         assertEquals(status, HttpStatus.SC_ACCEPTED);
60         assertEquals(respHandler.getResponse().getMessage(), "Successfully started the process");
61
62     }
63
64     @Test
65     public void tesParseBpelResponse() throws JsonGenerationException, JsonMappingException, IOException {
66         String body = "<layer3activate:service-response xmlns:layer3activate=\"http://org.openecomp/mso/request/layer3serviceactivate/schema/v1\""
67                 + "xmlns:reqtype=\"http://org.openecomp/mso/request/types/v1\""
68                 + "xmlns:aetgt=\"http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd\""
69                 + "xmlns:types=\"http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd\">"
70                 + "<reqtype:request-id>req5</reqtype:request-id>"
71                 + "<reqtype:request-action>Layer3ServiceActivateRequest</reqtype:request-action>"
72                 + "<reqtype:source>OMX</reqtype:source>"
73                 + "<reqtype:ack-final-indicator>n</reqtype:ack-final-indicator>"
74                 + "</layer3activate:service-response>";
75
76         HttpResponse response = createResponse(200, body, "text/xml");
77
78         ResponseHandler respHandler = new ResponseHandler(response, 0);
79
80         int status = respHandler.getStatus();
81         assertEquals(status, HttpStatus.SC_ACCEPTED);
82         assertTrue(respHandler.getResponseBody() != null);
83     }
84
85     @Test
86     public void tes404ErrorResponse() throws JsonGenerationException, JsonMappingException, IOException {
87
88
89         HttpResponse response = createResponse(HttpStatus.SC_NOT_FOUND, "<html>error</html>", "text/html");
90         ResponseHandler respHandler = new ResponseHandler(response, 1);
91
92         int status = respHandler.getStatus();
93
94         assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, status);
95
96     }
97
98     @Test
99     public void tesGenricErrorResponse() throws JsonGenerationException, JsonMappingException, IOException {
100
101         String body = "{ \"response\": \"<xml>xml</xml>\"," + "\"messageCode\": 500,"
102                 + "\"message\": \"Something went wrong\"}";
103
104         HttpResponse response = createResponse(500, body, "application/json");
105
106         ResponseHandler respHandler = new ResponseHandler(response, 1);
107
108         int status = respHandler.getStatus();
109         assertEquals(HttpStatus.SC_BAD_GATEWAY, status);
110         assertEquals(respHandler.getResponse().getMessage(), "Something went wrong");
111         System.out.println(respHandler.getResponseBody());
112
113     }
114
115     private HttpResponse createResponse(int respStatus, String respBody, String contentType) {
116         HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
117                 respStatus,
118                 ""));
119         response.setStatusCode(respStatus);
120         try {
121             response.setEntity(new StringEntity(respBody));
122             response.setHeader("Content-Type", contentType);
123         } catch (Exception e) {
124             e.printStackTrace();
125         }
126         return response;
127     }
128
129 }