Springboot 2.0 upgrade
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeleteVfModuleVolumeV2Test.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.delegate.BpmnError
27 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
28 import org.camunda.bpm.engine.repository.ProcessDefinition
29 import org.junit.Assert
30 import org.junit.Before
31 import org.junit.Ignore
32 import org.junit.Rule
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 import org.mockito.ArgumentCaptor
36 import org.mockito.Captor
37 import org.mockito.Mockito
38 import org.mockito.MockitoAnnotations
39 import org.mockito.Spy
40 import org.mockito.runners.MockitoJUnitRunner
41 import org.onap.aai.domain.yang.VolumeGroup
42 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
43 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
44 import org.onap.so.bpmn.core.WorkflowException
45 import org.onap.so.bpmn.mock.FileUtil
46 import org.onap.so.client.aai.AAIObjectType
47 import org.onap.so.client.aai.entities.AAIResultWrapper
48 import org.onap.so.client.aai.entities.uri.AAIResourceUri
49 import org.onap.so.client.aai.entities.uri.AAIUriFactory
50 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException
51
52 import javax.ws.rs.NotFoundException
53
54 import static com.github.tomakehurst.wiremock.client.WireMock.*
55 import static org.mockito.Mockito.*
56
57 @RunWith(MockitoJUnitRunner.class)
58 class DoDeleteVfModuleVolumeV2Test extends MsoGroovyTest{
59     @Rule
60     public WireMockRule wireMockRule = new WireMockRule(28090);
61
62     @Captor
63     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
64
65     @Spy
66     DoDeleteVfModuleVolumeV2 deleteVfModuleVolumeV2;
67
68     @Before
69     public void init() throws IOException {
70         super.init("DoDeleteVfModuleVolumeV2")
71         MockitoAnnotations.initMocks(this);
72         when(deleteVfModuleVolumeV2.getAAIClient()).thenReturn(client)
73
74     }
75
76     @Test
77     public void testCallRESTQueryAAICloudRegion() {
78         ExecutionEntity mockExecution = setupMock()
79         when(mockExecution.getVariable("prefix")).thenReturn("DDVMV_")
80         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
81         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
82         when(mockExecution.getVariable("mso.workflow.DoDeleteVfModuleVolumeV2.aai.cloud-region.uri")).thenReturn("/aai/v8/cloud-infrastructure/cloud-regions/cloud-region")
83         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
84         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
85
86         mockData()
87         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
88         obj.callRESTQueryAAICloudRegion(mockExecution, "true")
89         Mockito.verify(mockExecution).setVariable("DDVMV_queryCloudRegionReturnCode", "200")
90         Mockito.verify(mockExecution).setVariable("DDVMV_aicCloudRegion", "RDM2WAGPLCP")
91     }
92
93     @Test
94     public void testCallRESTQueryAAICloudRegionAAiEndpointNull() {
95         ExecutionEntity mockExecution = setupMock()
96         when(mockExecution.getVariable("prefix")).thenReturn("DDVMV_")
97         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
98         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
99         when(mockExecution.getVariable("mso.workflow.DoDeleteVfModuleVolumeV2.aai.cloud-region.uri")).thenReturn("/aai/v8/cloud-infrastructure/cloud-regions/cloud-region")
100         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
101         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
102
103         mockData()
104         try {
105             DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
106             obj.callRESTQueryAAICloudRegion(mockExecution, "true")
107
108         } catch (Exception ex) {
109             println " Test End - Handle catch-throw BpmnError()! "
110         }
111         Mockito.verify(mockExecution,atLeastOnce()).setVariable(captor.capture(),captor.capture())
112         WorkflowException workflowException = captor.getValue()
113         Assert.assertEquals(9999, workflowException.getErrorCode())
114     }
115
116     @Test
117     public void testPrepareVnfAdapterDeleteRequest() {
118         ExecutionEntity mockExecution = setupMock()
119         when(mockExecution.getVariable("prefix")).thenReturn("DDVMV_")
120         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
121         when(mockExecution.getVariable("aicCloudRegion")).thenReturn("RegionOne")
122         when(mockExecution.getVariable("tenantId")).thenReturn("12345")
123         when(mockExecution.getVariable("volumeGroupId")).thenReturn("12345")
124         when(mockExecution.getVariable("volumeGroupHeatStackId")).thenReturn("12345")
125         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
126         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
127         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
128         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("http://localhost:18080/mso/WorkflowMessage")
129
130         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
131         obj.prepareVnfAdapterDeleteRequest(mockExecution, "true")
132
133         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
134         String str = FileUtil.readResourceFile("__files/DoCreateVfModuleVolumeV2/vnfAdapterDeleteRequest.xml")
135         XmlComparator.assertXMLEquals(str, captor.getValue(),"messageId","notificationUrl")
136     }
137
138     @Test
139     void testCallRESTQueryAAIForVolumeGroup(){
140         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
141         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
142         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
143         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
144         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithTenant.json");
145         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
146         deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution,"true")
147         Mockito.verify(mockExecution).setVariable("DDVMV_queryAAIVolGrpResponse", volumeGroup.get())
148         Mockito.verify(mockExecution).setVariable("DDVMV_volumeGroupHeatStackId", volumeGroup.get().getHeatStackId())
149     }
150
151     @Test
152     void testCallRESTQueryAAIForVolumeGroupNoTenant(){
153         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
154         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
155         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
156         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
157         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
158         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
159         try {
160             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
161         }catch(BpmnError error) {
162             println " Test End - Handle catch-throw BpmnError()! "
163         }
164     }
165
166     @Test
167     void testCallRESTQueryAAIForVolumeGroupDifferentTenant(){
168         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant12345")
169         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
170         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
171         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
172         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithTenant.json");
173         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
174         try {
175             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
176         }catch(BpmnError error) {
177             println " Test End - Handle catch-throw BpmnError()! "
178         }
179     }
180
181     @Test
182     void testCallRESTQueryAAIForVolumeGroupNotFound(){
183         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
184         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
185         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
186         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
187         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(Optional.empty())
188         try {
189             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
190         }catch(BpmnError error) {
191             println " Test End - Handle catch-throw BpmnError()! "
192         }
193     }
194
195     @Test
196     void testCallRESTQueryAAIForVolumeGroupWithVfModule(){
197         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
198         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
199         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
200         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
201         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithVfModule.json");
202         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
203         try {
204             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
205         }catch(BpmnError error) {
206             println " Test End - Handle catch-throw BpmnError()! "
207         }
208     }
209
210     @Test
211     void testCallRESTDeleteAAIVolumeGroup(){
212         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
213         when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
214         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
215         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
216         doNothing().when(client).delete(resourceUri)
217         deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution,"true")
218     }
219
220     @Test
221     void testCallRESTDeleteAAIVolumeGroupAaiError(){
222         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithVfModule.json");
223         when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
224         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
225         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1",volumeGroup.get().getVolumeGroupId())
226         doThrow(new GraphInventoryUriComputationException("Error")).when(client).delete(resourceUri)
227         try {
228             deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
229         } catch (BpmnError error) {
230                 println " Test End - Handle catch-throw BpmnError()! "
231         }
232     }
233
234     @Test
235     void testCallRESTDeleteAAIVolumeGroupNotFound(){
236         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
237         when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
238         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
239         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
240         doThrow(new NotFoundException("VolumeGroup Not found")).when(client).delete(resourceUri)
241         try {
242             deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
243         } catch (BpmnError error) {
244             println " Test End - Handle catch-throw BpmnError()! "
245         }
246     }
247
248
249
250
251     private ExecutionEntity setupMock() {
252
253         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
254         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVfModuleVolumeV2")
255         RepositoryService mockRepositoryService = mock(RepositoryService.class)
256         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
257         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVfModuleVolumeV2")
258         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
259         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
260         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
261
262         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
263         // Initialize prerequisite variables
264
265         when(mockExecution.getId()).thenReturn("100")
266         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVfModuleVolumeV2")
267         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVfModuleVolumeV2")
268         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
269         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
270
271         return mockExecution
272
273     }
274
275     private void mockData() {
276         stubFor(get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/12345"))
277                 .willReturn(aResponse()
278                 .withStatus(200).withHeader("Content-Type", "text/xml")
279                 .withBodyFile("DoCreateVfModuleVolumeV2/cloudRegion_AAIResponse_Success.xml")))
280     }
281 }