replace all fixed wiremock ports
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeleteVfModuleFromVnfTest.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.GenericVnf
41 import org.onap.aai.domain.yang.NetworkPolicies
42 import org.onap.aai.domain.yang.NetworkPolicy
43 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
44 import org.onap.so.bpmn.core.WorkflowException
45 import org.onap.so.client.aai.AAIObjectPlurals
46 import org.onap.so.client.aai.AAIObjectType
47 import org.onap.so.client.aai.entities.uri.AAIResourceUri
48 import org.onap.so.client.aai.entities.uri.AAIUriFactory
49 import org.onap.so.client.graphinventory.entities.uri.Depth
50
51 import static com.github.tomakehurst.wiremock.client.WireMock.*
52 import static org.mockito.Mockito.*
53
54 @RunWith(MockitoJUnitRunner.class)
55 class DoDeleteVfModuleFromVnfTest extends MsoGroovyTest {
56
57     @Captor
58     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
59
60     @Spy
61     DoDeleteVfModuleFromVnf deleteVfModuleFromVnf
62
63     @Before
64     public void init() throws IOException {
65         super.init("DoDeleteVfModuleFromVnf")
66         MockitoAnnotations.initMocks(this)
67         when(deleteVfModuleFromVnf.getAAIClient()).thenReturn(client)
68     }
69
70     @Test
71     public void testPreProcessRequest() {
72         ExecutionEntity mockExecution = setupMock()
73         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
74         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
75         when(mockExecution.getVariable("source")).thenReturn("VID")
76         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
77         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
78         when(mockExecution.getVariable("tenantId")).thenReturn("19123c2924c648eb8e42a3c1f14b7682")
79         when(mockExecution.getVariable("vfModuleId")).thenReturn("12345")
80         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
81         when(mockExecution.getVariable("sdncVersion")).thenReturn("8")
82         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapterCallback")
83
84         DoDeleteVfModuleFromVnf obj = new DoDeleteVfModuleFromVnf()
85         obj.preProcessRequest(mockExecution)
86
87         Mockito.verify(mockExecution).setVariable("prefix", "DDVFMV_")
88         Mockito.verify(mockExecution).setVariable("DDVFMV_contrailNetworkPolicyFqdnList", null)
89         Mockito.verify(mockExecution).setVariable("mso-request-id", "12345")
90         Mockito.verify(mockExecution).setVariable("requestId", "12345")
91         Mockito.verify(mockExecution).setVariable("cloudSiteId", "12345")
92         Mockito.verify(mockExecution).setVariable("source", "VID")
93         Mockito.verify(mockExecution).setVariable("isVidRequest", "true")
94         Mockito.verify(mockExecution).setVariable("srvInstId", "")
95         Mockito.verify(mockExecution).setVariable("DDVFMV_serviceInstanceIdToSdnc", "12345")
96         Mockito.verify(mockExecution).setVariable("DDVFMV_sdncVersion", "8")
97         Mockito.verify(mockExecution).setVariable("sdncCallbackUrl", "http://localhost:8090/SDNCAdapterCallback")
98     }
99
100
101
102     @Test
103     void testDeleteNetworkPoliciesFromAAI() {
104
105         List fqdnList = new ArrayList()
106         fqdnList.add("test")
107         when(mockExecution.getVariable("DDVFMV_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
108         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
109         uri.queryParam("network-policy-fqdn", "test")
110         NetworkPolicies networkPolicies = new NetworkPolicies();
111         NetworkPolicy networkPolicy = new NetworkPolicy();
112         networkPolicy.setNetworkPolicyId("NP1")
113         networkPolicies.getNetworkPolicy().add(networkPolicy)
114         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
115
116         AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, networkPolicy.getNetworkPolicyId())
117         doNothing().when(client).delete(delUri)
118         deleteVfModuleFromVnf.deleteNetworkPoliciesFromAAI(mockExecution)
119
120         Mockito.verify(mockExecution).setVariable("prefix", 'DDVFMV_')
121         Mockito.verify(mockExecution).setVariable("DCVFM_aaiQueryNetworkPolicyByFqdnReturnCode", 200)
122     }
123
124     @Test
125     void testDeleteNetworkPoliciesFromAAINotFound() {
126
127         List fqdnList = new ArrayList()
128         fqdnList.add("test")
129         when(mockExecution.getVariable("DDVFMV_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
130         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
131         uri.queryParam("network-policy-fqdn", "test")
132         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.empty())
133         deleteVfModuleFromVnf.deleteNetworkPoliciesFromAAI(mockExecution)
134
135         Mockito.verify(mockExecution).setVariable("prefix", 'DDVFMV_')
136         Mockito.verify(mockExecution).setVariable("DCVFM_aaiQueryNetworkPolicyByFqdnReturnCode", 404)
137     }
138
139
140     @Test
141     void testQueryAAIForVfModule() {
142         ExecutionEntity mockExecution = setupMock()
143         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
144         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "12345").depth(Depth.ONE)
145         GenericVnf genericVnf = new GenericVnf()
146         genericVnf.setVnfId("test1")
147         when(client.get(GenericVnf.class, uri)).thenReturn(Optional.of(genericVnf))
148         deleteVfModuleFromVnf.queryAAIForVfModule(mockExecution)
149
150         Mockito.verify(mockExecution, atLeastOnce()).setVariable("DDVMFV_getVnfResponseCode", 200)
151         Mockito.verify(mockExecution, atLeastOnce()).setVariable("DDVMFV_getVnfResponse", genericVnf)
152     }
153
154     @Test
155     void testQueryAAIForVfModuleNotFound() {
156         ExecutionEntity mockExecution = setupMock()
157         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
158         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "12345").depth(Depth.ONE)
159         when(client.get(GenericVnf.class, uri)).thenReturn(Optional.empty())
160         deleteVfModuleFromVnf.queryAAIForVfModule(mockExecution)
161         Mockito.verify(mockExecution, atLeastOnce()).setVariable("DDVMFV_getVnfResponseCode", 404)
162     }
163
164
165
166     private ExecutionEntity setupMock() {
167
168         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
169         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVfModuleFromVnf")
170         RepositoryService mockRepositoryService = mock(RepositoryService.class)
171         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
172         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVfModuleFromVnf")
173         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
174         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
175         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
176
177         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
178         // Initialize prerequisite variables
179
180         when(mockExecution.getId()).thenReturn("100")
181         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVfModuleFromVnf")
182         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVfModuleFromVnf")
183         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
184         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
185
186         return mockExecution
187
188     }
189
190     private static void mockData() {
191         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345[?]depth=1"))
192                 .willReturn(aResponse()
193                 .withStatus(200).withHeader("Content-Type", "text/xml")
194                 .withBodyFile("VfModularity/GenerateVfModuleName_AAIResponse_Success.xml")))
195
196         stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
197                 .willReturn(aResponse()
198                 .withStatus(200)
199                 .withHeader("Content-Type", "text/xml")
200                 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
201
202         stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
203                 .willReturn(aResponse()
204                 .withStatus(200)));
205
206     }
207 }