243b452d18f142f3307081c4e923e5a714724f3d
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / aai / tasks / AAIDeleteTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.aai.tasks;
22
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.mockito.ArgumentCaptor;
39 import org.mockito.ArgumentMatchers;
40 import org.mockito.Captor;
41 import org.mockito.InjectMocks;
42 import org.onap.aai.domain.yang.NetworkPolicies;
43 import org.onap.so.bpmn.BaseTaskTest;
44 import org.onap.so.bpmn.common.BuildingBlockExecution;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
49 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
50 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
51 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
52 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
53 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
54 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
55 import org.onap.aaiclient.client.aai.entities.uri.AAIBaseResourceUri;
56 import org.onap.so.client.exception.BBObjectNotFoundException;
57
58
59 public class AAIDeleteTasksTest extends BaseTaskTest {
60     private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
61
62     @InjectMocks
63     private AAIDeleteTasks aaiDeleteTasks = new AAIDeleteTasks();
64
65     private L3Network network;
66     private ServiceInstance serviceInstance;
67     private GenericVnf genericVnf;
68     private VfModule vfModule;
69     private VolumeGroup volumeGroup;
70     private CloudRegion cloudRegion;
71     private Configuration configuration;
72     private InstanceGroup instanceGroup;
73
74     @Captor
75     ArgumentCaptor<String> stringCaptor;
76
77     @Before
78     public void before() throws BBObjectNotFoundException {
79         serviceInstance = setServiceInstance();
80         genericVnf = setGenericVnf();
81         vfModule = setVfModule();
82         network = setL3Network();
83         volumeGroup = setVolumeGroup();
84         cloudRegion = setCloudRegion();
85         configuration = setConfiguration();
86         instanceGroup = setInstanceGroupVnf();
87
88         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
89                 .thenReturn(genericVnf);
90         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
91         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.NETWORK_ID))).thenReturn(network);
92         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VOLUME_GROUP_ID)))
93                 .thenReturn(volumeGroup);
94         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID)))
95                 .thenReturn(serviceInstance);
96         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.CONFIGURATION_ID)))
97                 .thenReturn(configuration);
98         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.INSTANCE_GROUP_ID)))
99                 .thenReturn(instanceGroup);
100
101
102         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
103                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
104     }
105
106     @Test
107     public void deleteVfModuleTest() throws Exception {
108
109         doNothing().when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
110
111         aaiDeleteTasks.deleteVfModule(execution);
112         verify(aaiVfModuleResources, times(1)).deleteVfModule(vfModule, genericVnf);
113     }
114
115     @Test
116     public void deleteVfModuleExceptionTest() throws Exception {
117         expectedException.expect(BpmnError.class);
118         doThrow(RuntimeException.class).when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
119         aaiDeleteTasks.deleteVfModule(execution);
120     }
121
122     @Test
123     public void deleteServiceInstanceTest() throws Exception {
124         doNothing().when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
125
126         aaiDeleteTasks.deleteServiceInstance(execution);
127
128         verify(aaiServiceInstanceResources, times(1)).deleteServiceInstance(serviceInstance);
129     }
130
131     @Test
132     public void deleteServiceInstanceExceptionTest() throws Exception {
133         expectedException.expect(BpmnError.class);
134
135         doThrow(RuntimeException.class).when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
136
137         aaiDeleteTasks.deleteServiceInstance(execution);
138     }
139
140     @Test
141     public void deleteVnfTest() throws Exception {
142         doNothing().when(aaiVnfResources).deleteVnf(genericVnf);
143         aaiDeleteTasks.deleteVnf(execution);
144         verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
145     }
146
147     @Test
148     public void deleteVnfTestException() throws Exception {
149         expectedException.expect(BpmnError.class);
150         doThrow(RuntimeException.class).when(aaiVnfResources).deleteVnf(genericVnf);
151
152         aaiDeleteTasks.deleteVnf(execution);
153         verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
154     }
155
156     @Test
157     public void deleteNetworkTest() throws Exception {
158         doNothing().when(aaiNetworkResources).deleteNetwork(network);
159         aaiDeleteTasks.deleteNetwork(execution);
160         verify(aaiNetworkResources, times(1)).deleteNetwork(network);
161     }
162
163     @Test
164     public void deleteCollectionTest() throws Exception {
165         doNothing().when(aaiNetworkResources).deleteCollection(serviceInstance.getCollection());
166         aaiDeleteTasks.deleteCollection(execution);
167         verify(aaiNetworkResources, times(1)).deleteCollection(serviceInstance.getCollection());
168     }
169
170     @Test
171     public void deleteInstanceGroupTest() throws Exception {
172         doNothing().when(aaiNetworkResources)
173                 .deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
174         aaiDeleteTasks.deleteInstanceGroup(execution);
175         verify(aaiNetworkResources, times(1))
176                 .deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
177     }
178
179     @Test
180     public void deleteVolumeGroupTest() {
181         doNothing().when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
182
183         aaiDeleteTasks.deleteVolumeGroup(execution);
184
185         verify(aaiVolumeGroupResources, times(1)).deleteVolumeGroup(volumeGroup, cloudRegion);
186     }
187
188     @Test
189     public void deleteVolumeGroupExceptionTest() {
190         expectedException.expect(BpmnError.class);
191
192         doThrow(RuntimeException.class).when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
193
194         aaiDeleteTasks.deleteVolumeGroup(execution);
195     }
196
197     @Test
198     public void deleteConfigurationTest() throws Exception {
199         gBBInput = execution.getGeneralBuildingBlock();
200         doNothing().when(aaiConfigurationResources).deleteConfiguration(configuration);
201         aaiDeleteTasks.deleteConfiguration(execution);
202         verify(aaiConfigurationResources, times(1)).deleteConfiguration(configuration);
203     }
204
205     @Test
206     public void deleteInstanceGroupVnfTest() throws Exception {
207         doNothing().when(aaiInstanceGroupResources).deleteInstanceGroup(instanceGroup);
208         aaiDeleteTasks.deleteInstanceGroupVnf(execution);
209         verify(aaiInstanceGroupResources, times(1)).deleteInstanceGroup(instanceGroup);
210     }
211
212     @Test
213     public void deleteNetworkPolicyNeedToDeleteAllTest() throws Exception {
214         execution.setVariable("contrailNetworkPolicyFqdnList", "ABC123,DEF456");
215         final String content0 = new String(
216                 Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "queryAaiNetworkPoliciesForDelete0.json")));
217         AAIResultWrapper aaiResultWrapper0 = new AAIResultWrapper(content0);
218         NetworkPolicies networkPolicies0 = aaiResultWrapper0.asBean(NetworkPolicies.class).get();
219         final String content1 = new String(
220                 Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "queryAaiNetworkPoliciesForDelete1.json")));
221         AAIResultWrapper aaiResultWrapper1 = new AAIResultWrapper(content1);
222         NetworkPolicies networkPolicies1 = aaiResultWrapper1.asBean(NetworkPolicies.class).get();
223
224         doReturn(Optional.of(networkPolicies0), Optional.of(networkPolicies1)).when(aaiNetworkResources)
225                 .getNetworkPolicies(any(AAIBaseResourceUri.class));
226         doNothing().when(aaiNetworkResources).deleteNetworkPolicy(any(String.class));
227         aaiDeleteTasks.deleteNetworkPolicies(execution);
228         verify(aaiNetworkResources, times(2)).deleteNetworkPolicy(stringCaptor.capture());
229         assertEquals("testNetworkPolicyId0", stringCaptor.getAllValues().get(0));
230         assertEquals("testNetworkPolicyId1", stringCaptor.getAllValues().get(1));
231     }
232
233     @Test
234     public void deleteNetworkPolicyNeedToDeleteNoneTest() throws Exception {
235         execution.setVariable("contrailNetworkPolicyFqdnList", "ABC123");
236         Optional<NetworkPolicies> networkPolicies = Optional.empty();
237         doReturn(networkPolicies).when(aaiNetworkResources).getNetworkPolicies(any(AAIBaseResourceUri.class));
238         aaiDeleteTasks.deleteNetworkPolicies(execution);
239         verify(aaiNetworkResources, times(0)).deleteNetworkPolicy(any(String.class));
240     }
241
242     @Test
243     public void deleteNetworkPolicyNoNetworkPoliciesTest() throws Exception {
244         aaiDeleteTasks.deleteNetworkPolicies(execution);
245         verify(aaiNetworkResources, times(0)).deleteNetworkPolicy(any(String.class));
246     }
247 }