2885d6919e64ae97675786223d6eae48e944ff06
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.scripts
24
25 import com.github.tomakehurst.wiremock.junit.WireMockRule
26 import org.camunda.bpm.engine.ProcessEngineServices
27 import org.camunda.bpm.engine.RepositoryService
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
30 import org.camunda.bpm.engine.repository.ProcessDefinition
31 import org.junit.After
32 import org.junit.AfterClass
33 import org.junit.Assert
34 import org.junit.Before
35 import org.junit.BeforeClass
36 import org.junit.Rule
37 import org.junit.Test
38 import org.junit.runner.RunWith
39 import org.mockito.ArgumentCaptor
40 import org.mockito.Captor
41 import org.mockito.Mock
42 import org.mockito.Mockito
43 import org.mockito.MockitoAnnotations
44 import org.mockito.Spy
45 import org.mockito.junit.MockitoJUnitRunner
46 import org.onap.aai.domain.yang.VolumeGroup
47 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
48 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
49 import org.onap.so.bpmn.core.UrnPropertiesReader
50 import org.onap.so.bpmn.core.WorkflowException
51 import org.onap.so.bpmn.mock.FileUtil
52 import org.onap.so.client.aai.AAIObjectType
53 import org.onap.so.client.aai.entities.AAIResultWrapper
54 import org.onap.so.client.aai.entities.uri.AAIResourceUri
55 import org.onap.so.client.aai.entities.uri.AAIUriFactory
56 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException
57 import org.springframework.core.env.Environment
58 import org.springframework.mock.env.MockEnvironment
59
60 import javax.ws.rs.NotFoundException
61
62 import static com.github.tomakehurst.wiremock.client.WireMock.*
63 import static org.mockito.Mockito.*
64
65 @RunWith(MockitoJUnitRunner.Silent.class)
66 class DoDeleteVfModuleVolumeV2Test extends MsoGroovyTest{
67
68     @Rule
69     public WireMockRule wireMockRule = new WireMockRule(8090)
70
71     @Captor
72     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
73
74     @Spy
75     DoDeleteVfModuleVolumeV2 deleteVfModuleVolumeV2
76
77     @Mock
78     Environment mockEnvironment
79
80     private String Prefix = "DDVMV_"
81     private RepositoryService mockRepositoryService
82
83
84     @After
85     void cleanupEnv() {
86         UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader()
87         urnPropertiesReader.setEnvironment(null)
88     }
89
90     @Before
91     public void init() throws IOException {
92         super.init("DoDeleteVfModuleVolumeV2")
93         MockitoAnnotations.initMocks(this);
94         when(deleteVfModuleVolumeV2.getAAIClient()).thenReturn(client)
95         when(mockEnvironment.getProperty("mso.workflow.global.default.aai.version")).thenReturn("14")
96         when(mockEnvironment.getProperty("mso.workflow.global.default.aai.namespace")).thenReturn("defaultTestNamespace")
97         when(mockEnvironment.getProperty("aai.endpoint")).thenReturn("http://localhost:8090")
98         UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader()
99         urnPropertiesReader.setEnvironment(mockEnvironment)
100     }
101
102     @Test
103     public void testCallRESTQueryAAICloudRegion() {
104         ExecutionEntity mockExecution = setupMock()
105         when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
106         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
107         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
108         mockSuccessfulCloudData()
109
110         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
111         obj.callRESTQueryAAICloudRegion(mockExecution, "true")
112         Mockito.verify(mockExecution).setVariable("DDVMV_queryCloudRegionReturnCode", "200")
113         Mockito.verify(mockExecution).setVariable("DDVMV_aicCloudRegion", "RDM2WAGPLCP")
114     }
115
116     @Test(expected = GroovyRuntimeException.class)
117     public void testCallRESTQueryAAICloudRegionAAiEndpointNull() {
118         ExecutionEntity mockExecution = setupMock()
119         when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
120         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
121         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
122         when(mockEnvironment.getProperty("aai.endpoint")).thenReturn(null)
123
124         mockSuccessfulCloudData()
125         try {
126             DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
127             obj.callRESTQueryAAICloudRegion(mockExecution, "true")
128
129         } catch (Exception ex) {
130             println " Test End - Handle catch-throw Exception! "
131             Mockito.verify(mockExecution).getVariable(eq("lcpCloudRegionId"))
132             Assert.assertEquals(GroovyRuntimeException.class, ex.class)
133             throw ex
134         }
135     }
136
137     @Test
138     public void testCallRESTQueryAAICloudRegionNotFound() {
139         ExecutionEntity mockExecution = setupMock()
140         when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
141         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
142         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
143         when(mockExecution.getVariable(Prefix + "queryCloudRegionReturnCode")).thenReturn("404")
144
145         wireMockRule.stubFor(
146                 get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/12345"))
147                 .willReturn(aResponse()
148                 .withStatus(404)))
149
150         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
151         obj.callRESTQueryAAICloudRegion(mockExecution, "true")
152
153         Mockito.verify(mockExecution).getVariable(eq("lcpCloudRegionId"))
154         Mockito.verify(mockExecution).setVariable(eq(Prefix + "queryCloudRegionReturnCode"), eq("404"))
155         Mockito.verify(mockExecution).setVariable(eq(Prefix + "aicCloudRegion"), eq("AAIAIC25"))
156     }
157
158     @Test
159     public void testPrepareVnfAdapterDeleteRequest() {
160         ExecutionEntity mockExecution = setupMock()
161         when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
162         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
163         when(mockExecution.getVariable("aicCloudRegion")).thenReturn("RegionOne")
164         when(mockExecution.getVariable("tenantId")).thenReturn("12345")
165         when(mockExecution.getVariable("volumeGroupId")).thenReturn("12345")
166         when(mockExecution.getVariable("volumeGroupHeatStackId")).thenReturn("12345")
167         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
168         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
169         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
170         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("http://localhost:18080/mso/WorkflowMessage")
171
172         DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
173         obj.prepareVnfAdapterDeleteRequest(mockExecution, "true")
174
175         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
176         String str = FileUtil.readResourceFile("__files/DoCreateVfModuleVolumeV2/vnfAdapterDeleteRequest.xml")
177         XmlComparator.assertXMLEquals(str, captor.getValue(),"messageId","notificationUrl")
178     }
179
180     @Test
181     void testCallRESTQueryAAIForVolumeGroup(){
182         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
183         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
184         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
185         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
186         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/AAI/VolumeGroupWithTenant.json");
187         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
188         when(client.get(resourceUri)).thenReturn(
189                 new AAIResultWrapper(FileUtil.readResourceFile("__files/AAI/VolumeGroupWithTenant.json")))
190         deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution,"true")
191         Mockito.verify(mockExecution).setVariable("DDVMV_queryAAIVolGrpResponse", volumeGroup.get())
192         Mockito.verify(mockExecution).setVariable("DDVMV_volumeGroupHeatStackId", volumeGroup.get().getHeatStackId())
193     }
194
195     @Test
196     void testCallRESTQueryAAIForVolumeGroupNoTenant(){
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/VolumeGroup.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 testCallRESTQueryAAIForVolumeGroupDifferentTenant(){
212         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant12345")
213         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
214         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
215         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
216         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/AAI/VolumeGroupWithTenant.json");
217         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
218         try {
219             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
220         }catch(BpmnError error) {
221             println " Test End - Handle catch-throw BpmnError()! "
222         }
223     }
224
225     @Test
226     void testCallRESTQueryAAIForVolumeGroupNotFound(){
227         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
228         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
229         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
230         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
231         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(Optional.empty())
232         try {
233             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
234         }catch(BpmnError error) {
235             println " Test End - Handle catch-throw BpmnError()! "
236         }
237     }
238
239     @Test
240     void testCallRESTQueryAAIForVolumeGroupWithVfModule(){
241         when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
242         when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
243         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
244         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
245         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithVfModule.json");
246         when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
247         try {
248             deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
249         }catch(BpmnError error) {
250             println " Test End - Handle catch-throw BpmnError()! "
251         }
252     }
253
254     @Test
255     void testCallRESTDeleteAAIVolumeGroup(){
256         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
257         when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
258         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
259         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
260         doNothing().when(client).delete(resourceUri)
261         deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution,"true")
262     }
263
264     @Test
265     void testCallRESTDeleteAAIVolumeGroupAaiError(){
266         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithVfModule.json");
267         when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
268         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
269         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1",volumeGroup.get().getVolumeGroupId())
270         doThrow(new GraphInventoryUriComputationException("Error")).when(client).delete(resourceUri)
271         try {
272             deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
273         } catch (BpmnError error) {
274                 println " Test End - Handle catch-throw BpmnError()! "
275         }
276     }
277
278     @Test
279     void testCallRESTDeleteAAIVolumeGroupNotFound(){
280         Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
281         when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
282         when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
283         AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP,CLOUD_OWNER, "Region1","VolumeGroup123")
284         doThrow(new NotFoundException("VolumeGroup Not found")).when(client).delete(resourceUri)
285         try {
286             deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
287         } catch (BpmnError error) {
288             println " Test End - Handle catch-throw BpmnError()! "
289         }
290     }
291
292
293
294
295     private ExecutionEntity setupMock() {
296
297         mockRepositoryService = mock(RepositoryService.class)
298         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
299         when(mockProcessEngineServices.getRepositoryService()).thenReturn(this.mockRepositoryService)
300
301         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
302         // Initialize prerequisite variables
303         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
304
305         return mockExecution
306
307     }
308
309     private void mockSuccessfulCloudData() {
310         wireMockRule.stubFor(get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/12345"))
311                 .willReturn(aResponse()
312                 .withStatus(200).withHeader("Content-Type", "text/xml")
313                 .withBodyFile("DoCreateVfModuleVolumeV2/cloudRegion_AAIResponse_Success.xml")))
314     }
315 }