2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
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
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.AAIObjectType
47 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri
48 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
49 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
50 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
51 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
52 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException
54 import javax.ws.rs.NotFoundException
56 import static com.github.tomakehurst.wiremock.client.WireMock.*
57 import static org.mockito.Mockito.*
59 @RunWith(MockitoJUnitRunner.class)
60 class DoCreateVfModuleRollbackTest extends MsoGroovyTest{
62 def prefix = "DCVFMR_"
65 DoCreateVfModuleRollback doCreateVfModuleRollback
68 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
71 void init() throws IOException {
72 super.init("CreateVFCNSResource")
73 MockitoAnnotations.initMocks(this)
74 when(doCreateVfModuleRollback.getAAIClient()).thenReturn(client)
78 void testPrepSDNCAdapterRequest() {
79 ExecutionEntity mockExecution = setupMock()
80 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
81 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
82 when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
83 when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
84 when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
85 when(mockExecution.getVariable(prefix + "source")).thenReturn("VID")
86 when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
87 when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
88 when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
89 when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
90 when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
91 when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
92 when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
93 when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
94 when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
95 when(mockExecution.getVariable(prefix + "cloudSiteId")).thenReturn("RDM2WAGPLCP")
97 when(mockExecution.getVariable(prefix + "rollbackSDNCRequestActivate")).thenReturn("true")
100 DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
101 obj.prepSDNCAdapterRequest(mockExecution)
103 Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
104 String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/sdncAdapterWorkflowRequest.xml")
105 XmlComparator.assertXMLEquals(expectedValue, captor.getValue())
111 void testBuildSDNCRequest() {
112 ExecutionEntity mockExecution = setupMock()
113 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
114 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
115 when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
116 when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
117 when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
118 when(mockExecution.getVariable(prefix + "source")).thenReturn("VID")
119 when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
120 when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
121 when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
122 when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
123 when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
124 when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
125 when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
127 when(mockExecution.getVariable(prefix + "rollbackSDNCRequestActivate")).thenReturn("true")
130 DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
131 String sdncRequest = obj.buildSDNCRequest(mockExecution, "svcInstId_test", "deactivate")
132 String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/deactivateSDNCRequest.xml")
133 XmlComparator.assertXMLEquals(expectedValue, sdncRequest)
139 void testPrepVNFAdapterRequest() {
140 ExecutionEntity mockExecution = setupMock()
141 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
142 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
143 when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
144 when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
145 when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
146 when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
147 when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
148 when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
149 when(mockExecution.getVariable(prefix + "mso-request-id")).thenReturn("12345")
150 when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:18080/mso/WorkflowMessage/')
153 DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
154 String sdncRequest = obj.prepVNFAdapterRequest(mockExecution)
156 Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
157 String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/vnfAdapterRestV1Request.xml")
158 XmlComparator.assertXMLEquals(expectedValue, captor.getValue(), "messageId", "notificationUrl")
162 void testDeleteNetworkPoliciesFromAAI() {
163 ExecutionEntity mockExecution = setupMock()
164 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
165 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
166 when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
167 when(mockExecution.getVariable("DCVFM_cloudOwner")).thenReturn("CloudOwner")
168 when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
169 List fqdnList = new ArrayList()
171 when(mockExecution.getVariable(prefix + "createdNetworkPolicyFqdnList")).thenReturn(fqdnList)
173 AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicies())
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))
181 AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicy.getNetworkPolicyId()))
182 doNothing().when(client).delete(delUri)
184 doCreateVfModuleRollback.deleteNetworkPoliciesFromAAI(mockExecution)
186 Mockito.verify(mockExecution).setVariable("prefix", prefix)
187 Mockito.verify(mockExecution).setVariable(prefix + "networkPolicyFqdnCount", 1)
188 Mockito.verify(mockExecution).setVariable(prefix + "aaiQueryNetworkPolicyByFqdnReturnCode", 200)
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("DCVFM_cloudOwner")).thenReturn("CloudOwner")
198 when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
199 List fqdnList = new ArrayList()
201 when(mockExecution.getVariable(prefix + "createdNetworkPolicyFqdnList")).thenReturn(fqdnList)
203 AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicies())
204 uri.queryParam("network-policy-fqdn", "test")
205 NetworkPolicies networkPolicies = new NetworkPolicies();
206 NetworkPolicy networkPolicy = new NetworkPolicy();
207 networkPolicy.setNetworkPolicyId("NP1")
208 networkPolicies.getNetworkPolicy().add(networkPolicy)
209 when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
211 AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicy.getNetworkPolicyId()))
212 doThrow(new NotFoundException("Not Found!")).when(client).delete(delUri)
214 doCreateVfModuleRollback.deleteNetworkPoliciesFromAAI(mockExecution)
216 Mockito.verify(mockExecution).setVariable("prefix", prefix)
217 Mockito.verify(mockExecution).setVariable(prefix + "networkPolicyFqdnCount", 1)
218 Mockito.verify(mockExecution).setVariable(prefix + "aaiDeleteNetworkPolicyReturnCode", 404)
222 void testDeleteNetworkPoliciesFromAAIError() {
223 ExecutionEntity mockExecution = setupMock()
224 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
225 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
226 when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
227 when(mockExecution.getVariable("DCVFM_cloudOwner")).thenReturn("CloudOwner")
228 when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
229 List fqdnList = new ArrayList()
231 when(mockExecution.getVariable(prefix + "createdNetworkPolicyFqdnList")).thenReturn(fqdnList)
233 AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicies())
234 uri.queryParam("network-policy-fqdn", "test")
235 NetworkPolicies networkPolicies = new NetworkPolicies();
236 NetworkPolicy networkPolicy = new NetworkPolicy();
237 networkPolicy.setNetworkPolicyId("NP1")
238 networkPolicies.getNetworkPolicy().add(networkPolicy)
239 when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
241 AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicy.getNetworkPolicyId()))
242 doThrow(new GraphInventoryUriComputationException("Error!")).when(client).delete(delUri)
244 doCreateVfModuleRollback.deleteNetworkPoliciesFromAAI(mockExecution)
245 } catch (Exception ex) {
246 println " Test End - Handle catch-throw BpmnError()! "
248 Mockito.verify(mockExecution, times(4)).setVariable(captor.capture(), captor.capture())
249 WorkflowException workflowException = captor.getValue()
250 Assert.assertEquals(2500, workflowException.getErrorCode())
251 Assert.assertEquals("Unable to delete network-policy to AAI deleteNetworkPoliciesFromAAI - Error!", workflowException.getErrorMessage())
255 private static ExecutionEntity setupMock() {
256 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
257 when(mockProcessDefinition.getKey()).thenReturn("DoCreateVfModuleRollback")
258 RepositoryService mockRepositoryService = mock(RepositoryService.class)
259 when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
260 when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoCreateVfModuleRollback")
261 when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
262 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
263 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
265 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
266 // Initialize prerequisite variables
267 when(mockExecution.getId()).thenReturn("100")
268 when(mockExecution.getProcessDefinitionId()).thenReturn("DoCreateVfModuleRollback")
269 when(mockExecution.getProcessInstanceId()).thenReturn("DoCreateVfModuleRollback")
270 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
271 when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
276 private static void mockData() {
277 stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
278 .willReturn(aResponse()
280 .withHeader("Content-Type", "text/xml")
281 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
282 stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
283 .willReturn(aResponse()