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.aai.tasks;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.util.Optional;
37 import org.camunda.bpm.engine.delegate.BpmnError;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.ArgumentMatchers;
42 import org.mockito.Captor;
43 import org.mockito.InjectMocks;
44 import org.onap.aai.domain.yang.NetworkPolicies;
45 import org.onap.aai.domain.yang.NetworkPolicy;
46 import org.onap.so.bpmn.BaseTaskTest;
47 import org.onap.so.bpmn.common.BuildingBlockExecution;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
49 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
50 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
51 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
52 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
53 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
54 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
55 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
56 import org.onap.so.client.aai.entities.AAIResultWrapper;
57 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
58 import org.onap.so.client.exception.BBObjectNotFoundException;
61 public class AAIDeleteTasksTest extends BaseTaskTest {
62 private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
65 private AAIDeleteTasks aaiDeleteTasks = new AAIDeleteTasks();
67 private L3Network network;
68 private ServiceInstance serviceInstance;
69 private GenericVnf genericVnf;
70 private VfModule vfModule;
71 private VolumeGroup volumeGroup;
72 private CloudRegion cloudRegion;
73 private Configuration configuration;
76 ArgumentCaptor<String> stringCaptor;
79 public void before() throws BBObjectNotFoundException {
80 serviceInstance = setServiceInstance();
81 genericVnf = setGenericVnf();
82 vfModule = setVfModule();
83 network = setL3Network();
84 volumeGroup = setVolumeGroup();
85 cloudRegion = setCloudRegion();
86 configuration = setConfiguration();
88 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID), any())).thenReturn(genericVnf);
89 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID), any())).thenReturn(vfModule);
90 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.NETWORK_ID), any())).thenReturn(network);
91 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.VOLUME_GROUP_ID), any())).thenReturn(volumeGroup);
92 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID), any())).thenReturn(serviceInstance);
93 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.CONFIGURATION_ID), any())).thenReturn(configuration);
96 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
100 public void deleteVfModuleTest() throws Exception {
102 doNothing().when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
104 aaiDeleteTasks.deleteVfModule(execution);
105 verify(aaiVfModuleResources, times(1)).deleteVfModule(vfModule, genericVnf);
109 public void deleteVfModuleExceptionTest() throws Exception {
110 expectedException.expect(BpmnError.class);
111 doThrow(RuntimeException.class).when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
112 aaiDeleteTasks.deleteVfModule(execution);
116 public void deleteServiceInstanceTest() throws Exception {
117 doNothing().when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
119 aaiDeleteTasks.deleteServiceInstance(execution);
121 verify(aaiServiceInstanceResources, times(1)).deleteServiceInstance(serviceInstance);
125 public void deleteServiceInstanceExceptionTest() throws Exception {
126 expectedException.expect(BpmnError.class);
128 doThrow(RuntimeException.class).when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
130 aaiDeleteTasks.deleteServiceInstance(execution);
134 public void deleteVnfTest() throws Exception {
135 doNothing().when(aaiVnfResources).deleteVnf(genericVnf);
136 aaiDeleteTasks.deleteVnf(execution);
137 verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
141 public void deleteVnfTestException() throws Exception {
142 expectedException.expect(BpmnError.class);
143 doThrow(RuntimeException.class).when(aaiVnfResources).deleteVnf(genericVnf);
145 aaiDeleteTasks.deleteVnf(execution);
146 verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
150 public void deleteNetworkTest() throws Exception {
151 doNothing().when(aaiNetworkResources).deleteNetwork(network);
152 aaiDeleteTasks.deleteNetwork(execution);
153 verify(aaiNetworkResources, times(1)).deleteNetwork(network);
157 public void deleteCollectionTest() throws Exception {
158 doNothing().when(aaiNetworkResources).deleteCollection(serviceInstance.getCollection());
159 aaiDeleteTasks.deleteCollection(execution);
160 verify(aaiNetworkResources, times(1)).deleteCollection(serviceInstance.getCollection());
164 public void deleteInstanceGroupTest() throws Exception {
165 doNothing().when(aaiNetworkResources).deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
166 aaiDeleteTasks.deleteInstanceGroup(execution);
167 verify(aaiNetworkResources, times(1)).deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
171 public void deleteVolumeGroupTest() {
172 doNothing().when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
174 aaiDeleteTasks.deleteVolumeGroup(execution);
176 verify(aaiVolumeGroupResources, times(1)).deleteVolumeGroup(volumeGroup, cloudRegion);
180 public void deleteVolumeGroupExceptionTest() {
181 expectedException.expect(BpmnError.class);
183 doThrow(RuntimeException.class).when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
185 aaiDeleteTasks.deleteVolumeGroup(execution);
189 public void deleteConfigurationTest() throws Exception {
190 gBBInput = execution.getGeneralBuildingBlock();
191 doNothing().when(aaiConfigurationResources).deleteConfiguration(configuration);
192 aaiDeleteTasks.deleteConfiguration(execution);
193 verify(aaiConfigurationResources, times(1)).deleteConfiguration(configuration);
197 public void deleteNetworkPolicyNeedToDeleteAllTest() throws Exception {
198 execution.setVariable("contrailNetworkPolicyFqdnList", "ABC123,DEF456");
199 final String content0 = new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "queryAaiNetworkPoliciesForDelete0.json")));
200 AAIResultWrapper aaiResultWrapper0 = new AAIResultWrapper(content0);
201 NetworkPolicies networkPolicies0 = aaiResultWrapper0.asBean(NetworkPolicies.class).get();
202 final String content1 = new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "queryAaiNetworkPoliciesForDelete1.json")));
203 AAIResultWrapper aaiResultWrapper1 = new AAIResultWrapper(content1);
204 NetworkPolicies networkPolicies1 = aaiResultWrapper1.asBean(NetworkPolicies.class).get();
206 doReturn(Optional.of(networkPolicies0),Optional.of(networkPolicies1)).when(aaiNetworkResources).getNetworkPolicies(any(AAIResourceUri.class));
207 doNothing().when(aaiNetworkResources).deleteNetworkPolicy(any(String.class));
208 aaiDeleteTasks.deleteNetworkPolicies(execution);
209 verify(aaiNetworkResources, times(2)).deleteNetworkPolicy(stringCaptor.capture());
210 assertEquals("testNetworkPolicyId0", stringCaptor.getAllValues().get(0));
211 assertEquals("testNetworkPolicyId1", stringCaptor.getAllValues().get(1));
215 public void deleteNetworkPolicyNeedToDeleteNoneTest() throws Exception {
216 execution.setVariable("contrailNetworkPolicyFqdnList", "ABC123");
217 Optional<NetworkPolicies> networkPolicies = Optional.empty();
218 doReturn(networkPolicies).when(aaiNetworkResources).getNetworkPolicies(any(AAIResourceUri.class));
219 aaiDeleteTasks.deleteNetworkPolicies(execution);
220 verify(aaiNetworkResources, times(0)).deleteNetworkPolicy(any(String.class));
224 public void deleteNetworkPolicyNoNetworkPoliciesTest() throws Exception {
225 aaiDeleteTasks.deleteNetworkPolicies(execution);
226 verify(aaiNetworkResources, times(0)).deleteNetworkPolicy(any(String.class));