Minior Change - Licence Statement
[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.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;
59
60
61 public class AAIDeleteTasksTest extends BaseTaskTest {
62         private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
63         
64         @InjectMocks
65         private AAIDeleteTasks aaiDeleteTasks = new AAIDeleteTasks();
66         
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;
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                 
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);
94                 
95
96                 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
97         }
98         
99         @Test
100         public void deleteVfModuleTest() throws Exception {
101                 
102                 doNothing().when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
103                 
104                 aaiDeleteTasks.deleteVfModule(execution);
105                 verify(aaiVfModuleResources, times(1)).deleteVfModule(vfModule, genericVnf);
106         }
107         
108         @Test
109         public void deleteVfModuleExceptionTest() throws Exception {
110                 expectedException.expect(BpmnError.class);
111                 doThrow(RuntimeException.class).when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
112                 aaiDeleteTasks.deleteVfModule(execution);
113         }
114         
115         @Test
116         public void deleteServiceInstanceTest() throws Exception {
117                 doNothing().when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
118                 
119                 aaiDeleteTasks.deleteServiceInstance(execution);
120                 
121                 verify(aaiServiceInstanceResources, times(1)).deleteServiceInstance(serviceInstance);
122         }
123         
124         @Test 
125         public void deleteServiceInstanceExceptionTest() throws Exception {
126                 expectedException.expect(BpmnError.class);
127                 
128                 doThrow(RuntimeException.class).when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
129                 
130                 aaiDeleteTasks.deleteServiceInstance(execution);
131         }       
132         
133         @Test
134         public void deleteVnfTest() throws Exception {
135                 doNothing().when(aaiVnfResources).deleteVnf(genericVnf);
136                 aaiDeleteTasks.deleteVnf(execution);
137                 verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
138         }
139         
140         @Test
141         public void deleteVnfTestException() throws Exception {
142                 expectedException.expect(BpmnError.class);
143                 doThrow(RuntimeException.class).when(aaiVnfResources).deleteVnf(genericVnf);
144                 
145                 aaiDeleteTasks.deleteVnf(execution);
146                 verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
147         }
148         
149         @Test 
150         public void deleteNetworkTest() throws Exception {
151                 doNothing().when(aaiNetworkResources).deleteNetwork(network);
152                 aaiDeleteTasks.deleteNetwork(execution);
153                 verify(aaiNetworkResources, times(1)).deleteNetwork(network);
154         }
155         
156         @Test 
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());
161         }
162         
163         @Test 
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());
168         }
169
170         @Test
171         public void deleteVolumeGroupTest() {
172                 doNothing().when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
173                 
174                 aaiDeleteTasks.deleteVolumeGroup(execution);
175                 
176                 verify(aaiVolumeGroupResources, times(1)).deleteVolumeGroup(volumeGroup, cloudRegion);
177         }
178         
179         @Test
180         public void deleteVolumeGroupExceptionTest() {
181                 expectedException.expect(BpmnError.class);
182                 
183                 doThrow(RuntimeException.class).when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
184                 
185                 aaiDeleteTasks.deleteVolumeGroup(execution);
186         }
187         
188         @Test
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);
194         }
195         
196         @Test
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();
205                 
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));
212         }
213         
214         @Test
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));
221         }
222         
223         @Test
224         public void deleteNetworkPolicyNoNetworkPoliciesTest() throws Exception {                       
225                 aaiDeleteTasks.deleteNetworkPolicies(execution);
226                 verify(aaiNetworkResources, times(0)).deleteNetworkPolicy(any(String.class));
227         }
228 }