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