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