261b64f2e6cb6e529b5c9e148d7f105707987b8e
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 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.onap.so.apihandlerinfra;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.List;
35 import org.camunda.bpm.engine.impl.persistence.entity.HistoricActivityInstanceEntity;
36 import org.camunda.bpm.engine.impl.persistence.entity.HistoricProcessInstanceEntity;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Spy;
45 import org.mockito.junit.MockitoJUnitRunner;
46 import org.onap.so.apihandlerinfra.exceptions.ContactCamundaException;
47 import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
48 import org.springframework.core.ParameterizedTypeReference;
49 import org.springframework.core.env.Environment;
50 import org.springframework.http.HttpEntity;
51 import org.springframework.http.HttpHeaders;
52 import org.springframework.http.HttpMethod;
53 import org.springframework.http.HttpStatus;
54 import org.springframework.http.ResponseEntity;
55 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
56 import org.springframework.web.client.HttpClientErrorException;
57 import org.springframework.web.client.HttpStatusCodeException;
58 import org.springframework.web.client.ResourceAccessException;
59 import org.springframework.web.client.RestClientException;
60 import org.springframework.web.client.RestTemplate;
61 import com.fasterxml.jackson.core.type.TypeReference;
62 import com.fasterxml.jackson.databind.DeserializationFeature;
63 import com.fasterxml.jackson.databind.ObjectMapper;
64
65 @RunWith(MockitoJUnitRunner.class)
66 public class CamundaRequestHandlerUnitTest {
67
68     @Mock
69     private RestTemplate restTemplate;
70
71     @Mock
72     private RestTemplate restTemplateRetry;
73
74     @Mock
75     private Environment env;
76
77     @InjectMocks
78     @Spy
79     private CamundaRequestHandler camundaRequestHandler;
80
81     @Rule
82     public ExpectedException thrown = ExpectedException.none();
83
84     private static final String REQUEST_ID = "eca3a1b1-43ab-457e-ab1c-367263d148b4";
85     private ResponseEntity<List<HistoricActivityInstanceEntity>> activityInstanceResponse = null;
86     private ResponseEntity<List<HistoricProcessInstanceEntity>> processInstanceResponse = null;
87     private List<HistoricActivityInstanceEntity> activityInstanceList = null;
88     private List<HistoricProcessInstanceEntity> processInstanceList = null;
89
90
91
92     @Before
93     public void setup() throws IOException {
94         ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
95         activityInstanceList = mapper.readValue(
96                 new String(Files.readAllBytes(
97                         Paths.get("src/test/resources/OrchestrationRequest/ActivityInstanceHistoryResponse.json"))),
98                 new TypeReference<List<HistoricActivityInstanceEntity>>() {});
99         processInstanceList = mapper.readValue(
100                 new String(Files.readAllBytes(
101                         Paths.get("src/test/resources/OrchestrationRequest/ProcessInstanceHistoryResponse.json"))),
102                 new TypeReference<List<HistoricProcessInstanceEntity>>() {});
103         processInstanceResponse =
104                 new ResponseEntity<List<HistoricProcessInstanceEntity>>(processInstanceList, HttpStatus.ACCEPTED);
105         activityInstanceResponse =
106                 new ResponseEntity<List<HistoricActivityInstanceEntity>>(activityInstanceList, HttpStatus.ACCEPTED);
107
108         doReturn("/sobpmnengine/history/process-instance?variables=mso-request-id_eq_").when(env)
109                 .getProperty("mso.camunda.rest.history.uri");
110         doReturn("/sobpmnengine/history/activity-instance?processInstanceId=").when(env)
111                 .getProperty("mso.camunda.rest.activity.uri");
112         doReturn("auth").when(env).getRequiredProperty("mso.camundaAuth");
113         doReturn("key").when(env).getRequiredProperty("mso.msoKey");
114         doReturn("http://localhost:8089").when(env).getProperty("mso.camundaURL");
115
116         HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
117         factory.setReadTimeout(30000);
118         factory.setConnectTimeout(30000);
119         restTemplate.setRequestFactory(factory);
120         doReturn(restTemplate).when(camundaRequestHandler).getRestTemplate(false);
121
122         HttpComponentsClientHttpRequestFactory factoryRetry = new HttpComponentsClientHttpRequestFactory();
123         factoryRetry.setReadTimeout(15000);
124         factoryRetry.setConnectTimeout(15000);
125         restTemplate.setRequestFactory(factoryRetry);
126         doReturn(restTemplateRetry).when(camundaRequestHandler).getRestTemplate(true);
127     }
128
129     public HttpHeaders setHeaders() {
130         HttpHeaders headers = new HttpHeaders();
131         List<org.springframework.http.MediaType> acceptableMediaTypes = new ArrayList<>();
132         acceptableMediaTypes.add(org.springframework.http.MediaType.APPLICATION_JSON);
133         headers.setAccept(acceptableMediaTypes);
134         headers.add(HttpHeaders.AUTHORIZATION, "auth");
135
136         return headers;
137     }
138
139     @Test
140     public void getActivityNameTest() throws IOException {
141         String expectedActivityName = "Last task executed: BB to Execute";
142         String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList);
143
144         assertEquals(expectedActivityName, actualActivityName);
145     }
146
147     @Test
148     public void getActivityNameNullActivityNameTest() throws IOException {
149         String expectedActivityName = "Task name is null.";
150         HistoricActivityInstanceEntity activityInstance = new HistoricActivityInstanceEntity();
151         List<HistoricActivityInstanceEntity> activityInstanceList = new ArrayList<>();
152         activityInstanceList.add(activityInstance);
153
154         String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList);
155
156         assertEquals(expectedActivityName, actualActivityName);
157     }
158
159     @Test
160     public void getActivityNameNullListTest() throws IOException {
161         String expectedActivityName = "No results returned on activityInstance history lookup.";
162         List<HistoricActivityInstanceEntity> activityInstanceList = null;
163         String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList);
164
165         assertEquals(expectedActivityName, actualActivityName);
166     }
167
168     @Test
169     public void getActivityNameEmptyListTest() throws IOException {
170         String expectedActivityName = "No results returned on activityInstance history lookup.";
171         List<HistoricActivityInstanceEntity> activityInstanceList = new ArrayList<>();
172         String actualActivityName = camundaRequestHandler.getActivityName(activityInstanceList);
173
174         assertEquals(expectedActivityName, actualActivityName);
175     }
176
177     @Test
178     public void getCamundActivityHistoryNullTest() throws IOException, ContactCamundaException {
179         HistoricProcessInstanceEntity processInstance = new HistoricProcessInstanceEntity();
180         processInstance.setId("c4c6b647-a26e-11e9-b144-0242ac14000b");
181         List<HistoricProcessInstanceEntity> processInstanceList = new ArrayList<>();
182         processInstanceList.add(processInstance);
183         ResponseEntity<List<HistoricProcessInstanceEntity>> response =
184                 new ResponseEntity<>(processInstanceList, HttpStatus.OK);
185         doReturn(null).when(camundaRequestHandler).getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b");
186
187         String actualTaskName = camundaRequestHandler.getTaskInformation(response, REQUEST_ID);
188
189         assertNull(actualTaskName);
190     }
191
192     @Test
193     public void getCamundActivityHistoryErrorTest() throws IOException, ContactCamundaException {
194         HistoricProcessInstanceEntity processInstance = new HistoricProcessInstanceEntity();
195         processInstance.setId("c4c6b647-a26e-11e9-b144-0242ac14000b");
196         List<HistoricProcessInstanceEntity> processInstanceList = new ArrayList<>();
197         processInstanceList.add(processInstance);
198         ResponseEntity<List<HistoricProcessInstanceEntity>> response =
199                 new ResponseEntity<>(processInstanceList, HttpStatus.OK);
200         doThrow(RestClientException.class).when(camundaRequestHandler)
201                 .getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b");
202
203         String actualTaskName = camundaRequestHandler.getTaskInformation(response, REQUEST_ID);
204
205         assertNull(actualTaskName);
206     }
207
208     @Test
209     public void getTaskName() throws IOException, ContactCamundaException {
210         doReturn(processInstanceResponse).when(camundaRequestHandler).getCamundaProcessInstanceHistory(REQUEST_ID,
211                 false);
212         doReturn(activityInstanceResponse).when(camundaRequestHandler)
213                 .getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b");
214         doReturn("Last task executed: BB to Execute").when(camundaRequestHandler).getActivityName(activityInstanceList);
215         String expectedTaskName = "Last task executed: BB to Execute";
216
217         String actualTaskName = camundaRequestHandler.getTaskName(REQUEST_ID);
218
219         assertEquals(expectedTaskName, actualTaskName);
220     }
221
222     @Test
223     public void getTaskNameNullProcessInstanceListTest() throws IOException, ContactCamundaException {
224         ResponseEntity<List<HistoricProcessInstanceEntity>> response = new ResponseEntity<>(null, HttpStatus.OK);
225
226         String actual = camundaRequestHandler.getTaskInformation(response, REQUEST_ID);
227
228         assertNull(actual);
229     }
230
231     @Test
232     public void getTaskNameNullProcessInstanceIdTest() throws IOException, ContactCamundaException {
233         HistoricProcessInstanceEntity processInstance = new HistoricProcessInstanceEntity();
234         List<HistoricProcessInstanceEntity> processInstanceList = new ArrayList<>();
235         processInstanceList.add(processInstance);
236         ResponseEntity<List<HistoricProcessInstanceEntity>> response =
237                 new ResponseEntity<>(processInstanceList, HttpStatus.OK);
238
239         String actual = camundaRequestHandler.getTaskInformation(response, REQUEST_ID);
240
241         assertNull(actual);
242     }
243
244     @Test
245     public void getTaskNameEmptyProcessInstanceListTest() throws IOException, ContactCamundaException {
246         List<HistoricProcessInstanceEntity> processInstanceList = new ArrayList<>();
247         ResponseEntity<List<HistoricProcessInstanceEntity>> response =
248                 new ResponseEntity<>(processInstanceList, HttpStatus.OK);
249
250         String actual = camundaRequestHandler.getTaskInformation(response, REQUEST_ID);
251
252         assertNull(actual);
253     }
254
255     @Test
256     public void getTaskNameProcessInstanceLookupFailureTest() throws IOException, ContactCamundaException {
257         doThrow(HttpClientErrorException.class).when(camundaRequestHandler).getCamundaProcessInstanceHistory(REQUEST_ID,
258                 false);
259
260         String result = camundaRequestHandler.getTaskName(REQUEST_ID);
261         assertNull(result);
262     }
263
264     @Test
265     public void getCamundaActivityHistoryTest() throws IOException, ContactCamundaException {
266         HttpHeaders headers = setHeaders();
267         HttpEntity<?> requestEntity = new HttpEntity<>(headers);
268         String targetUrl = "http://localhost:8089/sobpmnengine/history/activity-instance?processInstanceId="
269                 + "c4c6b647-a26e-11e9-b144-0242ac14000b";
270         doReturn(activityInstanceResponse).when(restTemplate).exchange(targetUrl, HttpMethod.GET, requestEntity,
271                 new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {});
272         doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key");
273         ResponseEntity<List<HistoricActivityInstanceEntity>> actualResponse =
274                 camundaRequestHandler.getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b");
275         assertEquals(activityInstanceResponse, actualResponse);
276     }
277
278     @Test
279     public void getCamundaActivityHistoryErrorTest() {
280         HttpHeaders headers = setHeaders();
281         HttpEntity<?> requestEntity = new HttpEntity<>(headers);
282         String targetUrl = "http://localhost:8089/sobpmnengine/history/activity-instance?processInstanceId="
283                 + "c4c6b647-a26e-11e9-b144-0242ac14000b";
284         doThrow(new ResourceAccessException("IOException")).when(restTemplate).exchange(targetUrl, HttpMethod.GET,
285                 requestEntity, new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {});
286         doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key");
287
288         thrown.expect(ResourceAccessException.class);
289         camundaRequestHandler.getCamundaActivityHistory("c4c6b647-a26e-11e9-b144-0242ac14000b");
290
291         verify(restTemplate, times(1)).exchange(targetUrl, HttpMethod.GET, requestEntity,
292                 new ParameterizedTypeReference<List<HistoricActivityInstanceEntity>>() {});
293     }
294
295     @Test
296     public void getCamundaProccesInstanceHistoryTest() throws IOException, ContactCamundaException {
297         HttpHeaders headers = setHeaders();
298         HttpEntity<?> requestEntity = new HttpEntity<>(headers);
299         String targetUrl =
300                 "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID;
301         doReturn(processInstanceResponse).when(restTemplate).exchange(targetUrl, HttpMethod.GET, requestEntity,
302                 new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {});
303         doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key");
304
305         ResponseEntity<List<HistoricProcessInstanceEntity>> actualResponse =
306                 camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID, false);
307         assertEquals(processInstanceResponse, actualResponse);
308     }
309
310     @Test
311     public void getCamundaProccesInstanceHistoryRetryTest() {
312         HttpHeaders headers = setHeaders();
313         HttpEntity<?> requestEntity = new HttpEntity<>(headers);
314         String targetUrl =
315                 "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID;
316         doThrow(new ResourceAccessException("I/O error")).when(restTemplateRetry).exchange(targetUrl, HttpMethod.GET,
317                 requestEntity, new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {});
318         doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key");
319
320         thrown.expect(ResourceAccessException.class);
321         camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID, true);
322
323         verify(restTemplateRetry, times(2)).exchange(targetUrl, HttpMethod.GET, requestEntity,
324                 new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {});
325     }
326
327     @Test
328     public void getCamundaProccesInstanceHistoryNoRetryTest() {
329         HttpHeaders headers = setHeaders();
330         HttpEntity<?> requestEntity = new HttpEntity<>(headers);
331         String targetUrl =
332                 "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID;
333         doThrow(HttpClientErrorException.class).when(restTemplate).exchange(targetUrl, HttpMethod.GET, requestEntity,
334                 new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {});
335         doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key");
336
337         thrown.expect(HttpStatusCodeException.class);
338         camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID, false);
339
340         verify(restTemplate, times(1)).exchange(targetUrl, HttpMethod.GET, requestEntity,
341                 new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {});
342     }
343
344     @Test
345     public void getCamundaProccesInstanceHistoryFailThenSuccessTest() throws IOException, ContactCamundaException {
346         HttpHeaders headers = setHeaders();
347         HttpEntity<?> requestEntity = new HttpEntity<>(headers);
348         String targetUrl =
349                 "http://localhost:8089/sobpmnengine/history/process-instance?variables=mso-request-id_eq_" + REQUEST_ID;
350         when(restTemplateRetry.exchange(targetUrl, HttpMethod.GET, requestEntity,
351                 new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {}))
352                         .thenThrow(new ResourceAccessException("I/O Exception")).thenReturn(processInstanceResponse);
353         doReturn(headers).when(camundaRequestHandler).setCamundaHeaders("auth", "key");
354
355         ResponseEntity<List<HistoricProcessInstanceEntity>> actualResponse =
356                 camundaRequestHandler.getCamundaProcessInstanceHistory(REQUEST_ID, true);
357
358         assertEquals(processInstanceResponse, actualResponse);
359         verify(restTemplateRetry, times(2)).exchange(targetUrl, HttpMethod.GET, requestEntity,
360                 new ParameterizedTypeReference<List<HistoricProcessInstanceEntity>>() {});
361     }
362
363     @Test
364     public void setCamundaHeadersTest() throws ContactCamundaException, RequestDbFailureException {
365         String encryptedAuth = "015E7ACF706C6BBF85F2079378BDD2896E226E09D13DC2784BA309E27D59AB9FAD3A5E039DF0BB8408"; // user:password
366         String key = "07a7159d3bf51a0e53be7a8f89699be7";
367
368         HttpHeaders headers = camundaRequestHandler.setCamundaHeaders(encryptedAuth, key);
369         List<org.springframework.http.MediaType> acceptedType = headers.getAccept();
370
371         String expectedAcceptedType = "application/json";
372         assertEquals(expectedAcceptedType, acceptedType.get(0).toString());
373         String basicAuth = headers.getFirst(HttpHeaders.AUTHORIZATION);
374         String expectedBasicAuth = "Basic dXNlcjpwYXNzd29yZA==";
375
376         assertEquals(expectedBasicAuth, basicAuth);
377     }
378 }