59a2b0eda3905ee95417bcea81a015bd1d8c32ac
[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 + "cloudRegion")).thenReturn("CloudOwner")
186         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
187         when(mockExecution.getVariable(prefix + "volumeGroupStackId")).thenReturn("12345")
188         when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
189         when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
190         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
191         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
192         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
193         when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
194         when(mockExecution.getVariable(prefix + "vfModuleIndex")).thenReturn("index")
195         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
196         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
197         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("MIS/1604/0026/SW_INTERNET")
198         when(mockExecution.getVariable(prefix + "backoutOnFailure")).thenReturn("12345")
199         when(mockExecution.getVariable(prefix + "volumeGroupId")).thenReturn("12345")
200         when(mockExecution.getVariable(prefix + "asdcServiceModelVersion")).thenReturn("1.0")
201         when(mockExecution.getVariable(prefix + "modelCustomizationUuid")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ced3")
202         when(mockExecution.getVariable(prefix + "baseVfModuleId")).thenReturn("12345")
203         when(mockExecution.getVariable(prefix + "baseVfModuleHeatStackId")).thenReturn("12345")
204
205         def node = new Node(null, 'vfModule')
206         new Node(node, 'heat-stack-id', "abc")
207         VfModule vfModule = new VfModule(node, true);
208         when(mockExecution.getVariable(prefix + "vfModule")).thenReturn(vfModule)
209
210         String sdncGetResponse = FileUtil.readResourceFile("__files/DoUpdateVfModule/sdncGetResponse.xml");
211         when(mockExecution.getVariable(prefix + "sdncTopologyResponse")).thenReturn(sdncGetResponse)
212         Map<String, String> map = new HashMap<String, String>();
213         map.put("vrr_image_name", "MDT17");
214         map.put("availability_zone_0", "nova");
215         map.put("vrr_flavor_name", "ns.c16r32d128.v1");
216         when(mockExecution.getVariable(prefix + "vnfParamsMap")).thenReturn(map)
217         when(mockExecution.getVariable("mso-request-id")).thenReturn("testRequestId-1503410089303")
218         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
219         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("http://localhost:28080/mso/WorkflowMesssage")
220
221         mockData()
222         DoUpdateVfModule obj = new DoUpdateVfModule()
223         obj.prepVnfAdapterRest(mockExecution)
224
225         String createVnfARequest = FileUtil.readResourceFile("__files/DoUpdateVfModule/vnfAdapterRestRequest.xml")
226         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
227         XmlComparator.assertXMLEquals(createVnfARequest, captor.getValue(), "messageId", "notificationUrl")
228     }
229
230
231
232     @Test
233     void testPrepSDNCTopologyAct() {
234         ExecutionEntity mockExecution = setupMock()
235         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
236         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
237         when(mockExecution.getVariable(prefix + "aicCloudRegion")).thenReturn("RDM2WAGPLCP")
238         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
239         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
240         when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
241         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
242         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
243         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
244         when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
245         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
246         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
247         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("MIS/1604/0026/SW_INTERNET")
248         when(mockExecution.getVariable(prefix + "usePreload")).thenReturn("Y")
249         when(mockExecution.getVariable(prefix + "modelCustomizationUuid")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ced3")
250
251         def node = new Node(null, 'vfModule')
252         new Node(node, 'vf-module-name', "abc")
253         VfModule vfModule = new VfModule(node, true);
254         when(mockExecution.getVariable(prefix + "vfModule")).thenReturn(vfModule)
255
256         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapter")
257
258         mockData()
259         DoUpdateVfModule obj = new DoUpdateVfModule()
260         obj.prepSDNCTopologyAct(mockExecution)
261
262         String createVnfARequest = FileUtil.readResourceFile("__files/DoUpdateVfModule/sdncActivateRequest.xml")
263         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
264         XmlComparator.assertXMLEquals(createVnfARequest, captor.getValue())
265     }
266
267     @Test
268     void testQueryAAIVfModule() {
269         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
270         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("12345")
271
272         mockAAIGenericVnf("12345","__files/AAI/GenericVnfVfModule.json")
273         doUpdateVfModule.queryAAIVfModule(mockExecution)
274         Mockito.verify(mockExecution).setVariable(prefix + "queryAAIVfModuleResponseCode", 200)
275         Mockito.verify(mockExecution).setVariable("DOUPVfMod_baseVfModuleId", "lukewarm")
276         Mockito.verify(mockExecution).setVariable("DOUPVfMod_baseVfModuleHeatStackId", "fastburn")
277
278     }
279
280
281     private static ExecutionEntity setupMock() {
282         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
283         when(mockProcessDefinition.getKey()).thenReturn("DoUpdateVfModule")
284         RepositoryService mockRepositoryService = mock(RepositoryService.class)
285         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
286         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoUpdateVfModule")
287         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
288         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
289         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
290
291         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
292         // Initialize prerequisite variables
293         when(mockExecution.getId()).thenReturn("100")
294         when(mockExecution.getProcessDefinitionId()).thenReturn("DoUpdateVfModule")
295         when(mockExecution.getProcessInstanceId()).thenReturn("DoUpdateVfModule")
296         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
297         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
298
299         return mockExecution
300     }
301
302     private static void mockData() {
303         stubFor(get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner"))
304                 .willReturn(aResponse()
305                 .withStatus(200).withHeader("Content-Type", "text/xml")
306                 .withBodyFile("DoUpdateVfModule/cloudRegion_AAIResponse_Success.xml")))
307
308         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345[?]depth=1"))
309                 .willReturn(aResponse()
310                 .withStatus(200).withHeader("Content-Type", "text/xml")
311                 .withBodyFile("DoUpdateVfModule/getGenericVnfResponse.xml")))
312     }
313 }