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.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;
 
  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;
 
  38 import javax.ws.rs.core.Response;
 
  39 import javax.ws.rs.core.Response.Status;
 
  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;
 
  62  * @author waqas.ikram@ericsson.com
 
  64 @RunWith(SpringJUnit4ClassRunner.class)
 
  65 @ActiveProfiles("test")
 
  67 public class SoMonitoringControllerTest {
 
  69     private static final String PROCRESS_DEF_ID = "AFRFLOW:1:c6eea1b7-9722-11e8-8caf-022ac9304eeb";
 
  71     private static final String EMPTY_ARRAY_RESPONSE = "[]";
 
  73     private static final String PROCESS_INSTACE_ID = "5956a99d-9736-11e8-8caf-022ac9304eeb";
 
  75     private static final String EMPTY_STRING = "";
 
  77     private static final String SOURCE_TEST_FOLDER = "src/test/resources/camundaResponses/";
 
  79     private static final Path PROCESS_DEF_RESPONSE_JSON_FILE = Paths.get(SOURCE_TEST_FOLDER + "processDefinition.json");
 
  81     private static final Path ACTIVITY_INSTANCE_RESPONSE_JSON_FILE =
 
  82             Paths.get(SOURCE_TEST_FOLDER + "activityInstance.json");
 
  84     private static final Path PROCESS_INSTANCE_VARIABLES_RESPONSE_JSON_FILE =
 
  85             Paths.get(SOURCE_TEST_FOLDER + "processInstanceVariables.json");
 
  87     private static final Path PROCCESS_INSTANCE_RESPONSE_JSON_FILE =
 
  88             Paths.get(SOURCE_TEST_FOLDER + "processInstance.json");
 
  90     private static final Path SINGLE_PROCCESS_INSTANCE_RESPONSE_JSON_FILE =
 
  91             Paths.get(SOURCE_TEST_FOLDER + "singleprocessInstance.json");
 
  93     private static final String ID = UUID.randomUUID().toString();
 
  96     @Qualifier(CAMUNDA_REST_TEMPLATE)
 
  97     private RestTemplate restTemplate;
 
 100     private CamundaRestUrlProvider urlProvider;
 
 102     private MockRestServiceServer camundaMockServer;
 
 105     private SoMonitoringController objUnderTest;
 
 108     public void setUp() throws Exception {
 
 109         camundaMockServer = MockRestServiceServer.bindTo(restTemplate).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         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());
 
 396     private String getJsonResponse(final Path path) throws IOException {
 
 397         return new String(Files.readAllBytes(path));