Springboot 2.0 upgrade
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / sdnc / tasks / SDNCUnassignTasksTest.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.sdnc.tasks;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34
35 import org.camunda.bpm.engine.delegate.BpmnError;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.ArgumentMatchers;
39 import org.mockito.InjectMocks;
40 import org.onap.so.bpmn.BaseTaskTest;
41 import org.onap.so.bpmn.common.BuildingBlockExecution;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
48 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
49 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
50 import org.onap.so.client.exception.BBObjectNotFoundException;
51 import org.onap.so.db.catalog.beans.OrchestrationStatus;
52 import org.springframework.beans.factory.annotation.Autowired;
53
54 public class SDNCUnassignTasksTest extends BaseTaskTest{
55         @InjectMocks
56         private SDNCUnassignTasks sdncUnassignTasks = new SDNCUnassignTasks();
57         
58         private ServiceInstance serviceInstance;
59         private RequestContext requestContext;
60         private GenericVnf genericVnf;
61         private VfModule vfModule;
62         private CloudRegion cloudRegion;
63         private L3Network network;
64         private Customer customer;
65         
66         @Before
67         public void before() throws BBObjectNotFoundException {
68                 customer = setCustomer();
69                 serviceInstance = setServiceInstance();
70                 requestContext = setRequestContext();
71                 genericVnf = setGenericVnf();
72                 vfModule = setVfModule();
73                 cloudRegion = setCloudRegion();
74                 network = setL3Network();
75                 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
76                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID), any())).thenReturn(genericVnf);
77                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.NETWORK_ID), any())).thenReturn(network);
78                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID), any())).thenReturn(vfModule);
79                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID), any())).thenReturn(serviceInstance);
80         }
81         
82         @Test
83         public void unassignServiceInstanceTest() throws Exception {
84                 doReturn("test").when(sdncServiceInstanceResources).unassignServiceInstance(serviceInstance, customer, requestContext);
85                 
86                 sdncUnassignTasks.unassignServiceInstance(execution);
87                 
88                 verify(sdncServiceInstanceResources, times(1)).unassignServiceInstance(serviceInstance, customer, requestContext);
89         }
90
91         @Test
92         public void unassignServiceInstanceTest_inventoried() throws Exception {
93                 doReturn("test").when(sdncServiceInstanceResources).unassignServiceInstance(serviceInstance, customer, requestContext);
94                 
95                 serviceInstance.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
96                 
97                 sdncUnassignTasks.unassignServiceInstance(execution);
98                 
99                 verify(sdncServiceInstanceResources, times(0)).unassignServiceInstance(serviceInstance, customer, requestContext);
100         }
101
102         @Test
103         public void unassignServiceInstanceExceptionTest() throws Exception {
104                 expectedException.expect(BpmnError.class);
105                 
106                 doThrow(RuntimeException.class).when(sdncServiceInstanceResources).unassignServiceInstance(serviceInstance, customer, requestContext);
107                 
108                 sdncUnassignTasks.unassignServiceInstance(execution);
109         }       
110                 
111         @Test
112         public void unassignVfModuleTest() throws Exception {
113                 doReturn("response").when(sdncVfModuleResources).unassignVfModule(vfModule, genericVnf, serviceInstance);
114
115                 sdncUnassignTasks.unassignVfModule(execution);
116
117                 verify(sdncVfModuleResources, times(1)).unassignVfModule(vfModule, genericVnf, serviceInstance);
118                 assertEquals("response", execution.getVariable("SDNCResponse"));
119         }
120         
121         @Test
122         public void unassignVfModuleTest_inventoried() throws Exception {
123                 vfModule.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
124                 
125                 sdncUnassignTasks.unassignVfModule(execution);
126
127                 verify(sdncVfModuleResources, times(0)).unassignVfModule(vfModule, genericVnf, serviceInstance);
128                 assertNull(execution.getVariable("SDNCResponse"));
129         }
130         
131         @Test
132         public void unassignVfModuleTest_pendingCreate() throws Exception {
133                 vfModule.setOrchestrationStatus(OrchestrationStatus.PENDING_CREATE);
134                 
135                 sdncUnassignTasks.unassignVfModule(execution);
136
137                 verify(sdncVfModuleResources, times(0)).unassignVfModule(vfModule, genericVnf, serviceInstance);
138                 assertNull(execution.getVariable("SDNCResponse"));
139         }
140         
141         @Test
142         public void unassignVfModuleExceptionTest() throws Exception {
143                 expectedException.expect(BpmnError.class);
144                 
145                 doThrow(RuntimeException.class).when(sdncVfModuleResources).unassignVfModule(vfModule, genericVnf, serviceInstance);
146
147                 sdncUnassignTasks.unassignVfModule(execution);
148         }
149         
150         @Test
151         public void unassignVnfTest() throws Exception {
152                 doReturn("response").when(sdncVnfResources).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
153
154                 sdncUnassignTasks.unassignVnf(execution);
155
156                 verify(sdncVnfResources, times(1)).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
157                 assertTrue(execution.getVariable("sdncUnassignVnfResponse").equals("response"));
158         }
159         
160         @Test
161         public void unassignVnfTest_inventoried() throws Exception {
162                 genericVnf.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
163                 
164                 sdncUnassignTasks.unassignVnf(execution);
165
166                 verify(sdncVnfResources, times(0)).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
167                 assertNull(execution.getVariable("sdncUnassignVnfResponse"));
168         }
169         
170         @Test
171         public void unassignVnfTest_created() throws Exception {
172                 genericVnf.setOrchestrationStatus(OrchestrationStatus.CREATED);
173                 
174                 sdncUnassignTasks.unassignVnf(execution);
175
176                 verify(sdncVnfResources, times(0)).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
177                 assertNull(execution.getVariable("sdncUnassignVnfResponse"));
178         }
179         
180         @Test
181         public void unassignVnfExceptionTest() throws Exception {
182                 expectedException.expect(BpmnError.class);
183                 doThrow(RuntimeException.class).when(sdncVnfResources).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
184                 sdncUnassignTasks.unassignVnf(execution);
185         }
186
187         @Test
188         public void unassignNetworkTest() throws Exception {
189                 String cloudRegionSdnc = "AAIAIC25";
190                 
191                 cloudRegion.setCloudRegionVersion("2.5");
192                 
193                 execution.setVariable("cloudRegionSdnc", cloudRegionSdnc);
194                 
195                 doReturn("response").when(sdncNetworkResources).unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
196                 
197                 assertNotEquals(cloudRegionSdnc, cloudRegion.getLcpCloudRegionId());
198                 sdncUnassignTasks.unassignNetwork(execution);
199
200                 verify(sdncNetworkResources, times(1)).unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
201                 assertEquals("response", execution.getVariable("SDNCUnAssignNetworkResponse"));
202                 assertEquals(cloudRegionSdnc, cloudRegion.getLcpCloudRegionId());
203         }
204         
205         @Test
206         public void unassignNetworkTest_inventoried() throws Exception {
207                 network.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
208                 
209                 sdncUnassignTasks.unassignNetwork(execution);
210
211                 verify(sdncNetworkResources, times(0)).unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
212                 assertNull(execution.getVariable("SDNCUnAssignNetworkResponse"));
213         }
214 }