Springboot 2.0 upgrade
[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("rollbackData")).thenReturn(new RollbackData())
169         List fqdnList = new ArrayList()
170         fqdnList.add("test")
171         when(mockExecution.getVariable(prefix + "createdNetworkPolicyFqdnList")).thenReturn(fqdnList)
172
173         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
174         uri.queryParam("network-policy-fqdn", "test")
175         NetworkPolicies networkPolicies = new NetworkPolicies();
176         NetworkPolicy networkPolicy = new NetworkPolicy();
177         networkPolicy.setNetworkPolicyId("NP1")
178         networkPolicies.getNetworkPolicy().add(networkPolicy)
179         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
180
181         AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, networkPolicy.getNetworkPolicyId())
182         doNothing().when(client).delete(delUri)
183
184         doCreateVfModuleRollback.deleteNetworkPoliciesFromAAI(mockExecution)
185
186         Mockito.verify(mockExecution).setVariable("prefix", prefix)
187         Mockito.verify(mockExecution).setVariable(prefix + "networkPolicyFqdnCount", 1)
188         Mockito.verify(mockExecution).setVariable(prefix + "aaiQueryNetworkPolicyByFqdnReturnCode", 200)
189     }
190
191     @Test
192     void testDeleteNetworkPoliciesFromAAINotFound() {
193         ExecutionEntity mockExecution = setupMock()
194         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
195         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
196         when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
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         AAIResourceUri 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("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 }