7d8e94d1e9ca3cc10ca0edb6023a7b7758e38d35
[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 org.camunda.bpm.engine.delegate.BpmnError;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentMatchers;
35 import org.mockito.InjectMocks;
36 import org.onap.sdnc.northbound.client.model.GenericResourceApiNetworkOperationInformation;
37 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
38 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfModuleOperationInformation;
39 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfOperationInformation;
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.bbobjects.VolumeGroup;
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
56 public class SDNCAssignTasksTest extends BaseTaskTest {
57     @InjectMocks
58     private SDNCAssignTasks sdncAssignTasks = new SDNCAssignTasks();
59
60     private L3Network network;
61     private ServiceInstance serviceInstance;
62     private RequestContext requestContext;
63     private CloudRegion cloudRegion;
64     private GenericVnf genericVnf;
65     private VfModule vfModule;
66     private VolumeGroup volumeGroup;
67     private Customer customer;
68
69     @Before
70     public void before() throws BBObjectNotFoundException {
71         customer = setCustomer();
72         serviceInstance = setServiceInstance();
73         network = setL3Network();
74         cloudRegion = setCloudRegion();
75         requestContext = setRequestContext();
76         genericVnf = setGenericVnf();
77         vfModule = setVfModule();
78         volumeGroup = setVolumeGroup();
79
80         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
81                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
82         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
83                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
84         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
85                 .thenReturn(genericVnf);
86         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
87         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.NETWORK_ID))).thenReturn(network);
88         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID)))
89                 .thenReturn(serviceInstance);
90         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VOLUME_GROUP_ID)))
91                 .thenReturn(volumeGroup);
92     }
93
94     @Test
95     public void assignServiceInstanceTest() throws Exception {
96         doReturn(new GenericResourceApiServiceOperationInformation()).when(sdncServiceInstanceResources)
97                 .assignServiceInstance(serviceInstance, customer, requestContext);
98         sdncAssignTasks.assignServiceInstance(execution);
99         verify(sdncServiceInstanceResources, times(1)).assignServiceInstance(serviceInstance, customer, requestContext);
100         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
101         assertEquals(SDNCTopology.SERVICE, sdncRequest.getTopology());
102     }
103
104     @Test
105     public void assignServiceInstanceExceptionTest() throws Exception {
106         expectedException.expect(BpmnError.class);
107         doThrow(RuntimeException.class).when(sdncServiceInstanceResources).assignServiceInstance(serviceInstance,
108                 customer, requestContext);
109         sdncAssignTasks.assignServiceInstance(execution);
110     }
111
112     @Test
113     public void assignVnfTest() throws Exception {
114         doReturn(new GenericResourceApiVnfOperationInformation()).when(sdncVnfResources).assignVnf(genericVnf,
115                 serviceInstance, customer, cloudRegion, requestContext, false);
116         execution.setVariable("generalBuildingBlock", gBBInput);
117         sdncAssignTasks.assignVnf(execution);
118         verify(sdncVnfResources, times(1)).assignVnf(genericVnf, serviceInstance, customer, cloudRegion, requestContext,
119                 false);
120         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
121         assertEquals(SDNCTopology.VNF, sdncRequest.getTopology());
122     }
123
124     @Test
125     public void assignVnfExceptionTest() throws Exception {
126         expectedException.expect(BpmnError.class);
127         doThrow(RuntimeException.class).when(sdncVnfResources).assignVnf(genericVnf, serviceInstance, customer,
128                 cloudRegion, requestContext, false);
129         sdncAssignTasks.assignVnf(execution);
130     }
131
132     @Test
133     public void assignVfModuleTest() throws Exception {
134         doReturn(new GenericResourceApiVfModuleOperationInformation()).when(sdncVfModuleResources).assignVfModule(
135                 vfModule, volumeGroup, genericVnf, serviceInstance, customer, cloudRegion, requestContext);
136         sdncAssignTasks.assignVfModule(execution);
137         verify(sdncVfModuleResources, times(1)).assignVfModule(vfModule, volumeGroup, genericVnf, serviceInstance,
138                 customer, cloudRegion, requestContext);
139         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
140         assertEquals(SDNCTopology.VFMODULE, sdncRequest.getTopology());
141     }
142
143     @Test
144     public void assignVfModuleExceptionTest() throws Exception {
145         expectedException.expect(BpmnError.class);
146         doThrow(RuntimeException.class).when(sdncVfModuleResources).assignVfModule(vfModule, volumeGroup, genericVnf,
147                 serviceInstance, customer, cloudRegion, requestContext);
148         sdncAssignTasks.assignVfModule(execution);
149     }
150
151     @Test
152     public void assignNetworkTest() throws Exception {
153         doReturn(new GenericResourceApiNetworkOperationInformation()).when(sdncNetworkResources).assignNetwork(network,
154                 serviceInstance, customer, requestContext, cloudRegion);
155         sdncAssignTasks.assignNetwork(execution);
156         verify(sdncNetworkResources, times(1)).assignNetwork(network, serviceInstance, customer, requestContext,
157                 cloudRegion);
158         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
159         assertEquals(SDNCTopology.NETWORK, sdncRequest.getTopology());
160     }
161
162     @Test
163     public void assignNetworkExceptionTest() throws Exception {
164         expectedException.expect(BpmnError.class);
165         doThrow(RuntimeException.class).when(sdncNetworkResources).assignNetwork(network, serviceInstance, customer,
166                 requestContext, cloudRegion);
167         sdncAssignTasks.assignNetwork(execution);
168     }
169 }