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