0f0946dca2f08c143d3fd5d7c01b270b57928d74
[so.git] /
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.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
45
46 import javax.ws.rs.NotFoundException
47
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
54
55 class DeleteVfModuleVolumeInfraV1Test extends MsoGroovyTest {
56
57         @Spy
58         DeleteVfModuleVolumeInfraV1 deleteVfModuleVolumeInfraV1 ;
59
60         @Captor
61         static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
62
63         @Before
64         void init() throws IOException {
65                 super.init("DeleteVfModuleVolumeInfraV1")
66                 MockitoAnnotations.initMocks(this);
67                 when(deleteVfModuleVolumeInfraV1.getAAIClient()).thenReturn(client)
68         }
69
70         String deleteVnfAdapterRequestXml = """<deleteVolumeGroupRequest>
71    <cloudSiteId>RDM2WAGPLCP</cloudSiteId>
72    <tenantId>fba1bd1e195a404cacb9ce17a9b2b421</tenantId>
73    <volumeGroupId>78987</volumeGroupId>
74    <volumeGroupStackId/>
75    <skipAAI>true</skipAAI>
76    <msoRequest>
77       <requestId>TEST-REQUEST-ID-0123</requestId>
78       <serviceInstanceId>1234</serviceInstanceId>
79    </msoRequest>
80    <messageId>ebb9ef7b-a6a5-40e6-953e-f868f1767677</messageId>
81    <notificationUrl>http://localhost:28080/mso/WorkflowMessage/VNFAResponse/ebb9ef7b-a6a5-40e6-953e-f868f1767677</notificationUrl>
82 </deleteVolumeGroupRequest>"""
83         
84         String dbRequestXml = """<soapenv:Envelope xmlns:req="http://org.onap.so/requestsdb"
85                   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
86    <soapenv:Header/>
87    <soapenv:Body>
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>
94          <vnfOutputs/>
95       </req:updateInfraRequest>
96    </soapenv:Body>
97 </soapenv:Envelope>"""
98         
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">
102    <request-info>
103       <request-id>TEST-REQUEST-ID-0123</request-id>
104       <action>DELETE</action>
105       <source>VID</source>
106    </request-info>
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>"""
110         
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">
114    <request-info>
115       <request-id>TEST-REQUEST-ID-0123</request-id>
116       <action>DELETE</action>
117       <source>VID</source>
118    </request-info>
119    <aetgt:WorkflowException>
120       <aetgt:ErrorMessage>Unexpected Error</aetgt:ErrorMessage>
121       <aetgt:ErrorCode>5000</aetgt:ErrorCode>
122    </aetgt:WorkflowException>
123 </aetgt:FalloutHandlerRequest>"""
124         
125
126         @Test
127         public void testPrepareVnfAdapterDeleteRequest() {
128                 
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('')
139
140                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
141                 myproc.prepareVnfAdapterDeleteRequest(mockExecution, 'true')
142                 
143                 verify(mockExecution).setVariable("DELVfModVol_deleteVnfARequest", deleteVnfAdapterRequestXml)
144
145         }
146         
147         @Test
148         //@Ignore
149         public void testPrepareDbRequest() {
150                 
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")
156                 
157                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
158                 myproc.prepareDBRequest(mockExecution, 'true')
159                 
160                 verify(mockExecution).setVariable("DELVfModVol_updateInfraRequest", dbRequestXml)
161         }
162
163         @Test
164         public void testPrepareCompletionHandlerRequest() {
165                 
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')
169                 
170                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
171                 myproc.prepareCompletionHandlerRequest(mockExecution, 'true')
172                 
173                 verify(mockExecution).setVariable("DELVfModVol_CompleteMsoProcessRequest", completionRequestXml)
174         }
175         
176         @Test
177         public void testPrepareFalloutHandler() {
178                 
179                 WorkflowException workflowException = new WorkflowException('DeleteVfModuleVolumeInfraV1', 5000, 'Unexpected Error')
180                 
181                 ExecutionEntity mockExecution = setupMock('DeleteVfModuleVolumeInfraV1')
182                 
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')
186                 
187                 DeleteVfModuleVolumeInfraV1 myproc = new DeleteVfModuleVolumeInfraV1()
188                 myproc.prepareFalloutHandler(mockExecution, 'true')
189                 
190                 verify(mockExecution).setVariable("DELVfModVol_Success", false)
191                 verify(mockExecution).setVariable("DELVfModVol_FalloutHandlerRequest", falloutHandlerRequestXml)
192         }
193
194         @Test
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")
202     }
203
204     @Test
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")
209         try {
210             deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
211         } catch (Exception ex) {
212             println " Test End - Handle catch-throw BpmnError()! "
213         }
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())
218     }
219
220     @Test
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")
225         try {
226             deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
227         } catch (Exception ex) {
228             println " Test End - Handle catch-throw BpmnError()! "
229         }
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())
234     }
235
236     @Test
237     void testQueryAAIForVolumeGroupNoId(){
238         when(mockExecution.getVariable("DELVfModVol_aicCloudRegion")).thenReturn("region1")
239         try {
240             deleteVfModuleVolumeInfraV1.queryAAIForVolumeGroup(mockExecution, true)
241         } catch (Exception ex) {
242             println " Test End - Handle catch-throw BpmnError()! "
243         }
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())
248     }
249
250     @Test
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)
260     }
261
262     @Test
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)
270         try {
271             deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
272         } catch (Exception ex) {
273             println " Test End - Handle catch-throw BpmnError()! "
274         }
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())
279     }
280
281     @Test
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)
289         try {
290             deleteVfModuleVolumeInfraV1.deleteVolGrpId(mockExecution, true)
291         } catch (Exception ex) {
292             println " Test End - Handle catch-throw BpmnError()! "
293         }
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())
298     }
299 }