mso is not updating the vip in aai for the vnf
[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.util.Optional;
34
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.NetworkPolicy;
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.L3Network;
49 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
50 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
51 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
52 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
53 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
54 import org.onap.so.client.exception.BBObjectNotFoundException;
55
56
57 public class AAIDeleteTasksTest extends BaseTaskTest {
58         
59         @InjectMocks
60         private AAIDeleteTasks aaiDeleteTasks = new AAIDeleteTasks();
61         
62         private L3Network network;
63         private ServiceInstance serviceInstance;
64         private GenericVnf genericVnf;
65         private VfModule vfModule;
66         private VolumeGroup volumeGroup;
67         private CloudRegion cloudRegion;
68         private Configuration configuration;
69         
70         @Captor
71         ArgumentCaptor<String> stringCaptor;    
72         
73         @Before
74         public void before() throws BBObjectNotFoundException {
75                 serviceInstance = setServiceInstance();
76                 genericVnf = setGenericVnf();
77                 vfModule = setVfModule();
78                 network = setL3Network();
79                 volumeGroup = setVolumeGroup();
80                 cloudRegion = setCloudRegion();
81                 configuration = setConfiguration();
82                 
83                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID), any())).thenReturn(genericVnf);
84                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID), any())).thenReturn(vfModule);
85                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.NETWORK_ID), any())).thenReturn(network);
86                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.VOLUME_GROUP_ID), any())).thenReturn(volumeGroup);
87                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID), any())).thenReturn(serviceInstance);
88                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.CONFIGURATION_ID), any())).thenReturn(configuration);
89                 
90
91                 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
92         }
93         
94         @Test
95         public void deleteVfModuleTest() throws Exception {
96                 
97                 doNothing().when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
98                 
99                 aaiDeleteTasks.deleteVfModule(execution);
100                 verify(aaiVfModuleResources, times(1)).deleteVfModule(vfModule, genericVnf);
101         }
102         
103         @Test
104         public void deleteVfModuleExceptionTest() throws Exception {
105                 expectedException.expect(BpmnError.class);
106                 doThrow(RuntimeException.class).when(aaiVfModuleResources).deleteVfModule(vfModule, genericVnf);
107                 aaiDeleteTasks.deleteVfModule(execution);
108         }
109         
110         @Test
111         public void deleteServiceInstanceTest() throws Exception {
112                 doNothing().when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
113                 
114                 aaiDeleteTasks.deleteServiceInstance(execution);
115                 
116                 verify(aaiServiceInstanceResources, times(1)).deleteServiceInstance(serviceInstance);
117         }
118         
119         @Test 
120         public void deleteServiceInstanceExceptionTest() throws Exception {
121                 expectedException.expect(BpmnError.class);
122                 
123                 doThrow(RuntimeException.class).when(aaiServiceInstanceResources).deleteServiceInstance(serviceInstance);
124                 
125                 aaiDeleteTasks.deleteServiceInstance(execution);
126         }       
127         
128         @Test
129         public void deleteVnfTest() throws Exception {
130                 doNothing().when(aaiVnfResources).deleteVnf(genericVnf);
131                 aaiDeleteTasks.deleteVnf(execution);
132                 verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
133         }
134         
135         @Test
136         public void deleteVnfTestException() throws Exception {
137                 expectedException.expect(BpmnError.class);
138                 doThrow(RuntimeException.class).when(aaiVnfResources).deleteVnf(genericVnf);
139                 
140                 aaiDeleteTasks.deleteVnf(execution);
141                 verify(aaiVnfResources, times(1)).deleteVnf(genericVnf);
142         }
143         
144         @Test 
145         public void deleteNetworkTest() throws Exception {
146                 doNothing().when(aaiNetworkResources).deleteNetwork(network);
147                 aaiDeleteTasks.deleteNetwork(execution);
148                 verify(aaiNetworkResources, times(1)).deleteNetwork(network);
149         }
150         
151         @Test 
152         public void deleteCollectionTest() throws Exception {
153                 doNothing().when(aaiNetworkResources).deleteCollection(serviceInstance.getCollection());
154                 aaiDeleteTasks.deleteCollection(execution);
155                 verify(aaiNetworkResources, times(1)).deleteCollection(serviceInstance.getCollection());
156         }
157         
158         @Test 
159         public void deleteInstanceGroupTest() throws Exception {
160                 doNothing().when(aaiNetworkResources).deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
161                 aaiDeleteTasks.deleteInstanceGroup(execution);
162                 verify(aaiNetworkResources, times(1)).deleteNetworkInstanceGroup(serviceInstance.getCollection().getInstanceGroup());
163         }
164
165         @Test
166         public void deleteVolumeGroupTest() {
167                 doNothing().when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
168                 
169                 aaiDeleteTasks.deleteVolumeGroup(execution);
170                 
171                 verify(aaiVolumeGroupResources, times(1)).deleteVolumeGroup(volumeGroup, cloudRegion);
172         }
173         
174         @Test
175         public void deleteVolumeGroupExceptionTest() {
176                 expectedException.expect(BpmnError.class);
177                 
178                 doThrow(RuntimeException.class).when(aaiVolumeGroupResources).deleteVolumeGroup(volumeGroup, cloudRegion);
179                 
180                 aaiDeleteTasks.deleteVolumeGroup(execution);
181         }
182         
183         @Test
184         public void deleteConfigurationTest() throws Exception {
185                 gBBInput = execution.getGeneralBuildingBlock();
186                 doNothing().when(aaiConfigurationResources).deleteConfiguration(configuration);
187                 aaiDeleteTasks.deleteConfiguration(execution);
188                 verify(aaiConfigurationResources, times(1)).deleteConfiguration(configuration);
189         }
190         
191         @Test
192         public void deleteNetworkPolicyNeedToDeleteAllTest() throws Exception {         
193                 execution.setVariable("contrailNetworkPolicyFqdnList", "ABC123,DEF456");
194                 NetworkPolicy networkPolicy0 = new NetworkPolicy();
195                 networkPolicy0.setNetworkPolicyId("testNetworkPolicyId0");
196                 NetworkPolicy networkPolicy1 = new NetworkPolicy();
197                 networkPolicy1.setNetworkPolicyId("testNetworkPolicyId1");
198                 doReturn(Optional.of(networkPolicy0),Optional.of(networkPolicy1)).when(aaiNetworkResources).getNetworkPolicy(any(AAIResourceUri.class));
199                 doNothing().when(aaiNetworkResources).deleteNetworkPolicy(any(String.class));
200                 aaiDeleteTasks.deleteNetworkPolicies(execution);
201                 verify(aaiNetworkResources, times(2)).deleteNetworkPolicy(stringCaptor.capture());
202                 assertEquals("testNetworkPolicyId0", stringCaptor.getAllValues().get(0));
203                 assertEquals("testNetworkPolicyId1", stringCaptor.getAllValues().get(1));
204         }
205         
206         @Test
207         public void deleteNetworkPolicyNeedToDeleteNoneTest() throws Exception {                
208                 execution.setVariable("contrailNetworkPolicyFqdnList", "ABC123");
209                 Optional<NetworkPolicy> networkPolicy = Optional.empty();               
210                 doReturn(networkPolicy).when(aaiNetworkResources).getNetworkPolicy(any(AAIResourceUri.class));
211                 aaiDeleteTasks.deleteNetworkPolicies(execution);
212                 verify(aaiNetworkResources, times(0)).deleteNetworkPolicy(any(String.class));
213         }
214         
215         @Test
216         public void deleteNetworkPolicyNoNetworkPoliciesTest() throws Exception {                       
217                 aaiDeleteTasks.deleteNetworkPolicies(execution);
218                 verify(aaiNetworkResources, times(0)).deleteNetworkPolicy(any(String.class));
219         }
220 }