13953b03318ff700b9f3db35071e02a6d0f1410a
[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.montoring.configuration.rest.RestTemplateConfigration.CAMUNDA_REST_TEMPLATE;
26 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
27 import static org.springframework.test.web.client.response.MockRestResponseCreators.withBadRequest;
28 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
29 import static org.springframework.test.web.client.response.MockRestResponseCreators.withUnauthorizedRequest;
30
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.List;
36 import java.util.UUID;
37
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.Response.Status;
40
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.onap.so.montoring.configuration.camunda.CamundaRestUrlProvider;
45 import org.onap.so.montoring.model.ActivityInstanceDetail;
46 import org.onap.so.montoring.model.ProcessDefinitionDetail;
47 import org.onap.so.montoring.model.ProcessInstanceDetail;
48 import org.onap.so.montoring.model.ProcessInstanceIdDetail;
49 import org.onap.so.montoring.model.ProcessInstanceVariableDetail;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.beans.factory.annotation.Qualifier;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.http.MediaType;
54 import org.springframework.test.context.ActiveProfiles;
55 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
56 import org.springframework.test.web.client.MockRestServiceServer;
57 import org.springframework.web.client.RestTemplate;
58
59
60
61 /**
62  * @author waqas.ikram@ericsson.com
63  */
64 @RunWith(SpringJUnit4ClassRunner.class)
65 @ActiveProfiles("test")
66 @SpringBootTest
67 public class SoMonitoringControllerTest {
68
69     private static final String PROCRESS_DEF_ID = "AFRFLOW:1:c6eea1b7-9722-11e8-8caf-022ac9304eeb";
70
71     private static final String EMPTY_ARRAY_RESPONSE = "[]";
72
73     private static final String PROCESS_INSTACE_ID = "5956a99d-9736-11e8-8caf-022ac9304eeb";
74
75     private static final String EMPTY_STRING = "";
76
77     private static final String SOURCE_TEST_FOLDER = "src/test/resources/camundaResponses/";
78
79     private static final Path PROCESS_DEF_RESPONSE_JSON_FILE = Paths.get(SOURCE_TEST_FOLDER + "processDefinition.json");
80
81     private static final Path ACTIVITY_INSTANCE_RESPONSE_JSON_FILE =
82             Paths.get(SOURCE_TEST_FOLDER + "activityInstance.json");
83
84     private static final Path PROCESS_INSTANCE_VARIABLES_RESPONSE_JSON_FILE =
85             Paths.get(SOURCE_TEST_FOLDER + "processInstanceVariables.json");
86
87     private static final Path PROCCESS_INSTANCE_RESPONSE_JSON_FILE =
88             Paths.get(SOURCE_TEST_FOLDER + "processInstance.json");
89
90     private static final Path SINGLE_PROCCESS_INSTANCE_RESPONSE_JSON_FILE =
91             Paths.get(SOURCE_TEST_FOLDER + "singleprocessInstance.json");
92
93     private static final String ID = UUID.randomUUID().toString();
94
95     @Autowired
96     @Qualifier(CAMUNDA_REST_TEMPLATE)
97     private RestTemplate restTemplate;
98
99     @Autowired
100     private CamundaRestUrlProvider urlProvider;
101
102     private MockRestServiceServer camundaMockServer;
103
104     @Autowired
105     private SoMonitoringController objUnderTest;
106
107     @Before
108     public void setUp() throws Exception {
109         camundaMockServer = MockRestServiceServer.bindTo(restTemplate).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         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     private String getJsonResponse(final Path path) throws IOException {
397         return new String(Files.readAllBytes(path));
398     }
399
400 }