add httpsutil and modify query from aai
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / dmaap / DmaapServiceTest.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.dmaap;
17
18 import static org.easymock.EasyMock.anyObject;
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.junit.Assert.assertThat;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.junit.runner.RunWith;
25 import org.onap.holmes.common.aai.AaiQuery;
26 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
27 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
28 import org.onap.holmes.common.aai.entity.VmEntity;
29 import org.onap.holmes.common.aai.entity.VnfEntity;
30 import org.onap.holmes.common.api.stat.VesAlarm;
31 import org.onap.holmes.common.exception.CorrelationException;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
39 import org.powermock.api.easymock.PowerMock;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import org.powermock.reflect.Whitebox;
42
43 @PrepareForTest({DmaapService.class, Publisher.class, AaiQuery.class})
44 @RunWith(PowerMockRunner.class)
45 public class DmaapServiceTest {
46
47     @Rule
48     public ExpectedException thrown = ExpectedException.none();
49
50     private Publisher publisher;
51
52     private AaiQuery aaiQuery;
53
54     @Before
55     public void setUp() {
56         publisher = PowerMock.createMock(Publisher.class);
57         Whitebox.setInternalState(DmaapService.class, "publisher", publisher);
58         aaiQuery = PowerMock.createMock(AaiQuery.class);
59         Whitebox.setInternalState(DmaapService.class, "aaiQuery", aaiQuery);
60         PowerMock.replayAll();
61     }
62
63     @Test
64     public void testDmaapService_publish_ok() throws Exception {
65         PowerMock.resetAll();
66         PolicyMsg policyMsg = new PolicyMsg();
67         PowerMock.expectPrivate(publisher, "publish", anyObject(PolicyMsg.class)).andReturn(true)
68                 .anyTimes();
69         PowerMock.replayAll();
70         Whitebox.invokeMethod(DmaapService.class, "publishPolicyMsg", policyMsg);
71         PowerMock.verifyAll();
72     }
73
74     @Test
75     public void testDmaapService_publish_exception() throws Exception {
76         PowerMock.resetAll();
77         final PolicyMsg policyMsg = new PolicyMsg();
78         PowerMock.expectPrivate(publisher, "publish", policyMsg)
79                 .andThrow(new CorrelationException("")).anyTimes();
80         PowerMock.replayAll();
81         Whitebox.invokeMethod(DmaapService.class, "publishPolicyMsg", policyMsg);
82         PowerMock.verifyAll();
83     }
84
85     @Test
86     public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception {
87         PowerMock.resetAll();
88
89         PowerMock.replayAll();
90         PolicyMsg policyMsg = Whitebox
91                 .invokeMethod(DmaapService.class, "getDefaultPolicyMsg", "tetss");
92         PowerMock.verifyAll();
93
94         assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
95         assertThat(policyMsg.getTargetType(), equalTo("VM"));
96         assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("tetss"));
97     }
98
99     @Test
100     public void testDmaapService_getVnfEntity_ok() throws Exception {
101         PowerMock.resetAll();
102         VnfEntity expect = new VnfEntity();
103         expect.setVnfName("test");
104         PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
105                 anyObject(String.class)).andReturn(expect).anyTimes();
106         PowerMock.replayAll();
107         VnfEntity actual = Whitebox
108                 .invokeMethod(DmaapService.class, "getVnfEntity", "tset", "test");
109         PowerMock.verifyAll();
110
111         assertThat(actual.getVnfName(), equalTo("test"));
112     }
113
114     @Test
115     public void testDmaapService_getVnfEntity_exception() throws Exception {
116         PowerMock.resetAll();
117         PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
118                 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
119         PowerMock.replayAll();
120         VnfEntity actual = Whitebox.invokeMethod(DmaapService.class, "getVnfEntity", "tset", "test");
121         PowerMock.verifyAll();
122
123         assertThat(actual == null, equalTo(true));
124     }
125
126     @Test
127     public void testDmaapService_getVmEntity_ok() throws Exception {
128         PowerMock.resetAll();
129         VmEntity expect = new VmEntity();
130         expect.setVserverId("11111");
131         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
132                 anyObject(String.class)).andReturn(expect).anyTimes();
133         PowerMock.replayAll();
134         VmEntity actual = Whitebox
135                 .invokeMethod(DmaapService.class, "getVmEntity", "tset", "test");
136         PowerMock.verifyAll();
137
138         assertThat(actual.getVserverId(), equalTo("11111"));
139     }
140
141     @Test
142     public void testDmaapService_getVmEntity_exception() throws Exception {
143         PowerMock.resetAll();
144         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
145                 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
146         PowerMock.replayAll();
147         VnfEntity actual = Whitebox.invokeMethod(DmaapService.class, "getVmEntity", "tset", "test");
148         PowerMock.verifyAll();
149
150         assertThat(actual == null, equalTo(true));
151     }
152
153     @Test
154     public void testDmaapService_getVserverInstanceId_ok() throws Exception {
155         PowerMock.resetAll();
156         VnfEntity vnfEntity = new VnfEntity();
157         Relationship relationship = new Relationship();
158         relationship.setRelatedTo("service-instance");
159
160         List<RelationshipData> relationshipDataList = new ArrayList<>();
161
162         RelationshipData relationshipData = new RelationshipData();
163         relationshipData.setRelationshipKey("service-instance.service-instance-id");
164         relationshipData.setRelationshipValue("USUCP0PCOIL0110UJZZ01");
165         relationshipDataList.add(relationshipData);
166         relationship.setRelationshipDataList(relationshipDataList);
167
168         List<Relationship> relationships = new ArrayList<>();
169         relationships.add(relationship);
170         vnfEntity.getRelationshipList().setRelationships(relationships);
171
172         PowerMock.replayAll();
173         String actual = Whitebox.invokeMethod(DmaapService.class, "getVserverInstanceId", vnfEntity);
174         PowerMock.verifyAll();
175
176         assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
177     }
178
179     @Test
180     public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
181         PowerMock.resetAll();
182         VnfEntity vnfEntity = null;
183
184         PowerMock.replayAll();
185         String actual = Whitebox.invokeMethod(DmaapService.class, "getVserverInstanceId", vnfEntity);
186         PowerMock.verifyAll();
187
188         assertThat(actual, equalTo(""));
189     }
190
191     @Test
192     public void testDmaapService_getEnrichedPolicyMsg_ok() throws Exception {
193         PowerMock.resetAll();
194         VmEntity vmEntity = new VmEntity();
195         vmEntity.setInMaint(false);
196         vmEntity.setClosedLoopDisable(true);
197         vmEntity.setProvStatus("prov");
198         vmEntity.setResourceVersion("kkkk");
199         VesAlarm vesAlarm = new VesAlarm();
200         vesAlarm.setEventId("11111");
201         vesAlarm.setEventName("3333");
202
203         PowerMock.expectPrivate(DmaapService.class, "getVnfEntity", anyObject(String.class),
204                 anyObject(String.class)).andReturn(null).anyTimes();
205
206         PowerMock.replayAll();
207         PolicyMsg actual = Whitebox
208                 .invokeMethod(DmaapService.class, "getEnrichedPolicyMsg", vmEntity, vesAlarm);
209         PowerMock.verifyAll();
210
211         assertThat(actual.getPolicyName(), equalTo("vLoadBalancer"));
212         assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov"));
213         assertThat(actual.getAai().get("vserver.vserver-name2") == null, equalTo(true));
214         assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo(""));
215     }
216 }