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.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;
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;
54 public class SDNCUnassignTasksTest extends BaseTaskTest{
56 private SDNCUnassignTasks sdncUnassignTasks = new SDNCUnassignTasks();
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;
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);
83 public void unassignServiceInstanceTest() throws Exception {
84 doReturn("test").when(sdncServiceInstanceResources).unassignServiceInstance(serviceInstance, customer, requestContext);
86 sdncUnassignTasks.unassignServiceInstance(execution);
88 verify(sdncServiceInstanceResources, times(1)).unassignServiceInstance(serviceInstance, customer, requestContext);
92 public void unassignServiceInstanceTest_inventoried() throws Exception {
93 doReturn("test").when(sdncServiceInstanceResources).unassignServiceInstance(serviceInstance, customer, requestContext);
95 serviceInstance.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
97 sdncUnassignTasks.unassignServiceInstance(execution);
99 verify(sdncServiceInstanceResources, times(0)).unassignServiceInstance(serviceInstance, customer, requestContext);
103 public void unassignServiceInstanceExceptionTest() throws Exception {
104 expectedException.expect(BpmnError.class);
106 doThrow(RuntimeException.class).when(sdncServiceInstanceResources).unassignServiceInstance(serviceInstance, customer, requestContext);
108 sdncUnassignTasks.unassignServiceInstance(execution);
112 public void unassignVfModuleTest() throws Exception {
113 doReturn("response").when(sdncVfModuleResources).unassignVfModule(vfModule, genericVnf, serviceInstance);
115 sdncUnassignTasks.unassignVfModule(execution);
117 verify(sdncVfModuleResources, times(1)).unassignVfModule(vfModule, genericVnf, serviceInstance);
118 assertEquals("response", execution.getVariable("SDNCResponse"));
122 public void unassignVfModuleTest_inventoried() throws Exception {
123 vfModule.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
125 sdncUnassignTasks.unassignVfModule(execution);
127 verify(sdncVfModuleResources, times(0)).unassignVfModule(vfModule, genericVnf, serviceInstance);
128 assertNull(execution.getVariable("SDNCResponse"));
132 public void unassignVfModuleTest_pendingCreate() throws Exception {
133 vfModule.setOrchestrationStatus(OrchestrationStatus.PENDING_CREATE);
135 sdncUnassignTasks.unassignVfModule(execution);
137 verify(sdncVfModuleResources, times(0)).unassignVfModule(vfModule, genericVnf, serviceInstance);
138 assertNull(execution.getVariable("SDNCResponse"));
142 public void unassignVfModuleExceptionTest() throws Exception {
143 expectedException.expect(BpmnError.class);
145 doThrow(RuntimeException.class).when(sdncVfModuleResources).unassignVfModule(vfModule, genericVnf, serviceInstance);
147 sdncUnassignTasks.unassignVfModule(execution);
151 public void unassignVnfTest() throws Exception {
152 doReturn("response").when(sdncVnfResources).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
154 sdncUnassignTasks.unassignVnf(execution);
156 verify(sdncVnfResources, times(1)).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
157 assertTrue(execution.getVariable("sdncUnassignVnfResponse").equals("response"));
161 public void unassignVnfTest_inventoried() throws Exception {
162 genericVnf.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
164 sdncUnassignTasks.unassignVnf(execution);
166 verify(sdncVnfResources, times(0)).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
167 assertNull(execution.getVariable("sdncUnassignVnfResponse"));
171 public void unassignVnfTest_created() throws Exception {
172 genericVnf.setOrchestrationStatus(OrchestrationStatus.CREATED);
174 sdncUnassignTasks.unassignVnf(execution);
176 verify(sdncVnfResources, times(0)).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
177 assertNull(execution.getVariable("sdncUnassignVnfResponse"));
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);
188 public void unassignNetworkTest() throws Exception {
189 String cloudRegionSdnc = "AAIAIC25";
191 cloudRegion.setCloudRegionVersion("2.5");
193 execution.setVariable("cloudRegionSdnc", cloudRegionSdnc);
195 doReturn("response").when(sdncNetworkResources).unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
197 assertNotEquals(cloudRegionSdnc, cloudRegion.getLcpCloudRegionId());
198 sdncUnassignTasks.unassignNetwork(execution);
200 verify(sdncNetworkResources, times(1)).unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
201 assertEquals("response", execution.getVariable("SDNCUnAssignNetworkResponse"));
202 assertEquals(cloudRegionSdnc, cloudRegion.getLcpCloudRegionId());
206 public void unassignNetworkTest_inventoried() throws Exception {
207 network.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
209 sdncUnassignTasks.unassignNetwork(execution);
211 verify(sdncNetworkResources, times(0)).unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
212 assertNull(execution.getVariable("SDNCUnAssignNetworkResponse"));