2 * Copyright 2017-2020 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 org.easymock.EasyMock;
19 import org.junit.Before;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.ExpectedException;
23 import org.junit.runner.RunWith;
24 import org.onap.holmes.common.aai.AaiQuery;
25 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
26 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
27 import org.onap.holmes.common.aai.entity.VmEntity;
28 import org.onap.holmes.common.aai.entity.VnfEntity;
29 import org.onap.holmes.common.api.stat.VesAlarm;
30 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
31 import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser;
32 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
33 import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS;
34 import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache;
35 import org.onap.holmes.common.dmaap.store.UniqueRequestIdCache;
36 import org.onap.holmes.common.exception.CorrelationException;
37 import org.powermock.api.easymock.PowerMock;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40 import org.powermock.reflect.Whitebox;
44 import java.net.URISyntaxException;
46 import java.util.ArrayList;
47 import java.util.List;
49 import static org.easymock.EasyMock.anyObject;
50 import static org.hamcrest.CoreMatchers.*;
51 import static org.hamcrest.core.Is.is;
52 import static org.junit.Assert.assertThat;
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;
65 private ClosedLoopControlNameCache closedLoopControlNameCache = new ClosedLoopControlNameCache();
67 private UniqueRequestIdCache uniqueRequestIdCache = new UniqueRequestIdCache();
71 aaiQuery = PowerMock.createMock(AaiQuery.class);
73 dmaapService = new DmaapService();
74 dmaapService.setClosedLoopControlNameCache(closedLoopControlNameCache);
75 dmaapService.setUniqueRequestIdCache(uniqueRequestIdCache);
76 dmaapService.setAaiQuery(aaiQuery);
80 public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception {
81 String packageName = "org.onap.holmes.rule";
82 closedLoopControlNameCache.put(packageName, "Control-loop-VoLTE");
83 long startTime = System.currentTimeMillis();
84 long endTime = System.currentTimeMillis() + 1000000;
85 VesAlarm vesAlarm = new VesAlarm();
86 vesAlarm.setStartEpochMicrosec(startTime);
87 vesAlarm.setLastEpochMicrosec(endTime);
88 vesAlarm.setAlarmIsCleared(1);
89 vesAlarm.setSourceName("test");
90 vesAlarm.setSourceId("782d-4dfa-88ef");
91 vesAlarm.setEventName("alarmCleared");
94 PowerMock.replayAll();
95 PolicyMsg policyMsg = Whitebox
96 .invokeMethod(dmaapService, "getDefaultPolicyMsg", vesAlarm, packageName);
97 PowerMock.verifyAll();
99 assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
100 assertThat(policyMsg.getTargetType(), equalTo("VM"));
101 assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("test"));
102 assertThat(policyMsg.getClosedLoopEventStatus(), is(EVENT_STATUS.ABATED));
103 assertThat(policyMsg.getClosedLoopControlName(), equalTo("Control-loop-VoLTE"));
104 assertThat(policyMsg.getClosedLoopAlarmStart(), is(startTime));
105 assertThat(policyMsg.getClosedLoopAlarmEnd(), is(endTime));
106 assertThat(policyMsg.getRequestID(), notNullValue());
110 public void testDmaapService_getVnfEntity_ok() throws Exception {
111 PowerMock.resetAll();
112 VnfEntity expect = new VnfEntity();
113 expect.setVnfName("test");
114 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
115 anyObject(String.class)).andReturn(expect).anyTimes();
116 PowerMock.replayAll();
117 VnfEntity actual = Whitebox
118 .invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
119 PowerMock.verifyAll();
121 assertThat(actual.getVnfName(), equalTo("test"));
125 public void testDmaapService_getVnfEntity_exception() throws Exception {
126 PowerMock.resetAll();
127 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
128 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
129 PowerMock.replayAll();
130 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
131 PowerMock.verifyAll();
133 assertThat(actual == null, equalTo(true));
137 public void testDmaapService_getVmEntity_ok() throws Exception {
138 PowerMock.resetAll();
139 VmEntity expect = new VmEntity();
140 expect.setVserverId("11111");
141 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
142 anyObject(String.class)).andReturn(expect).anyTimes();
143 PowerMock.replayAll();
144 VmEntity actual = Whitebox
145 .invokeMethod(dmaapService, "getVmEntity", "tset", "test");
146 PowerMock.verifyAll();
148 assertThat(actual.getVserverId(), equalTo("11111"));
152 public void testDmaapService_getVmEntity_exception() throws Exception {
153 PowerMock.resetAll();
154 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
155 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
156 PowerMock.replayAll();
157 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVmEntity", "tset", "test");
158 PowerMock.verifyAll();
160 assertThat(actual == null, equalTo(true));
164 public void testDmaapService_getVserverInstanceId_ok() throws Exception {
165 PowerMock.resetAll();
166 VnfEntity vnfEntity = new VnfEntity();
167 Relationship relationship = new Relationship();
168 relationship.setRelatedTo("service-instance");
170 List<RelationshipData> relationshipDataList = new ArrayList<>();
172 RelationshipData relationshipData = new RelationshipData();
173 relationshipData.setRelationshipKey("service-instance.service-instance-id");
174 relationshipData.setRelationshipValue("USUCP0PCOIL0110UJZZ01");
175 relationshipDataList.add(relationshipData);
176 relationship.setRelationshipDataList(relationshipDataList);
178 List<Relationship> relationships = new ArrayList<>();
179 relationships.add(relationship);
180 vnfEntity.getRelationshipList().setRelationships(relationships);
182 PowerMock.replayAll();
183 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
184 PowerMock.verifyAll();
186 assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
190 public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
191 PowerMock.resetAll();
192 VnfEntity vnfEntity = null;
194 PowerMock.replayAll();
195 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
196 PowerMock.verifyAll();
198 assertThat(actual, equalTo(""));
202 public void testDmaapService_getEnrichedPolicyMsg_onset() throws Exception {
203 PowerMock.resetAll();
204 VmEntity vmEntity = new VmEntity();
205 vmEntity.setInMaint(false);
206 vmEntity.setClosedLoopDisable(true);
207 vmEntity.setProvStatus("prov");
208 vmEntity.setResourceVersion("kkkk");
209 VesAlarm vesAlarm = new VesAlarm();
210 vesAlarm.setEventId("11111");
211 vesAlarm.setEventName("3333");
212 vesAlarm.setSourceId("111");
213 vesAlarm.setStartEpochMicrosec(11111L);
215 PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
216 anyObject(String.class)).andReturn(null).anyTimes();
218 PowerMock.replayAll();
219 PolicyMsg actual = Whitebox
220 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName");
221 PowerMock.verifyAll();
223 assertThat(actual.getClosedLoopControlName(), nullValue());
224 assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov"));
225 assertThat(actual.getAai().get("vserver.vserver-name2"), nullValue());
226 assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo(""));
230 public void testDmaapService_getEnrichedPolicyMsg_abated() throws Exception {
231 PowerMock.resetAll();
232 VmEntity vmEntity = new VmEntity();
233 vmEntity.setInMaint(false);
234 vmEntity.setClosedLoopDisable(true);
235 vmEntity.setProvStatus("prov");
236 vmEntity.setResourceVersion("kkkk");
237 VesAlarm vesAlarm = new VesAlarm();
238 vesAlarm.setEventId("11111");
239 vesAlarm.setEventName("3333");
240 vesAlarm.setSourceId("111");
241 vesAlarm.setStartEpochMicrosec(11111L);
242 vesAlarm.setLastEpochMicrosec(21111L);
243 vesAlarm.setAlarmIsCleared(PolicyMassgeConstant.POLICY_MESSAGE_ABATED);
245 PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
246 anyObject(String.class)).andReturn(null).anyTimes();
248 PowerMock.replayAll();
249 PolicyMsg actual = Whitebox
250 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName");
251 PowerMock.verifyAll();
253 assertThat(actual.getClosedLoopControlName(), nullValue());
254 assertThat(actual.getAai().get("vserver.prov-status"), nullValue());
255 assertThat(actual.getAai().get("vserver.vserver-name2"), nullValue());
256 assertThat(actual.getAai().get("generic-vnf.service-instance-id"), nullValue());
260 public void testPublishPolicyMsg_onset() throws Exception {
261 PowerMock.resetAll();
262 Publisher publisher = PowerMock.createPartialMock(Publisher.class, "publish", PolicyMsg.class);
263 PolicyMsg policyMsg = new PolicyMsg();
264 policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET);
265 PowerMock.expectNew(Publisher.class).andReturn(publisher);
266 EasyMock.expect(publisher.publish(policyMsg)).andReturn(true);
268 DcaeConfigurationsCache.setDcaeConfigurations(
269 DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json")));
271 PowerMock.replayAll();
272 dmaapService.publishPolicyMsg(policyMsg, "sec_fault_unsecure");
273 PowerMock.verifyAll();
278 public void testPublishPolicyMsg_abated() throws Exception {
279 PowerMock.resetAll();
280 Publisher publisher = PowerMock.createPartialMock(Publisher.class, "publish", PolicyMsg.class);
281 PolicyMsg policyMsg = new PolicyMsg();
282 policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED);
283 policyMsg.setRequestID("testRequestid");
284 uniqueRequestIdCache.put("testAlarmId", "testRequestid");
285 PowerMock.expectNew(Publisher.class).andReturn(publisher);
286 EasyMock.expect(publisher.publish(policyMsg)).andReturn(true);
288 DcaeConfigurationsCache.setDcaeConfigurations(
289 DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json")));
291 PowerMock.replayAll();
292 dmaapService.publishPolicyMsg(policyMsg, "sec_fault_unsecure");
293 PowerMock.verifyAll();
297 private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException {
298 URL url = DmaapServiceTest.class.getClassLoader().getResource(fileName);
299 File configFile = new File(new URI(url.toString()).getPath());
300 BufferedReader br = new BufferedReader(new FileReader(configFile));
302 final StringBuilder sb = new StringBuilder();
303 br.lines().forEach(line -> {
308 } catch (IOException e) {
311 return sb.toString();