2  * ============LICENSE_START======================================================= 
 
   4  * ================================================================================ 
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
 
   6  * ================================================================================ 
 
   7  * Licensed under the Apache License, Version 2.0 (the "License"); 
 
   8  * you may not use this file except in compliance with the License. 
 
   9  * You may obtain a copy of the License at 
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0 
 
  13  * Unless required by applicable law or agreed to in writing, software 
 
  14  * distributed under the License is distributed on an "AS IS" BASIS, 
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 
  16  * See the License for the specific language governing permissions and 
 
  17  * limitations under the License. 
 
  18  * ============LICENSE_END========================================================= 
 
  21 package org.onap.so.bpmn.infrastructure.scripts
 
  23 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
 
  24 import org.junit.Assert
 
  25 import org.junit.Before
 
  27 import org.junit.runner.RunWith
 
  28 import org.mockito.ArgumentCaptor
 
  29 import org.mockito.Captor
 
  30 import org.mockito.Mockito
 
  31 import org.mockito.MockitoAnnotations
 
  32 import org.mockito.Spy
 
  33 import org.mockito.runners.MockitoJUnitRunner
 
  34 import org.onap.aai.domain.yang.VolumeGroup
 
  35 import org.onap.so.bpmn.common.scripts.DeleteAAIVfModule
 
  36 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
 
  37 import org.onap.so.bpmn.core.WorkflowException
 
  38 import org.onap.aaiclient.client.aai.AAIObjectType
 
  39 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
 
  40 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
 
  41 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
 
  42 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException
 
  44 import javax.ws.rs.NotFoundException
 
  46 import static org.mockito.ArgumentMatchers.eq
 
  47 import static org.mockito.Mockito.doNothing
 
  48 import static org.mockito.Mockito.doThrow
 
  49 import static org.mockito.Mockito.times
 
  50 import static org.mockito.Mockito.verify
 
  51 import static org.mockito.Mockito.when
 
  53 class DeleteVfModuleVolumeInfraV1Test extends MsoGroovyTest {
 
  56         DeleteVfModuleVolumeInfraV1 deleteVfModuleVolumeInfraV1 ;
 
  59         static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
 
  62         void init() throws IOException {
 
  63                 super.init("DeleteVfModuleVolumeInfraV1")
 
  64                 MockitoAnnotations.initMocks(this);
 
  65                 when(deleteVfModuleVolumeInfraV1.getAAIClient()).thenReturn(client)
 
  68         String deleteVnfAdapterRequestXml = """<deleteVolumeGroupRequest>
 
  69    <cloudSiteId>RDM2WAGPLCP</cloudSiteId>
 
  70    <tenantId>fba1bd1e195a404cacb9ce17a9b2b421</tenantId>
 
  71    <volumeGroupId>78987</volumeGroupId>
 
  73    <skipAAI>true</skipAAI>
 
  75       <requestId>TEST-REQUEST-ID-0123</requestId>
 
  76       <serviceInstanceId>1234</serviceInstanceId>
 
  78    <messageId>ebb9ef7b-a6a5-40e6-953e-f868f1767677</messageId>
 
  79    <notificationUrl>http://localhost:28080/mso/WorkflowMessage/VNFAResponse/ebb9ef7b-a6a5-40e6-953e-f868f1767677</notificationUrl>
 
  80 </deleteVolumeGroupRequest>"""
 
  82         String dbRequestXml = """<soapenv:Envelope xmlns:req="http://org.onap.so/requestsdb"
 
  83                   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 
  86       <req:updateInfraRequest>
 
  87          <requestId>TEST-REQUEST-ID-0123</requestId>
 
  88          <lastModifiedBy>BPMN</lastModifiedBy>
 
  89          <statusMessage>VolumeGroup successfully deleted</statusMessage>
 
  90          <requestStatus>COMPLETE</requestStatus>
 
  91          <progress>100</progress>
 
  93       </req:updateInfraRequest>
 
  95 </soapenv:Envelope>"""
 
  97         String completionRequestXml = """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
 
  98                             xmlns:ns="http://org.onap/so/request/types/v1"
 
  99                             xmlns="http://org.onap/so/infra/vnf-request/v1">
 
 101       <request-id>TEST-REQUEST-ID-0123</request-id>
 
 102       <action>DELETE</action>
 
 105    <aetgt:status-message>Volume Group has been deleted successfully.</aetgt:status-message>
 
 106    <aetgt:mso-bpel-name>BPMN VF Module Volume action: DELETE</aetgt:mso-bpel-name>
 
 107 </aetgt:MsoCompletionRequest>"""
 
 109         String falloutHandlerRequestXml = """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
 
 110                              xmlns:ns="http://org.onap/so/request/types/v1"
 
 111                              xmlns="http://org.onap/so/infra/vnf-request/v1">
 
 113       <request-id>TEST-REQUEST-ID-0123</request-id>
 
 114       <action>DELETE</action>
 
 117    <aetgt:WorkflowException>
 
 118       <aetgt:ErrorMessage>Unexpected Error</aetgt:ErrorMessage>
 
 119       <aetgt:ErrorCode>5000</aetgt:ErrorCode>
 
 120    </aetgt:WorkflowException>
 
 121 </aetgt:FalloutHandlerRequest>"""
 
 125         public void testPrepareVnfAdapterDeleteRequest() {
 
 127                 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
 
 128                 when(mockExecution.getVariable("DELVfModVol_cloudRegion")).thenReturn('RDM2WAGPLCP')
 
 129                 when(mockExecution.getVariable("DELVfModVol_tenantId")).thenReturn('fba1bd1e195a404cacb9ce17a9b2b421')
 
 130                 when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn('78987')
 
 131                 when(mockExecution.getVariable("DELVfModVol_volumeGroupHeatStackId")).thenReturn('')
 
 132                 when(mockExecution.getVariable("DELVfModVol_requestId")).thenReturn('TEST-REQUEST-ID-0123')
 
 133                 when(mockExecution.getVariable("DELVfModVol_serviceId")).thenReturn('1234')
 
 134                 when(mockExecution.getVariable("DELVfModVol_messageId")).thenReturn('ebb9ef7b-a6a5-40e6-953e-f868f1767677')
 
 135                 when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:28080/mso/WorkflowMessage')
 
 136                 when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn('')
 
 138                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
 
 139                 myproc.prepareVnfAdapterDeleteRequest(mockExecution, 'true')
 
 141                 verify(mockExecution).setVariable("DELVfModVol_deleteVnfARequest", deleteVnfAdapterRequestXml)
 
 147         public void testPrepareDbRequest() {
 
 149                 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
 
 150                 when(mockExecution.getVariable("DELVfModVol_requestId")).thenReturn('TEST-REQUEST-ID-0123')
 
 151                 when(mockExecution.getVariable("DELVfModVol_volumeOutputs")).thenReturn('')
 
 152                 when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC")
 
 153                 when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
 
 155                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
 
 156                 myproc.prepareDBRequest(mockExecution, 'true')
 
 158                 verify(mockExecution).setVariable("DELVfModVol_updateInfraRequest", dbRequestXml)
 
 162         public void testPrepareCompletionHandlerRequest() {
 
 164                 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
 
 165                 when(mockExecution.getVariable("mso-request-id")).thenReturn('TEST-REQUEST-ID-0123')
 
 166                 when(mockExecution.getVariable("DELVfModVol_source")).thenReturn('VID')
 
 168                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
 
 169                 myproc.prepareCompletionHandlerRequest(mockExecution, 'true')
 
 171                 verify(mockExecution).setVariable("DELVfModVol_CompleteMsoProcessRequest", completionRequestXml)
 
 175         public void testPrepareFalloutHandler() {
 
 177                 WorkflowException workflowException = new WorkflowException('DeleteVfModuleVolumeInfraV1', 5000, 'Unexpected Error')
 
 179                 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
 
 181                 when(mockExecution.getVariable("DELVfModVol_requestId")).thenReturn('TEST-REQUEST-ID-0123')
 
 182                 when(mockExecution.getVariable("WorkflowException")).thenReturn(workflowException)
 
 183                 when(mockExecution.getVariable("DELVfModVol_source")).thenReturn('VID')
 
 185                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
 
 186                 myproc.prepareFalloutHandler(mockExecution, 'true')
 
 188                 verify(mockExecution).setVariable("DELVfModVol_Success", false)
 
 189                 verify(mockExecution).setVariable("DELVfModVol_FalloutHandlerRequest", falloutHandlerRequestXml)
 
 193     void testQueryAAIForVolumeGroup(){
 
 194         when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn("volumeGroupId1")
 
 195         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
 
 196         AAIResultWrapper wrapper = mockVolumeGroupWrapper("region1", "volumeGroupId1", "__files/AAI/VolumeGroupWithTenant.json")
 
 197         Optional<VolumeGroup> volumeGroupOp = wrapper.asBean(VolumeGroup.class)
 
 198         deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
 
 199         verify(mockExecution).setVariable("DELVfModVol_volumeGroupTenantId", "Tenant123")
 
 203     void testQueryAAIForVolumeGroupWithVfModule(){
 
 204         when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn("volumeGroupId1")
 
 205         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
 
 206         AAIResultWrapper wrapper = mockVolumeGroupWrapper("region1", "volumeGroupId1", "__files/AAI/VolumeGroupWithVfModule.json")
 
 208             deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
 
 209         } catch (Exception ex) {
 
 210             println " Test End - Handle catch-throw BpmnError()! "
 
 212         Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
 
 213         WorkflowException workflowException = captor.getValue()
 
 214         Assert.assertEquals(2500, workflowException.getErrorCode())
 
 215         Assert.assertEquals("Volume Group volumeGroupId1 currently in use - found vf-module relationship.", workflowException.getErrorMessage())
 
 219     void testQueryAAIForVolumeGroupNoTenant(){
 
 220         when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn("volumeGroupId1")
 
 221         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
 
 222         AAIResultWrapper wrapper = mockVolumeGroupWrapper("region1", "volumeGroupId1", "__files/AAI/VolumeGroup.json")
 
 224             deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
 
 225         } catch (Exception ex) {
 
 226             println " Test End - Handle catch-throw BpmnError()! "
 
 228         Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
 
 229         WorkflowException workflowException = captor.getValue()
 
 230         Assert.assertEquals(2500, workflowException.getErrorCode())
 
 231         Assert.assertEquals( "Could not find Tenant Id element in Volume Group with Volume Group Id volumeGroupId1", workflowException.getErrorMessage())
 
 235     void testQueryAAIForVolumeGroupNoId(){
 
 236         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
 
 238             deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
 
 239         } catch (Exception ex) {
 
 240             println " Test End - Handle catch-throw BpmnError()! "
 
 242         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
 
 243         WorkflowException workflowException = captor.getValue()
 
 244         Assert.assertEquals(2500, workflowException.getErrorCode())
 
 245         Assert.assertEquals("volume-group-id is not provided in the request", workflowException.getErrorMessage())
 
 249     void testDeleteVolGrpId(){
 
 250         VolumeGroup volumeGroup = new VolumeGroup()
 
 251         volumeGroup.setVolumeGroupId("volumeGroupId1")
 
 252         when(mockExecution.getVariable("DELVfModVol_queryAAIVolGrpResponse")).thenReturn(volumeGroup)
 
 253         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
 
 254         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "region1","volumeGroupId1")
 
 255         doNothing().when(client).delete(resourceUri)
 
 256         deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
 
 257         verify(client).delete(resourceUri)
 
 261     void testDeleteVolGrpIdNotFound(){
 
 262         VolumeGroup volumeGroup = new VolumeGroup()
 
 263         volumeGroup.setVolumeGroupId("volumeGroupId1")
 
 264         when(mockExecution.getVariable("DELVfModVol_queryAAIVolGrpResponse")).thenReturn(volumeGroup)
 
 265         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
 
 266         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "region1","volumeGroupId1")
 
 267         doThrow(new NotFoundException("Not Found")).when(client).delete(resourceUri)
 
 269             deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
 
 270         } catch (Exception ex) {
 
 271             println " Test End - Handle catch-throw BpmnError()! "
 
 273         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
 
 274         WorkflowException workflowException = captor.getValue()
 
 275         Assert.assertEquals(2500, workflowException.getErrorCode())
 
 276         Assert.assertEquals("Volume group volumeGroupId1 not found for delete in AAI Response code: 404", workflowException.getErrorMessage())
 
 280     void testDeleteVolGrpIdError(){
 
 281         VolumeGroup volumeGroup = new VolumeGroup()
 
 282         volumeGroup.setVolumeGroupId("volumeGroupId1")
 
 283         when(mockExecution.getVariable("DELVfModVol_queryAAIVolGrpResponse")).thenReturn(volumeGroup)
 
 284         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
 
 285         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "region1","volumeGroupId1")
 
 286         doThrow(new GraphInventoryUriComputationException("Error")).when(client).delete(resourceUri)
 
 288             deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
 
 289         } catch (Exception ex) {
 
 290             println " Test End - Handle catch-throw BpmnError()! "
 
 292         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
 
 293         WorkflowException workflowException = captor.getValue()
 
 294         Assert.assertEquals(5000, workflowException.getErrorCode())
 
 295         Assert.assertEquals("Received error from A&AI ()", workflowException.getErrorMessage())