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