2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
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
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
52 import javax.ws.rs.NotFoundException
54 import static com.github.tomakehurst.wiremock.client.WireMock.*
55 import static org.mockito.Mockito.*
57 @RunWith(MockitoJUnitRunner.class)
58 class DoDeleteVfModuleVolumeV2Test extends MsoGroovyTest{
60 public WireMockRule wireMockRule = new WireMockRule(28090);
63 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
66 DoDeleteVfModuleVolumeV2 deleteVfModuleVolumeV2;
69 public void init() throws IOException {
70 super.init("DoDeleteVfModuleVolumeV2")
71 MockitoAnnotations.initMocks(this);
72 when(deleteVfModuleVolumeV2.getAAIClient()).thenReturn(client)
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/")
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")
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/")
105 DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
106 obj.callRESTQueryAAICloudRegion(mockExecution, "true")
108 } catch (Exception ex) {
109 println " Test End - Handle catch-throw BpmnError()! "
111 Mockito.verify(mockExecution,atLeastOnce()).setVariable(captor.capture(),captor.capture())
112 WorkflowException workflowException = captor.getValue()
113 Assert.assertEquals(9999, workflowException.getErrorCode())
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")
130 DoDeleteVfModuleVolumeV2 obj = new DoDeleteVfModuleVolumeV2()
131 obj.prepareVnfAdapterDeleteRequest(mockExecution, "true")
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")
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())
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)
160 deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
161 }catch(BpmnError error) {
162 println " Test End - Handle catch-throw BpmnError()! "
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)
175 deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
176 }catch(BpmnError error) {
177 println " Test End - Handle catch-throw BpmnError()! "
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())
189 deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
190 }catch(BpmnError error) {
191 println " Test End - Handle catch-throw BpmnError()! "
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)
204 deleteVfModuleVolumeV2.callRESTQueryAAIForVolumeGroup(mockExecution, "true")
205 }catch(BpmnError error) {
206 println " Test End - Handle catch-throw BpmnError()! "
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")
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)
228 deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
229 } catch (BpmnError error) {
230 println " Test End - Handle catch-throw BpmnError()! "
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)
242 deleteVfModuleVolumeV2.callRESTDeleteAAIVolumeGroup(mockExecution, "true")
243 } catch (BpmnError error) {
244 println " Test End - Handle catch-throw BpmnError()! "
251 private ExecutionEntity setupMock() {
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)
262 ExecutionEntity mockExecution = mock(ExecutionEntity.class)
263 // Initialize prerequisite variables
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)
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")))