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