ffd4f74b6311114dfdf818aa1dcf02592b99f4a0
[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.ArgumentMatchers.isA;
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 java.net.URI;
33 import org.camunda.bpm.engine.delegate.BpmnError;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.ArgumentMatchers;
37 import org.mockito.InjectMocks;
38 import org.onap.sdnc.northbound.client.model.GenericResourceApiNetworkOperationInformation;
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 SDNCActivateTaskTest extends BaseTaskTest {
56
57     @InjectMocks
58     private SDNCActivateTasks sdncActivateTasks = new SDNCActivateTasks();
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 Customer customer;
67
68     @Before
69     public void before() throws BBObjectNotFoundException {
70         serviceInstance = setServiceInstance();
71         network = setL3Network();
72         genericVnf = setGenericVnf();
73         vfModule = setVfModule();
74         cloudRegion = setCloudRegion();
75         requestContext = setRequestContext();
76         customer = setCustomer();
77
78         customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
79         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
80                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
81         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
82                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
83         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
84                 .thenReturn(genericVnf);
85         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
86         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.NETWORK_ID))).thenReturn(network);
87         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID)))
88                 .thenReturn(serviceInstance);
89         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID)))
90                 .thenReturn(serviceInstance);
91
92         when(env.getRequiredProperty("mso.workflow.message.endpoint")).thenReturn("http://localhost:9090");
93     }
94
95     @Test
96     public void activateVnfTest() throws Exception {
97         doReturn(new GenericResourceApiVnfOperationInformation()).when(sdncVnfResources).activateVnf(genericVnf,
98                 serviceInstance, customer, cloudRegion, requestContext);
99         sdncActivateTasks.activateVnf(execution);
100         verify(sdncVnfResources, times(1)).activateVnf(genericVnf, serviceInstance, customer, cloudRegion,
101                 requestContext);
102         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
103         assertEquals(SDNCTopology.VNF, sdncRequest.getTopology());
104     }
105
106     @Test
107     public void activateVnfTestException() throws Exception {
108         expectedException.expect(BpmnError.class);
109         doThrow(RuntimeException.class).when(sdncVnfResources).activateVnf(genericVnf, serviceInstance, customer,
110                 cloudRegion, requestContext);
111         sdncActivateTasks.activateVnf(execution);
112     }
113
114     @Test
115     public void activateNetworkTest() throws Exception {
116         doReturn(new GenericResourceApiNetworkOperationInformation()).when(sdncNetworkResources).activateNetwork(
117                 isA(L3Network.class), isA(ServiceInstance.class), isA(Customer.class), isA(RequestContext.class),
118                 isA(CloudRegion.class));
119         sdncActivateTasks.activateNetwork(execution);
120         verify(sdncNetworkResources, times(1)).activateNetwork(network, serviceInstance, customer, requestContext,
121                 cloudRegion);
122         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
123         assertEquals(SDNCTopology.NETWORK, sdncRequest.getTopology());
124     }
125
126     @Test
127     public void activateNetworkExceptionTest() throws Exception {
128         expectedException.expect(BpmnError.class);
129         doThrow(RuntimeException.class).when(sdncNetworkResources).activateNetwork(isA(L3Network.class),
130                 isA(ServiceInstance.class), isA(Customer.class), isA(RequestContext.class), isA(CloudRegion.class));
131         sdncActivateTasks.activateNetwork(execution);
132     }
133
134     @Test
135     public void activateVfModuleTest() throws Exception {
136         doReturn(new GenericResourceApiVfModuleOperationInformation()).when(sdncVfModuleResources).activateVfModule(
137                 eq(vfModule), eq(genericVnf), eq(serviceInstance), eq(customer), eq(cloudRegion), eq(requestContext),
138                 any(URI.class));
139         sdncActivateTasks.activateVfModule(execution);
140         verify(sdncVfModuleResources, times(1)).activateVfModule(eq(vfModule), eq(genericVnf), eq(serviceInstance),
141                 eq(customer), eq(cloudRegion), eq(requestContext), any(URI.class));
142         SDNCRequest sdncRequest = execution.getVariable("SDNCRequest");
143         assertEquals(SDNCTopology.VFMODULE, sdncRequest.getTopology());
144     }
145
146     @Test
147     public void activateVfModuleTestException() throws Exception {
148         expectedException.expect(BpmnError.class);
149         doThrow(RuntimeException.class).when(sdncVfModuleResources).activateVfModule(eq(vfModule), eq(genericVnf),
150                 eq(serviceInstance), eq(customer), eq(cloudRegion), eq(requestContext), any(URI.class));
151         sdncActivateTasks.activateVfModule(execution);
152     }
153 }