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.Spy
 
  39 import org.mockito.runners.MockitoJUnitRunner
 
  40 import org.onap.aai.domain.yang.NetworkPolicies
 
  41 import org.onap.aai.domain.yang.NetworkPolicy
 
  42 import org.onap.aai.domain.yang.VfModule
 
  43 import org.onap.aai.domain.yang.VfModules
 
  44 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
 
  45 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
 
  46 import org.onap.so.bpmn.core.WorkflowException
 
  47 import org.onap.so.bpmn.mock.FileUtil
 
  48 import org.onap.so.client.aai.AAIObjectPlurals
 
  49 import org.onap.so.client.aai.AAIObjectType
 
  50 import org.onap.so.client.aai.entities.uri.AAIPluralResourceUri
 
  51 import org.onap.so.client.aai.entities.uri.AAIResourceUri
 
  52 import org.onap.so.client.aai.entities.uri.AAIUriFactory
 
  54 import javax.ws.rs.NotFoundException
 
  56 import static com.github.tomakehurst.wiremock.client.WireMock.*
 
  57 import static org.mockito.Mockito.*
 
  59 @RunWith(MockitoJUnitRunner.class)
 
  60 class DoDeleteVfModuleTest extends MsoGroovyTest{
 
  63     DoDeleteVfModule doDeleteVfModule
 
  66     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
 
  69     public void init() throws IOException {
 
  70         super.init("DoDeleteVfModule")
 
  71         MockitoAnnotations.initMocks(this);
 
  72         when(doDeleteVfModule.getAAIClient()).thenReturn(client)
 
  76     public void testPrepSDNCAdapterRequest() {
 
  77         ExecutionEntity mockExecution = setupMock()
 
  78         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
 
  79         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
 
  80         when(mockExecution.getVariable("requestId")).thenReturn("12345")
 
  81         when(mockExecution.getVariable("source")).thenReturn("VID")
 
  82         when(mockExecution.getVariable("serviceId")).thenReturn("12345")
 
  83         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
 
  84         when(mockExecution.getVariable("tenantId")).thenReturn("19123c2924c648eb8e42a3c1f14b7682")
 
  85         when(mockExecution.getVariable("vfModuleId")).thenReturn("12345")
 
  86         when(mockExecution.getVariable("DoDVfMod_serviceInstanceIdToSdnc")).thenReturn("123456789")
 
  87         when(mockExecution.getVariable("vfModuleName")).thenReturn("vfModuleName_test")
 
  88         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapterCallback")
 
  90         DoDeleteVfModule obj = new DoDeleteVfModule()
 
  91         obj.prepSDNCAdapterRequest(mockExecution, 'release')
 
  93         String expectedValue = FileUtil.readResourceFile("__files/DoDeleteVfModule/sdncAdapterWorkflowRequest.xml")
 
  94         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
 
  95         XmlComparator.assertXMLEquals(expectedValue, captor.getValue())
 
 100     void testDeleteNetworkPoliciesFromAAI() {
 
 101         List fqdnList = new ArrayList()
 
 103         when(mockExecution.getVariable("DoDVfMod_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
 
 104         NetworkPolicies networkPolicies = new NetworkPolicies()
 
 105         NetworkPolicy networkPolicy = new NetworkPolicy()
 
 106         networkPolicy.setNetworkPolicyId("NP1")
 
 107         networkPolicies.getNetworkPolicy().add(networkPolicy)
 
 108         AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
 
 109         uri.queryParam("network-policy-fqdn", "test")
 
 110         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
 
 111         doDeleteVfModule.deleteNetworkPoliciesFromAAI(mockExecution)
 
 112         Mockito.verify(mockExecution).setVariable("DCVFM_aaiQueryNetworkPolicyByFqdnReturnCode", 200)
 
 116     void testDeleteNetworkPoliciesFromAAIError() {
 
 117         List fqdnList = new ArrayList()
 
 119         when(mockExecution.getVariable("DoDVfMod_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
 
 120         NetworkPolicies networkPolicies = new NetworkPolicies()
 
 121         NetworkPolicy networkPolicy = new NetworkPolicy()
 
 122         networkPolicy.setNetworkPolicyId("NP1")
 
 123         networkPolicies.getNetworkPolicy().add(networkPolicy)
 
 124         AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
 
 125         uri.queryParam("network-policy-fqdn", "test")
 
 126         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
 
 127         AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, "NP1")
 
 128         doThrow(new NotFoundException(("Not Found !"))).when(client).delete(delUri)
 
 129         doDeleteVfModule.deleteNetworkPoliciesFromAAI(mockExecution)
 
 130         Mockito.verify(client).delete(delUri)
 
 134     void testQueryAAIVfModuleForStatus() {
 
 135         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
 
 136         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
 
 137         when(mockExecution.getVariable("vfModuleId")).thenReturn("module-0")
 
 138         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE,"12345","module-0")
 
 139         VfModule vfModule = new VfModule()
 
 140         vfModule.setOrchestrationStatus("Created")
 
 141         when(client.get(VfModule.class, uri)).thenReturn(Optional.of(vfModule))
 
 142         doDeleteVfModule.queryAAIVfModuleForStatus(mockExecution)
 
 143         Mockito.verify(mockExecution).setVariable("DoDVfMod_queryAAIVfModuleForStatusResponseCode", 200)
 
 148     private ExecutionEntity setupMock() {
 
 150         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
 
 151         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVfModule")
 
 152         RepositoryService mockRepositoryService = mock(RepositoryService.class)
 
 153         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
 
 154         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVfModule")
 
 155         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
 
 156         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
 
 157         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
 
 159         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
 
 160         // Initialize prerequisite variables
 
 162         when(mockExecution.getId()).thenReturn("100")
 
 163         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVfModule")
 
 164         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVfModule")
 
 165         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
 
 166         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
 
 172     private static void mockData() {
 
 173         stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
 
 174                 .willReturn(aResponse()
 
 176                 .withHeader("Content-Type", "text/xml")
 
 177                 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
 
 179         stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
 
 180                 .willReturn(aResponse()
 
 183         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345/vf-modules/vf-module[?]vf-module-name=module-0"))
 
 184                 .willReturn(aResponse()
 
 185                 .withStatus(200).withHeader("Content-Type", "text/xml")
 
 186                 .withBodyFile("DoDeleteVfModule/getGenericVnfResponse.xml")))