423029c59b7c2dc1233d493495fe80a522f8dae1
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / CreateNetworkCollectionTest.java
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 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.doNothing;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
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.so.bpmn.BaseTaskTest;
39 import org.onap.so.bpmn.common.BuildingBlockExecution;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
43 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
44 import org.onap.so.bpmn.servicedecomposition.generalobjects.OrchestrationContext;
45 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoInstanceGroup;
46 import org.onap.so.client.exception.BBObjectNotFoundException;
47
48 public class CreateNetworkCollectionTest extends BaseTaskTest{
49         @InjectMocks
50         private CreateNetworkCollection createNetworkCollection = new CreateNetworkCollection();
51         
52         private L3Network network;
53         private ServiceInstance serviceInstance;
54         private OrchestrationContext orchestrationContext;
55         private CloudRegion cloudRegion;
56         
57         @Before
58         public void before() throws BBObjectNotFoundException {
59                 serviceInstance = setServiceInstance();
60                 network = setL3Network();
61                 cloudRegion = setCloudRegion();
62                 
63                 List<L3Network> l3NetworkList = new ArrayList<L3Network>();
64                 l3NetworkList.add(network);
65                 ModelInfoInstanceGroup modelInfoInstanceGroup = new ModelInfoInstanceGroup();
66                 modelInfoInstanceGroup.setFunction("function");
67                 serviceInstance.getCollection().getInstanceGroup().setModelInfoInstanceGroup(modelInfoInstanceGroup);
68                 
69                 orchestrationContext = setOrchestrationContext();
70                 orchestrationContext.setIsRollbackEnabled(true);
71                 
72                 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
73                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.NETWORK_ID))).thenReturn(network);
74                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID))).thenReturn(serviceInstance);
75         }
76         
77         @Test
78         public void buildCreateNetworkRequestTest() throws Exception {
79                 createNetworkCollection.buildNetworkCollectionName(execution);
80                 
81                 assertEquals(serviceInstance.getServiceInstanceName() + "_" + serviceInstance.getCollection().getInstanceGroup().getModelInfoInstanceGroup().getFunction(), execution.getVariable("networkCollectionName"));
82         }
83         
84         @Test(expected = BpmnError.class)
85         public void buildCreateNetworkRequestInstanceGroupModelInfoFunctionNullExceptionTest() throws Exception {
86                 ModelInfoInstanceGroup modelInfoInstanceGroup = new ModelInfoInstanceGroup();
87                 serviceInstance.getCollection().getInstanceGroup().setModelInfoInstanceGroup(modelInfoInstanceGroup);
88                 createNetworkCollection.buildNetworkCollectionName(execution);
89         }
90         
91         @Test(expected = BpmnError.class)
92         public void buildCreateNetworkRequestInstanceGroupModelInfoNullTest() throws Exception {
93                 serviceInstance.getCollection().getInstanceGroup().setModelInfoInstanceGroup(null);
94                 createNetworkCollection.buildNetworkCollectionName(execution);
95         }
96         
97         @Test
98         public void connectCollectionToInstanceGroupTest() throws Exception {
99                 doNothing().when(aaiNetworkResources).connectNetworkCollectionInstanceGroupToNetworkCollection(serviceInstance.getCollection().getInstanceGroup(), serviceInstance.getCollection());
100                 createNetworkCollection.connectCollectionToInstanceGroup(execution);
101                 verify(aaiNetworkResources, times(1)).connectNetworkCollectionInstanceGroupToNetworkCollection(serviceInstance.getCollection().getInstanceGroup(), serviceInstance.getCollection());
102         }
103         
104         @Test
105         public void connectCollectionToServiceInstanceTest() throws Exception {
106                 doNothing().when(aaiNetworkResources).connectNetworkCollectionToServiceInstance(serviceInstance.getCollection(), serviceInstance);
107                 createNetworkCollection.connectCollectionToServiceInstance(execution);
108                 verify(aaiNetworkResources, times(1)).connectNetworkCollectionToServiceInstance(serviceInstance.getCollection(), serviceInstance);
109         }
110         
111         @Test
112         public void connectInstanceGroupToCloudRegionTest() throws Exception {
113                 doNothing().when(aaiNetworkResources).connectInstanceGroupToCloudRegion(serviceInstance.getCollection().getInstanceGroup(), cloudRegion);
114                 createNetworkCollection.connectInstanceGroupToCloudRegion(execution);
115                 verify(aaiNetworkResources, times(1)).connectInstanceGroupToCloudRegion(serviceInstance.getCollection().getInstanceGroup(), cloudRegion);
116         }
117 }