2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.sdnc.tasks;
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;
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;
53 public class SDNCDeactivateTaskTest extends BaseTaskTest {
55 private SDNCDeactivateTasks sdncDeactivateTasks = new SDNCDeactivateTasks();
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;
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();
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);
84 public void deactivateVfModuleTest() throws Exception {
85 doReturn("success").when(sdncVfModuleResources).deactivateVfModule(vfModule, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
87 sdncDeactivateTasks.deactivateVfModule(execution);
89 verify(sdncVfModuleResources, times(1)).deactivateVfModule(vfModule, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
93 public void deactivateVfModuleExceptionTest() throws Exception {
94 expectedException.expect(BpmnError.class);
96 doThrow(RuntimeException.class).when(sdncVfModuleResources).deactivateVfModule(vfModule, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
98 sdncDeactivateTasks.deactivateVfModule(execution);
102 public void deactivateVnfTest() throws Exception {
103 doReturn("success").when(sdncVnfResources).deactivateVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
105 sdncDeactivateTasks.deactivateVnf(execution);
107 verify(sdncVnfResources, times(1)).deactivateVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
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);
118 public void deactivateServiceInstanceTest() throws Exception {
119 doReturn("response").when(sdncServiceInstanceResources).deactivateServiceInstance(serviceInstance, customer, requestContext);
121 sdncDeactivateTasks.deactivateServiceInstance(execution);
123 verify(sdncServiceInstanceResources, times(1)).deactivateServiceInstance(serviceInstance, customer, requestContext);
124 assertEquals("response", execution.getVariable("deactivateServiceInstanceSDNCResponse"));
125 assertTrue(execution.getVariable("sdncServiceInstanceRollback"));
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);
136 public void test_deactivateNetwork() throws Exception {
137 String expectedResponse = "return";
139 doReturn(expectedResponse).when(sdncNetworkResources).deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
141 sdncDeactivateTasks.deactivateNetwork(execution);
143 verify(sdncNetworkResources, times(1)).deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
145 assertEquals(expectedResponse, execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.response"));
147 assertTrue(execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.rollback"));
151 public void test_deactivateNetwork_exception() throws Exception {
152 expectedException.expect(BpmnError.class);
155 doThrow(RuntimeException.class).when(extractPojosForBB).extractByKey(any(),ArgumentMatchers.eq(ResourceKey.NETWORK_ID), any());
157 sdncDeactivateTasks.deactivateNetwork(execution);
159 verify(sdncNetworkResources, times(0)).deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
161 assertNull(execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.response"));
163 assertFalse(execution.getVariable("SDNCDeactivateTasks.deactivateNetwork.rollback"));