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