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