Replaced all tabs with spaces in java and pom.xml
[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.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
44 import org.onap.so.client.aai.entities.AAIResultWrapper;
45 import org.springframework.beans.factory.annotation.Autowired;
46
47 public class UnassignNetworkBBTest extends BaseTaskTest {
48
49     @Mock
50     private NetworkBBUtils networkBBUtils;
51
52     @InjectMocks
53     private UnassignNetworkBB unassignNetworkBB = new UnassignNetworkBB();
54
55     private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/Network/";
56     private L3Network network;
57
58     @Before
59     public void setup() {
60         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
61                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
62         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
63                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(String.class));
64     }
65
66     @Test
67     public void checkRelationshipRelatedToTrueTest() throws Exception {
68         expectedException.expect(BpmnError.class);
69         network = setL3Network();
70         network.setNetworkId("testNetworkId1");
71         final String aaiResponse = new String(
72                 Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "unassignNetworkBB_queryAAIResponse_.json")));
73         AAIResultWrapper aaiResultWrapper = new AAIResultWrapper(aaiResponse);
74         Optional<org.onap.aai.domain.yang.L3Network> l3network =
75                 aaiResultWrapper.asBean(org.onap.aai.domain.yang.L3Network.class);
76
77         doReturn(network).when(extractPojosForBB).extractByKey(execution, ResourceKey.NETWORK_ID);
78         doReturn(aaiResultWrapper).when(aaiNetworkResources).queryNetworkWrapperById(network);
79
80         doReturn(true).when(networkBBUtils).isRelationshipRelatedToExists(any(Optional.class), eq("vf-module"));
81
82         unassignNetworkBB.checkRelationshipRelatedTo(execution, "vf-module");
83         assertThat(execution.getVariable("ErrorUnassignNetworkBB"), notNullValue());
84     }
85
86     @Test
87     public void getCloudSdncRegion25Test() throws Exception {
88         CloudRegion cloudRegion = setCloudRegion();
89         cloudRegion.setCloudRegionVersion("2.5");
90         doReturn("AAIAIC25").when(networkBBUtils).getCloudRegion(execution, SourceSystem.SDNC);
91         unassignNetworkBB.getCloudSdncRegion(execution);
92         assertEquals("AAIAIC25", execution.getVariable("cloudRegionSdnc"));
93     }
94
95     @Test
96     public void getCloudSdncRegion30Test() throws Exception {
97         CloudRegion cloudRegion = setCloudRegion();
98         cloudRegion.setCloudRegionVersion("3.0");
99         gBBInput.setCloudRegion(cloudRegion);
100         doReturn(cloudRegion.getLcpCloudRegionId()).when(networkBBUtils).getCloudRegion(execution, SourceSystem.SDNC);
101         unassignNetworkBB.getCloudSdncRegion(execution);
102         assertEquals(cloudRegion.getLcpCloudRegionId(), execution.getVariable("cloudRegionSdnc"));
103     }
104
105     @Test
106     public void errorEncounteredTest_rollback() throws Exception {
107         expectedException.expect(BpmnError.class);
108         execution.setVariable("ErrorUnassignNetworkBB",
109                 "Relationship's RelatedTo still exists in AAI, remove the relationship vf-module first.");
110         execution.setVariable("isRollbackNeeded", true);
111         unassignNetworkBB.errorEncountered(execution);
112     }
113
114     @Test
115     public void errorEncounteredTest_noRollback() throws Exception {
116         expectedException.expect(BpmnError.class);
117         execution.setVariable("ErrorUnassignNetworkBB",
118                 "Relationship's RelatedTo still exists in AAI, remove the relationship vf-module first.");
119         unassignNetworkBB.errorEncountered(execution);
120     }
121 }