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