Springboot 2.0 upgrade
[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.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.springframework.beans.factory.annotation.Autowired;
52
53 public class SDNCDeactivateTaskTest extends BaseTaskTest {
54         @InjectMocks
55         private SDNCDeactivateTasks sdncDeactivateTasks = new SDNCDeactivateTasks();
56         
57         private ServiceInstance serviceInstance;
58         private CloudRegion cloudRegion;
59         private RequestContext requestContext;
60         private GenericVnf genericVnf;
61         private VfModule vfModule;
62         private L3Network network;
63         private Customer customer;
64         
65         @Before
66         public void before() throws BBObjectNotFoundException {
67                 customer = setCustomer();
68                 serviceInstance = setServiceInstance();
69                 cloudRegion = setCloudRegion();
70                 requestContext = setRequestContext();
71                 genericVnf = setGenericVnf();
72                 vfModule = setVfModule();
73                 network = setL3Network();
74                 
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         
83         @Test
84         public void deactivateVfModuleTest() throws Exception {
85                 doReturn("success").when(sdncVfModuleResources).deactivateVfModule(vfModule, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
86
87                 sdncDeactivateTasks.deactivateVfModule(execution);
88
89                 verify(sdncVfModuleResources, times(1)).deactivateVfModule(vfModule, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
90         }
91         
92         @Test
93         public void deactivateVfModuleExceptionTest() throws Exception {
94                 expectedException.expect(BpmnError.class);
95                 
96                 doThrow(RuntimeException.class).when(sdncVfModuleResources).deactivateVfModule(vfModule, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
97
98                 sdncDeactivateTasks.deactivateVfModule(execution);
99         }
100         
101         @Test
102         public void deactivateVnfTest() throws Exception {
103                 doReturn("success").when(sdncVnfResources).deactivateVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
104
105                 sdncDeactivateTasks.deactivateVnf(execution);
106
107                 verify(sdncVnfResources, times(1)).deactivateVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
108         }
109         
110         @Test
111         public void deactivateVnfExceptionTest() throws Exception {
112                 doThrow(RuntimeException.class).when(sdncVnfResources).deactivateVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
113                 expectedException.expect(BpmnError.class);
114                 sdncDeactivateTasks.deactivateVnf(execution);
115         }
116         
117         @Test
118         public void deactivateServiceInstanceTest() throws Exception {
119                 doReturn("response").when(sdncServiceInstanceResources).deactivateServiceInstance(serviceInstance, customer, requestContext);
120
121                 sdncDeactivateTasks.deactivateServiceInstance(execution);
122
123                 verify(sdncServiceInstanceResources, times(1)).deactivateServiceInstance(serviceInstance, customer, requestContext);
124                 assertEquals("response", execution.getVariable("deactivateServiceInstanceSDNCResponse"));
125                 assertTrue(execution.getVariable("sdncServiceInstanceRollback"));
126         }
127         
128         @Test
129         public void deactivateServiceInstanceExceptionTest() throws Exception {
130                 doThrow(RuntimeException.class).when(sdncServiceInstanceResources).deactivateServiceInstance(serviceInstance, customer, requestContext);
131                 expectedException.expect(BpmnError.class);
132                 sdncDeactivateTasks.deactivateServiceInstance(execution);
133         }
134         
135         @Test
136         public void test_deactivateNetwork() throws Exception {
137                 String expectedResponse = "return";
138                 
139                 doReturn(expectedResponse).when(sdncNetworkResources).deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
140                 
141                 sdncDeactivateTasks.deactivateNetwork(execution);
142                 
143                 verify(sdncNetworkResources, times(1)).deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
144                 
145                 assertEquals(expectedResponse, execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.response"));
146                 
147                 assertTrue(execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.rollback"));
148         }
149         
150         @Test
151         public void test_deactivateNetwork_exception() throws Exception {
152                 expectedException.expect(BpmnError.class);
153                 
154                 try {
155                         doThrow(RuntimeException.class).when(extractPojosForBB).extractByKey(any(),ArgumentMatchers.eq(ResourceKey.NETWORK_ID), any());
156                         
157                         sdncDeactivateTasks.deactivateNetwork(execution);
158                 } finally {
159                         verify(sdncNetworkResources, times(0)).deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
160                         
161                         assertNull(execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.response"));
162                         
163                         assertFalse(execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.rollback"));
164                 }
165         }
166
167 }