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