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.core.Is.is;
22 import static org.junit.Assert.assertThat;
24 import java.util.ArrayList;
25 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.junit.runner.RunWith;
31 import org.onap.holmes.common.aai.AaiQuery;
32 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
33 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
34 import org.onap.holmes.common.aai.entity.VmEntity;
35 import org.onap.holmes.common.aai.entity.VnfEntity;
36 import org.onap.holmes.common.api.stat.VesAlarm;
37 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
38 import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS;
39 import org.onap.holmes.common.exception.CorrelationException;
40 import org.powermock.api.easymock.PowerMock;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
43 import org.powermock.reflect.Whitebox;
45 @PrepareForTest({DmaapService.class, Publisher.class, AaiQuery.class})
46 @RunWith(PowerMockRunner.class)
47 public class DmaapServiceTest {
50 public ExpectedException thrown = ExpectedException.none();
52 private AaiQuery aaiQuery;
54 private DmaapService dmaapService;
58 dmaapService = new DmaapService();
59 aaiQuery = PowerMock.createMock(AaiQuery.class);
60 Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery);
64 public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception {
65 String packageName = "org.onap.holmes.rule";
66 DmaapService.loopControlNames.put(packageName, "Control-loop-VoLTE");
67 long startTime = System.currentTimeMillis();
68 long endTime = System.currentTimeMillis() + 1000000;
69 VesAlarm vesAlarm = new VesAlarm();
70 vesAlarm.setStartEpochMicrosec(startTime);
71 vesAlarm.setLastEpochMicrosec(endTime);
72 vesAlarm.setAlarmIsCleared(1);
73 vesAlarm.setSourceName("test");
74 vesAlarm.setSourceId("782d-4dfa-88ef");
75 vesAlarm.setEventName("alarmCleared");
78 PowerMock.replayAll();
79 PolicyMsg policyMsg = Whitebox
80 .invokeMethod(dmaapService, "getDefaultPolicyMsg", vesAlarm, packageName);
81 PowerMock.verifyAll();
83 assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
84 assertThat(policyMsg.getTargetType(), equalTo("VM"));
85 assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("test"));
86 assertThat(policyMsg.getClosedLoopEventStatus(), is(EVENT_STATUS.ABATED));
87 assertThat(policyMsg.getClosedLoopControlName(), equalTo("Control-loop-VoLTE"));
88 assertThat(policyMsg.getClosedLoopAlarmStart(), is(startTime));
89 assertThat(policyMsg.getClosedLoopAlarmEnd(), is(endTime));
90 assertThat(policyMsg.getRequestID(), notNullValue());
94 public void testDmaapService_getVnfEntity_ok() throws Exception {
96 VnfEntity expect = new VnfEntity();
97 expect.setVnfName("test");
98 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
99 anyObject(String.class)).andReturn(expect).anyTimes();
100 PowerMock.replayAll();
101 VnfEntity actual = Whitebox
102 .invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
103 PowerMock.verifyAll();
105 assertThat(actual.getVnfName(), equalTo("test"));
109 public void testDmaapService_getVnfEntity_exception() throws Exception {
110 PowerMock.resetAll();
111 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
112 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
113 PowerMock.replayAll();
114 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
115 PowerMock.verifyAll();
117 assertThat(actual == null, equalTo(true));
121 public void testDmaapService_getVmEntity_ok() throws Exception {
122 PowerMock.resetAll();
123 VmEntity expect = new VmEntity();
124 expect.setVserverId("11111");
125 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
126 anyObject(String.class)).andReturn(expect).anyTimes();
127 PowerMock.replayAll();
128 VmEntity actual = Whitebox
129 .invokeMethod(dmaapService, "getVmEntity", "tset", "test");
130 PowerMock.verifyAll();
132 assertThat(actual.getVserverId(), equalTo("11111"));
136 public void testDmaapService_getVmEntity_exception() throws Exception {
137 PowerMock.resetAll();
138 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
139 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
140 PowerMock.replayAll();
141 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVmEntity", "tset", "test");
142 PowerMock.verifyAll();
144 assertThat(actual == null, equalTo(true));
148 public void testDmaapService_getVserverInstanceId_ok() throws Exception {
149 PowerMock.resetAll();
150 VnfEntity vnfEntity = new VnfEntity();
151 Relationship relationship = new Relationship();
152 relationship.setRelatedTo("service-instance");
154 List<RelationshipData> relationshipDataList = new ArrayList<>();
156 RelationshipData relationshipData = new RelationshipData();
157 relationshipData.setRelationshipKey("service-instance.service-instance-id");
158 relationshipData.setRelationshipValue("USUCP0PCOIL0110UJZZ01");
159 relationshipDataList.add(relationshipData);
160 relationship.setRelationshipDataList(relationshipDataList);
162 List<Relationship> relationships = new ArrayList<>();
163 relationships.add(relationship);
164 vnfEntity.getRelationshipList().setRelationships(relationships);
166 PowerMock.replayAll();
167 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
168 PowerMock.verifyAll();
170 assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
174 public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
175 PowerMock.resetAll();
176 VnfEntity vnfEntity = null;
178 PowerMock.replayAll();
179 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
180 PowerMock.verifyAll();
182 assertThat(actual, equalTo(""));
186 public void testDmaapService_getEnrichedPolicyMsg_ok() throws Exception {
187 PowerMock.resetAll();
188 VmEntity vmEntity = new VmEntity();
189 vmEntity.setInMaint(false);
190 vmEntity.setClosedLoopDisable(true);
191 vmEntity.setProvStatus("prov");
192 vmEntity.setResourceVersion("kkkk");
193 VesAlarm vesAlarm = new VesAlarm();
194 vesAlarm.setEventId("11111");
195 vesAlarm.setEventName("3333");
196 vesAlarm.setSourceId("111");
197 vesAlarm.setStartEpochMicrosec(11111L);
199 PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
200 anyObject(String.class)).andReturn(null).anyTimes();
202 PowerMock.replayAll();
203 PolicyMsg actual = Whitebox
204 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName");
205 PowerMock.verifyAll();
207 assertThat(actual.getClosedLoopControlName(), equalTo(null));
208 assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov"));
209 assertThat(actual.getAai().get("vserver.vserver-name2") == null, equalTo(true));
210 assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo(""));