replace all fixed wiremock ports
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeleteVfModuleTest.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.scripts
22
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
31 import org.junit.Rule
32 import org.junit.Test
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.AAIResourceUri
51 import org.onap.so.client.aai.entities.uri.AAIUriFactory
52
53 import javax.ws.rs.NotFoundException
54
55 import static com.github.tomakehurst.wiremock.client.WireMock.*
56 import static org.mockito.Mockito.*
57
58 @RunWith(MockitoJUnitRunner.class)
59 class DoDeleteVfModuleTest extends MsoGroovyTest{
60
61     @Spy
62     DoDeleteVfModule doDeleteVfModule
63
64     @Captor
65     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
66
67     @Before
68     public void init() throws IOException {
69         super.init("DoDeleteVfModule")
70         MockitoAnnotations.initMocks(this);
71         when(doDeleteVfModule.getAAIClient()).thenReturn(client)
72     }
73
74     @Test
75     public void testPrepSDNCAdapterRequest() {
76         ExecutionEntity mockExecution = setupMock()
77         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
78         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
79         when(mockExecution.getVariable("requestId")).thenReturn("12345")
80         when(mockExecution.getVariable("source")).thenReturn("VID")
81         when(mockExecution.getVariable("serviceId")).thenReturn("12345")
82         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
83         when(mockExecution.getVariable("tenantId")).thenReturn("19123c2924c648eb8e42a3c1f14b7682")
84         when(mockExecution.getVariable("vfModuleId")).thenReturn("12345")
85         when(mockExecution.getVariable("DoDVfMod_serviceInstanceIdToSdnc")).thenReturn("123456789")
86         when(mockExecution.getVariable("vfModuleName")).thenReturn("vfModuleName_test")
87         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapterCallback")
88
89         DoDeleteVfModule obj = new DoDeleteVfModule()
90         obj.prepSDNCAdapterRequest(mockExecution, 'release')
91
92         String expectedValue = FileUtil.readResourceFile("__files/DoDeleteVfModule/sdncAdapterWorkflowRequest.xml")
93         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
94         XmlComparator.assertXMLEquals(expectedValue, captor.getValue())
95     }
96    
97
98     @Test
99     void testDeleteNetworkPoliciesFromAAI() {
100         List fqdnList = new ArrayList()
101         fqdnList.add("test")
102         when(mockExecution.getVariable("DoDVfMod_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
103         NetworkPolicies networkPolicies = new NetworkPolicies()
104         NetworkPolicy networkPolicy = new NetworkPolicy()
105         networkPolicy.setNetworkPolicyId("NP1")
106         networkPolicies.getNetworkPolicy().add(networkPolicy)
107         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
108         uri.queryParam("network-policy-fqdn", "test")
109         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
110         doDeleteVfModule.deleteNetworkPoliciesFromAAI(mockExecution)
111         Mockito.verify(mockExecution).setVariable("DCVFM_aaiQueryNetworkPolicyByFqdnReturnCode", 200)
112     }
113
114     @Test
115     void testDeleteNetworkPoliciesFromAAIError() {
116         List fqdnList = new ArrayList()
117         fqdnList.add("test")
118         when(mockExecution.getVariable("DoDVfMod_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
119         NetworkPolicies networkPolicies = new NetworkPolicies()
120         NetworkPolicy networkPolicy = new NetworkPolicy()
121         networkPolicy.setNetworkPolicyId("NP1")
122         networkPolicies.getNetworkPolicy().add(networkPolicy)
123         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
124         uri.queryParam("network-policy-fqdn", "test")
125         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
126         AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, "NP1")
127         doThrow(new NotFoundException(("Not Found !"))).when(client).delete(delUri)
128         doDeleteVfModule.deleteNetworkPoliciesFromAAI(mockExecution)
129         Mockito.verify(client).delete(delUri)
130     }
131
132     @Test
133     void testQueryAAIVfModuleForStatus() {
134         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
135         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
136         when(mockExecution.getVariable("vfModuleId")).thenReturn("module-0")
137         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE,"12345","module-0")
138         VfModule vfModule = new VfModule()
139         vfModule.setOrchestrationStatus("Created")
140         when(client.get(VfModule.class, uri)).thenReturn(Optional.of(vfModule))
141         doDeleteVfModule.queryAAIVfModuleForStatus(mockExecution)
142         Mockito.verify(mockExecution).setVariable("DoDVfMod_queryAAIVfModuleForStatusResponseCode", 200)
143     }
144
145   
146
147     private ExecutionEntity setupMock() {
148
149         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
150         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVfModule")
151         RepositoryService mockRepositoryService = mock(RepositoryService.class)
152         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
153         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVfModule")
154         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
155         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
156         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
157
158         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
159         // Initialize prerequisite variables
160
161         when(mockExecution.getId()).thenReturn("100")
162         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVfModule")
163         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVfModule")
164         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
165         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
166
167         return mockExecution
168
169     }
170
171     private static void mockData() {
172         stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
173                 .willReturn(aResponse()
174                 .withStatus(200)
175                 .withHeader("Content-Type", "text/xml")
176                 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
177
178         stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
179                 .willReturn(aResponse()
180                 .withStatus(200)))
181
182         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345/vf-modules/vf-module[?]vf-module-name=module-0"))
183                 .willReturn(aResponse()
184                 .withStatus(200).withHeader("Content-Type", "text/xml")
185                 .withBodyFile("DoDeleteVfModule/getGenericVnfResponse.xml")))
186
187     }
188 }
189