0e7114dd039e2b14552d80078f8b6823471d8d72
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DeleteVfModuleVolumeInfraV1Test.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 org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
24 import org.junit.Assert
25 import org.junit.Before
26 import org.junit.Test
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.so.client.aai.AAIObjectType
39 import org.onap.so.client.aai.entities.AAIResultWrapper
40 import org.onap.so.client.aai.entities.uri.AAIResourceUri
41 import org.onap.so.client.aai.entities.uri.AAIUriFactory
42 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException
43
44 import javax.ws.rs.NotFoundException
45
46 import static org.mockito.Matchers.eq
47 import static org.mockito.Mockito.doNothing
48 import static org.mockito.Mockito.doThrow
49 import static org.mockito.Mockito.times
50 import static org.mockito.Mockito.verify
51 import static org.mockito.Mockito.when
52
53 class DeleteVfModuleVolumeInfraV1Test extends MsoGroovyTest {
54
55         @Spy
56         DeleteVfModuleVolumeInfraV1 deleteVfModuleVolumeInfraV1 ;
57
58         @Captor
59         static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
60
61         @Before
62         void init() throws IOException {
63                 super.init("DeleteVfModuleVolumeInfraV1")
64                 MockitoAnnotations.initMocks(this);
65                 when(deleteVfModuleVolumeInfraV1.getAAIClient()).thenReturn(client)
66         }
67
68         String deleteVnfAdapterRequestXml = """<deleteVolumeGroupRequest>
69    <cloudSiteId>RDM2WAGPLCP</cloudSiteId>
70    <tenantId>fba1bd1e195a404cacb9ce17a9b2b421</tenantId>
71    <volumeGroupId>78987</volumeGroupId>
72    <volumeGroupStackId/>
73    <skipAAI>true</skipAAI>
74    <msoRequest>
75       <requestId>TEST-REQUEST-ID-0123</requestId>
76       <serviceInstanceId>1234</serviceInstanceId>
77    </msoRequest>
78    <messageId>ebb9ef7b-a6a5-40e6-953e-f868f1767677</messageId>
79    <notificationUrl>http://localhost:28080/mso/WorkflowMessage/VNFAResponse/ebb9ef7b-a6a5-40e6-953e-f868f1767677</notificationUrl>
80 </deleteVolumeGroupRequest>"""
81         
82         String dbRequestXml = """<soapenv:Envelope xmlns:req="http://org.onap.so/requestsdb"
83                   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
84    <soapenv:Header/>
85    <soapenv:Body>
86       <req:updateInfraRequest>
87          <requestId>TEST-REQUEST-ID-0123</requestId>
88          <lastModifiedBy>BPMN</lastModifiedBy>
89          <statusMessage>VolumeGroup successfully deleted</statusMessage>
90          <requestStatus>COMPLETE</requestStatus>
91          <progress>100</progress>
92          <vnfOutputs/>
93       </req:updateInfraRequest>
94    </soapenv:Body>
95 </soapenv:Envelope>"""
96         
97         String completionRequestXml = """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
98                             xmlns:ns="http://org.onap/so/request/types/v1"
99                             xmlns="http://org.onap/so/infra/vnf-request/v1">
100    <request-info>
101       <request-id>TEST-REQUEST-ID-0123</request-id>
102       <action>DELETE</action>
103       <source>VID</source>
104    </request-info>
105    <aetgt:status-message>Volume Group has been deleted successfully.</aetgt:status-message>
106    <aetgt:mso-bpel-name>BPMN VF Module Volume action: DELETE</aetgt:mso-bpel-name>
107 </aetgt:MsoCompletionRequest>"""
108         
109         String falloutHandlerRequestXml = """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
110                              xmlns:ns="http://org.onap/so/request/types/v1"
111                              xmlns="http://org.onap/so/infra/vnf-request/v1">
112    <request-info>
113       <request-id>TEST-REQUEST-ID-0123</request-id>
114       <action>DELETE</action>
115       <source>VID</source>
116    </request-info>
117    <aetgt:WorkflowException>
118       <aetgt:ErrorMessage>Unexpected Error</aetgt:ErrorMessage>
119       <aetgt:ErrorCode>5000</aetgt:ErrorCode>
120    </aetgt:WorkflowException>
121 </aetgt:FalloutHandlerRequest>"""
122         
123
124         @Test
125         public void testPrepareVnfAdapterDeleteRequest() {
126                 
127                 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
128                 when(mockExecution.getVariable("DELVfModVol_cloudRegion")).thenReturn('RDM2WAGPLCP')
129                 when(mockExecution.getVariable("DELVfModVol_tenantId")).thenReturn('fba1bd1e195a404cacb9ce17a9b2b421')
130                 when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn('78987')
131                 when(mockExecution.getVariable("DELVfModVol_volumeGroupHeatStackId")).thenReturn('')
132                 when(mockExecution.getVariable("DELVfModVol_requestId")).thenReturn('TEST-REQUEST-ID-0123')
133                 when(mockExecution.getVariable("DELVfModVol_serviceId")).thenReturn('1234')
134                 when(mockExecution.getVariable("DELVfModVol_messageId")).thenReturn('ebb9ef7b-a6a5-40e6-953e-f868f1767677')
135                 when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:28080/mso/WorkflowMessage')
136                 when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn('')
137
138                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
139                 myproc.prepareVnfAdapterDeleteRequest(mockExecution, 'true')
140                 
141                 verify(mockExecution).setVariable("DELVfModVol_deleteVnfARequest", deleteVnfAdapterRequestXml)
142
143         }
144         
145         @Test
146         //@Ignore
147         public void testPrepareDbRequest() {
148                 
149                 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
150                 when(mockExecution.getVariable("DELVfModVol_requestId")).thenReturn('TEST-REQUEST-ID-0123')
151                 when(mockExecution.getVariable("DELVfModVol_volumeOutputs")).thenReturn('')
152                 when(mockExecution.getVariable("mso.adapters.db.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC")
153                 when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
154                 
155                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
156                 myproc.prepareDBRequest(mockExecution, 'true')
157                 
158                 verify(mockExecution).setVariable("DELVfModVol_updateInfraRequest", dbRequestXml)
159         }
160
161         @Test
162         public void testPrepareCompletionHandlerRequest() {
163                 
164                 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
165                 when(mockExecution.getVariable("mso-request-id")).thenReturn('TEST-REQUEST-ID-0123')
166                 when(mockExecution.getVariable("DELVfModVol_source")).thenReturn('VID')
167                 
168                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
169                 myproc.prepareCompletionHandlerRequest(mockExecution, 'true')
170                 
171                 verify(mockExecution).setVariable("DELVfModVol_CompleteMsoProcessRequest", completionRequestXml)
172         }
173         
174         @Test
175         public void testPrepareFalloutHandler() {
176                 
177                 WorkflowException workflowException = new WorkflowException('DeleteVfModuleVolumeInfraV1', 5000, 'Unexpected Error')
178                 
179                 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
180                 
181                 when(mockExecution.getVariable("DELVfModVol_requestId")).thenReturn('TEST-REQUEST-ID-0123')
182                 when(mockExecution.getVariable("WorkflowException")).thenReturn(workflowException)
183                 when(mockExecution.getVariable("DELVfModVol_source")).thenReturn('VID')
184                 
185                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
186                 myproc.prepareFalloutHandler(mockExecution, 'true')
187                 
188                 verify(mockExecution).setVariable("DELVfModVol_Success", false)
189                 verify(mockExecution).setVariable("DELVfModVol_FalloutHandlerRequest", falloutHandlerRequestXml)
190         }
191
192         @Test
193     void testQueryAAIForVolumeGroup(){
194         when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn("volumeGroupId1")
195         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
196         AAIResultWrapper wrapper = mockVolumeGroupWrapper("region1", "volumeGroupId1", "__files/AAI/VolumeGroupWithTenant.json")
197         Optional<VolumeGroup> volumeGroupOp = wrapper.asBean(VolumeGroup.class)
198         deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
199         verify(mockExecution).setVariable("DELVfModVol_volumeGroupTenantId", "Tenant123")
200     }
201
202     @Test
203     void testQueryAAIForVolumeGroupWithVfModule(){
204         when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn("volumeGroupId1")
205         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
206         AAIResultWrapper wrapper = mockVolumeGroupWrapper("region1", "volumeGroupId1", "__files/AAI/VolumeGroupWithVfModule.json")
207         try {
208             deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
209         } catch (Exception ex) {
210             println " Test End - Handle catch-throw BpmnError()! "
211         }
212         Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
213         WorkflowException workflowException = captor.getValue()
214         Assert.assertEquals(2500, workflowException.getErrorCode())
215         Assert.assertEquals("Volume Group volumeGroupId1 currently in use - found vf-module relationship.", workflowException.getErrorMessage())
216     }
217
218     @Test
219     void testQueryAAIForVolumeGroupNoTenant(){
220         when(mockExecution.getVariable("DELVfModVol_volumeGroupId")).thenReturn("volumeGroupId1")
221         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
222         AAIResultWrapper wrapper = mockVolumeGroupWrapper("region1", "volumeGroupId1", "__files/AAI/VolumeGroup.json")
223         try {
224             deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
225         } catch (Exception ex) {
226             println " Test End - Handle catch-throw BpmnError()! "
227         }
228         Mockito.verify(mockExecution, times(3)).setVariable(captor.capture(), captor.capture())
229         WorkflowException workflowException = captor.getValue()
230         Assert.assertEquals(2500, workflowException.getErrorCode())
231         Assert.assertEquals( "Could not find Tenant Id element in Volume Group with Volume Group Id volumeGroupId1", workflowException.getErrorMessage())
232     }
233
234     @Test
235     void testQueryAAIForVolumeGroupNoId(){
236         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
237         try {
238             deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
239         } catch (Exception ex) {
240             println " Test End - Handle catch-throw BpmnError()! "
241         }
242         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
243         WorkflowException workflowException = captor.getValue()
244         Assert.assertEquals(2500, workflowException.getErrorCode())
245         Assert.assertEquals("volume-group-id is not provided in the request", workflowException.getErrorMessage())
246     }
247
248     @Test
249     void testDeleteVolGrpId(){
250         VolumeGroup volumeGroup = new VolumeGroup()
251         volumeGroup.setVolumeGroupId("volumeGroupId1")
252         when(mockExecution.getVariable("DELVfModVol_queryAAIVolGrpResponse")).thenReturn(volumeGroup)
253         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
254         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "region1","volumeGroupId1")
255         doNothing().when(client).delete(resourceUri)
256         deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
257         verify(client).delete(resourceUri)
258     }
259
260     @Test
261     void testDeleteVolGrpIdNotFound(){
262         VolumeGroup volumeGroup = new VolumeGroup()
263         volumeGroup.setVolumeGroupId("volumeGroupId1")
264         when(mockExecution.getVariable("DELVfModVol_queryAAIVolGrpResponse")).thenReturn(volumeGroup)
265         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
266         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "region1","volumeGroupId1")
267         doThrow(new NotFoundException("Not Found")).when(client).delete(resourceUri)
268         try {
269             deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
270         } catch (Exception ex) {
271             println " Test End - Handle catch-throw BpmnError()! "
272         }
273         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
274         WorkflowException workflowException = captor.getValue()
275         Assert.assertEquals(2500, workflowException.getErrorCode())
276         Assert.assertEquals("Volume group volumeGroupId1 not found for delete in AAI Response code: 404", workflowException.getErrorMessage())
277     }
278
279     @Test
280     void testDeleteVolGrpIdError(){
281         VolumeGroup volumeGroup = new VolumeGroup()
282         volumeGroup.setVolumeGroupId("volumeGroupId1")
283         when(mockExecution.getVariable("DELVfModVol_queryAAIVolGrpResponse")).thenReturn(volumeGroup)
284         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
285         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "region1","volumeGroupId1")
286         doThrow(new GraphInventoryUriComputationException("Error")).when(client).delete(resourceUri)
287         try {
288             deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
289         } catch (Exception ex) {
290             println " Test End - Handle catch-throw BpmnError()! "
291         }
292         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
293         WorkflowException workflowException = captor.getValue()
294         Assert.assertEquals(5000, workflowException.getErrorCode())
295         Assert.assertEquals("Received error from A&AI ()", workflowException.getErrorMessage())
296     }
297 }