3ca184ebaa7b22941f7dda4724ef7b5e2eb62a24
[so.git] / so-monitoring / so-monitoring-service / src / test / java / org / onap / so / monitoring / rest / api / SoMonitoringControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.monitoring.rest.api;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.onap.so.monitoring.configuration.rest.RestTemplateConfiguration.CAMUNDA_REST_TEMPLATE;
26 import static org.onap.so.monitoring.configuration.rest.RestTemplateConfiguration.DATABASE_REST_TEMPLATE;
27 import static org.onap.so.monitoring.rest.api.Constants.ACTIVITY_INSTANCE_RESPONSE_JSON_FILE;
28 import static org.onap.so.monitoring.rest.api.Constants.EMPTY_ARRAY_RESPONSE;
29 import static org.onap.so.monitoring.rest.api.Constants.EMPTY_STRING;
30 import static org.onap.so.monitoring.rest.api.Constants.END_TIME_IN_MS;
31 import static org.onap.so.monitoring.rest.api.Constants.ID;
32 import static org.onap.so.monitoring.rest.api.Constants.PROCCESS_INSTANCE_RESPONSE_JSON_FILE;
33 import static org.onap.so.monitoring.rest.api.Constants.PROCESS_DEF_RESPONSE_JSON_FILE;
34 import static org.onap.so.monitoring.rest.api.Constants.PROCESS_INSTACE_ID;
35 import static org.onap.so.monitoring.rest.api.Constants.PROCESS_INSTANCE_VARIABLES_RESPONSE_JSON_FILE;
36 import static org.onap.so.monitoring.rest.api.Constants.PROCRESS_DEF_ID;
37 import static org.onap.so.monitoring.rest.api.Constants.SEARCH_RESULT_RESPONSE_JSON_FILE;
38 import static org.onap.so.monitoring.rest.api.Constants.SINGLE_PROCCESS_INSTANCE_RESPONSE_JSON_FILE;
39 import static org.onap.so.monitoring.rest.api.Constants.START_TIME_IN_MS;
40 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
41 import static org.springframework.test.web.client.response.MockRestResponseCreators.withBadRequest;
42 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
43 import static org.springframework.test.web.client.response.MockRestResponseCreators.withUnauthorizedRequest;
44
45 import java.io.IOException;
46 import java.nio.file.Files;
47 import java.nio.file.Path;
48 import java.util.Collections;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52
53 import javax.ws.rs.core.Response;
54 import javax.ws.rs.core.Response.Status;
55
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.junit.runner.RunWith;
59 import org.onap.so.monitoring.configuration.camunda.CamundaRestUrlProvider;
60 import org.onap.so.monitoring.configuration.database.DatabaseUrlProvider;
61 import org.onap.so.monitoring.model.ActivityInstanceDetail;
62 import org.onap.so.monitoring.model.ProcessDefinitionDetail;
63 import org.onap.so.monitoring.model.ProcessInstanceDetail;
64 import org.onap.so.monitoring.model.ProcessInstanceIdDetail;
65 import org.onap.so.monitoring.model.ProcessInstanceVariableDetail;
66 import org.onap.so.monitoring.model.SoInfraRequest;
67 import org.springframework.beans.factory.annotation.Autowired;
68 import org.springframework.beans.factory.annotation.Qualifier;
69 import org.springframework.boot.test.context.SpringBootTest;
70 import org.springframework.http.MediaType;
71 import org.springframework.test.context.ActiveProfiles;
72 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
73 import org.springframework.test.web.client.MockRestServiceServer;
74 import org.springframework.web.client.RestTemplate;
75
76
77 /**
78  * @author waqas.ikram@ericsson.com, andrei.barcovschi@ericsson.com
79  */
80 @RunWith(SpringJUnit4ClassRunner.class)
81 @ActiveProfiles("test")
82 @SpringBootTest
83 public class SoMonitoringControllerTest {
84
85     @Autowired
86     @Qualifier(CAMUNDA_REST_TEMPLATE)
87     private RestTemplate restTemplate;
88
89     @Autowired
90     @Qualifier(DATABASE_REST_TEMPLATE)
91     private RestTemplate dataBaseRestTemplate;
92
93     @Autowired
94     private CamundaRestUrlProvider urlProvider;
95
96     @Autowired
97     private DatabaseUrlProvider databaseUrlProvider;
98
99     private MockRestServiceServer camundaMockServer;
100
101     private MockRestServiceServer databaseMockServer;
102
103     @Autowired
104     private SoMonitoringController objUnderTest;
105
106     @Before
107     public void setUp() throws Exception {
108         camundaMockServer = MockRestServiceServer.bindTo(restTemplate).build();
109         databaseMockServer = MockRestServiceServer.bindTo(dataBaseRestTemplate).build();
110     }
111
112     @Test
113     public void test_GetProcessInstance_SuccessResponseWithDataFromCamunda() throws Exception {
114         final String jsonString = getJsonResponse(PROCCESS_INSTANCE_RESPONSE_JSON_FILE);
115         this.camundaMockServer.expect(requestTo(urlProvider.getHistoryProcessInstanceUrl(ID)))
116                 .andRespond(withSuccess(jsonString, MediaType.APPLICATION_JSON));
117
118         final Response response = objUnderTest.getProcessInstanceId(ID);
119
120         assertEquals(Status.OK.getStatusCode(), response.getStatus());
121         final ProcessInstanceIdDetail actualProcessInstance = (ProcessInstanceIdDetail) response.getEntity();
122         assertEquals("dba707b6-8c02-11e8-a6ba-022a5dba5402", actualProcessInstance.getProcessInstanceId());
123     }
124
125     @Test
126     public void test_GetProcessInstance_SuccessResponseWithEmptyDataFromCamunda() throws Exception {
127         final String jsonString = EMPTY_ARRAY_RESPONSE;
128         this.camundaMockServer.expect(requestTo(urlProvider.getHistoryProcessInstanceUrl(ID)))
129                 .andRespond(withSuccess(jsonString, MediaType.APPLICATION_JSON));
130
131         final Response response = objUnderTest.getProcessInstanceId(ID);
132         assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());
133         assertNull(response.getEntity());
134     }
135
136     @Test
137     public void test_GetProcessInstance_FailureResponseWithEmptyDataFromCamunda() throws Exception {
138         this.camundaMockServer.expect(requestTo(urlProvider.getHistoryProcessInstanceUrl(ID)))
139                 .andRespond(withBadRequest());
140
141         final Response response = objUnderTest.getProcessInstanceId(ID);
142         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
143     }
144
145     @Test
146     public void test_GetProcessInstance_UnauthorizedRequestFromCamunda() throws Exception {
147         this.camundaMockServer.expect(requestTo(urlProvider.getHistoryProcessInstanceUrl(ID)))
148                 .andRespond(withUnauthorizedRequest());
149
150         final Response response = objUnderTest.getProcessInstanceId(ID);
151         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
152         assertNotNull(response.getEntity());
153     }
154
155     @Test
156     public void test_GetSinlgeProcessInstance_SuccessResponseWithDataFromCamunda() throws Exception {
157         final String jsonString = getJsonResponse(SINGLE_PROCCESS_INSTANCE_RESPONSE_JSON_FILE);
158         this.camundaMockServer.expect(requestTo(urlProvider.getSingleProcessInstanceUrl(PROCESS_INSTACE_ID)))
159                 .andRespond(withSuccess(jsonString, MediaType.APPLICATION_JSON));
160
161         final Response response = objUnderTest.getSingleProcessInstance(PROCESS_INSTACE_ID);
162
163         assertEquals(Status.OK.getStatusCode(), response.getStatus());
164         final ProcessInstanceDetail actualProcessInstance = (ProcessInstanceDetail) response.getEntity();
165         assertEquals(PROCESS_INSTACE_ID, actualProcessInstance.getProcessInstanceId());
166         assertEquals("EricssonNetworkSliceV1:3:28f9e0fc-9b00-11e8-a57a-022ac90273ed",
167                 actualProcessInstance.getProcessDefinitionId());
168         assertEquals("EricssonNetworkSliceV1", actualProcessInstance.getProcessDefinitionName());
169         assertNull(actualProcessInstance.getSuperProcessInstanceId());
170     }
171
172     @Test
173     public void test_GetSingleProcessInstance_WithBadRequestResponseFromCamunda() throws Exception {
174         this.camundaMockServer.expect(requestTo(urlProvider.getSingleProcessInstanceUrl(PROCESS_INSTACE_ID)))
175                 .andRespond(withBadRequest());
176
177         final Response response = objUnderTest.getSingleProcessInstance(PROCESS_INSTACE_ID);
178         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
179         assertNotNull(response.getEntity());
180     }
181
182     @Test
183     public void test_GetSingleProcessInstance_WithUnauthorizedRequestResponseFromCamunda() throws Exception {
184         this.camundaMockServer.expect(requestTo(urlProvider.getSingleProcessInstanceUrl(PROCESS_INSTACE_ID)))
185                 .andRespond(withUnauthorizedRequest());
186
187         final Response response = objUnderTest.getSingleProcessInstance(PROCESS_INSTACE_ID);
188         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
189         assertNotNull(response.getEntity());
190     }
191
192     @Test
193     public void test_GetSingleProcessInstance_NullAndEmptyProcessInstanceIdFromCamunda() throws Exception {
194
195         Response response = objUnderTest.getSingleProcessInstance(null);
196         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
197         assertNotNull(response.getEntity());
198
199         response = objUnderTest.getSingleProcessInstance("");
200         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
201         assertNotNull(response.getEntity());
202     }
203
204
205     @Test
206     public void test_GetProcessInstance_EmptyRequestID() throws Exception {
207
208         Response response = objUnderTest.getProcessInstanceId(EMPTY_STRING);
209         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
210
211         response = objUnderTest.getProcessInstanceId(null);
212         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
213     }
214
215
216     @Test
217     public void test_GetProcessDefinitionXml_SuccessResponseWithDataFromCamunda() throws Exception {
218         final String jsonString = getJsonResponse(PROCESS_DEF_RESPONSE_JSON_FILE);
219         this.camundaMockServer.expect(requestTo(urlProvider.getProcessDefinitionUrl(PROCRESS_DEF_ID)))
220                 .andRespond(withSuccess(jsonString, MediaType.APPLICATION_JSON));
221
222         final Response response = objUnderTest.getProcessDefinitionXml(PROCRESS_DEF_ID);
223         assertEquals(Status.OK.getStatusCode(), response.getStatus());
224
225         final ProcessDefinitionDetail actual = (ProcessDefinitionDetail) response.getEntity();
226         assertEquals(PROCRESS_DEF_ID, actual.getProcessDefinitionId());
227     }
228
229     @Test
230     public void test_GetProcessDefinitionXml_BadRequestResponseFromCamunda() throws Exception {
231         this.camundaMockServer.expect(requestTo(urlProvider.getProcessDefinitionUrl(PROCRESS_DEF_ID)))
232                 .andRespond(withBadRequest());
233
234         final Response response = objUnderTest.getProcessDefinitionXml(PROCRESS_DEF_ID);
235         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
236         assertNotNull(response.getEntity());
237     }
238
239     @Test
240     public void test_GetProcessDefinitionXml_UnauthorizedRequestFromCamunda() throws Exception {
241         this.camundaMockServer.expect(requestTo(urlProvider.getProcessDefinitionUrl(PROCRESS_DEF_ID)))
242                 .andRespond(withUnauthorizedRequest());
243
244         final Response response = objUnderTest.getProcessDefinitionXml(PROCRESS_DEF_ID);
245         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
246         assertNotNull(response.getEntity());
247     }
248
249     @Test
250     public void test_GetProcessDefinitionXml_NullValues() throws Exception {
251         Response response = objUnderTest.getProcessDefinitionXml(EMPTY_STRING);
252         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
253         assertNotNull(response.getEntity());
254
255         response = objUnderTest.getProcessDefinitionXml(null);
256         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
257         assertNotNull(response.getEntity());
258     }
259
260     @Test
261     public void test_GetActivityInstanceDetail_SuccessResponseWithDataFromCamunda() throws Exception {
262         final String jsonString = getJsonResponse(ACTIVITY_INSTANCE_RESPONSE_JSON_FILE);
263         this.camundaMockServer.expect(requestTo(urlProvider.getActivityInstanceUrl(PROCESS_INSTACE_ID)))
264                 .andRespond(withSuccess(jsonString, MediaType.APPLICATION_JSON));
265
266         final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
267         @SuppressWarnings("unchecked")
268         final List<ActivityInstanceDetail> actual = (List<ActivityInstanceDetail>) response.getEntity();
269         assertEquals(Status.OK.getStatusCode(), response.getStatus());
270         assertEquals(12, actual.size());
271         final ActivityInstanceDetail activityInstanceDetail = actual.get(0);
272         assertEquals("createVCPE_startEvent", activityInstanceDetail.getActivityId());
273         assertEquals("Start Flow", activityInstanceDetail.getActivityName());
274         assertEquals("startEvent", activityInstanceDetail.getActivityType());
275         assertEquals(PROCESS_INSTACE_ID, activityInstanceDetail.getProcessInstanceId());
276         assertNull(activityInstanceDetail.getCalledProcessInstanceId());
277         assertEquals("26", activityInstanceDetail.getDurationInMillis());
278         assertEquals("2018-08-03T16:00:31.815+0000", activityInstanceDetail.getStartTime());
279         assertEquals("2018-08-03T16:00:31.841+0000", activityInstanceDetail.getEndTime());
280
281         final ActivityInstanceDetail callActivityInstanceDetail = actual.get(4);
282         assertEquals("DecomposeService", callActivityInstanceDetail.getActivityId());
283         assertEquals("Call Decompose Service", callActivityInstanceDetail.getActivityName());
284         assertEquals("callActivity", callActivityInstanceDetail.getActivityType());
285         assertEquals("59d99609-9736-11e8-8caf-022ac9304eeb", callActivityInstanceDetail.getCalledProcessInstanceId());
286     }
287
288     @Test
289     public void test_GetActivityInstanceDetail_SuccessResponseWithEmptyDataFromCamunda() throws Exception {
290         this.camundaMockServer.expect(requestTo(urlProvider.getActivityInstanceUrl(PROCESS_INSTACE_ID)))
291                 .andRespond(withSuccess(EMPTY_ARRAY_RESPONSE, MediaType.APPLICATION_JSON));
292
293         final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
294         assertEquals(Status.OK.getStatusCode(), response.getStatus());
295         assertNotNull(response.getEntity());
296     }
297
298     @Test
299     public void test_GetActivityInstanceDetail_UnauthorizedRequestFromCamunda() throws Exception {
300         this.camundaMockServer.expect(requestTo(urlProvider.getActivityInstanceUrl(PROCESS_INSTACE_ID)))
301                 .andRespond(withUnauthorizedRequest());
302
303         final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
304         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
305         assertNotNull(response.getEntity());
306     }
307
308     @Test
309     public void test_GetActivityInstanceDetail_BadRequestFromCamunda() throws Exception {
310         this.camundaMockServer.expect(requestTo(urlProvider.getActivityInstanceUrl(PROCESS_INSTACE_ID)))
311                 .andRespond(withBadRequest());
312
313         final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
314         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
315         assertNotNull(response.getEntity());
316     }
317
318     @Test
319     public void test_GetActivityInstanceDetail_NullValues() throws Exception {
320         Response response = objUnderTest.getActivityInstanceDetail(EMPTY_STRING);
321         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
322         assertNotNull(response.getEntity());
323
324         response = objUnderTest.getActivityInstanceDetail(null);
325         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
326         assertNotNull(response.getEntity());
327     }
328
329     @Test
330     public void test_GetProcessInstanceVariables_SuccessResponseWithDataFromCamunda() throws Exception {
331         final String jsonString = getJsonResponse(PROCESS_INSTANCE_VARIABLES_RESPONSE_JSON_FILE);
332         this.camundaMockServer.expect(requestTo(urlProvider.getProcessInstanceVariablesUrl(PROCESS_INSTACE_ID)))
333                 .andRespond(withSuccess(jsonString, MediaType.APPLICATION_JSON));
334
335         final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
336
337         assertEquals(Status.OK.getStatusCode(), response.getStatus());
338         @SuppressWarnings("unchecked")
339         final List<ProcessInstanceVariableDetail> actual = (List<ProcessInstanceVariableDetail>) response.getEntity();
340         assertEquals(230, actual.size());
341
342         final ProcessInstanceVariableDetail variableDetail = actual.get(0);
343         assertEquals("serviceType", variableDetail.getName());
344         assertEquals("String", variableDetail.getType());
345         assertEquals("PNFSERVICE", variableDetail.getValue());
346     }
347
348     @Test
349     public void test_GetProcessInstanceVariables_SuccessResponseWithEmptyDataFromCamunda() throws Exception {
350         this.camundaMockServer.expect(requestTo(urlProvider.getProcessInstanceVariablesUrl(PROCESS_INSTACE_ID)))
351                 .andRespond(withSuccess(EMPTY_ARRAY_RESPONSE, MediaType.APPLICATION_JSON));
352
353         final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
354
355         assertEquals(Status.OK.getStatusCode(), response.getStatus());
356         assertNotNull(response.getEntity());
357     }
358
359     @Test
360     public void test_GetProcessInstanceVariables_BadRequestFromCamunda() throws Exception {
361         this.camundaMockServer.expect(requestTo(urlProvider.getProcessInstanceVariablesUrl(PROCESS_INSTACE_ID)))
362                 .andRespond(withBadRequest());
363
364         final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
365
366         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
367         assertNotNull(response.getEntity());
368     }
369
370     @Test
371     public void test_GetProcessInstanceVariables_UnauthorizedRequestFromCamunda() throws Exception {
372         this.camundaMockServer.expect(requestTo(urlProvider.getProcessInstanceVariablesUrl(PROCESS_INSTACE_ID)))
373                 .andRespond(withUnauthorizedRequest());
374
375         final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
376
377         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
378         assertNotNull(response.getEntity());
379     }
380
381     @Test
382     public void test_GetProcessInstanceVariables_NullAndEmptyValues() throws Exception {
383
384         Response response = objUnderTest.getProcessInstanceVariables(EMPTY_STRING);
385
386         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
387         assertNotNull(response.getEntity());
388
389         response = objUnderTest.getProcessInstanceVariables(null);
390
391         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
392         assertNotNull(response.getEntity());
393
394     }
395
396     @Test
397     public void test_GetInfraActiveRequests_SuccessResponseWithSoInfraRequestList() throws Exception {
398         final String jsonString = getJsonResponse(SEARCH_RESULT_RESPONSE_JSON_FILE);
399         this.databaseMockServer
400                 .expect(requestTo(databaseUrlProvider.getSearchUrl(START_TIME_IN_MS, END_TIME_IN_MS, null)))
401                 .andRespond(withSuccess(jsonString, MediaType.APPLICATION_JSON));
402
403         final Response response =
404                 objUnderTest.getInfraActiveRequests(Collections.emptyMap(), START_TIME_IN_MS, END_TIME_IN_MS, null);
405
406         assertEquals(Status.OK.getStatusCode(), response.getStatus());
407         @SuppressWarnings("unchecked")
408         final List<SoInfraRequest> actual = (List<SoInfraRequest>) response.getEntity();
409         assertEquals(3, actual.size());
410
411         final Map<String, SoInfraRequest> actualRequests = new HashMap<>();
412         for (final SoInfraRequest soInfraRequest : actual) {
413             actualRequests.put(soInfraRequest.getRequestId(), soInfraRequest);
414         }
415         final SoInfraRequest infraRequest = actualRequests.get("9383dc81-7a6c-4673-8082-650d50a82a1a");
416         assertNull(infraRequest.getEndTime());
417         assertEquals("IN_PROGRESS", infraRequest.getRequestStatus());
418     }
419
420     private String getJsonResponse(final Path path) throws IOException {
421         return new String(Files.readAllBytes(path));
422     }
423
424 }