replace all fixed wiremock ports
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoUpdateVfModuleTest.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.Ignore
31 import org.junit.Rule
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 import org.mockito.ArgumentCaptor
35 import org.mockito.Captor
36 import org.mockito.Mockito
37 import org.mockito.MockitoAnnotations
38 import org.mockito.Spy
39 import org.mockito.runners.MockitoJUnitRunner
40 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
41 import org.onap.so.bpmn.common.scripts.VfModule
42 import org.onap.so.bpmn.core.WorkflowException
43 import org.onap.so.bpmn.mock.FileUtil
44
45 import static com.github.tomakehurst.wiremock.client.WireMock.*
46 import static org.mockito.Mockito.*
47 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
48
49 @RunWith(MockitoJUnitRunner.class)
50 class DoUpdateVfModuleTest extends MsoGroovyTest{
51
52     def prefix = "DOUPVfMod_"
53     String doUpdateVfModuleRequest = FileUtil.readResourceFile("__files/VfModularity/DoUpdateVfModuleRequest.xml");
54     String sdncChangeAssignRequest = FileUtil.readResourceFile("__files/DoUpdateVfModule/sdncChangeAssignRequest.xml")
55     String sdncTopologyRequest = FileUtil.readResourceFile("__files/DoUpdateVfModule/sdncTopologyRequest.xml")
56
57     @Captor
58     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
59
60     @Spy
61     DoUpdateVfModule doUpdateVfModule
62
63     @Before
64     public void init() {
65         super.init("DoUpdateVfModule")
66         MockitoAnnotations.initMocks(this)
67         when(doUpdateVfModule.getAAIClient()).thenReturn(client)
68     }
69
70     @Test
71     void testPreProcessRequest() {
72         ExecutionEntity mockExecution = setupMock()
73         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
74         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
75         when(mockExecution.getVariable("DoUpdateVfModuleRequest")).thenReturn(doUpdateVfModuleRequest)
76         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
77
78         DoUpdateVfModule obj = new DoUpdateVfModule()
79         obj.preProcessRequest(mockExecution)
80
81         Mockito.verify(mockExecution, atLeastOnce()).getVariable("mso.workflow.sdncadapter.callback")
82     }
83
84
85
86     @Test
87     void testPrepConfirmVolumeGroupTenant() {
88         ExecutionEntity mockExecution = setupMock()
89         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
90         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
91         when(mockExecution.getVariable(prefix + "aicCloudRegion")).thenReturn("CloudOwner")
92         when(mockExecution.getVariable("mso.workflow.default.aai.v8.cloud-region.uri")).thenReturn("/aai/v8/cloud-infrastructure/cloud-regions/cloud-region")
93         when(mockExecution.getVariable("mso.workflow.default.aai.cloud-region.version")).thenReturn("8")
94         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
95         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
96
97         mockData()
98         DoUpdateVfModule obj = new DoUpdateVfModule()
99         obj.prepConfirmVolumeGroupTenant(mockExecution)
100
101         Mockito.verify(mockExecution).setVariable(prefix + "queryCloudRegionRequest", "http://localhost:28090/aai/v8/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner")
102         Mockito.verify(mockExecution).setVariable(prefix + "queryCloudRegionReturnCode", "200")
103         Mockito.verify(mockExecution).setVariable(prefix + "cloudRegionForVolume", "AAIAIC25")
104         Mockito.verify(mockExecution).setVariable(prefix + "isCloudRegionGood", true)
105     }
106
107
108     @Test
109     void testPrepSDNCTopologyChg() {
110         ExecutionEntity mockExecution = setupMock()
111         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
112         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("12345")
113         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
114
115
116         when(mockExecution.getVariable(prefix + "cloudSiteId")).thenReturn("RDM2WAGPLCP")
117         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
118         when(mockExecution.getVariable("volumeGroupStackId")).thenReturn("12345")
119         when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
120         when(mockExecution.getVariable(prefix + "aicCloudRegion")).thenReturn("MDTWNJ21")
121         when(mockExecution.getVariable(prefix + "usePreload")).thenReturn("Y")
122         when(mockExecution.getVariable(prefix + "vnfNameFromAAI")).thenReturn("skask-test")
123
124         def node = new Node(null, 'vfModule')
125         new Node(node, 'vf-module-name', "abc")
126         VfModule vfModule = new VfModule(node, true);
127         when(mockExecution.getVariable(prefix + "vfModule")).thenReturn(vfModule)
128
129         when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
130         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
131         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
132         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
133         when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
134         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
135         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
136         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("MIS/1604/0026/SW_INTERNET")
137         Map<String, String> map = new HashMap<String, String>();
138         map.put("vrr_image_name", "MDT17");
139         map.put("availability_zone_0", "nova");
140         map.put("vrr_flavor_name", "ns.c16r32d128.v1");
141         when(mockExecution.getVariable("vnfParamsMap")).thenReturn(map)
142         when(mockExecution.getVariable("mso-request-id")).thenReturn("testRequestId-1503410089303")
143         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapter")
144
145         mockData()
146         DoUpdateVfModule obj = new DoUpdateVfModule()
147         obj.prepSDNCTopologyChg(mockExecution)
148
149         Mockito.verify(mockExecution).setVariable(prefix + "vnfName", "skask-test")
150
151         Mockito.verify(mockExecution, times(2)).setVariable(captor.capture(), captor.capture())
152         XmlComparator.assertXMLEquals(sdncChangeAssignRequest, captor.getValue())
153     }
154
155
156
157     @Test
158     void testPrepSDNCTopologyQuery() {
159         ExecutionEntity mockExecution = setupMock()
160         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
161         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
162         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("12345")
163         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
164         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("MIS/1604/0026/SW_INTERNET")
165         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapter")
166
167         mockData()
168         DoUpdateVfModule obj = new DoUpdateVfModule()
169         obj.prepSDNCTopologyQuery(mockExecution)
170
171         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
172         XmlComparator.assertXMLEquals(sdncTopologyRequest, captor.getValue())
173     }
174
175
176
177     @Test
178     void testPrepVnfAdapterRest() {
179         ExecutionEntity mockExecution = setupMock()
180         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
181         when(mockExecution.getVariable(prefix + "aicCloudRegion")).thenReturn("RDM2WAGPLCP")
182         when(mockExecution.getVariable(prefix + "cloudRegion")).thenReturn("CloudOwner")
183         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
184         when(mockExecution.getVariable(prefix + "volumeGroupStackId")).thenReturn("12345")
185         when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
186         when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
187         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
188         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
189         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
190         when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
191         when(mockExecution.getVariable(prefix + "vfModuleIndex")).thenReturn("index")
192         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
193         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
194         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("MIS/1604/0026/SW_INTERNET")
195         when(mockExecution.getVariable(prefix + "backoutOnFailure")).thenReturn("12345")
196         when(mockExecution.getVariable(prefix + "volumeGroupId")).thenReturn("12345")
197         when(mockExecution.getVariable(prefix + "asdcServiceModelVersion")).thenReturn("1.0")
198         when(mockExecution.getVariable(prefix + "modelCustomizationUuid")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ced3")
199         when(mockExecution.getVariable(prefix + "baseVfModuleId")).thenReturn("12345")
200         when(mockExecution.getVariable(prefix + "baseVfModuleHeatStackId")).thenReturn("12345")
201
202         def node = new Node(null, 'vfModule')
203         new Node(node, 'heat-stack-id', "abc")
204         VfModule vfModule = new VfModule(node, true);
205         when(mockExecution.getVariable(prefix + "vfModule")).thenReturn(vfModule)
206
207         String sdncGetResponse = FileUtil.readResourceFile("__files/DoUpdateVfModule/sdncGetResponse.xml");
208         when(mockExecution.getVariable(prefix + "sdncTopologyResponse")).thenReturn(sdncGetResponse)
209         Map<String, String> map = new HashMap<String, String>();
210         map.put("vrr_image_name", "MDT17");
211         map.put("availability_zone_0", "nova");
212         map.put("vrr_flavor_name", "ns.c16r32d128.v1");
213         when(mockExecution.getVariable(prefix + "vnfParamsMap")).thenReturn(map)
214         when(mockExecution.getVariable("mso-request-id")).thenReturn("testRequestId-1503410089303")
215         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
216         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("http://localhost:28080/mso/WorkflowMesssage")
217
218         mockData()
219         DoUpdateVfModule obj = new DoUpdateVfModule()
220         obj.prepVnfAdapterRest(mockExecution)
221
222         String createVnfARequest = FileUtil.readResourceFile("__files/DoUpdateVfModule/vnfAdapterRestRequest.xml")
223         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
224         XmlComparator.assertXMLEquals(createVnfARequest, captor.getValue(), "messageId", "notificationUrl")
225     }
226
227
228
229     @Test
230     void testPrepSDNCTopologyAct() {
231         ExecutionEntity mockExecution = setupMock()
232         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
233         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
234         when(mockExecution.getVariable(prefix + "aicCloudRegion")).thenReturn("RDM2WAGPLCP")
235         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
236         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
237         when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
238         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
239         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
240         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
241         when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
242         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
243         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
244         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("MIS/1604/0026/SW_INTERNET")
245         when(mockExecution.getVariable(prefix + "usePreload")).thenReturn("Y")
246         when(mockExecution.getVariable(prefix + "modelCustomizationUuid")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ced3")
247
248         def node = new Node(null, 'vfModule')
249         new Node(node, 'vf-module-name', "abc")
250         VfModule vfModule = new VfModule(node, true);
251         when(mockExecution.getVariable(prefix + "vfModule")).thenReturn(vfModule)
252
253         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapter")
254
255         mockData()
256         DoUpdateVfModule obj = new DoUpdateVfModule()
257         obj.prepSDNCTopologyAct(mockExecution)
258
259         String createVnfARequest = FileUtil.readResourceFile("__files/DoUpdateVfModule/sdncActivateRequest.xml")
260         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
261         XmlComparator.assertXMLEquals(createVnfARequest, captor.getValue())
262     }
263
264     @Test
265     void testQueryAAIVfModule() {
266         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
267         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("12345")
268
269         mockAAIGenericVnf("12345","__files/AAI/GenericVnfVfModule.json")
270         doUpdateVfModule.queryAAIVfModule(mockExecution)
271         Mockito.verify(mockExecution).setVariable(prefix + "queryAAIVfModuleResponseCode", 200)
272         Mockito.verify(mockExecution).setVariable("DOUPVfMod_baseVfModuleId", "lukewarm")
273         Mockito.verify(mockExecution).setVariable("DOUPVfMod_baseVfModuleHeatStackId", "fastburn")
274
275     }
276
277
278     private static ExecutionEntity setupMock() {
279         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
280         when(mockProcessDefinition.getKey()).thenReturn("DoUpdateVfModule")
281         RepositoryService mockRepositoryService = mock(RepositoryService.class)
282         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
283         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoUpdateVfModule")
284         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
285         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
286         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
287
288         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
289         // Initialize prerequisite variables
290         when(mockExecution.getId()).thenReturn("100")
291         when(mockExecution.getProcessDefinitionId()).thenReturn("DoUpdateVfModule")
292         when(mockExecution.getProcessInstanceId()).thenReturn("DoUpdateVfModule")
293         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
294         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
295
296         return mockExecution
297     }
298
299     private static void mockData() {
300         stubFor(get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner"))
301                 .willReturn(aResponse()
302                 .withStatus(200).withHeader("Content-Type", "text/xml")
303                 .withBodyFile("DoUpdateVfModule/cloudRegion_AAIResponse_Success.xml")))
304
305         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345[?]depth=1"))
306                 .willReturn(aResponse()
307                 .withStatus(200).withHeader("Content-Type", "text/xml")
308                 .withBodyFile("DoUpdateVfModule/getGenericVnfResponse.xml")))
309     }
310 }