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