2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.infrastructure.scripts
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
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.aaiclient.client.aai.AAIObjectType
53 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
54 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
55 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
56 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
57 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
58 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException
59 import org.springframework.core.env.Environment
60 import org.springframework.mock.env.MockEnvironment
62 import javax.ws.rs.NotFoundException
64 import static com.github.tomakehurst.wiremock.client.WireMock.*
65 import static org.mockito.Mockito.*
67 @RunWith(MockitoJUnitRunner.Silent.class)
68 class DoDeleteVfModuleVolumeV2Test extends MsoGroovyTest{
71 public WireMockRule wireMockRule = new WireMockRule(8090)
74 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
77 DoDeleteVfModuleVolumeV2 deleteVfModuleVolumeV2
80 Environment mockEnvironment
82 private String Prefix = "DDVMV_"
83 private RepositoryService mockRepositoryService
88 UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader()
89 urnPropertiesReader.setEnvironment(null)
93 public void init() throws IOException {
94 super.init("DoDeleteVfModuleVolumeV2")
95 MockitoAnnotations.initMocks(this);
96 when(deleteVfModuleVolumeV2.getAAIClient()).thenReturn(client)
97 when(mockEnvironment.getProperty("mso.workflow.global.default.aai.version")).thenReturn("14")
98 when(mockEnvironment.getProperty("mso.workflow.global.default.aai.namespace")).thenReturn("defaultTestNamespace")
99 when(mockEnvironment.getProperty("aai.endpoint")).thenReturn("http://localhost:8090")
100 UrnPropertiesReader urnPropertiesReader = new UrnPropertiesReader()
101 urnPropertiesReader.setEnvironment(mockEnvironment)
105 public void testCallRESTQueryAAICloudRegion() {
106 ExecutionEntity mockExecution = setupMock()
107 when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
108 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
109 when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
110 mockSuccessfulCloudData()
112 DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
113 obj.callRESTQueryAAICloudRegion(mockExecution, "true")
114 Mockito.verify(mockExecution).setVariable("DDVMV_queryCloudRegionReturnCode", "200")
115 Mockito.verify(mockExecution).setVariable("DDVMV_aicCloudRegion", "RDM2WAGPLCP")
118 @Test(expected = GroovyRuntimeException.class)
119 public void testCallRESTQueryAAICloudRegionAAiEndpointNull() {
120 ExecutionEntity mockExecution = setupMock()
121 when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
122 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
123 when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
124 when(mockEnvironment.getProperty("aai.endpoint")).thenReturn(null)
126 mockSuccessfulCloudData()
128 DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
129 obj.callRESTQueryAAICloudRegion(mockExecution, "true")
131 } catch (Exception ex) {
132 println " Test End - Handle catch-throw Exception! "
133 Mockito.verify(mockExecution).getVariable(eq("lcpCloudRegionId"))
134 Assert.assertEquals(GroovyRuntimeException.class, ex.class)
140 public void testCallRESTQueryAAICloudRegionNotFound() {
141 ExecutionEntity mockExecution = setupMock()
142 when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
143 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
144 when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
145 when(mockExecution.getVariable(Prefix + "queryCloudRegionReturnCode")).thenReturn("404")
147 wireMockRule.stubFor(
148 get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/12345"))
149 .willReturn(aResponse()
152 DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
153 obj.callRESTQueryAAICloudRegion(mockExecution, "true")
155 Mockito.verify(mockExecution).getVariable(eq("lcpCloudRegionId"))
156 Mockito.verify(mockExecution).setVariable(eq(Prefix + "queryCloudRegionReturnCode"), eq("404"))
157 Mockito.verify(mockExecution).setVariable(eq(Prefix + "aicCloudRegion"), eq("AAIAIC25"))
161 public void testPrepareVnfAdapterDeleteRequest() {
162 ExecutionEntity mockExecution = setupMock()
163 when(mockExecution.getVariable("prefix")).thenReturn(Prefix)
164 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
165 when(mockExecution.getVariable("aicCloudRegion")).thenReturn("RegionOne")
166 when(mockExecution.getVariable("tenantId")).thenReturn("12345")
167 when(mockExecution.getVariable("volumeGroupId")).thenReturn("12345")
168 when(mockExecution.getVariable("volumeGroupHeatStackId")).thenReturn("12345")
169 when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
170 when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
171 when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
172 when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("http://localhost:18080/mso/WorkflowMessage")
174 DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
175 obj.prepareVnfAdapterDeleteRequest(mockExecution, "true")
177 Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
178 String str = FileUtil.readResourceFile("__files/DoCreateVfModuleVolumeV2/vnfAdapterDeleteRequest.xml")
179 XmlComparator.assertXMLEquals(str, captor.getValue(),"messageId","notificationUrl")
183 void testCallRESTQueryAAIForVolumeGroup(){
184 when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
185 when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
186 when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
187 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "Region1").volumeGroup("VolumeGroup123"))
188 Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/AAI/VolumeGroupWithTenant.json");
189 when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
190 when(client.get(resourceUri)).thenReturn(
191 new AAIResultWrapper(FileUtil.readResourceFile("__files/AAI/VolumeGroupWithTenant.json")))
192 deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution,"true")
193 Mockito.verify(mockExecution).setVariable("DDVMV_queryAAIVolGrpResponse", volumeGroup.get())
194 Mockito.verify(mockExecution).setVariable("DDVMV_volumeGroupHeatStackId", volumeGroup.get().getHeatStackId())
198 void testCallRESTQueryAAIForVolumeGroupNoTenant(){
199 when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
200 when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
201 when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
202 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "Region1").volumeGroup("VolumeGroup123"))
203 Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
204 when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
206 deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
207 }catch(BpmnError error) {
208 println " Test End - Handle catch-throw BpmnError()! "
213 void testCallRESTQueryAAIForVolumeGroupDifferentTenant(){
214 when(mockExecution.getVariable("tenantId")).thenReturn("Tenant12345")
215 when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
216 when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
217 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "Region1").volumeGroup("VolumeGroup123"))
218 Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/AAI/VolumeGroupWithTenant.json");
219 when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
221 deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
222 }catch(BpmnError error) {
223 println " Test End - Handle catch-throw BpmnError()! "
228 void testCallRESTQueryAAIForVolumeGroupNotFound(){
229 when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
230 when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
231 when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
232 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "Region1").volumeGroup("VolumeGroup123"))
233 when(client.get(VolumeGroup.class,resourceUri)).thenReturn(Optional.empty())
235 deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
236 }catch(BpmnError error) {
237 println " Test End - Handle catch-throw BpmnError()! "
242 void testCallRESTQueryAAIForVolumeGroupWithVfModule(){
243 when(mockExecution.getVariable("tenantId")).thenReturn("Tenant123")
244 when(mockExecution.getVariable("volumeGroupId")).thenReturn("VolumeGroup123")
245 when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
246 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "Region1").volumeGroup("VolumeGroup123"))
247 Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithVfModule.json");
248 when(client.get(VolumeGroup.class,resourceUri)).thenReturn(volumeGroup)
250 deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
251 }catch(BpmnError error) {
252 println " Test End - Handle catch-throw BpmnError()! "
257 void testCallRESTDeleteAAIVolumeGroup(){
258 Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
259 when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
260 when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
261 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "Region1").volumeGroup("VolumeGroup123"))
262 doNothing().when(client).delete(resourceUri)
263 deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution,"true")
267 void testCallRESTDeleteAAIVolumeGroupAaiError(){
268 Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroupWithVfModule.json");
269 when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
270 when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
271 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "Region1").volumeGroup(volumeGroup.get()).getVolumeGroupId())
272 doThrow(new GraphInventoryUriComputationException("Error")).when(client).delete(resourceUri)
274 deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
275 } catch (BpmnError error) {
276 println " Test End - Handle catch-throw BpmnError()! "
281 void testCallRESTDeleteAAIVolumeGroupNotFound(){
282 Optional<VolumeGroup> volumeGroup = getAAIObjectFromJson(VolumeGroup.class,"__files/aai/VolumeGroup.json");
283 when(mockExecution.getVariable("DDVMV_queryAAIVolGrpResponse")).thenReturn(volumeGroup.get())
284 when(mockExecution.getVariable("DDVMV_aicCloudRegion")).thenReturn("Region1")
285 AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(CLOUD_OWNER, "Region1").volumeGroup("VolumeGroup123"))
286 doThrow(new NotFoundException("VolumeGroup Not found")).when(client).delete(resourceUri)
288 deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
289 } catch (BpmnError error) {
290 println " Test End - Handle catch-throw BpmnError()! "
297 private ExecutionEntity setupMock() {
299 mockRepositoryService = mock(RepositoryService.class)
300 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
301 when(mockProcessEngineServices.getRepositoryService()).thenReturn(this.mockRepositoryService)
303 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
304 // Initialize prerequisite variables
305 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
311 private void mockSuccessfulCloudData() {
312 wireMockRule.stubFor(get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/12345"))
313 .willReturn(aResponse()
314 .withStatus(200).withHeader("Content-Type", "text/xml")
315 .withBodyFile("DoCreateVfModuleVolumeV2/cloudRegion_AAIResponse_Success.xml")))