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;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.Optional;
35 import org.camunda.bpm.engine.delegate.BpmnError;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.ArgumentMatchers;
41 import org.mockito.Captor;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.aai.domain.yang.NetworkPolicies;
46 import org.onap.so.bpmn.common.BuildingBlockExecution;
47 import org.onap.so.bpmn.common.data.TestDataSetup;
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.InstanceGroup;
52 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
53 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
54 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
55 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
56 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
57 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
58 import org.onap.aaiclient.client.aai.entities.uri.AAIBaseResourceUri;
59 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
60 import org.onap.so.client.exception.BBObjectNotFoundException;
61 import org.onap.so.client.exception.ExceptionBuilder;
62 import org.onap.so.client.orchestration.AAIConfigurationResources;
63 import org.onap.so.client.orchestration.AAIInstanceGroupResources;
64 import org.onap.so.client.orchestration.AAINetworkResources;
65 import org.onap.so.client.orchestration.AAIServiceInstanceResources;
66 import org.onap.so.client.orchestration.AAIVfModuleResources;
67 import org.onap.so.client.orchestration.AAIVnfResources;
68 import org.onap.so.client.orchestration.AAIVolumeGroupResources;
70 @RunWith(MockitoJUnitRunner.Silent.class)
71 public class AAIDeleteTasksTest extends TestDataSetup {
72 private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
75 protected ExceptionBuilder exceptionUtil;
77 protected AAIVfModuleResources aaiVfModuleResources;
79 protected AAIServiceInstanceResources aaiServiceInstanceResources;
81 protected AAIVnfResources aaiVnfResources;
83 protected AAINetworkResources aaiNetworkResources;
85 protected AAIVolumeGroupResources aaiVolumeGroupResources;
87 protected AAIConfigurationResources aaiConfigurationResources;
89 protected AAIInstanceGroupResources aaiInstanceGroupResources;
91 protected ExtractPojosForBB extractPojosForBBMock;
93 private AAIDeleteTasks aaiDeleteTasks;
95 private L3Network network;
96 private ServiceInstance serviceInstance;
97 private GenericVnf genericVnf;
98 private VfModule vfModule;
99 private VolumeGroup volumeGroup;
100 private CloudRegion cloudRegion;
101 private Configuration configuration;
102 private InstanceGroup instanceGroup;
105 ArgumentCaptor<String> stringCaptor;
108 public void before() throws BBObjectNotFoundException {
109 serviceInstance = setServiceInstance();
110 genericVnf = setGenericVnf();
111 vfModule = setVfModule();
112 network = setL3Network();
113 volumeGroup = setVolumeGroup();
114 cloudRegion = setCloudRegion();
115 configuration = setConfiguration();
116 instanceGroup = setInstanceGroupVnf();
118 when(extractPojosForBBMock.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
119 .thenReturn(genericVnf);
120 when(extractPojosForBBMock.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID)))
121 .thenReturn(vfModule);
122 when(extractPojosForBBMock.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.NETWORK_ID)))
123 .thenReturn(network);
124 when(extractPojosForBBMock.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VOLUME_GROUP_ID)))
125 .thenReturn(volumeGroup);
126 when(extractPojosForBBMock.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID)))
127 .thenReturn(serviceInstance);
128 when(extractPojosForBBMock.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.CONFIGURATION_ID)))
129 .thenReturn(configuration);
130 when(extractPojosForBBMock.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.INSTANCE_GROUP_ID)))
131 .thenReturn(instanceGroup);
134 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
135 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
139 public void deleteVfModuleTest() throws Exception {
141 doNothing().when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
143 aaiDeleteTasks.deleteVfModule(execution);
144 verify(aaiVfModuleResources, times(1)).deleteVfModule(vfModule, genericVnf);
148 public void deleteVfModuleExceptionTest() throws Exception {
149 expectedException.expect(BpmnError.class);
150 doThrow(RuntimeException.class).when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
151 aaiDeleteTasks.deleteVfModule(execution);
155 public void deleteServiceInstanceTest() throws Exception {
156 doNothing().when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
158 aaiDeleteTasks.deleteServiceInstance(execution);
160 verify(aaiServiceInstanceResources, times(1)).deleteServiceInstance(serviceInstance);
164 public void deleteServiceInstanceExceptionTest() throws Exception {
165 expectedException.expect(BpmnError.class);
167 doThrow(RuntimeException.class).when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
169 aaiDeleteTasks.deleteServiceInstance(execution);
173 public void deleteVnfTest() throws Exception {
174 doNothing().when(aaiVnfResources).deleteVnf(genericVnf);
175 aaiDeleteTasks.deleteVnf(execution);
176 verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
180 public void deleteVnfTestException() throws Exception {
181 expectedException.expect(BpmnError.class);
182 doThrow(RuntimeException.class).when(aaiVnfResources).deleteVnf(genericVnf);
184 aaiDeleteTasks.deleteVnf(execution);
185 verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
189 public void deleteNetworkTest() throws Exception {
190 doNothing().when(aaiNetworkResources).deleteNetwork(network);
191 aaiDeleteTasks.deleteNetwork(execution);
192 verify(aaiNetworkResources, times(1)).deleteNetwork(network);
196 public void deleteCollectionTest() throws Exception {
197 doNothing().when(aaiNetworkResources).deleteCollection(serviceInstance.getCollection());
198 aaiDeleteTasks.deleteCollection(execution);
199 verify(aaiNetworkResources, times(1)).deleteCollection(serviceInstance.getCollection());
203 public void deleteInstanceGroupTest() throws Exception {
204 doNothing().when(aaiNetworkResources)
205 .deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
206 aaiDeleteTasks.deleteInstanceGroup(execution);
207 verify(aaiNetworkResources, times(1))
208 .deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
212 public void deleteVolumeGroupTest() {
213 doNothing().when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
215 aaiDeleteTasks.deleteVolumeGroup(execution);
217 verify(aaiVolumeGroupResources, times(1)).deleteVolumeGroup(volumeGroup, cloudRegion);
221 public void deleteVolumeGroupExceptionTest() {
222 expectedException.expect(BpmnError.class);
224 doThrow(RuntimeException.class).when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
226 aaiDeleteTasks.deleteVolumeGroup(execution);
230 public void deleteConfigurationTest() {
231 gBBInput = execution.getGeneralBuildingBlock();
232 doNothing().when(aaiConfigurationResources).deleteConfiguration(configuration);
233 aaiDeleteTasks.deleteConfiguration(execution);
234 verify(aaiConfigurationResources, times(1)).deleteConfiguration(configuration);
238 public void deleteInstanceGroupVnfTest() {
239 doNothing().when(aaiInstanceGroupResources).deleteInstanceGroup(instanceGroup);
240 aaiDeleteTasks.deleteInstanceGroupVnf(execution);
241 verify(aaiInstanceGroupResources, times(1)).deleteInstanceGroup(instanceGroup);
245 public void deleteNetworkPolicyNeedToDeleteAllTest() throws Exception {
246 execution.setVariable("contrailNetworkPolicyFqdnList", "ABC123,DEF456");
247 final String content0 = new String(
248 Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "queryAaiNetworkPoliciesForDelete0.json")));
249 AAIResultWrapper aaiResultWrapper0 = new AAIResultWrapper(content0);
250 NetworkPolicies networkPolicies0 = aaiResultWrapper0.asBean(NetworkPolicies.class).get();
251 final String content1 = new String(
252 Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "queryAaiNetworkPoliciesForDelete1.json")));
253 AAIResultWrapper aaiResultWrapper1 = new AAIResultWrapper(content1);
254 NetworkPolicies networkPolicies1 = aaiResultWrapper1.asBean(NetworkPolicies.class).get();
256 doReturn(Optional.of(networkPolicies0), Optional.of(networkPolicies1)).when(aaiNetworkResources)
257 .getNetworkPolicies(any(AAIBaseResourceUri.class));
258 doNothing().when(aaiNetworkResources).deleteNetworkPolicy(any(String.class));
259 aaiDeleteTasks.deleteNetworkPolicies(execution);
260 verify(aaiNetworkResources, times(2)).deleteNetworkPolicy(stringCaptor.capture());
261 assertEquals("testNetworkPolicyId0", stringCaptor.getAllValues().get(0));
262 assertEquals("testNetworkPolicyId1", stringCaptor.getAllValues().get(1));
266 public void deleteNetworkPolicyNeedToDeleteNoneTest() {
267 execution.setVariable("contrailNetworkPolicyFqdnList", "ABC123");
268 Optional<NetworkPolicies> networkPolicies = Optional.empty();
269 doReturn(networkPolicies).when(aaiNetworkResources).getNetworkPolicies(any(AAIBaseResourceUri.class));
270 aaiDeleteTasks.deleteNetworkPolicies(execution);
271 verify(aaiNetworkResources, times(0)).deleteNetworkPolicy(any(String.class));
275 public void deleteNetworkPolicyNoNetworkPoliciesTest() {
276 aaiDeleteTasks.deleteNetworkPolicies(execution);
277 verify(aaiNetworkResources, times(0)).deleteNetworkPolicy(any(String.class));