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