replace all fixed wiremock ports
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateVfModuleRollbackTest.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.Rule
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.ArgumentCaptor
34 import org.mockito.Captor
35 import org.mockito.Mockito
36 import org.mockito.MockitoAnnotations
37 import org.mockito.Spy
38 import org.mockito.runners.MockitoJUnitRunner
39 import org.onap.aai.domain.yang.NetworkPolicies
40 import org.onap.aai.domain.yang.NetworkPolicy
41 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
42 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
43 import org.onap.so.bpmn.core.RollbackData
44 import org.onap.so.bpmn.core.WorkflowException
45 import org.onap.so.bpmn.mock.FileUtil
46 import org.onap.so.client.aai.AAIObjectPlurals
47 import org.onap.so.client.aai.AAIObjectType
48 import org.onap.so.client.aai.entities.uri.AAIResourceUri
49 import org.onap.so.client.aai.entities.uri.AAIUriFactory
50 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException
51
52 import javax.ws.rs.NotFoundException
53
54 import static com.github.tomakehurst.wiremock.client.WireMock.*
55 import static org.mockito.Mockito.*
56
57 @RunWith(MockitoJUnitRunner.class)
58 class DoCreateVfModuleRollbackTest extends MsoGroovyTest{
59
60     def prefix = "DCVFMR_"
61
62     @Spy
63     DoCreateVfModuleRollback doCreateVfModuleRollback
64
65     @Captor
66     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
67
68     @Before
69     void init() throws IOException {
70         super.init("CreateVFCNSResource")
71         MockitoAnnotations.initMocks(this)
72         when(doCreateVfModuleRollback.getAAIClient()).thenReturn(client)
73     }
74
75     @Test
76     void testPrepSDNCAdapterRequest() {
77         ExecutionEntity mockExecution = setupMock()
78         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
79         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
80         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
81         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
82         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
83         when(mockExecution.getVariable(prefix + "source")).thenReturn("VID")
84         when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
85         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
86         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
87         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
88         when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
89         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
90         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
91         when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
92         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
93         when(mockExecution.getVariable(prefix + "cloudSiteId")).thenReturn("RDM2WAGPLCP")
94
95         when(mockExecution.getVariable(prefix + "rollbackSDNCRequestActivate")).thenReturn("true")
96
97
98         DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
99         obj.prepSDNCAdapterRequest(mockExecution)
100
101         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
102         String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/sdncAdapterWorkflowRequest.xml")
103         XmlComparator.assertXMLEquals(expectedValue, captor.getValue())
104     }
105
106     
107
108     @Test
109     void testBuildSDNCRequest() {
110         ExecutionEntity mockExecution = setupMock()
111         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
112         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
113         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
114         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
115         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
116         when(mockExecution.getVariable(prefix + "source")).thenReturn("VID")
117         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
118         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
119         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
120         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
121         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
122         when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
123         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
124
125         when(mockExecution.getVariable(prefix + "rollbackSDNCRequestActivate")).thenReturn("true")
126
127
128         DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
129         String sdncRequest = obj.buildSDNCRequest(mockExecution, "svcInstId_test", "deactivate")
130         String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/deactivateSDNCRequest.xml")
131         XmlComparator.assertXMLEquals(expectedValue, sdncRequest)
132     }
133
134    
135
136     @Test
137     void testPrepVNFAdapterRequest() {
138         ExecutionEntity mockExecution = setupMock()
139         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
140         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
141         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
142         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
143         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
144         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
145         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
146         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
147         when(mockExecution.getVariable(prefix + "mso-request-id")).thenReturn("12345")
148         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:18080/mso/WorkflowMessage/')
149
150
151         DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
152         String sdncRequest = obj.prepVNFAdapterRequest(mockExecution)
153
154         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
155         String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/vnfAdapterRestV1Request.xml")
156         XmlComparator.assertXMLEquals(expectedValue, captor.getValue(), "messageId", "notificationUrl")
157     }
158
159     @Test
160     void testDeleteNetworkPoliciesFromAAI() {
161         ExecutionEntity mockExecution = setupMock()
162         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
163         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
164         when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
165         when(mockExecution.getVariable("DCVFM_cloudOwner")).thenReturn("CloudOwner")
166         when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
167         List fqdnList = new ArrayList()
168         fqdnList.add("test")
169         when(mockExecution.getVariable(prefix + "createdNetworkPolicyFqdnList")).thenReturn(fqdnList)
170
171         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
172         uri.queryParam("network-policy-fqdn", "test")
173         NetworkPolicies networkPolicies = new NetworkPolicies();
174         NetworkPolicy networkPolicy = new NetworkPolicy();
175         networkPolicy.setNetworkPolicyId("NP1")
176         networkPolicies.getNetworkPolicy().add(networkPolicy)
177         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
178
179         AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, networkPolicy.getNetworkPolicyId())
180         doNothing().when(client).delete(delUri)
181
182         doCreateVfModuleRollback.deleteNetworkPoliciesFromAAI(mockExecution)
183
184         Mockito.verify(mockExecution).setVariable("prefix", prefix)
185         Mockito.verify(mockExecution).setVariable(prefix + "networkPolicyFqdnCount", 1)
186         Mockito.verify(mockExecution).setVariable(prefix + "aaiQueryNetworkPolicyByFqdnReturnCode", 200)
187     }
188
189     @Test
190     void testDeleteNetworkPoliciesFromAAINotFound() {
191         ExecutionEntity mockExecution = setupMock()
192         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
193         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
194         when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
195         when(mockExecution.getVariable("DCVFM_cloudOwner")).thenReturn("CloudOwner")
196         when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
197         List fqdnList = new ArrayList()
198         fqdnList.add("test")
199         when(mockExecution.getVariable(prefix + "createdNetworkPolicyFqdnList")).thenReturn(fqdnList)
200
201         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
202         uri.queryParam("network-policy-fqdn", "test")
203         NetworkPolicies networkPolicies = new NetworkPolicies();
204         NetworkPolicy networkPolicy = new NetworkPolicy();
205         networkPolicy.setNetworkPolicyId("NP1")
206         networkPolicies.getNetworkPolicy().add(networkPolicy)
207         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
208
209         AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, networkPolicy.getNetworkPolicyId())
210         doThrow(new NotFoundException("Not Found!")).when(client).delete(delUri)
211
212         doCreateVfModuleRollback.deleteNetworkPoliciesFromAAI(mockExecution)
213
214         Mockito.verify(mockExecution).setVariable("prefix", prefix)
215         Mockito.verify(mockExecution).setVariable(prefix + "networkPolicyFqdnCount", 1)
216         Mockito.verify(mockExecution).setVariable(prefix + "aaiDeleteNetworkPolicyReturnCode", 404)
217     }
218
219     @Test
220     void testDeleteNetworkPoliciesFromAAIError() {
221         ExecutionEntity mockExecution = setupMock()
222         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
223         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
224         when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
225         when(mockExecution.getVariable("DCVFM_cloudOwner")).thenReturn("CloudOwner")
226         when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
227         List fqdnList = new ArrayList()
228         fqdnList.add("test")
229         when(mockExecution.getVariable(prefix + "createdNetworkPolicyFqdnList")).thenReturn(fqdnList)
230
231         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
232         uri.queryParam("network-policy-fqdn", "test")
233         NetworkPolicies networkPolicies = new NetworkPolicies();
234         NetworkPolicy networkPolicy = new NetworkPolicy();
235         networkPolicy.setNetworkPolicyId("NP1")
236         networkPolicies.getNetworkPolicy().add(networkPolicy)
237         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
238
239         AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, networkPolicy.getNetworkPolicyId())
240         doThrow(new GraphInventoryUriComputationException("Error!")).when(client).delete(delUri)
241         try {
242             doCreateVfModuleRollback.deleteNetworkPoliciesFromAAI(mockExecution)
243         } catch (Exception ex) {
244             println " Test End - Handle catch-throw BpmnError()! "
245         }
246         Mockito.verify(mockExecution, times(4)).setVariable(captor.capture(), captor.capture())
247         WorkflowException workflowException = captor.getValue()
248         Assert.assertEquals(2500, workflowException.getErrorCode())
249         Assert.assertEquals("Unable to delete network-policy to AAI deleteNetworkPoliciesFromAAI - Error!", workflowException.getErrorMessage())
250     }
251
252
253     private static ExecutionEntity setupMock() {
254         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
255         when(mockProcessDefinition.getKey()).thenReturn("DoCreateVfModuleRollback")
256         RepositoryService mockRepositoryService = mock(RepositoryService.class)
257         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
258         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoCreateVfModuleRollback")
259         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
260         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
261         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
262
263         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
264         // Initialize prerequisite variables
265         when(mockExecution.getId()).thenReturn("100")
266         when(mockExecution.getProcessDefinitionId()).thenReturn("DoCreateVfModuleRollback")
267         when(mockExecution.getProcessInstanceId()).thenReturn("DoCreateVfModuleRollback")
268         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
269         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
270
271         return mockExecution
272     }
273
274     private static void mockData() {
275         stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
276                 .willReturn(aResponse()
277                 .withStatus(200)
278                 .withHeader("Content-Type", "text/xml")
279                 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
280         stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
281                 .willReturn(aResponse()
282                 .withStatus(200)));
283
284     }
285 }