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