JUnit tests for CamundaClient
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / onap / so / apihandler / common / CamundaClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.apihandler.common;
25
26
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNull;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36
37 import org.apache.http.HttpResponse;
38 import org.apache.http.HttpStatus;
39 import org.apache.http.ProtocolVersion;
40 import org.apache.http.client.HttpClient;
41 import org.apache.http.client.methods.HttpPost;
42 import org.apache.http.entity.StringEntity;
43 import org.apache.http.message.BasicHttpResponse;
44 import org.apache.http.message.BasicStatusLine;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.mockito.ArgumentCaptor;
49 import org.mockito.Mock;
50 import org.mockito.Mockito;
51 import org.mockito.MockitoAnnotations;
52 import org.springframework.mock.env.MockEnvironment;
53
54 import com.fasterxml.jackson.core.JsonGenerationException;
55 import com.fasterxml.jackson.databind.JsonMappingException;
56
57
58 /**
59  * This class implements test methods of Camunda Beans.
60  *
61  *
62  */
63 public class CamundaClientTest{
64
65
66
67     @Mock
68     private HttpClient mockHttpClient;
69     private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
70
71     @Before
72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74     }
75
76     @Test
77     public void tesCamundaPost() throws JsonGenerationException,
78     JsonMappingException, IOException {
79
80
81         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}";
82
83         HttpResponse mockResponse = createResponse(200, responseBody);
84         mockHttpClient = Mockito.mock(HttpClient.class);
85         ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
86         Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class)))
87         .thenReturn(mockResponse);
88
89         String reqXML = "<xml>test</xml>";
90         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
91         MockEnvironment environment = new MockEnvironment();
92         
93         environment.setProperty("mso.camundaUR", "yourValue1");
94         environment.setProperty("mso.camundaAuth", "E8E19DD16CC90D2E458E8FF9A884CC0452F8F3EB8E321F96038DE38D5C1B0B02DFAE00B88E2CF6E2A4101AB2C011FC161212EE");
95         environment.setProperty("org.onap.so.adapters.network.encryptionKey", "aa3871669d893c7fb8abbcda31b88b4f");
96  
97         
98         RequestClientFactory reqClientFactory = new RequestClientFactory();
99         reqClientFactory.setEnv(environment);
100         RequestClient requestClient = reqClientFactory.getRequestClient(orchestrationURI);
101         
102         requestClient.setClient(mockHttpClient);
103         HttpResponse response = requestClient.post(reqXML, "reqId", "timeout", "version", null, null);
104
105
106         int statusCode = response.getStatusLine().getStatusCode();
107         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
108         assertEquals(statusCode, HttpStatus.SC_OK);
109
110   
111         requestClient = reqClientFactory.getRequestClient(orchestrationURI);
112         requestClient.setClient(mockHttpClient);
113         response = requestClient.post(null, "reqId", null, null, null, null);
114         assertEquals(requestClient.getType(), CommonConstants.CAMUNDA);
115         assertEquals(statusCode, HttpStatus.SC_OK);
116         verify(mockHttpClient,times(2)).execute(httpPostCaptor.capture());
117         assertThat(httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
118         Assert.assertEquals("Basic YXBpaEJwbW46Y2FtdW5kYS1SMTUxMiE=",httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)[0].getValue());
119     }
120
121     private HttpResponse createResponse(int respStatus,
122                                         String respBody) {
123         HttpResponse response = new BasicHttpResponse(
124                                                       new BasicStatusLine(
125                                                                           new ProtocolVersion("HTTP", 1, 1), respStatus, ""));
126         response.setStatusCode(respStatus);
127         try {
128             response.setEntity(new StringEntity(respBody));
129             response.setHeader("Content-Type", "application/json");
130         } catch (Exception e) {
131             e.printStackTrace();
132         }
133         return response;
134     }
135     
136     public String inputStream(String JsonInput)throws IOException{
137         JsonInput = "src/test/resources/CamundaClientTest" + JsonInput;
138         String input = new String(Files.readAllBytes(Paths.get(JsonInput)));
139         return input;
140     }
141     
142     @Test
143     public void wrapVIDRequestTest() throws IOException{
144         CamundaClient testClient = new CamundaClient();
145         testClient.setUrl("/mso/async/services/CreateGenericALaCarteServiceInstance");
146
147         String requestId = "f7ce78bb-423b-11e7-93f8-0050569a796";
148         boolean isBaseVfModule = true;
149         int recipeTimeout = 10000;
150         String requestAction = "createInstance";
151         String serviceInstanceId = "12345679";
152         String pnfCorrelationId = "12345679";
153         String vnfId = "234567891";
154         String vfModuleId = "345678912";
155         String volumeGroupId = "456789123";
156         String networkId = "567891234";
157         String configurationId = "678912345";
158         String serviceType = "testService";
159         String vnfType = "testVnf";
160         String vfModuleType = "vfModuleType";
161         String networkType = "networkType";
162         String requestDetails = "{requestDetails: }";
163         String apiVersion = "6";
164         boolean aLaCarte = true;
165         String requestUri = "v7/serviceInstances/assign";
166         String instanceGroupId = "ff305d54-75b4-431b-adb2-eb6b9e5ff000";
167         
168         String testResult = testClient.wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction, serviceInstanceId, pnfCorrelationId,
169                                                 vnfId, vfModuleId, volumeGroupId, networkId, configurationId, serviceType, 
170                                                 vnfType, vfModuleType, networkType, requestDetails, apiVersion, aLaCarte, requestUri, "", instanceGroupId);
171         String expected = inputStream("/WrappedVIDRequest.json");
172         
173         assertEquals(expected, testResult);
174     }
175
176     @Test
177     public void testPost() throws Exception{
178         CamundaClient testClient = new CamundaClient();
179         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
180         MockEnvironment environment = new MockEnvironment();
181         
182         environment.setProperty("mso.camundaUR", "yourValue1");
183         testClient.setProps(environment);
184         testClient.setClient(mockHttpClient);
185         
186         testClient.setUrl(orchestrationURI);
187         
188         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}";
189         assertNull(testClient.post(responseBody));
190         
191     }
192
193     @Test
194     public void testPostWithRequestClientParameter() throws Exception {
195         CamundaClient testClient = new CamundaClient();
196         String orchestrationURI = "/engine-rest/process-definition/key/dummy/start";
197         HttpResponse mockResponse = createResponse(200, "{}");
198         mockHttpClient = Mockito.mock(HttpClient.class);
199         Mockito.when(mockHttpClient.execute(Mockito.any(HttpPost.class))).thenReturn(mockResponse);
200
201         testClient.setClient(mockHttpClient);
202         testClient.setUrl(orchestrationURI);
203
204         HttpResponse response = testClient.post(new RequestClientParameter.Builder().build());
205
206         assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
207
208     }
209
210
211 }