2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.holmes.common.aai;
18 import static org.hamcrest.CoreMatchers.equalTo;
19 import static org.hamcrest.MatcherAssert.assertThat;
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.junit.runner.RunWith;
28 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
29 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
30 import org.onap.holmes.common.aai.entity.VmEntity;
31 import org.onap.holmes.common.exception.CorrelationException;
32 import org.powermock.api.easymock.PowerMock;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35 import org.powermock.reflect.Whitebox;
37 @PrepareForTest({CorrelationUtil.class, AaiQuery.class})
38 @RunWith(PowerMockRunner.class)
39 public class CorrelationUtilTest {
42 public ExpectedException thrown = ExpectedException.none();
44 private CorrelationUtil correlationUtil;
45 private AaiQuery aaiQuery;
48 public void testCorrelationUtil() {
49 aaiQuery = PowerMock.createMock(AaiQuery.class);
50 Whitebox.setInternalState(CorrelationUtil.class, "aaiQuery", aaiQuery);
51 correlationUtil = CorrelationUtil.getInstance();
52 PowerMock.replayAll();
56 public void testCorrelationUtil_isTopologicallyRelated_true() throws Exception {
58 VmEntity vmEntity = new VmEntity();
59 List<Relationship> relationships = new ArrayList<>();
61 List<RelationshipData> relationshipDataList = new ArrayList<>();
62 RelationshipData relationshipData = new RelationshipData();
63 relationshipData.setRelationshipKey("vnf-id");
64 relationshipData.setRelationshipValue("123");
65 relationshipDataList.add(relationshipData);
66 Relationship relationship = new Relationship();
67 relationship.setRelationshipDataList(relationshipDataList);
68 relationships.add(relationship);
69 vmEntity.getRelationshipList().setRelationships(relationships);
71 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", "test1", "test2").andReturn(vmEntity).anyTimes();
73 PowerMock.replayAll();
74 boolean actual = Whitebox
75 .invokeMethod(correlationUtil, "isTopologicallyRelated", "123", "test1", "test2");
76 PowerMock.verifyAll();
78 assertThat(actual, equalTo(true));
82 public void testCorrelationUtil_isTopologicalRelated_false() throws Exception {
84 VmEntity vmEntity = new VmEntity();
85 List<Relationship> relationships = new ArrayList<>();
86 List<RelationshipData> relationshipDataList = new ArrayList<>();
87 RelationshipData relationshipData = new RelationshipData();
88 relationshipData.setRelationshipKey("vnf-id");
89 relationshipData.setRelationshipValue("1231");
90 relationshipDataList.add(relationshipData);
91 Relationship relationship = new Relationship();
92 relationship.setRelationshipDataList(relationshipDataList);
93 relationships.add(relationship);
94 vmEntity.getRelationshipList().setRelationships(relationships);
96 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", "test1", "test2").andReturn(vmEntity)
99 PowerMock.replayAll();
100 boolean actual = Whitebox
101 .invokeMethod(correlationUtil, "isTopologicallyRelated", "123", "test1", "test2");
102 PowerMock.verifyAll();
104 assertThat(actual, equalTo(false));
108 public void testCorrelationUtil_isTopologicalRelated_exception_false() throws Exception {
109 PowerMock.resetAll();
110 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", "test1", "test2")
111 .andThrow(new CorrelationException("")).anyTimes();
113 PowerMock.replayAll();
114 boolean actual = Whitebox
115 .invokeMethod(correlationUtil, "isTopologicallyRelated", "123", "test1", "test2");
116 PowerMock.verifyAll();
118 assertThat(actual, equalTo(false));