d8a1b0182e6bba8d2712d116ab1965997b2bba7f
[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.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import java.net.URI;
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 SDNCDeactivateTaskTest extends BaseTaskTest {
56     @InjectMocks
57     private SDNCDeactivateTasks sdncDeactivateTasks = new SDNCDeactivateTasks();
58
59     private ServiceInstance serviceInstance;
60     private CloudRegion cloudRegion;
61     private RequestContext requestContext;
62     private GenericVnf genericVnf;
63     private VfModule vfModule;
64     private L3Network network;
65     private Customer customer;
66
67     @Before
68     public void before() throws BBObjectNotFoundException {
69         customer = setCustomer();
70         serviceInstance = setServiceInstance();
71         cloudRegion = setCloudRegion();
72         requestContext = setRequestContext();
73         genericVnf = setGenericVnf();
74         vfModule = setVfModule();
75         network = setL3Network();
76
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         when(env.getRequiredProperty("mso.workflow.message.endpoint")).thenReturn("http://localhost:9090");
86     }
87
88     @Test
89     public void deactivateVfModuleTest() throws Exception {
90         doReturn(new GenericResourceApiVfModuleOperationInformation()).when(sdncVfModuleResources).deactivateVfModule(
91                 eq(vfModule), eq(genericVnf), eq(serviceInstance), eq(customer), eq(cloudRegion), eq(requestContext),
92                 any(URI.class));
93         sdncDeactivateTasks.deactivateVfModule(execution);
94         verify(sdncVfModuleResources, times(1)).deactivateVfModule(eq(vfModule), eq(genericVnf), eq(serviceInstance),
95                 eq(customer), eq(cloudRegion), eq(requestContext), any(URI.class));
96         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
97         assertEquals(SDNCTopology.VFMODULE, sdncRequest.getTopology());
98     }
99
100     @Test
101     public void deactivateVfModuleExceptionTest() throws Exception {
102         expectedException.expect(BpmnError.class);
103         doThrow(RuntimeException.class).when(sdncVfModuleResources).deactivateVfModule(eq(vfModule), eq(genericVnf),
104                 eq(serviceInstance), eq(customer), eq(cloudRegion), eq(requestContext), any(URI.class));
105         sdncDeactivateTasks.deactivateVfModule(execution);
106     }
107
108     @Test
109     public void deactivateVnfTest() throws Exception {
110         doReturn(new GenericResourceApiVnfOperationInformation()).when(sdncVnfResources).deactivateVnf(genericVnf,
111                 serviceInstance, customer, cloudRegion, requestContext);
112         sdncDeactivateTasks.deactivateVnf(execution);
113         verify(sdncVnfResources, times(1)).deactivateVnf(genericVnf, serviceInstance, customer, cloudRegion,
114                 requestContext);
115         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
116         assertEquals(SDNCTopology.VNF, sdncRequest.getTopology());
117     }
118
119     @Test
120     public void deactivateVnfExceptionTest() throws Exception {
121         doThrow(RuntimeException.class).when(sdncVnfResources).deactivateVnf(genericVnf, serviceInstance, customer,
122                 cloudRegion, requestContext);
123         expectedException.expect(BpmnError.class);
124         sdncDeactivateTasks.deactivateVnf(execution);
125     }
126
127     @Test
128     public void deactivateServiceInstanceTest() throws Exception {
129         doReturn(new GenericResourceApiServiceOperationInformation()).when(sdncServiceInstanceResources)
130                 .deactivateServiceInstance(serviceInstance, customer, requestContext);
131         sdncDeactivateTasks.deactivateServiceInstance(execution);
132         verify(sdncServiceInstanceResources, times(1)).deactivateServiceInstance(serviceInstance, customer,
133                 requestContext);
134         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
135         assertEquals(SDNCTopology.SERVICE, sdncRequest.getTopology());
136     }
137
138     @Test
139     public void deactivateServiceInstanceExceptionTest() throws Exception {
140         doThrow(RuntimeException.class).when(sdncServiceInstanceResources).deactivateServiceInstance(serviceInstance,
141                 customer, requestContext);
142         expectedException.expect(BpmnError.class);
143         sdncDeactivateTasks.deactivateServiceInstance(execution);
144     }
145
146     @Test
147     public void test_deactivateNetwork() throws Exception {
148         doReturn(new GenericResourceApiNetworkOperationInformation()).when(sdncNetworkResources)
149                 .deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
150         sdncDeactivateTasks.deactivateNetwork(execution);
151         verify(sdncNetworkResources, times(1)).deactivateNetwork(network, serviceInstance, customer, requestContext,
152                 cloudRegion);
153         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
154         assertEquals(SDNCTopology.NETWORK, sdncRequest.getTopology());
155     }
156
157     @Test
158     public void test_deactivateNetwork_exception() throws Exception {
159         expectedException.expect(BpmnError.class);
160         doThrow(RuntimeException.class).when(extractPojosForBB).extractByKey(any(),
161                 ArgumentMatchers.eq(ResourceKey.NETWORK_ID));
162         sdncDeactivateTasks.deactivateNetwork(execution);
163         verify(sdncNetworkResources, times(0)).deactivateNetwork(network, serviceInstance, customer, requestContext,
164                 cloudRegion);
165     }
166 }