ed281ce31788c99af124314bcb4be77baaa1a594
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / sdnc / tasks / SDNCDeactivateTaskTest.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.assertFalse;
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.entities.ResourceKey;
43 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
44 import org.springframework.beans.factory.annotation.Autowired;
45
46 public class SDNCDeactivateTaskTest extends BaseTaskTest {
47         @Autowired
48         private SDNCDeactivateTasks sdncDeactivateTasks;
49         
50         private ServiceInstance serviceInstance;
51         private CloudRegion cloudRegion;
52         private RequestContext requestContext;
53         private GenericVnf genericVnf;
54         private VfModule vfModule;
55         private L3Network network;
56         private Customer customer;
57         
58         @Before
59         public void before() {
60                 customer = setCustomer();
61                 serviceInstance = setServiceInstance();
62                 cloudRegion = setCloudRegion();
63                 requestContext = setRequestContext();
64                 genericVnf = setGenericVnf();
65                 vfModule = setVfModule();
66                 network = setL3Network();
67
68         }
69         
70         @Test
71         public void deactivateVfModuleTest() throws Exception {
72                 doReturn("success").when(sdncVfModuleResources).deactivateVfModule(vfModule, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
73
74                 sdncDeactivateTasks.deactivateVfModule(execution);
75
76                 verify(sdncVfModuleResources, times(1)).deactivateVfModule(vfModule, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
77         }
78         
79         @Test
80         public void deactivateVfModuleExceptionTest() throws Exception {
81                 expectedException.expect(BpmnError.class);
82                 
83                 doThrow(Exception.class).when(sdncVfModuleResources).deactivateVfModule(vfModule, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
84
85                 sdncDeactivateTasks.deactivateVfModule(execution);
86         }
87         
88         @Test
89         public void deactivateVnfTest() throws Exception {
90                 doReturn("success").when(sdncVnfResources).deactivateVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
91
92                 sdncDeactivateTasks.deactivateVnf(execution);
93
94                 verify(sdncVnfResources, times(1)).deactivateVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
95         }
96         
97         @Test
98         public void deactivateVnfExceptionTest() throws Exception {
99                 doThrow(Exception.class).when(sdncVnfResources).deactivateVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
100                 expectedException.expect(BpmnError.class);
101                 sdncDeactivateTasks.deactivateVnf(execution);
102         }
103         
104         @Test
105         public void deactivateServiceInstanceTest() throws Exception {
106                 doReturn("response").when(sdncServiceInstanceResources).deactivateServiceInstance(serviceInstance, customer, requestContext);
107
108                 sdncDeactivateTasks.deactivateServiceInstance(execution);
109
110                 verify(sdncServiceInstanceResources, times(1)).deactivateServiceInstance(serviceInstance, customer, requestContext);
111                 assertEquals("response", execution.getVariable("deactivateServiceInstanceSDNCResponse"));
112                 assertTrue(execution.getVariable("sdncServiceInstanceRollback"));
113         }
114         
115         @Test
116         public void deactivateServiceInstanceExceptionTest() throws Exception {
117                 doThrow(Exception.class).when(sdncServiceInstanceResources).deactivateServiceInstance(serviceInstance, customer, requestContext);
118                 expectedException.expect(BpmnError.class);
119                 sdncDeactivateTasks.deactivateServiceInstance(execution);
120         }
121         
122         @Test
123         public void test_deactivateNetwork() throws Exception {
124                 String expectedResponse = "return";
125                 
126                 doReturn(expectedResponse).when(sdncNetworkResources).deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
127                 
128                 sdncDeactivateTasks.deactivateNetwork(execution);
129                 
130                 verify(sdncNetworkResources, times(1)).deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
131                 
132                 assertEquals(expectedResponse, execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.response"));
133                 
134                 assertTrue(execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.rollback"));
135         }
136         
137         @Test
138         public void test_deactivateNetwork_exception() throws Exception {
139                 expectedException.expect(BpmnError.class);
140                 
141                 try {
142                         lookupKeyMap.remove(ResourceKey.NETWORK_ID);
143                         
144                         sdncDeactivateTasks.deactivateNetwork(execution);
145                 } finally {
146                         verify(sdncNetworkResources, times(0)).deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
147                         
148                         assertNull(execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.response"));
149                         
150                         assertFalse(execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.rollback"));
151                 }
152         }
153
154 }