Change the header to SO
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / openecomp / mso / camunda / tests / CamundaClientTest.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
26 import java.io.IOException;
27
28 import java.util.Properties;
29
30 import org.apache.http.HttpResponse;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.ProtocolVersion;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.entity.StringEntity;
36 import org.apache.http.message.BasicHttpResponse;
37 import org.apache.http.message.BasicStatusLine;
38 import org.codehaus.jackson.JsonGenerationException;
39 import org.codehaus.jackson.map.JsonMappingException;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.mockito.MockitoAnnotations;
45
46 import org.openecomp.mso.apihandler.common.CommonConstants;
47 import org.openecomp.mso.apihandler.common.RequestClient;
48 import org.openecomp.mso.apihandler.common.RequestClientFactory;
49 import org.openecomp.mso.properties.MsoJavaProperties;
50
51
52 /**
53  * This class implements test methods of Camunda Beans.
54  *
55  *
56  */
57 public class CamundaClientTest {
58
59
60
61     @Mock
62     private HttpClient mockHttpClient;
63
64     @Before
65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67     }
68
69     @Test
70     public void tesCamundaPost() throws JsonGenerationException,
71     JsonMappingException, IOException {
72
73
74         String responseBody ="{\"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}";
75
76         HttpResponse mockResponse = createResponse(200, responseBody);
77         mockHttpClient = Mockito.mock(HttpClient.class);
78         Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class)))
79         .thenReturn(mockResponse);
80
81         String reqXML = "<xml>test</xml>";
82         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
83
84         MsoJavaProperties props = new MsoJavaProperties();
85         props.setProperty(CommonConstants.CAMUNDA_URL, "http://localhost:8089");
86
87         RequestClient requestClient = RequestClientFactory.getRequestClient(orchestrationURI, props);
88         requestClient.setClient(mockHttpClient);
89         HttpResponse response = requestClient.post(reqXML, "reqId", "timeout", "version", null, null);
90
91
92         int statusCode = response.getStatusLine().getStatusCode();
93         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
94         assertEquals(statusCode, HttpStatus.SC_OK);
95
96         props.setProperty (CommonConstants.CAMUNDA_AUTH, "ABCD1234");
97         requestClient = RequestClientFactory.getRequestClient(orchestrationURI, props);
98         requestClient.setClient(mockHttpClient);
99         response = requestClient.post(null, "reqId", null, null, null, null);
100         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
101         assertEquals(statusCode, HttpStatus.SC_OK);
102     }
103
104     private HttpResponse createResponse(int respStatus,
105                                         String respBody) {
106         HttpResponse response = new BasicHttpResponse(
107                                                       new BasicStatusLine(
108                                                                           new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
109         response.setStatusCode(respStatus);
110         try {
111             response.setEntity(new StringEntity(respBody));
112             response.setHeader("Content-Type", "application/json");
113         } catch (Exception e) {
114             e.printStackTrace();
115         }
116         return response;
117     }
118
119
120
121
122
123 }