2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Modifications Copyright 2018 Nokia
 
   8  * ================================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.so.bpmn.common.scripts
 
  25 import org.camunda.bpm.engine.delegate.BpmnError
 
  26 import org.camunda.bpm.engine.delegate.DelegateExecution
 
  27 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake
 
  28 import org.junit.Before
 
  30 import org.mockito.Mock
 
  31 import org.mockito.MockitoAnnotations
 
  32 import org.onap.aai.domain.yang.RelationshipList
 
  33 import org.onap.aai.domain.yang.VolumeGroup
 
  34 import org.onap.so.bpmn.common.scripts.ConfirmVolumeGroupName
 
  35 import org.onap.so.bpmn.common.scripts.ConfirmVolumeGroupNameFactory
 
  36 import org.onap.so.bpmn.common.scripts.ExceptionUtil
 
  37 import org.onap.so.client.aai.AAIObjectType
 
  38 import org.onap.so.client.aai.AAIResourcesClient
 
  39 import org.onap.so.client.aai.entities.uri.AAIResourceUri
 
  40 import org.onap.so.client.aai.entities.uri.AAIUriFactory
 
  41 import org.onap.so.constants.Defaults
 
  42 import org.springframework.http.HttpStatus
 
  44 import javax.ws.rs.core.UriBuilder
 
  46 import static org.assertj.core.api.Assertions.catchThrowableOfType
 
  47 import static org.junit.Assert.assertEquals
 
  48 import static org.junit.Assert.assertFalse
 
  49 import static org.junit.Assert.assertTrue
 
  50 import static org.mockito.Mockito.spy
 
  51 import static org.mockito.Mockito.when
 
  53 class ConfirmVolumeGroupNameTest {
 
  55     private static final AAIResourceUri RESOURCE_URI = AAIUriFactory.createResourceFromExistingURI(
 
  56             AAIObjectType.VOLUME_GROUP, UriBuilder.fromPath('/aai/test/volume-groups/volume-group/testVolumeGroup').build())
 
  58     private ConfirmVolumeGroupName confirmVolumeGroupName
 
  60     private VolumeGroup volumeGroup
 
  62     private AAIResourcesClient client
 
  63     private ExceptionUtilFake exceptionUtilFake
 
  65     private DelegateExecution delegateExecution
 
  68     public void init() throws IOException {
 
  69         exceptionUtilFake = new ExceptionUtilFake()
 
  70         confirmVolumeGroupName = spy(new ConfirmVolumeGroupName(exceptionUtilFake))
 
  71         MockitoAnnotations.initMocks(this)
 
  72         delegateExecution = new DelegateExecutionFake()
 
  73         volumeGroup = createVolumeGroup()
 
  74         when(confirmVolumeGroupName.getAAIClient()).thenReturn(client)
 
  78     public void preProcessRequest_shouldSetUpVariables() {
 
  79         String volumeGroupId = "volume-group-id-1"
 
  80         String volumeGroupName = "volume-group-name-1"
 
  81         String aicCloudRegion = "aic-cloud-region-1"
 
  82         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, Defaults.CLOUD_OWNER, aicCloudRegion, volumeGroupId)
 
  84         delegateExecution.setVariable("ConfirmVolumeGroupName_volumeGroupId", volumeGroupId)
 
  85         delegateExecution.setVariable("ConfirmVolumeGroupName_volumeGroupName", volumeGroupName)
 
  86         delegateExecution.setVariable("ConfirmVolumeGroupName_aicCloudRegion", aicCloudRegion)
 
  87         delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", uri)
 
  89         confirmVolumeGroupName.preProcessRequest(delegateExecution)
 
  91         assertEquals(ConfirmVolumeGroupName.Prefix, delegateExecution.getVariable("prefix"))
 
  93         assertEquals(volumeGroupId, delegateExecution.getVariable("CVGN_volumeGroupId"))
 
  94         assertEquals(volumeGroupName, delegateExecution.getVariable("CVGN_volumeGroupName"))
 
  95         assertEquals(aicCloudRegion, delegateExecution.getVariable("CVGN_aicCloudRegion"))
 
  99     public void queryAAIForVolumeGroupId_shouldSucceed_whenVolumeGroupExists() {
 
 100         delegateExecution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.OK)
 
 101         delegateExecution.setVariable("CVGN_queryVolumeGroupResponse", volumeGroup)
 
 102         delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", RESOURCE_URI)
 
 103         when(client.get(VolumeGroup.class, RESOURCE_URI)).thenReturn(Optional.of(volumeGroup))
 
 105         confirmVolumeGroupName.queryAAIForVolumeGroupId(delegateExecution)
 
 107         assertEquals(HttpStatus.OK.value(), delegateExecution.getVariable("CVGN_queryVolumeGroupResponseCode"))
 
 108         assertEquals(volumeGroup, delegateExecution.getVariable("CVGN_queryVolumeGroupResponse"))
 
 112     public void queryAAIForVolumeGroupId_shouldFailWith404_whenVolumeGroupDoesNotExist() {
 
 113         delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", RESOURCE_URI)
 
 114         when(client.get(VolumeGroup.class, RESOURCE_URI)).thenReturn(Optional.empty())
 
 116         confirmVolumeGroupName.queryAAIForVolumeGroupId(delegateExecution)
 
 118         assertEquals(HttpStatus.NOT_FOUND.value(), delegateExecution.getVariable("CVGN_queryVolumeGroupResponseCode"))
 
 119         assertEquals("Volume Group not Found!", delegateExecution.getVariable("CVGN_queryVolumeGroupResponse"))
 
 123     public void queryAAIForVolumeGroupId_shouldThrowWorkflowException_whenRuntimeExceptionIsThrown() throws BpmnError {
 
 124         delegateExecution.setVariable("CVGN_volumeGroupGetEndpoint", RESOURCE_URI)
 
 125         delegateExecution.setVariable("testProcessKey", "process-key1")
 
 127         def errorMsg = "my runtime exception"
 
 128         when(client.get(VolumeGroup.class, RESOURCE_URI)).thenThrow(new RuntimeException(errorMsg))
 
 130         def exceptionMsg = "AAI GET Failed"
 
 132         BpmnError error = catchThrowableOfType(
 
 133                 { -> confirmVolumeGroupName.queryAAIForVolumeGroupId(delegateExecution) }, BpmnError.class)
 
 135         assertEquals(String.format("MSOWorkflowException: %s", exceptionMsg), error.getMessage())
 
 136         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value().toString(), error.getErrorCode())
 
 138         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), delegateExecution.getVariable("CVGN_queryVolumeGroupResponseCode"))
 
 139         assertEquals(String.format("AAI GET Failed:%s", errorMsg), delegateExecution.getVariable("CVGN_queryVolumeGroupResponse"))
 
 140         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), exceptionUtilFake.getErrorCode())
 
 141         assertEquals(exceptionMsg, exceptionUtilFake.getErrorMessage())
 
 142         assertEquals(delegateExecution, exceptionUtilFake.getDelegateExecution())
 
 146     public void checkAAIQueryResult_shouldSetVolumeGroupNameMatchesToFalse_whenResponseCodeIs404() {
 
 147         delegateExecution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.NOT_FOUND)
 
 148         delegateExecution.setVariable("CVGN_volumeGroupName", "")
 
 150         confirmVolumeGroupName.checkAAIQueryResult(delegateExecution)
 
 152         assertFalse(delegateExecution.getVariable("CVGN_volumeGroupNameMatches"))
 
 156     public void checkAAIQueryResult_shouldSetVolumeGroupNameMatchesToTrue_whenResponseCodeIs200AndVolumeGroupNameExists() {
 
 157         delegateExecution.setVariable("CVGN_queryVolumeGroupResponseCode", HttpStatus.OK.value())
 
 158         delegateExecution.setVariable("CVGN_queryVolumeGroupResponse", volumeGroup)
 
 159         delegateExecution.setVariable("CVGN_volumeGroupName", volumeGroup.getVolumeGroupName())
 
 161         confirmVolumeGroupName.checkAAIQueryResult(delegateExecution)
 
 163         assertTrue(delegateExecution.getVariable("CVGN_volumeGroupNameMatches"))
 
 167     public void handleVolumeGroupNameNoMatch_shouldThrowBpmnErrorException() {
 
 168         def volumeGroupId = "volume-group-id"
 
 169         def volumeGroupName = "volume-group-name"
 
 171         delegateExecution.setVariable("CVGN_volumeGroupId", volumeGroupId)
 
 172         delegateExecution.setVariable("CVGN_volumeGroupName", volumeGroupName)
 
 174         def errorMessage = String.format("Error occurred - volume group id %s is not associated with %s",
 
 175                 delegateExecution.getVariable('CVGN_volumeGroupId'), delegateExecution.getVariable('CVGN_volumeGroupName'))
 
 177         BpmnError error = catchThrowableOfType(
 
 178                 { -> confirmVolumeGroupName.handleVolumeGroupNameNoMatch(delegateExecution) }, BpmnError.class)
 
 180         assertEquals(String.format("MSOWorkflowException: %s", errorMessage), error.getMessage())
 
 181         assertEquals("1002", error.getErrorCode())
 
 183         assertEquals(1002, exceptionUtilFake.getErrorCode())
 
 184         assertEquals(errorMessage, exceptionUtilFake.getErrorMessage())
 
 185         assertEquals(delegateExecution, exceptionUtilFake.getDelegateExecution())
 
 189     public void reportSuccess_shouldSetWorkflowResponseToEmptyString() {
 
 190         confirmVolumeGroupName.reportSuccess(delegateExecution)
 
 191         assertEquals("", delegateExecution.getVariable("WorkflowResponse"))
 
 194     private VolumeGroup createVolumeGroup() {
 
 195         VolumeGroup volumeGroup = new VolumeGroup()
 
 197         volumeGroup.setVolumeGroupId("volume-group-id")
 
 198         volumeGroup.setVolumeGroupName("volume-group-name")
 
 199         volumeGroup.setHeatStackId("heat-stack-id")
 
 200         volumeGroup.setVnfType("vnf-type")
 
 201         volumeGroup.setOrchestrationStatus("orchestration-status")
 
 202         volumeGroup.setModelCustomizationId("model-customization-id")
 
 203         volumeGroup.setVfModuleModelCustomizationId("vf-module-model-customization-id")
 
 204         volumeGroup.setResourceVersion("resource-version")
 
 205         volumeGroup.setRelationshipList(new RelationshipList())
 
 210     private static class ExceptionUtilFake extends ExceptionUtil {
 
 212         private int errorCode
 
 213         private String errorMessage
 
 214         private DelegateExecution execution
 
 217         public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
 
 218             this.errorCode = errorCode
 
 219             this.errorMessage = errorMessage
 
 220             this.execution = execution
 
 221             throw new BpmnError(errorCode.toString(), "MSOWorkflowException: ${errorMessage}")
 
 224         public int getErrorCode() {
 
 228         public String getErrorMessage() {
 
 232         public DelegateExecution getDelegateExecution() {