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