07f03b592bfdacff926c951e468a5641e1628324
[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.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;
34
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;
53
54 public class SDNCUnassignTasksTest extends BaseTaskTest{
55         @InjectMocks
56         private SDNCUnassignTasks sdncUnassignTasks = new SDNCUnassignTasks();
57         
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;
65         
66         @Before
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);
80         }
81         
82         @Test
83         public void unassignServiceInstanceTest() throws Exception {
84                 doReturn("test").when(sdncServiceInstanceResources).unassignServiceInstance(serviceInstance, customer, requestContext);
85                 
86                 sdncUnassignTasks.unassignServiceInstance(execution);
87                 
88                 verify(sdncServiceInstanceResources, times(1)).unassignServiceInstance(serviceInstance, customer, requestContext);
89         }
90
91
92
93         @Test
94         public void unassignServiceInstanceExceptionTest() throws Exception {
95                 expectedException.expect(BpmnError.class);
96                 
97                 doThrow(RuntimeException.class).when(sdncServiceInstanceResources).unassignServiceInstance(serviceInstance, customer, requestContext);
98                 
99                 sdncUnassignTasks.unassignServiceInstance(execution);
100         }       
101                 
102         @Test
103         public void unassignVfModuleTest() throws Exception {
104                 doReturn("response").when(sdncVfModuleResources).unassignVfModule(vfModule, genericVnf, serviceInstance);
105
106                 sdncUnassignTasks.unassignVfModule(execution);
107
108                 verify(sdncVfModuleResources, times(1)).unassignVfModule(vfModule, genericVnf, serviceInstance);
109                 assertEquals("response", execution.getVariable("SDNCResponse"));
110         }
111         
112
113         
114
115         @Test
116         public void unassignVfModuleExceptionTest() throws Exception {
117                 expectedException.expect(BpmnError.class);
118                 
119                 doThrow(RuntimeException.class).when(sdncVfModuleResources).unassignVfModule(vfModule, genericVnf, serviceInstance);
120
121                 sdncUnassignTasks.unassignVfModule(execution);
122         }
123         
124         @Test
125         public void unassignVnfTest() throws Exception {
126                 doReturn("response").when(sdncVnfResources).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
127
128                 sdncUnassignTasks.unassignVnf(execution);
129
130                 verify(sdncVnfResources, times(1)).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
131                 assertTrue(execution.getVariable("sdncUnassignVnfResponse").equals("response"));
132         }
133         
134         
135         @Test
136         public void unassignVnfExceptionTest() throws Exception {
137                 expectedException.expect(BpmnError.class);
138                 doThrow(RuntimeException.class).when(sdncVnfResources).unassignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext);
139                 sdncUnassignTasks.unassignVnf(execution);
140         }
141
142         @Test
143         public void unassignNetworkTest() throws Exception {
144                 String cloudRegionSdnc = "AAIAIC25";
145                 
146                 cloudRegion.setCloudRegionVersion("2.5");
147                 
148                 execution.setVariable("cloudRegionSdnc", cloudRegionSdnc);
149                 
150                 doReturn("response").when(sdncNetworkResources).unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
151                 
152                 assertNotEquals(cloudRegionSdnc, cloudRegion.getLcpCloudRegionId());
153                 sdncUnassignTasks.unassignNetwork(execution);
154
155                 verify(sdncNetworkResources, times(1)).unassignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
156                 assertEquals("response", execution.getVariable("SDNCUnAssignNetworkResponse"));
157                 assertEquals(cloudRegionSdnc, cloudRegion.getLcpCloudRegionId());
158         }
159 }