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.dmaap;
18 import static org.easymock.EasyMock.anyObject;
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.hamcrest.CoreMatchers.notNullValue;
21 import static org.hamcrest.CoreMatchers.nullValue;
22 import static org.hamcrest.core.Is.is;
23 import static org.junit.Assert.assertThat;
27 import java.net.URISyntaxException;
29 import java.util.ArrayList;
30 import java.util.List;
32 import org.easymock.EasyMock;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.junit.runner.RunWith;
38 import org.onap.holmes.common.aai.AaiQuery;
39 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
40 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
41 import org.onap.holmes.common.aai.entity.VmEntity;
42 import org.onap.holmes.common.aai.entity.VnfEntity;
43 import org.onap.holmes.common.api.stat.VesAlarm;
44 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
45 import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser;
46 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
47 import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS;
48 import org.onap.holmes.common.exception.CorrelationException;
49 import org.powermock.api.easymock.PowerMock;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
52 import org.powermock.reflect.Whitebox;
54 @PrepareForTest({DmaapService.class, Publisher.class, AaiQuery.class})
55 @RunWith(PowerMockRunner.class)
56 public class DmaapServiceTest {
59 public ExpectedException thrown = ExpectedException.none();
61 private AaiQuery aaiQuery;
63 private DmaapService dmaapService;
67 dmaapService = new DmaapService();
68 aaiQuery = PowerMock.createMock(AaiQuery.class);
69 Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery);
73 public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception {
74 String packageName = "org.onap.holmes.rule";
75 DmaapService.loopControlNames.put(packageName, "Control-loop-VoLTE");
76 long startTime = System.currentTimeMillis();
77 long endTime = System.currentTimeMillis() + 1000000;
78 VesAlarm vesAlarm = new VesAlarm();
79 vesAlarm.setStartEpochMicrosec(startTime);
80 vesAlarm.setLastEpochMicrosec(endTime);
81 vesAlarm.setAlarmIsCleared(1);
82 vesAlarm.setSourceName("test");
83 vesAlarm.setSourceId("782d-4dfa-88ef");
84 vesAlarm.setEventName("alarmCleared");
87 PowerMock.replayAll();
88 PolicyMsg policyMsg = Whitebox
89 .invokeMethod(dmaapService, "getDefaultPolicyMsg", vesAlarm, packageName);
90 PowerMock.verifyAll();
92 assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
93 assertThat(policyMsg.getTargetType(), equalTo("VM"));
94 assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("test"));
95 assertThat(policyMsg.getClosedLoopEventStatus(), is(EVENT_STATUS.ABATED));
96 assertThat(policyMsg.getClosedLoopControlName(), equalTo("Control-loop-VoLTE"));
97 assertThat(policyMsg.getClosedLoopAlarmStart(), is(startTime));
98 assertThat(policyMsg.getClosedLoopAlarmEnd(), is(endTime));
99 assertThat(policyMsg.getRequestID(), notNullValue());
103 public void testDmaapService_getVnfEntity_ok() throws Exception {
104 PowerMock.resetAll();
105 VnfEntity expect = new VnfEntity();
106 expect.setVnfName("test");
107 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
108 anyObject(String.class)).andReturn(expect).anyTimes();
109 PowerMock.replayAll();
110 VnfEntity actual = Whitebox
111 .invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
112 PowerMock.verifyAll();
114 assertThat(actual.getVnfName(), equalTo("test"));
118 public void testDmaapService_getVnfEntity_exception() throws Exception {
119 PowerMock.resetAll();
120 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
121 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
122 PowerMock.replayAll();
123 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
124 PowerMock.verifyAll();
126 assertThat(actual == null, equalTo(true));
130 public void testDmaapService_getVmEntity_ok() throws Exception {
131 PowerMock.resetAll();
132 VmEntity expect = new VmEntity();
133 expect.setVserverId("11111");
134 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
135 anyObject(String.class)).andReturn(expect).anyTimes();
136 PowerMock.replayAll();
137 VmEntity actual = Whitebox
138 .invokeMethod(dmaapService, "getVmEntity", "tset", "test");
139 PowerMock.verifyAll();
141 assertThat(actual.getVserverId(), equalTo("11111"));
145 public void testDmaapService_getVmEntity_exception() throws Exception {
146 PowerMock.resetAll();
147 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
148 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
149 PowerMock.replayAll();
150 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVmEntity", "tset", "test");
151 PowerMock.verifyAll();
153 assertThat(actual == null, equalTo(true));
157 public void testDmaapService_getVserverInstanceId_ok() throws Exception {
158 PowerMock.resetAll();
159 VnfEntity vnfEntity = new VnfEntity();
160 Relationship relationship = new Relationship();
161 relationship.setRelatedTo("service-instance");
163 List<RelationshipData> relationshipDataList = new ArrayList<>();
165 RelationshipData relationshipData = new RelationshipData();
166 relationshipData.setRelationshipKey("service-instance.service-instance-id");
167 relationshipData.setRelationshipValue("USUCP0PCOIL0110UJZZ01");
168 relationshipDataList.add(relationshipData);
169 relationship.setRelationshipDataList(relationshipDataList);
171 List<Relationship> relationships = new ArrayList<>();
172 relationships.add(relationship);
173 vnfEntity.getRelationshipList().setRelationships(relationships);
175 PowerMock.replayAll();
176 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
177 PowerMock.verifyAll();
179 assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
183 public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
184 PowerMock.resetAll();
185 VnfEntity vnfEntity = null;
187 PowerMock.replayAll();
188 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
189 PowerMock.verifyAll();
191 assertThat(actual, equalTo(""));
195 public void testDmaapService_getEnrichedPolicyMsg_onset() throws Exception {
196 PowerMock.resetAll();
197 VmEntity vmEntity = new VmEntity();
198 vmEntity.setInMaint(false);
199 vmEntity.setClosedLoopDisable(true);
200 vmEntity.setProvStatus("prov");
201 vmEntity.setResourceVersion("kkkk");
202 VesAlarm vesAlarm = new VesAlarm();
203 vesAlarm.setEventId("11111");
204 vesAlarm.setEventName("3333");
205 vesAlarm.setSourceId("111");
206 vesAlarm.setStartEpochMicrosec(11111L);
208 PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
209 anyObject(String.class)).andReturn(null).anyTimes();
211 PowerMock.replayAll();
212 PolicyMsg actual = Whitebox
213 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName");
214 PowerMock.verifyAll();
216 assertThat(actual.getClosedLoopControlName(), nullValue());
217 assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov"));
218 assertThat(actual.getAai().get("vserver.vserver-name2"), nullValue());
219 assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo(""));
223 public void testDmaapService_getEnrichedPolicyMsg_abated() throws Exception {
224 PowerMock.resetAll();
225 VmEntity vmEntity = new VmEntity();
226 vmEntity.setInMaint(false);
227 vmEntity.setClosedLoopDisable(true);
228 vmEntity.setProvStatus("prov");
229 vmEntity.setResourceVersion("kkkk");
230 VesAlarm vesAlarm = new VesAlarm();
231 vesAlarm.setEventId("11111");
232 vesAlarm.setEventName("3333");
233 vesAlarm.setSourceId("111");
234 vesAlarm.setStartEpochMicrosec(11111L);
235 vesAlarm.setLastEpochMicrosec(21111L);
236 vesAlarm.setAlarmIsCleared(PolicyMassgeConstant.POLICY_MESSAGE_ABATED);
238 PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
239 anyObject(String.class)).andReturn(null).anyTimes();
241 PowerMock.replayAll();
242 PolicyMsg actual = Whitebox
243 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName");
244 PowerMock.verifyAll();
246 assertThat(actual.getClosedLoopControlName(), nullValue());
247 assertThat(actual.getAai().get("vserver.prov-status"), nullValue());
248 assertThat(actual.getAai().get("vserver.vserver-name2"), nullValue());
249 assertThat(actual.getAai().get("generic-vnf.service-instance-id"), nullValue());
253 public void testPublishPolicyMsg_onset() throws Exception {
254 PowerMock.resetAll();
255 Publisher publisher = PowerMock.createPartialMock(Publisher.class, "publish", PolicyMsg.class);
256 PolicyMsg policyMsg = new PolicyMsg();
257 policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET);
258 PowerMock.expectNew(Publisher.class).andReturn(publisher);
259 EasyMock.expect(publisher.publish(policyMsg)).andReturn(true);
261 DcaeConfigurationsCache.setDcaeConfigurations(
262 DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json")));
264 PowerMock.replayAll();
265 dmaapService.publishPolicyMsg(policyMsg, "sec_fault_unsecure");
266 PowerMock.verifyAll();
271 public void testPublishPolicyMsg_abated() throws Exception {
272 PowerMock.resetAll();
273 Publisher publisher = PowerMock.createPartialMock(Publisher.class, "publish", PolicyMsg.class);
274 PolicyMsg policyMsg = new PolicyMsg();
275 policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED);
276 policyMsg.setRequestID("testRequestid");
277 DmaapService.alarmUniqueRequestID.put("testAlarmId", "testRequestid");
278 PowerMock.expectNew(Publisher.class).andReturn(publisher);
279 EasyMock.expect(publisher.publish(policyMsg)).andReturn(true);
281 DcaeConfigurationsCache.setDcaeConfigurations(
282 DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json")));
284 PowerMock.replayAll();
285 dmaapService.publishPolicyMsg(policyMsg, "sec_fault_unsecure");
286 PowerMock.verifyAll();
290 private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException {
291 URL url = DmaapServiceTest.class.getClassLoader().getResource(fileName);
292 File configFile = new File(new URI(url.toString()).getPath());
293 BufferedReader br = new BufferedReader(new FileReader(configFile));
295 final StringBuilder sb = new StringBuilder();
296 br.lines().forEach(line -> {
301 } catch (IOException e) {
304 return sb.toString();