Adding Generic VNF information in ControllerExeuctionBB flow
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / UnassignNetworkBBTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.flowspecific.tasks;
22
23 import static org.hamcrest.CoreMatchers.notNullValue;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.doThrow;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.Optional;
33 import org.camunda.bpm.engine.delegate.BpmnError;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
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.entities.ResourceKey;
43 import org.onap.so.client.aai.entities.AAIResultWrapper;
44
45 public class UnassignNetworkBBTest extends BaseTaskTest {
46
47     @Mock
48     private NetworkBBUtils networkBBUtils;
49
50     @InjectMocks
51     private UnassignNetworkBB unassignNetworkBB = new UnassignNetworkBB();
52
53     private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/Network/";
54     private L3Network network;
55
56     @Before
57     public void setup() {
58         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
59                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
60         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
61                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
62     }
63
64     @Test
65     public void checkRelationshipRelatedToTrueTest() throws Exception {
66         expectedException.expect(BpmnError.class);
67         network = setL3Network();
68         network.setNetworkId("testNetworkId1");
69         final String aaiResponse = new String(
70                 Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "unassignNetworkBB_queryAAIResponse_.json")));
71         AAIResultWrapper aaiResultWrapper = new AAIResultWrapper(aaiResponse);
72         Optional<org.onap.aai.domain.yang.L3Network> l3network =
73                 aaiResultWrapper.asBean(org.onap.aai.domain.yang.L3Network.class);
74
75         doReturn(network).when(extractPojosForBB).extractByKey(execution, ResourceKey.NETWORK_ID);
76         doReturn(aaiResultWrapper).when(aaiNetworkResources).queryNetworkWrapperById(network);
77
78         doReturn(true).when(networkBBUtils).isRelationshipRelatedToExists(any(Optional.class), eq("vf-module"));
79
80         unassignNetworkBB.checkRelationshipRelatedTo(execution, "vf-module");
81         assertThat(execution.getVariable("ErrorUnassignNetworkBB"), notNullValue());
82     }
83
84     @Test
85     public void checkRelationshipRelatedToUnassignNetworkExceptionTest() throws Exception {
86         String msg = "Cannot perform Unassign Network. Network is still related to vf-module";
87         expectedException.expect(BpmnError.class);
88         doReturn(true).when(networkBBUtils).isRelationshipRelatedToExists(any(Optional.class), eq("vf-module"));
89         unassignNetworkBB.checkRelationshipRelatedTo(execution, "vf-module");
90         assertEquals(execution.getVariable("ErrorUnassignNetworkBB"), msg);
91     }
92
93     @Test
94     public void getCloudSdncRegion25Test() throws Exception {
95         CloudRegion cloudRegion = setCloudRegion();
96         cloudRegion.setCloudRegionVersion("2.5");
97         doReturn("AAIAIC25").when(networkBBUtils).getCloudRegion(execution, SourceSystem.SDNC);
98         unassignNetworkBB.getCloudSdncRegion(execution);
99         assertEquals("AAIAIC25", execution.getVariable("cloudRegionSdnc"));
100     }
101
102     @Test
103     public void getCloudSdncRegion30Test() throws Exception {
104         CloudRegion cloudRegion = setCloudRegion();
105         cloudRegion.setCloudRegionVersion("3.0");
106         gBBInput.setCloudRegion(cloudRegion);
107         doReturn(cloudRegion.getLcpCloudRegionId()).when(networkBBUtils).getCloudRegion(execution, SourceSystem.SDNC);
108         unassignNetworkBB.getCloudSdncRegion(execution);
109         assertEquals(cloudRegion.getLcpCloudRegionId(), execution.getVariable("cloudRegionSdnc"));
110     }
111
112     @Test
113     public void errorEncounteredTest_rollback() throws Exception {
114         expectedException.expect(BpmnError.class);
115         execution.setVariable("ErrorUnassignNetworkBB",
116                 "Relationship's RelatedTo still exists in AAI, remove the relationship vf-module first.");
117         execution.setVariable("isRollbackNeeded", true);
118         unassignNetworkBB.errorEncountered(execution);
119     }
120
121     @Test
122     public void errorEncounteredTest_noRollback() throws Exception {
123         expectedException.expect(BpmnError.class);
124         execution.setVariable("ErrorUnassignNetworkBB",
125                 "Relationship's RelatedTo still exists in AAI, remove the relationship vf-module first.");
126         unassignNetworkBB.errorEncountered(execution);
127     }
128 }