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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.monitoring.rest.api;
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;
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;
76 * @author waqas.ikram@ericsson.com, andrei.barcovschi@ericsson.com
78 @RunWith(SpringJUnit4ClassRunner.class)
79 @ActiveProfiles("test")
81 public class SoMonitoringControllerTest {
82 private static final String CAMUNDA_BASIC_AUTH =
83 "Basic YWRtaW46S3A4Yko0U1hzek0wV1hsaGFrM2VIbGNzZTJnQXc4NHZhb0dHbUp2VXkyVQ==";
85 private static final String DATABASE_BASIC_AUTH = "Basic YnBlbDpwYXNzd29yZDEk";
88 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
89 private RestTemplate restTemplate;
92 private CamundaRestUrlProvider urlProvider;
95 private DatabaseUrlProvider databaseUrlProvider;
97 private MockRestServiceServer mockRestServiceServer;
101 private SoMonitoringController objUnderTest;
104 public void setUp() throws Exception {
105 mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).build();
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));
115 final Response response = objUnderTest.getProcessInstanceId(ID);
117 assertEquals(Status.OK.getStatusCode(), response.getStatus());
118 final ProcessInstanceIdDetail actualProcessInstance = (ProcessInstanceIdDetail) response.getEntity();
119 assertEquals("dba707b6-8c02-11e8-a6ba-022a5dba5402", actualProcessInstance.getProcessInstanceId());
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));
128 final Response response = objUnderTest.getProcessInstanceId(ID);
129 assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());
130 assertNull(response.getEntity());
134 public void test_GetProcessInstance_FailureResponseWithEmptyDataFromCamunda() throws Exception {
135 this.mockRestServiceServer.expect(requestTo(urlProvider.getHistoryProcessInstanceUrl(ID)))
136 .andRespond(withBadRequest());
138 final Response response = objUnderTest.getProcessInstanceId(ID);
139 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
143 public void test_GetProcessInstance_UnauthorizedRequestFromCamunda() throws Exception {
144 this.mockRestServiceServer.expect(requestTo(urlProvider.getHistoryProcessInstanceUrl(ID)))
145 .andRespond(withUnauthorizedRequest());
147 final Response response = objUnderTest.getProcessInstanceId(ID);
148 assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
149 assertNotNull(response.getEntity());
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));
158 final Response response = objUnderTest.getSingleProcessInstance(PROCESS_INSTACE_ID);
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());
170 public void test_GetSingleProcessInstance_WithBadRequestResponseFromCamunda() throws Exception {
171 this.mockRestServiceServer.expect(requestTo(urlProvider.getSingleProcessInstanceUrl(PROCESS_INSTACE_ID)))
172 .andRespond(withBadRequest());
174 final Response response = objUnderTest.getSingleProcessInstance(PROCESS_INSTACE_ID);
175 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
176 assertNotNull(response.getEntity());
180 public void test_GetSingleProcessInstance_WithUnauthorizedRequestResponseFromCamunda() throws Exception {
181 this.mockRestServiceServer.expect(requestTo(urlProvider.getSingleProcessInstanceUrl(PROCESS_INSTACE_ID)))
182 .andRespond(withUnauthorizedRequest());
184 final Response response = objUnderTest.getSingleProcessInstance(PROCESS_INSTACE_ID);
185 assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
186 assertNotNull(response.getEntity());
190 public void test_GetSingleProcessInstance_NullAndEmptyProcessInstanceIdFromCamunda() throws Exception {
192 Response response = objUnderTest.getSingleProcessInstance(null);
193 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
194 assertNotNull(response.getEntity());
196 response = objUnderTest.getSingleProcessInstance("");
197 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
198 assertNotNull(response.getEntity());
203 public void test_GetProcessInstance_EmptyRequestID() throws Exception {
205 Response response = objUnderTest.getProcessInstanceId(EMPTY_STRING);
206 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
208 response = objUnderTest.getProcessInstanceId(null);
209 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
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));
219 final Response response = objUnderTest.getProcessDefinitionXml(PROCRESS_DEF_ID);
220 assertEquals(Status.OK.getStatusCode(), response.getStatus());
222 final ProcessDefinitionDetail actual = (ProcessDefinitionDetail) response.getEntity();
223 assertEquals(PROCRESS_DEF_ID, actual.getProcessDefinitionId());
227 public void test_GetProcessDefinitionXml_BadRequestResponseFromCamunda() throws Exception {
228 this.mockRestServiceServer.expect(requestTo(urlProvider.getProcessDefinitionUrl(PROCRESS_DEF_ID)))
229 .andRespond(withBadRequest());
231 final Response response = objUnderTest.getProcessDefinitionXml(PROCRESS_DEF_ID);
232 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
233 assertNotNull(response.getEntity());
237 public void test_GetProcessDefinitionXml_UnauthorizedRequestFromCamunda() throws Exception {
238 this.mockRestServiceServer.expect(requestTo(urlProvider.getProcessDefinitionUrl(PROCRESS_DEF_ID)))
239 .andRespond(withUnauthorizedRequest());
241 final Response response = objUnderTest.getProcessDefinitionXml(PROCRESS_DEF_ID);
242 assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
243 assertNotNull(response.getEntity());
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());
252 response = objUnderTest.getProcessDefinitionXml(null);
253 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
254 assertNotNull(response.getEntity());
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));
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());
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());
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));
290 final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
291 assertEquals(Status.OK.getStatusCode(), response.getStatus());
292 assertNotNull(response.getEntity());
296 public void test_GetActivityInstanceDetail_UnauthorizedRequestFromCamunda() throws Exception {
297 this.mockRestServiceServer.expect(requestTo(urlProvider.getActivityInstanceUrl(PROCESS_INSTACE_ID)))
298 .andRespond(withUnauthorizedRequest());
300 final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
301 assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
302 assertNotNull(response.getEntity());
306 public void test_GetActivityInstanceDetail_BadRequestFromCamunda() throws Exception {
307 this.mockRestServiceServer.expect(requestTo(urlProvider.getActivityInstanceUrl(PROCESS_INSTACE_ID)))
308 .andRespond(withBadRequest());
310 final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
311 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
312 assertNotNull(response.getEntity());
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());
321 response = objUnderTest.getActivityInstanceDetail(null);
322 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
323 assertNotNull(response.getEntity());
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));
332 final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
334 assertEquals(Status.OK.getStatusCode(), response.getStatus());
335 @SuppressWarnings("unchecked")
336 final List<ProcessInstanceVariableDetail> actual = (List<ProcessInstanceVariableDetail>) response.getEntity();
337 assertEquals(230, actual.size());
339 final ProcessInstanceVariableDetail variableDetail = actual.get(0);
340 assertEquals("serviceType", variableDetail.getName());
341 assertEquals("String", variableDetail.getType());
342 assertEquals("PNFSERVICE", variableDetail.getValue());
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));
350 final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
352 assertEquals(Status.OK.getStatusCode(), response.getStatus());
353 assertNotNull(response.getEntity());
357 public void test_GetProcessInstanceVariables_BadRequestFromCamunda() throws Exception {
358 this.mockRestServiceServer.expect(requestTo(urlProvider.getProcessInstanceVariablesUrl(PROCESS_INSTACE_ID)))
359 .andRespond(withBadRequest());
361 final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
363 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
364 assertNotNull(response.getEntity());
368 public void test_GetProcessInstanceVariables_UnauthorizedRequestFromCamunda() throws Exception {
369 this.mockRestServiceServer.expect(requestTo(urlProvider.getProcessInstanceVariablesUrl(PROCESS_INSTACE_ID)))
370 .andRespond(withUnauthorizedRequest());
372 final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
374 assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
375 assertNotNull(response.getEntity());
379 public void test_GetProcessInstanceVariables_NullAndEmptyValues() throws Exception {
381 Response response = objUnderTest.getProcessInstanceVariables(EMPTY_STRING);
383 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
384 assertNotNull(response.getEntity());
386 response = objUnderTest.getProcessInstanceVariables(null);
388 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
389 assertNotNull(response.getEntity());
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));
401 final Response response =
402 objUnderTest.getInfraActiveRequests(Collections.emptyMap(), START_TIME_IN_MS, END_TIME_IN_MS, null);
404 assertEquals(Status.OK.getStatusCode(), response.getStatus());
405 @SuppressWarnings("unchecked")
406 final List<SoInfraRequest> actual = (List<SoInfraRequest>) response.getEntity();
407 assertEquals(3, actual.size());
409 final Map<String, SoInfraRequest> actualRequests = new HashMap<>();
410 for (final SoInfraRequest soInfraRequest : actual) {
411 actualRequests.put(soInfraRequest.getRequestId(), soInfraRequest);
413 final SoInfraRequest infraRequest = actualRequests.get("9383dc81-7a6c-4673-8082-650d50a82a1a");
414 assertNull(infraRequest.getEndTime());
415 assertEquals("IN_PROGRESS", infraRequest.getRequestStatus());
418 private String getJsonResponse(final Path path) throws IOException {
419 return new String(Files.readAllBytes(path));