modify unit test and aai query
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / CorrelationUtilTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.holmes.common.aai;
17
18 import static org.hamcrest.CoreMatchers.equalTo;
19 import static org.hamcrest.MatcherAssert.assertThat;
20
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;
36
37 @PrepareForTest({CorrelationUtil.class, AaiQuery.class})
38 @RunWith(PowerMockRunner.class)
39 public class CorrelationUtilTest {
40
41     @Rule
42     public ExpectedException thrown = ExpectedException.none();
43
44     private CorrelationUtil correlationUtil;
45     private AaiQuery aaiQuery;
46
47     @Before
48     public void testCorrelationUtil() {
49         aaiQuery = PowerMock.createMock(AaiQuery.class);
50         Whitebox.setInternalState(CorrelationUtil.class, "aaiQuery", aaiQuery);
51         correlationUtil = CorrelationUtil.getInstance();
52         PowerMock.replayAll();
53     }
54
55     @Test
56     public void testCorrelationUtil_isTopologicallyRelated_true() throws Exception {
57         PowerMock.resetAll();
58         VmEntity vmEntity = new VmEntity();
59         List<Relationship> relationships = new ArrayList<>();
60
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);
70
71         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", "test1", "test2").andReturn(vmEntity).anyTimes();
72
73         PowerMock.replayAll();
74         boolean actual = Whitebox
75                 .invokeMethod(correlationUtil, "isTopologicallyRelated", "123", "test1", "test2");
76         PowerMock.verifyAll();
77
78         assertThat(actual, equalTo(true));
79     }
80
81     @Test
82     public void testCorrelationUtil_isTopologicalRelated_false() throws Exception {
83         PowerMock.resetAll();
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);
95
96         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", "test1", "test2").andReturn(vmEntity)
97                 .anyTimes();
98
99         PowerMock.replayAll();
100         boolean actual = Whitebox
101                 .invokeMethod(correlationUtil, "isTopologicallyRelated", "123", "test1", "test2");
102         PowerMock.verifyAll();
103
104         assertThat(actual, equalTo(false));
105     }
106
107     @Test
108     public void testCorrelationUtil_isTopologicalRelated_exception_false() throws Exception {
109         PowerMock.resetAll();
110         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", "test1", "test2")
111                 .andThrow(new CorrelationException("")).anyTimes();
112
113         PowerMock.replayAll();
114         boolean actual = Whitebox
115                 .invokeMethod(correlationUtil, "isTopologicallyRelated", "123", "test1", "test2");
116         PowerMock.verifyAll();
117
118         assertThat(actual, equalTo(false));
119     }
120 }