c6ed1cca7a07eebf16562f033edee016b35d304d
[so.git] /
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.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33 import org.camunda.bpm.engine.delegate.BpmnError;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.ArgumentMatchers;
37 import org.mockito.InjectMocks;
38 import org.onap.sdnc.northbound.client.model.GenericResourceApiNetworkOperationInformation;
39 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
40 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfModuleOperationInformation;
41 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfOperationInformation;
42 import org.onap.so.bpmn.BaseTaskTest;
43 import org.onap.so.bpmn.common.BuildingBlockExecution;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
49 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
50 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
51 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
52 import org.onap.so.client.exception.BBObjectNotFoundException;
53 import org.onap.so.client.sdnc.beans.SDNCRequest;
54 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
55
56 public class SDNCUnassignTasksTest extends BaseTaskTest {
57     @InjectMocks
58     private SDNCUnassignTasks sdncUnassignTasks = new SDNCUnassignTasks();
59
60     private ServiceInstance serviceInstance;
61     private RequestContext requestContext;
62     private GenericVnf genericVnf;
63     private VfModule vfModule;
64     private CloudRegion cloudRegion;
65     private L3Network network;
66     private Customer customer;
67
68     @Before
69     public void before() throws BBObjectNotFoundException {
70         customer = setCustomer();
71         serviceInstance = setServiceInstance();
72         requestContext = setRequestContext();
73         genericVnf = setGenericVnf();
74         vfModule = setVfModule();
75         cloudRegion = setCloudRegion();
76         network = setL3Network();
77         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
78                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
79         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
80                 .thenReturn(genericVnf);
81         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.NETWORK_ID))).thenReturn(network);
82         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
83         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID)))
84                 .thenReturn(serviceInstance);
85     }
86
87     @Test
88     public void unassignServiceInstanceTest() throws Exception {
89         doReturn(new GenericResourceApiServiceOperationInformation()).when(sdncServiceInstanceResources)
90                 .unassignServiceInstance(serviceInstance, customer, requestContext);
91         sdncUnassignTasks.unassignServiceInstance(execution);
92         verify(sdncServiceInstanceResources, times(1)).unassignServiceInstance(serviceInstance, customer,
93                 requestContext);
94         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
95         assertEquals(SDNCTopology.SERVICE, sdncRequest.getTopology());
96     }
97
98
99
100     @Test
101     public void unassignServiceInstanceExceptionTest() throws Exception {
102         expectedException.expect(BpmnError.class);
103         doThrow(RuntimeException.class).when(sdncServiceInstanceResources).unassignServiceInstance(serviceInstance,
104                 customer, requestContext);
105         sdncUnassignTasks.unassignServiceInstance(execution);
106     }
107
108     @Test
109     public void unassignVfModuleTest() throws Exception {
110         doReturn(new GenericResourceApiVfModuleOperationInformation()).when(sdncVfModuleResources)
111                 .unassignVfModule(vfModule, genericVnf, serviceInstance);
112         sdncUnassignTasks.unassignVfModule(execution);
113         verify(sdncVfModuleResources, times(1)).unassignVfModule(vfModule, genericVnf, serviceInstance);
114         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
115         assertEquals(SDNCTopology.VFMODULE, sdncRequest.getTopology());
116     }
117
118
119
120     @Test
121     public void unassignVfModuleExceptionTest() throws Exception {
122         expectedException.expect(BpmnError.class);
123         doThrow(RuntimeException.class).when(sdncVfModuleResources).unassignVfModule(vfModule, genericVnf,
124                 serviceInstance);
125         sdncUnassignTasks.unassignVfModule(execution);
126     }
127
128     @Test
129     public void unassignVnfTest() throws Exception {
130         doReturn(new GenericResourceApiVnfOperationInformation()).when(sdncVnfResources).unassignVnf(genericVnf,
131                 serviceInstance, customer, cloudRegion, requestContext);
132         sdncUnassignTasks.unassignVnf(execution);
133         verify(sdncVnfResources, times(1)).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion,
134                 requestContext);
135         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
136         assertEquals(SDNCTopology.VNF, sdncRequest.getTopology());
137     }
138
139
140     @Test
141     public void unassignVnfExceptionTest() throws Exception {
142         expectedException.expect(BpmnError.class);
143         doThrow(RuntimeException.class).when(sdncVnfResources).unassignVnf(genericVnf, serviceInstance, customer,
144                 cloudRegion, requestContext);
145         sdncUnassignTasks.unassignVnf(execution);
146     }
147
148     @Test
149     public void unassignNetworkTest() throws Exception {
150         String cloudRegionSdnc = "AAIAIC25";
151         cloudRegion.setCloudRegionVersion("2.5");
152         execution.setVariable("cloudRegionSdnc", cloudRegionSdnc);
153         doReturn(new GenericResourceApiNetworkOperationInformation()).when(sdncNetworkResources)
154                 .unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
155         assertNotEquals(cloudRegionSdnc, cloudRegion.getLcpCloudRegionId());
156         sdncUnassignTasks.unassignNetwork(execution);
157         verify(sdncNetworkResources, times(1)).unassignNetwork(network, serviceInstance, customer, requestContext,
158                 cloudRegion);
159         assertEquals(cloudRegionSdnc, cloudRegion.getLcpCloudRegionId());
160         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
161         assertEquals(SDNCTopology.NETWORK, sdncRequest.getTopology());
162     }
163 }