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.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;
 
  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;
 
  53 import javax.ws.rs.core.Response;
 
  54 import javax.ws.rs.core.Response.Status;
 
  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;
 
  78  * @author waqas.ikram@ericsson.com, andrei.barcovschi@ericsson.com
 
  80 @RunWith(SpringJUnit4ClassRunner.class)
 
  81 @ActiveProfiles("test")
 
  83 public class SoMonitoringControllerTest {
 
  86     @Qualifier(CAMUNDA_REST_TEMPLATE)
 
  87     private RestTemplate restTemplate;
 
  90     @Qualifier(DATABASE_REST_TEMPLATE)
 
  91     private RestTemplate dataBaseRestTemplate;
 
  94     private CamundaRestUrlProvider urlProvider;
 
  97     private DatabaseUrlProvider databaseUrlProvider;
 
  99     private MockRestServiceServer camundaMockServer;
 
 101     private MockRestServiceServer databaseMockServer;
 
 104     private SoMonitoringController objUnderTest;
 
 107     public void setUp() throws Exception {
 
 108         camundaMockServer = MockRestServiceServer.bindTo(restTemplate).build();
 
 109         databaseMockServer = MockRestServiceServer.bindTo(dataBaseRestTemplate).build();
 
 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));
 
 118         final Response response = objUnderTest.getProcessInstanceId(ID);
 
 120         assertEquals(Status.OK.getStatusCode(), response.getStatus());
 
 121         final ProcessInstanceIdDetail actualProcessInstance = (ProcessInstanceIdDetail) response.getEntity();
 
 122         assertEquals("dba707b6-8c02-11e8-a6ba-022a5dba5402", actualProcessInstance.getProcessInstanceId());
 
 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));
 
 131         final Response response = objUnderTest.getProcessInstanceId(ID);
 
 132         assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());
 
 133         assertNull(response.getEntity());
 
 137     public void test_GetProcessInstance_FailureResponseWithEmptyDataFromCamunda() throws Exception {
 
 138         this.camundaMockServer.expect(requestTo(urlProvider.getHistoryProcessInstanceUrl(ID)))
 
 139                 .andRespond(withBadRequest());
 
 141         final Response response = objUnderTest.getProcessInstanceId(ID);
 
 142         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 146     public void test_GetProcessInstance_UnauthorizedRequestFromCamunda() throws Exception {
 
 147         this.camundaMockServer.expect(requestTo(urlProvider.getHistoryProcessInstanceUrl(ID)))
 
 148                 .andRespond(withUnauthorizedRequest());
 
 150         final Response response = objUnderTest.getProcessInstanceId(ID);
 
 151         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
 
 152         assertNotNull(response.getEntity());
 
 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));
 
 161         final Response response = objUnderTest.getSingleProcessInstance(PROCESS_INSTACE_ID);
 
 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());
 
 173     public void test_GetSingleProcessInstance_WithBadRequestResponseFromCamunda() throws Exception {
 
 174         this.camundaMockServer.expect(requestTo(urlProvider.getSingleProcessInstanceUrl(PROCESS_INSTACE_ID)))
 
 175                 .andRespond(withBadRequest());
 
 177         final Response response = objUnderTest.getSingleProcessInstance(PROCESS_INSTACE_ID);
 
 178         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 179         assertNotNull(response.getEntity());
 
 183     public void test_GetSingleProcessInstance_WithUnauthorizedRequestResponseFromCamunda() throws Exception {
 
 184         this.camundaMockServer.expect(requestTo(urlProvider.getSingleProcessInstanceUrl(PROCESS_INSTACE_ID)))
 
 185                 .andRespond(withUnauthorizedRequest());
 
 187         final Response response = objUnderTest.getSingleProcessInstance(PROCESS_INSTACE_ID);
 
 188         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
 
 189         assertNotNull(response.getEntity());
 
 193     public void test_GetSingleProcessInstance_NullAndEmptyProcessInstanceIdFromCamunda() throws Exception {
 
 195         Response response = objUnderTest.getSingleProcessInstance(null);
 
 196         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 197         assertNotNull(response.getEntity());
 
 199         response = objUnderTest.getSingleProcessInstance("");
 
 200         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 201         assertNotNull(response.getEntity());
 
 206     public void test_GetProcessInstance_EmptyRequestID() throws Exception {
 
 208         Response response = objUnderTest.getProcessInstanceId(EMPTY_STRING);
 
 209         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 211         response = objUnderTest.getProcessInstanceId(null);
 
 212         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 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));
 
 222         final Response response = objUnderTest.getProcessDefinitionXml(PROCRESS_DEF_ID);
 
 223         assertEquals(Status.OK.getStatusCode(), response.getStatus());
 
 225         final ProcessDefinitionDetail actual = (ProcessDefinitionDetail) response.getEntity();
 
 226         assertEquals(PROCRESS_DEF_ID, actual.getProcessDefinitionId());
 
 230     public void test_GetProcessDefinitionXml_BadRequestResponseFromCamunda() throws Exception {
 
 231         this.camundaMockServer.expect(requestTo(urlProvider.getProcessDefinitionUrl(PROCRESS_DEF_ID)))
 
 232                 .andRespond(withBadRequest());
 
 234         final Response response = objUnderTest.getProcessDefinitionXml(PROCRESS_DEF_ID);
 
 235         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 236         assertNotNull(response.getEntity());
 
 240     public void test_GetProcessDefinitionXml_UnauthorizedRequestFromCamunda() throws Exception {
 
 241         this.camundaMockServer.expect(requestTo(urlProvider.getProcessDefinitionUrl(PROCRESS_DEF_ID)))
 
 242                 .andRespond(withUnauthorizedRequest());
 
 244         final Response response = objUnderTest.getProcessDefinitionXml(PROCRESS_DEF_ID);
 
 245         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
 
 246         assertNotNull(response.getEntity());
 
 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());
 
 255         response = objUnderTest.getProcessDefinitionXml(null);
 
 256         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 257         assertNotNull(response.getEntity());
 
 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));
 
 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());
 
 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());
 
 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));
 
 293         final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
 
 294         assertEquals(Status.OK.getStatusCode(), response.getStatus());
 
 295         assertNotNull(response.getEntity());
 
 299     public void test_GetActivityInstanceDetail_UnauthorizedRequestFromCamunda() throws Exception {
 
 300         this.camundaMockServer.expect(requestTo(urlProvider.getActivityInstanceUrl(PROCESS_INSTACE_ID)))
 
 301                 .andRespond(withUnauthorizedRequest());
 
 303         final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
 
 304         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
 
 305         assertNotNull(response.getEntity());
 
 309     public void test_GetActivityInstanceDetail_BadRequestFromCamunda() throws Exception {
 
 310         this.camundaMockServer.expect(requestTo(urlProvider.getActivityInstanceUrl(PROCESS_INSTACE_ID)))
 
 311                 .andRespond(withBadRequest());
 
 313         final Response response = objUnderTest.getActivityInstanceDetail(PROCESS_INSTACE_ID);
 
 314         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 315         assertNotNull(response.getEntity());
 
 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());
 
 324         response = objUnderTest.getActivityInstanceDetail(null);
 
 325         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 326         assertNotNull(response.getEntity());
 
 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));
 
 335         final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
 
 337         assertEquals(Status.OK.getStatusCode(), response.getStatus());
 
 338         @SuppressWarnings("unchecked")
 
 339         final List<ProcessInstanceVariableDetail> actual = (List<ProcessInstanceVariableDetail>) response.getEntity();
 
 340         assertEquals(230, actual.size());
 
 342         final ProcessInstanceVariableDetail variableDetail = actual.get(0);
 
 343         assertEquals("serviceType", variableDetail.getName());
 
 344         assertEquals("String", variableDetail.getType());
 
 345         assertEquals("PNFSERVICE", variableDetail.getValue());
 
 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));
 
 353         final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
 
 355         assertEquals(Status.OK.getStatusCode(), response.getStatus());
 
 356         assertNotNull(response.getEntity());
 
 360     public void test_GetProcessInstanceVariables_BadRequestFromCamunda() throws Exception {
 
 361         this.camundaMockServer.expect(requestTo(urlProvider.getProcessInstanceVariablesUrl(PROCESS_INSTACE_ID)))
 
 362                 .andRespond(withBadRequest());
 
 364         final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
 
 366         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 367         assertNotNull(response.getEntity());
 
 371     public void test_GetProcessInstanceVariables_UnauthorizedRequestFromCamunda() throws Exception {
 
 372         this.camundaMockServer.expect(requestTo(urlProvider.getProcessInstanceVariablesUrl(PROCESS_INSTACE_ID)))
 
 373                 .andRespond(withUnauthorizedRequest());
 
 375         final Response response = objUnderTest.getProcessInstanceVariables(PROCESS_INSTACE_ID);
 
 377         assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
 
 378         assertNotNull(response.getEntity());
 
 382     public void test_GetProcessInstanceVariables_NullAndEmptyValues() throws Exception {
 
 384         Response response = objUnderTest.getProcessInstanceVariables(EMPTY_STRING);
 
 386         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 387         assertNotNull(response.getEntity());
 
 389         response = objUnderTest.getProcessInstanceVariables(null);
 
 391         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
 
 392         assertNotNull(response.getEntity());
 
 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));
 
 403         final Response response =
 
 404                 objUnderTest.getInfraActiveRequests(Collections.emptyMap(), START_TIME_IN_MS, END_TIME_IN_MS, null);
 
 406         assertEquals(Status.OK.getStatusCode(), response.getStatus());
 
 407         @SuppressWarnings("unchecked")
 
 408         final List<SoInfraRequest> actual = (List<SoInfraRequest>) response.getEntity();
 
 409         assertEquals(3, actual.size());
 
 411         final Map<String, SoInfraRequest> actualRequests = new HashMap<>();
 
 412         for (final SoInfraRequest soInfraRequest : actual) {
 
 413             actualRequests.put(soInfraRequest.getRequestId(), soInfraRequest);
 
 415         final SoInfraRequest infraRequest = actualRequests.get("9383dc81-7a6c-4673-8082-650d50a82a1a");
 
 416         assertNull(infraRequest.getEndTime());
 
 417         assertEquals("IN_PROGRESS", infraRequest.getRequestStatus());
 
 420     private String getJsonResponse(final Path path) throws IOException {
 
 421         return new String(Files.readAllBytes(path));