32c285b0fe6c29922393a0b499fbc697561115aa
[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.Mockito.doReturn;
27
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.Optional;
31
32 import org.camunda.bpm.engine.delegate.BpmnError;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.onap.so.bpmn.BaseTaskTest;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
38 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
39 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
40 import org.onap.so.client.aai.entities.AAIResultWrapper;
41 import org.springframework.beans.factory.annotation.Autowired;
42
43 public class UnassignNetworkBBTest extends BaseTaskTest {
44         
45         @Mock
46         private NetworkBBUtils networkBBUtils;
47         @Mock
48         private ExtractPojosForBB extractPojosForBB;
49         @Autowired
50         private UnassignNetworkBB unassignNetworkBB;
51         
52         private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/Network/";  
53         private L3Network network;
54         
55         @Test
56         public void checkRelationshipRelatedToTrueTest() throws Exception {
57                 expectedException.expect(BpmnError.class);
58                 network = setL3Network();
59                 network.setNetworkId("testNetworkId1");
60                 final String aaiResponse = new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "unassignNetworkBB_queryAAIResponse_.json")));
61                 AAIResultWrapper aaiResultWrapper = new AAIResultWrapper(aaiResponse); 
62                 Optional<org.onap.aai.domain.yang.L3Network> l3network = aaiResultWrapper.asBean(org.onap.aai.domain.yang.L3Network.class);
63                 doReturn(network).when(extractPojosForBB).extractByKey(execution, ResourceKey.NETWORK_ID, "testNetworkId1");
64                 doReturn(aaiResultWrapper).when(aaiNetworkResources).queryNetworkWrapperById(network);
65                 doReturn(true).when(networkBBUtils).isRelationshipRelatedToExists(l3network, "vf-module");
66                 unassignNetworkBB.checkRelationshipRelatedTo(execution, "vf-module");
67                 assertThat(execution.getVariable("ErrorUnassignNetworkBB"), notNullValue());
68         }       
69         
70         @Test
71         public void getCloudSdncRegion25Test() throws Exception {
72                 CloudRegion cloudRegion = setCloudRegion();
73                 cloudRegion.setCloudRegionVersion("2.5");
74                 doReturn("AAIAIC25").when(networkBBUtils).getCloudRegion(execution, SourceSystem.SDNC);
75                 unassignNetworkBB.getCloudSdncRegion(execution);
76                 assertEquals("AAIAIC25", execution.getVariable("cloudRegionSdnc"));
77         }       
78         
79         @Test
80         public void getCloudSdncRegion30Test() throws Exception {
81                 CloudRegion cloudRegion = setCloudRegion();
82                 cloudRegion.setCloudRegionVersion("3.0");
83                 gBBInput.setCloudRegion(cloudRegion);
84                 doReturn(cloudRegion.getLcpCloudRegionId()).when(networkBBUtils).getCloudRegion(execution, SourceSystem.SDNC);
85                 unassignNetworkBB.getCloudSdncRegion(execution);
86                 assertEquals(cloudRegion.getLcpCloudRegionId(), execution.getVariable("cloudRegionSdnc"));
87         }       
88         
89         @Test
90         public void errorEncounteredTest_rollback() throws Exception {
91                 expectedException.expect(BpmnError.class);
92                 execution.setVariable("ErrorUnassignNetworkBB", "Relationship's RelatedTo still exists in AAI, remove the relationship vf-module first.");
93                 execution.setVariable("isRollbackNeeded", true);
94                 unassignNetworkBB.errorEncountered(execution);
95         }
96         
97         @Test
98         public void errorEncounteredTest_noRollback() throws Exception {
99                 expectedException.expect(BpmnError.class);
100                 execution.setVariable("ErrorUnassignNetworkBB", "Relationship's RelatedTo still exists in AAI, remove the relationship vf-module first.");
101                 unassignNetworkBB.errorEncountered(execution);
102         }       
103 }