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 org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
24 import org.junit.Assert
25 import org.junit.Before
27 import org.junit.runner.RunWith
28 import org.mockito.ArgumentCaptor
29 import org.mockito.Captor
30 import org.mockito.Mockito
31 import org.mockito.MockitoAnnotations
32 import org.mockito.Spy
33 import org.mockito.runners.MockitoJUnitRunner
34 import org.onap.aai.domain.yang.VolumeGroup
35 import org.onap.so.bpmn.common.scripts.DeleteAAIVfModule
36 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
37 import org.onap.so.bpmn.core.WorkflowException
38 import org.onap.aaiclient.client.aai.AAIObjectType
39 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
40 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
41 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
42 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
43 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
44 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException
46 import javax.ws.rs.NotFoundException
48 import static org.mockito.ArgumentMatchers.eq
49 import static org.mockito.Mockito.doNothing
50 import static org.mockito.Mockito.doThrow
51 import static org.mockito.Mockito.times
52 import static org.mockito.Mockito.verify
53 import static org.mockito.Mockito.when
55 class DeleteVfModuleVolumeInfraV1Test extends MsoGroovyTest {
58 DeleteVfModuleVolumeInfraV1 deleteVfModuleVolumeInfraV1 ;
61 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
64 void init() throws IOException {
65 super.init("DeleteVfModuleVolumeInfraV1")
66 MockitoAnnotations.initMocks(this);
67 when(deleteVfModuleVolumeInfraV1.getAAIClient()).thenReturn(client)
70 String deleteVnfAdapterRequestXml = """<deleteVolumeGroupRequest>
71 <cloudSiteId>RDM2WAGPLCP</cloudSiteId>
72 <tenantId>fba1bd1e195a404cacb9ce17a9b2b421</tenantId>
73 <volumeGroupId>78987</volumeGroupId>
75 <skipAAI>true</skipAAI>
77 <requestId>TEST-REQUEST-ID-0123</requestId>
78 <serviceInstanceId>1234</serviceInstanceId>
80 <messageId>ebb9ef7b-a6a5-40e6-953e-f868f1767677</messageId>
81 <notificationUrl>http://localhost:28080/mso/WorkflowMessage/VNFAResponse/ebb9ef7b-a6a5-40e6-953e-f868f1767677</notificationUrl>
82 </deleteVolumeGroupRequest>"""
84 String dbRequestXml = """<soapenv:Envelope xmlns:req="http://org.onap.so/requestsdb"
85 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
88 <req:updateInfraRequest>
89 <requestId>TEST-REQUEST-ID-0123</requestId>
90 <lastModifiedBy>BPMN</lastModifiedBy>
91 <statusMessage>VolumeGroup successfully deleted</statusMessage>
92 <requestStatus>COMPLETE</requestStatus>
93 <progress>100</progress>
95 </req:updateInfraRequest>
97 </soapenv:Envelope>"""
99 String completionRequestXml = """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
100 xmlns:ns="http://org.onap/so/request/types/v1"
101 xmlns="http://org.onap/so/infra/vnf-request/v1">
103 <request-id>TEST-REQUEST-ID-0123</request-id>
104 <action>DELETE</action>
107 <aetgt:status-message>Volume Group has been deleted successfully.</aetgt:status-message>
108 <aetgt:mso-bpel-name>BPMN VF Module Volume action: DELETE</aetgt:mso-bpel-name>
109 </aetgt:MsoCompletionRequest>"""
111 String falloutHandlerRequestXml = """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
112 xmlns:ns="http://org.onap/so/request/types/v1"
113 xmlns="http://org.onap/so/infra/vnf-request/v1">
115 <request-id>TEST-REQUEST-ID-0123</request-id>
116 <action>DELETE</action>
119 <aetgt:WorkflowException>
120 <aetgt:ErrorMessage>Unexpected Error</aetgt:ErrorMessage>
121 <aetgt:ErrorCode>5000</aetgt:ErrorCode>
122 </aetgt:WorkflowException>
123 </aetgt:FalloutHandlerRequest>"""
127 public void testPrepareVnfAdapterDeleteRequest() {
129 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
130 when(mockExecution.getVariable("DELVfModVol_cloudRegion")).thenReturn('RDM2WAGPLCP')
131 when(mockExecution.getVariable("DELVfModVol_tenantId")).thenReturn('fba1bd1e195a404cacb9ce17a9b2b421')
132 when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn('78987')
133 when(mockExecution.getVariable("DELVfModVol_volumeGroupHeatStackId")).thenReturn('')
134 when(mockExecution.getVariable("DELVfModVol_requestId")).thenReturn('TEST-REQUEST-ID-0123')
135 when(mockExecution.getVariable("DELVfModVol_serviceId")).thenReturn('1234')
136 when(mockExecution.getVariable("DELVfModVol_messageId")).thenReturn('ebb9ef7b-a6a5-40e6-953e-f868f1767677')
137 when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:28080/mso/WorkflowMessage')
138 when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn('')
140 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
141 myproc.prepareVnfAdapterDeleteRequest(mockExecution, 'true')
143 verify(mockExecution).setVariable("DELVfModVol_deleteVnfARequest", deleteVnfAdapterRequestXml)
149 public void testPrepareDbRequest() {
151 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
152 when(mockExecution.getVariable("DELVfModVol_requestId")).thenReturn('TEST-REQUEST-ID-0123')
153 when(mockExecution.getVariable("DELVfModVol_volumeOutputs")).thenReturn('')
154 when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC")
155 when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
157 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
158 myproc.prepareDBRequest(mockExecution, 'true')
160 verify(mockExecution).setVariable("DELVfModVol_updateInfraRequest", dbRequestXml)
164 public void testPrepareCompletionHandlerRequest() {
166 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
167 when(mockExecution.getVariable("mso-request-id")).thenReturn('TEST-REQUEST-ID-0123')
168 when(mockExecution.getVariable("DELVfModVol_source")).thenReturn('VID')
170 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
171 myproc.prepareCompletionHandlerRequest(mockExecution, 'true')
173 verify(mockExecution).setVariable("DELVfModVol_CompleteMsoProcessRequest", completionRequestXml)
177 public void testPrepareFalloutHandler() {
179 WorkflowException workflowException = new WorkflowException('DeleteVfModuleVolumeInfraV1', 5000, 'Unexpected Error')
181 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
183 when(mockExecution.getVariable("DELVfModVol_requestId")).thenReturn('TEST-REQUEST-ID-0123')
184 when(mockExecution.getVariable("WorkflowException")).thenReturn(workflowException)
185 when(mockExecution.getVariable("DELVfModVol_source")).thenReturn('VID')
187 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
188 myproc.prepareFalloutHandler(mockExecution, 'true')
190 verify(mockExecution).setVariable("DELVfModVol_Success", false)
191 verify(mockExecution).setVariable("DELVfModVol_FalloutHandlerRequest", falloutHandlerRequestXml)
195 void testQueryAAIForVolumeGroup(){
196 when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn("volumeGroupId1")
197 when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
198 AAIResultWrapper wrapper = mockVolumeGroupWrapper("region1", "volumeGroupId1", "__files/AAI/VolumeGroupWithTenant.json")
199 Optional<VolumeGroup> volumeGroupOp = wrapper.asBean(VolumeGroup.class)
200 deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
201 verify(mockExecution).setVariable("DELVfModVol_volumeGroupTenantId", "Tenant123")
205 void testQueryAAIForVolumeGroupWithVfModule(){
206 when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn("volumeGroupId1")
207 when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
208 AAIResultWrapper wrapper = mockVolumeGroupWrapper("region1", "volumeGroupId1", "__files/AAI/VolumeGroupWithVfModule.json")
210 deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
211 } catch (Exception ex) {
212 println " Test End - Handle catch-throw BpmnError()! "
214 Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
215 WorkflowException workflowException = captor.getValue()
216 Assert.assertEquals(2500, workflowException.getErrorCode())
217 Assert.assertEquals("Volume Group volumeGroupId1 currently in use - found vf-module relationship.", workflowException.getErrorMessage())
221 void testQueryAAIForVolumeGroupNoTenant(){
222 when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn("volumeGroupId1")
223 when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
224 AAIResultWrapper wrapper = mockVolumeGroupWrapper("region1", "volumeGroupId1", "__files/AAI/VolumeGroup.json")
226 deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
227 } catch (Exception ex) {
228 println " Test End - Handle catch-throw BpmnError()! "
230 Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
231 WorkflowException workflowException = captor.getValue()
232 Assert.assertEquals(2500, workflowException.getErrorCode())
233 Assert.assertEquals( "Could not find Tenant Id element in Volume Group with Volume Group Id volumeGroupId1", workflowException.getErrorMessage())
237 void testQueryAAIForVolumeGroupNoId(){
238 when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
240 deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
241 } catch (Exception ex) {
242 println " Test End - Handle catch-throw BpmnError()! "
244 Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
245 WorkflowException workflowException = captor.getValue()
246 Assert.assertEquals(2500, workflowException.getErrorCode())
247 Assert.assertEquals("volume-group-id is not provided in the request", workflowException.getErrorMessage())
251 void testDeleteVolGrpId(){
252 VolumeGroup volumeGroup = new VolumeGroup()
253 volumeGroup.setVolumeGroupId("volumeGroupId1")
254 when(mockExecution.getVariable("DELVfModVol_queryAAIVolGrpResponse")).thenReturn(volumeGroup)
255 when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
256 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "region1").volumeGroup("volumeGroupId1"))
257 doNothing().when(client).delete(resourceUri)
258 deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
259 verify(client).delete(resourceUri)
263 void testDeleteVolGrpIdNotFound(){
264 VolumeGroup volumeGroup = new VolumeGroup()
265 volumeGroup.setVolumeGroupId("volumeGroupId1")
266 when(mockExecution.getVariable("DELVfModVol_queryAAIVolGrpResponse")).thenReturn(volumeGroup)
267 when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
268 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "region1").volumeGroup("volumeGroupId1"))
269 doThrow(new NotFoundException("Not Found")).when(client).delete(resourceUri)
271 deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
272 } catch (Exception ex) {
273 println " Test End - Handle catch-throw BpmnError()! "
275 Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
276 WorkflowException workflowException = captor.getValue()
277 Assert.assertEquals(2500, workflowException.getErrorCode())
278 Assert.assertEquals("Volume group volumeGroupId1 not found for delete in AAI Response code: 404", workflowException.getErrorMessage())
282 void testDeleteVolGrpIdError(){
283 VolumeGroup volumeGroup = new VolumeGroup()
284 volumeGroup.setVolumeGroupId("volumeGroupId1")
285 when(mockExecution.getVariable("DELVfModVol_queryAAIVolGrpResponse")).thenReturn(volumeGroup)
286 when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
287 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "region1").volumeGroup("volumeGroupId1"))
288 doThrow(new GraphInventoryUriComputationException("Error")).when(client).delete(resourceUri)
290 deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
291 } catch (Exception ex) {
292 println " Test End - Handle catch-throw BpmnError()! "
294 Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
295 WorkflowException workflowException = captor.getValue()
296 Assert.assertEquals(5000, workflowException.getErrorCode())
297 Assert.assertEquals("Received error from A&AI ()", workflowException.getErrorMessage())