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