add httpsutil and modify query from aai
[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         correlationUtil = CorrelationUtil.getInstance();
50         aaiQuery = PowerMock.createMock(AaiQuery.class);
51         Whitebox.setInternalState(correlationUtil, "aaiQuery", aaiQuery);
52     }
53
54     @Test
55     public void testCorrelationUtil_isTopologicallyRelated_true() throws Exception {
56         PowerMock.resetAll();
57         VmEntity vmEntity = new VmEntity();
58         List<Relationship> relationships = new ArrayList<>();
59
60         List<RelationshipData> relationshipDataList = new ArrayList<>();
61         RelationshipData relationshipData = new RelationshipData();
62         relationshipData.setRelationshipKey("vnf-id");
63         relationshipData.setRelationshipValue("123");
64         relationshipDataList.add(relationshipData);
65         Relationship relationship = new Relationship();
66         relationship.setRelationshipDataList(relationshipDataList);
67         relationships.add(relationship);
68         vmEntity.getRelationshipList().setRelationships(relationships);
69
70         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", "test1", "test2").andReturn(vmEntity).anyTimes();
71
72         PowerMock.replayAll();
73         boolean actual = Whitebox
74                 .invokeMethod(correlationUtil, "isTopologicallyRelated", "123", "test1", "test2");
75         PowerMock.verifyAll();
76
77         assertThat(actual, equalTo(true));
78     }
79
80     @Test
81     public void testCorrelationUtil_isTopologicalRelated_false() throws Exception {
82         PowerMock.resetAll();
83         VmEntity vmEntity = new VmEntity();
84         List<Relationship> relationships = new ArrayList<>();
85         List<RelationshipData> relationshipDataList = new ArrayList<>();
86         RelationshipData relationshipData = new RelationshipData();
87         relationshipData.setRelationshipKey("vnf-id");
88         relationshipData.setRelationshipValue("1231");
89         relationshipDataList.add(relationshipData);
90         Relationship relationship = new Relationship();
91         relationship.setRelationshipDataList(relationshipDataList);
92         relationships.add(relationship);
93         vmEntity.getRelationshipList().setRelationships(relationships);
94
95         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", "test1", "test2").andReturn(vmEntity)
96                 .anyTimes();
97
98         PowerMock.replayAll();
99         boolean actual = Whitebox
100                 .invokeMethod(correlationUtil, "isTopologicallyRelated", "123", "test1", "test2");
101         PowerMock.verifyAll();
102
103         assertThat(actual, equalTo(false));
104     }
105
106     @Test
107     public void testCorrelationUtil_isTopologicalRelated_exception_false() throws Exception {
108         PowerMock.resetAll();
109         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", "test1", "test2")
110                 .andThrow(new CorrelationException("")).anyTimes();
111
112         PowerMock.replayAll();
113         boolean actual = Whitebox
114                 .invokeMethod(correlationUtil, "isTopologicallyRelated", "123", "test1", "test2");
115         PowerMock.verifyAll();
116
117         assertThat(actual, equalTo(false));
118     }
119 }