2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Modifications Copyright (c) 2019 Samsung
 
   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.infrastructure.scripts
 
  25 import com.github.tomakehurst.wiremock.junit.WireMockRule
 
  26 import org.camunda.bpm.engine.ProcessEngineServices
 
  27 import org.camunda.bpm.engine.RepositoryService
 
  28 import org.camunda.bpm.engine.delegate.BpmnError
 
  29 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
 
  30 import org.camunda.bpm.engine.repository.ProcessDefinition
 
  31 import org.junit.After
 
  32 import org.junit.AfterClass
 
  33 import org.junit.Assert
 
  34 import org.junit.Before
 
  35 import org.junit.BeforeClass
 
  38 import org.junit.runner.RunWith
 
  39 import org.mockito.ArgumentCaptor
 
  40 import org.mockito.Captor
 
  41 import org.mockito.Mock
 
  42 import org.mockito.Mockito
 
  43 import org.mockito.MockitoAnnotations
 
  44 import org.mockito.Spy
 
  45 import org.mockito.junit.MockitoJUnitRunner
 
  46 import org.onap.aai.domain.yang.VolumeGroup
 
  47 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
 
  48 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
 
  49 import org.onap.so.bpmn.core.UrnPropertiesReader
 
  50 import org.onap.so.bpmn.core.WorkflowException
 
  51 import org.onap.so.bpmn.mock.FileUtil
 
  52 import org.onap.so.client.aai.AAIObjectType
 
  53 import org.onap.so.client.aai.entities.AAIResultWrapper
 
  54 import org.onap.so.client.aai.entities.uri.AAIResourceUri
 
  55 import org.onap.so.client.aai.entities.uri.AAIUriFactory
 
  56 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException
 
  57 import org.springframework.core.env.Environment
 
  58 import org.springframework.mock.env.MockEnvironment
 
  60 import javax.ws.rs.NotFoundException
 
  62 import static com.github.tomakehurst.wiremock.client.WireMock.*
 
  63 import static org.mockito.Mockito.*
 
  65 @RunWith(MockitoJUnitRunner.Silent.class)
 
  66 class DoDeleteVfModuleVolumeV2Test extends MsoGroovyTest{
 
  69     public WireMockRule wireMockRule = new WireMockRule(8090)
 
  72     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
 
  75     DoDeleteVfModuleVolumeV2 deleteVfModuleVolumeV2
 
  78     Environment mockEnvironment
 
  80     private String Prefix = "DDVMV_"
 
  81     private RepositoryService mockRepositoryService
 
  86         UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader()
 
  87         urnPropertiesReader.setEnvironment(null)
 
  91     public void init() throws IOException {
 
  92         super.init("DoDeleteVfModuleVolumeV2")
 
  93         MockitoAnnotations.initMocks(this);
 
  94         when(deleteVfModuleVolumeV2.getAAIClient()).thenReturn(client)
 
  95         when(mockEnvironment.getProperty("mso.workflow.global.default.aai.version")).thenReturn("14")
 
  96         when(mockEnvironment.getProperty("mso.workflow.global.default.aai.namespace")).thenReturn("defaultTestNamespace")
 
  97         when(mockEnvironment.getProperty("aai.endpoint")).thenReturn("http://localhost:8090")
 
  98         UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader()
 
  99         urnPropertiesReader.setEnvironment(mockEnvironment)
 
 103     public void testCallRESTQueryAAICloudRegion() {
 
 104         ExecutionEntity mockExecution = setupMock()
 
 105         when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
 
 106         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
 
 107         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
 
 108         mockSuccessfulCloudData()
 
 110         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
 
 111         obj.callRESTQueryAAICloudRegion(mockExecution, "true")
 
 112         Mockito.verify(mockExecution).setVariable("DDVMV_queryCloudRegionReturnCode", "200")
 
 113         Mockito.verify(mockExecution).setVariable("DDVMV_aicCloudRegion", "RDM2WAGPLCP")
 
 116     @Test(expected = GroovyRuntimeException.class)
 
 117     public void testCallRESTQueryAAICloudRegionAAiEndpointNull() {
 
 118         ExecutionEntity mockExecution = setupMock()
 
 119         when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
 
 120         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
 
 121         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
 
 122         when(mockEnvironment.getProperty("aai.endpoint")).thenReturn(null)
 
 124         mockSuccessfulCloudData()
 
 126             DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
 
 127             obj.callRESTQueryAAICloudRegion(mockExecution, "true")
 
 129         } catch (Exception ex) {
 
 130             println " Test End - Handle catch-throw Exception! "
 
 131             Mockito.verify(mockExecution).getVariable(eq("lcpCloudRegionId"))
 
 132             Assert.assertEquals(GroovyRuntimeException.class, ex.class)
 
 138     public void testCallRESTQueryAAICloudRegionNotFound() {
 
 139         ExecutionEntity mockExecution = setupMock()
 
 140         when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
 
 141         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
 
 142         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
 
 143         when(mockExecution.getVariable(Prefix + "queryCloudRegionReturnCode")).thenReturn("404")
 
 145         wireMockRule.stubFor(
 
 146                 get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/12345"))
 
 147                 .willReturn(aResponse()
 
 150         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
 
 151         obj.callRESTQueryAAICloudRegion(mockExecution, "true")
 
 153         Mockito.verify(mockExecution).getVariable(eq("lcpCloudRegionId"))
 
 154         Mockito.verify(mockExecution).setVariable(eq(Prefix + "queryCloudRegionReturnCode"), eq("404"))
 
 155         Mockito.verify(mockExecution).setVariable(eq(Prefix + "aicCloudRegion"), eq("AAIAIC25"))
 
 159     public void testPrepareVnfAdapterDeleteRequest() {
 
 160         ExecutionEntity mockExecution = setupMock()
 
 161         when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
 
 162         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
 
 163         when(mockExecution.getVariable("aicCloudRegion")).thenReturn("RegionOne")
 
 164         when(mockExecution.getVariable("tenantId")).thenReturn("12345")
 
 165         when(mockExecution.getVariable("volumeGroupId")).thenReturn("12345")
 
 166         when(mockExecution.getVariable("volumeGroupHeatStackId")).thenReturn("12345")
 
 167         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
 
 168         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
 
 169         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
 
 170         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("http://localhost:18080/mso/WorkflowMessage")
 
 172         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
 
 173         obj.prepareVnfAdapterDeleteRequest(mockExecution, "true")
 
 175         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
 
 176         String str = FileUtil.readResourceFile("__files/DoCreateVfModuleVolumeV2/vnfAdapterDeleteRequest.xml")
 
 177         XmlComparator.assertXMLEquals(str, captor.getValue(),"messageId","notificationUrl")
 
 181     void testCallRESTQueryAAIForVolumeGroup(){
 
 182         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
 
 183         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
 
 184         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
 
 185         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
 
 186         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/AAI/VolumeGroupWithTenant.json");
 
 187         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
 
 188         when(client.get(resourceUri)).thenReturn(
 
 189                 new AAIResultWrapper(FileUtil.readResourceFile("__files/AAI/VolumeGroupWithTenant.json")))
 
 190         deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution,"true")
 
 191         Mockito.verify(mockExecution).setVariable("DDVMV_queryAAIVolGrpResponse", volumeGroup.get())
 
 192         Mockito.verify(mockExecution).setVariable("DDVMV_volumeGroupHeatStackId", volumeGroup.get().getHeatStackId())
 
 196     void testCallRESTQueryAAIForVolumeGroupNoTenant(){
 
 197         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
 
 198         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
 
 199         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
 
 200         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
 
 201         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
 
 202         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
 
 204             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
 
 205         }catch(BpmnError error) {
 
 206             println " Test End - Handle catch-throw BpmnError()! "
 
 211     void testCallRESTQueryAAIForVolumeGroupDifferentTenant(){
 
 212         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant12345")
 
 213         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
 
 214         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
 
 215         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
 
 216         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/AAI/VolumeGroupWithTenant.json");
 
 217         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
 
 219             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
 
 220         }catch(BpmnError error) {
 
 221             println " Test End - Handle catch-throw BpmnError()! "
 
 226     void testCallRESTQueryAAIForVolumeGroupNotFound(){
 
 227         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
 
 228         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
 
 229         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
 
 230         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
 
 231         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(Optional.empty())
 
 233             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
 
 234         }catch(BpmnError error) {
 
 235             println " Test End - Handle catch-throw BpmnError()! "
 
 240     void testCallRESTQueryAAIForVolumeGroupWithVfModule(){
 
 241         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
 
 242         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
 
 243         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
 
 244         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
 
 245         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithVfModule.json");
 
 246         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
 
 248             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
 
 249         }catch(BpmnError error) {
 
 250             println " Test End - Handle catch-throw BpmnError()! "
 
 255     void testCallRESTDeleteAAIVolumeGroup(){
 
 256         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
 
 257         when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
 
 258         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
 
 259         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
 
 260         doNothing().when(client).delete(resourceUri)
 
 261         deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution,"true")
 
 265     void testCallRESTDeleteAAIVolumeGroupAaiError(){
 
 266         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithVfModule.json");
 
 267         when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
 
 268         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
 
 269         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1",volumeGroup.get().getVolumeGroupId())
 
 270         doThrow(new GraphInventoryUriComputationException("Error")).when(client).delete(resourceUri)
 
 272             deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
 
 273         } catch (BpmnError error) {
 
 274                 println " Test End - Handle catch-throw BpmnError()! "
 
 279     void testCallRESTDeleteAAIVolumeGroupNotFound(){
 
 280         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
 
 281         when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
 
 282         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
 
 283         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
 
 284         doThrow(new NotFoundException("VolumeGroup Not found")).when(client).delete(resourceUri)
 
 286             deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
 
 287         } catch (BpmnError error) {
 
 288             println " Test End - Handle catch-throw BpmnError()! "
 
 295     private ExecutionEntity setupMock() {
 
 297         mockRepositoryService = mock(RepositoryService.class)
 
 298         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
 
 299         when(mockProcessEngineServices.getRepositoryService()).thenReturn(this.mockRepositoryService)
 
 301         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
 
 302         // Initialize prerequisite variables
 
 303         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
 
 309     private void mockSuccessfulCloudData() {
 
 310         wireMockRule.stubFor(get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/12345"))
 
 311                 .willReturn(aResponse()
 
 312                 .withStatus(200).withHeader("Content-Type", "text/xml")
 
 313                 .withBodyFile("DoCreateVfModuleVolumeV2/cloudRegion_AAIResponse_Success.xml")))