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.runners.MockitoJUnitRunner
38 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
39 import org.onap.so.bpmn.core.RollbackData
40 import org.onap.so.bpmn.core.WorkflowException
41 import org.onap.so.bpmn.mock.FileUtil
43 import static com.github.tomakehurst.wiremock.client.WireMock.*
44 import static org.mockito.Mockito.*
46 @RunWith(MockitoJUnitRunner.class)
47 class DoCreateVfModuleRollbackTest {
49 def prefix = "DCVFMR_"
52 public WireMockRule wireMockRule = new WireMockRule(28090)
55 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
58 void init() throws IOException {
59 MockitoAnnotations.initMocks(this);
63 void testPrepSDNCAdapterRequest() {
64 ExecutionEntity mockExecution = setupMock()
65 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
66 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
67 when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
68 when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
69 when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
70 when(mockExecution.getVariable(prefix + "source")).thenReturn("VID")
71 when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
72 when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
73 when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
74 when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
75 when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
76 when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
77 when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
78 when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
79 when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
80 when(mockExecution.getVariable(prefix + "cloudSiteId")).thenReturn("RDM2WAGPLCP")
82 when(mockExecution.getVariable(prefix + "rollbackSDNCRequestActivate")).thenReturn("true")
85 DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
86 obj.prepSDNCAdapterRequest(mockExecution)
88 Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
89 String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/sdncAdapterWorkflowRequest.xml")
90 XmlComparator.assertXMLEquals(expectedValue, captor.getValue())
96 void testBuildSDNCRequest() {
97 ExecutionEntity mockExecution = setupMock()
98 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
99 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
100 when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
101 when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
102 when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
103 when(mockExecution.getVariable(prefix + "source")).thenReturn("VID")
104 when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
105 when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
106 when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
107 when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
108 when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
109 when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
110 when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
112 when(mockExecution.getVariable(prefix + "rollbackSDNCRequestActivate")).thenReturn("true")
115 DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
116 String sdncRequest = obj.buildSDNCRequest(mockExecution, "svcInstId_test", "deactivate")
117 String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/deactivateSDNCRequest.xml")
118 XmlComparator.assertXMLEquals(expectedValue, sdncRequest)
124 void testPrepVNFAdapterRequest() {
125 ExecutionEntity mockExecution = setupMock()
126 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
127 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
128 when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
129 when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
130 when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
131 when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
132 when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
133 when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
134 when(mockExecution.getVariable(prefix + "mso-request-id")).thenReturn("12345")
135 when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:18080/mso/WorkflowMessage/')
138 DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
139 String sdncRequest = obj.prepVNFAdapterRequest(mockExecution)
141 Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
142 String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/vnfAdapterRestV1Request.xml")
143 XmlComparator.assertXMLEquals(expectedValue, captor.getValue(), "messageId", "notificationUrl")
147 void testDeleteNetworkPoliciesFromAAI() {
148 ExecutionEntity mockExecution = setupMock()
149 when(mockExecution.getVariable("prefix")).thenReturn(prefix)
150 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
151 when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
152 when(mockExecution.getVariable("mso.workflow.DoCreateVfModuleRollback.aai.network-policy.uri")).thenReturn("/aai/v8/network/network-policies/network-policy")
153 when(mockExecution.getVariable("mso.workflow.custom.DoCreateVfModuleRollback.aai.version")).thenReturn("8")
154 when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
155 when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
156 when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
157 List fqdnList = new ArrayList()
159 when(mockExecution.getVariable(prefix + "createdNetworkPolicyFqdnList")).thenReturn(fqdnList)
161 DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
162 obj.deleteNetworkPoliciesFromAAI(mockExecution)
164 Mockito.verify(mockExecution).setVariable("prefix", prefix)
165 Mockito.verify(mockExecution).setVariable(prefix + "networkPolicyFqdnCount", 1)
166 Mockito.verify(mockExecution).setVariable(prefix + "aaiQueryNetworkPolicyByFqdnReturnCode", 200)
170 private static ExecutionEntity setupMock() {
171 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
172 when(mockProcessDefinition.getKey()).thenReturn("DoCreateVfModuleRollback")
173 RepositoryService mockRepositoryService = mock(RepositoryService.class)
174 when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
175 when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoCreateVfModuleRollback")
176 when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
177 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
178 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
180 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
181 // Initialize prerequisite variables
182 when(mockExecution.getId()).thenReturn("100")
183 when(mockExecution.getProcessDefinitionId()).thenReturn("DoCreateVfModuleRollback")
184 when(mockExecution.getProcessInstanceId()).thenReturn("DoCreateVfModuleRollback")
185 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
186 when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
191 private static void mockData() {
192 stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
193 .willReturn(aResponse()
195 .withHeader("Content-Type", "text/xml")
196 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
197 stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
198 .willReturn(aResponse()