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 com.github.tomakehurst.wiremock.junit.WireMockRule
 
  24 import org.camunda.bpm.engine.ProcessEngineServices
 
  25 import org.camunda.bpm.engine.RepositoryService
 
  26 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
 
  27 import org.camunda.bpm.engine.repository.ProcessDefinition
 
  28 import org.junit.Assert
 
  29 import org.junit.Before
 
  30 import org.junit.Ignore
 
  33 import org.junit.runner.RunWith
 
  34 import org.mockito.ArgumentCaptor
 
  35 import org.mockito.Captor
 
  36 import org.mockito.Mockito
 
  37 import org.mockito.MockitoAnnotations
 
  38 import org.mockito.runners.MockitoJUnitRunner
 
  39 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
 
  40 import org.onap.so.bpmn.core.WorkflowException
 
  41 import org.onap.so.bpmn.mock.FileUtil
 
  43 import static com.github.tomakehurst.wiremock.client.WireMock.*
 
  44 import static org.mockito.Mockito.*
 
  46 @RunWith(MockitoJUnitRunner.class)
 
  47 class DoDeleteVfModuleVolumeV2Test {
 
  49     public WireMockRule wireMockRule = new WireMockRule(28090);
 
  52     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
 
  55     public void init() throws IOException {
 
  56         MockitoAnnotations.initMocks(this);
 
  60     public void testCallRESTQueryAAICloudRegion() {
 
  61         ExecutionEntity mockExecution = setupMock()
 
  62         when(mockExecution.getVariable("prefix")).thenReturn("DDVMV_")
 
  63         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
 
  64         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
 
  65         when(mockExecution.getVariable("mso.workflow.DoDeleteVfModuleVolumeV2.aai.cloud-region.uri")).thenReturn("/aai/v8/cloud-infrastructure/cloud-regions/cloud-region")
 
  66         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
 
  67         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
 
  70         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
 
  71         obj.callRESTQueryAAICloudRegion(mockExecution, "true")
 
  72         Mockito.verify(mockExecution).setVariable("DDVMV_queryCloudRegionReturnCode", "200")
 
  73         Mockito.verify(mockExecution).setVariable("DDVMV_aicCloudRegion", "RDM2WAGPLCP")
 
  77     public void testCallRESTQueryAAICloudRegionAAiEndpointNull() {
 
  78         ExecutionEntity mockExecution = setupMock()
 
  79         when(mockExecution.getVariable("prefix")).thenReturn("DDVMV_")
 
  80         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
 
  81         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
 
  82         when(mockExecution.getVariable("mso.workflow.DoDeleteVfModuleVolumeV2.aai.cloud-region.uri")).thenReturn("/aai/v8/cloud-infrastructure/cloud-regions/cloud-region")
 
  83         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
 
  84         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
 
  88             DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
 
  89             obj.callRESTQueryAAICloudRegion(mockExecution, "true")
 
  91         } catch (Exception ex) {
 
  92             println " Test End - Handle catch-throw BpmnError()! "
 
  94         Mockito.verify(mockExecution,atLeastOnce()).setVariable(captor.capture(),captor.capture())
 
  95         WorkflowException workflowException = captor.getValue()
 
  96         Assert.assertEquals(9999, workflowException.getErrorCode())
 
 100     public void testPrepareVnfAdapterDeleteRequest() {
 
 101         ExecutionEntity mockExecution = setupMock()
 
 102         when(mockExecution.getVariable("prefix")).thenReturn("DDVMV_")
 
 103         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
 
 104         when(mockExecution.getVariable("aicCloudRegion")).thenReturn("RegionOne")
 
 105         when(mockExecution.getVariable("tenantId")).thenReturn("12345")
 
 106         when(mockExecution.getVariable("volumeGroupId")).thenReturn("12345")
 
 107         when(mockExecution.getVariable("volumeGroupHeatStackId")).thenReturn("12345")
 
 108         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
 
 109         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
 
 110         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
 
 111         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("http://localhost:18080/mso/WorkflowMessage")
 
 113         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
 
 114         obj.prepareVnfAdapterDeleteRequest(mockExecution, "true")
 
 116         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
 
 117         String str = FileUtil.readResourceFile("__files/DoCreateVfModuleVolumeV2/vnfAdapterDeleteRequest.xml")
 
 118         XmlComparator.assertXMLEquals(str, captor.getValue(),"messageId","notificationUrl")
 
 122     private ExecutionEntity setupMock() {
 
 124         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
 
 125         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVfModuleVolumeV2")
 
 126         RepositoryService mockRepositoryService = mock(RepositoryService.class)
 
 127         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
 
 128         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVfModuleVolumeV2")
 
 129         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
 
 130         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
 
 131         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
 
 133         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
 
 134         // Initialize prerequisite variables
 
 136         when(mockExecution.getId()).thenReturn("100")
 
 137         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVfModuleVolumeV2")
 
 138         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVfModuleVolumeV2")
 
 139         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
 
 140         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
 
 146     private void mockData() {
 
 147         stubFor(get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/12345"))
 
 148                 .willReturn(aResponse()
 
 149                 .withStatus(200).withHeader("Content-Type", "text/xml")
 
 150                 .withBodyFile("DoCreateVfModuleVolumeV2/cloudRegion_AAIResponse_Success.xml")))