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.junit.Assert.assertThat;
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;
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;
43 @PrepareForTest({DmaapService.class, Publisher.class, AaiQuery.class})
44 @RunWith(PowerMockRunner.class)
45 public class DmaapServiceTest {
48 public ExpectedException thrown = ExpectedException.none();
50 private Publisher publisher;
52 private AaiQuery aaiQuery;
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();
64 public void testDmaapService_publish_ok() throws Exception {
66 PolicyMsg policyMsg = new PolicyMsg();
67 PowerMock.expectPrivate(publisher, "publish", anyObject(PolicyMsg.class)).andReturn(true)
69 PowerMock.replayAll();
70 Whitebox.invokeMethod(DmaapService.class, "publishPolicyMsg", policyMsg);
71 PowerMock.verifyAll();
75 public void testDmaapService_publish_exception() throws Exception {
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();
86 public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception {
89 PowerMock.replayAll();
90 PolicyMsg policyMsg = Whitebox
91 .invokeMethod(DmaapService.class, "getDefaultPolicyMsg", "tetss");
92 PowerMock.verifyAll();
94 assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
95 assertThat(policyMsg.getTargetType(), equalTo("VM"));
96 assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("tetss"));
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();
111 assertThat(actual.getVnfName(), equalTo("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();
123 assertThat(actual == null, equalTo(true));
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();
138 assertThat(actual.getVserverId(), equalTo("11111"));
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();
150 assertThat(actual == null, equalTo(true));
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");
160 List<RelationshipData> relationshipDataList = new ArrayList<>();
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);
168 List<Relationship> relationships = new ArrayList<>();
169 relationships.add(relationship);
170 vnfEntity.getRelationshipList().setRelationships(relationships);
172 PowerMock.replayAll();
173 String actual = Whitebox.invokeMethod(DmaapService.class, "getVserverInstanceId", vnfEntity);
174 PowerMock.verifyAll();
176 assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
180 public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
181 PowerMock.resetAll();
182 VnfEntity vnfEntity = null;
184 PowerMock.replayAll();
185 String actual = Whitebox.invokeMethod(DmaapService.class, "getVserverInstanceId", vnfEntity);
186 PowerMock.verifyAll();
188 assertThat(actual, equalTo(""));
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");
203 PowerMock.expectPrivate(DmaapService.class, "getVnfEntity", anyObject(String.class),
204 anyObject(String.class)).andReturn(null).anyTimes();
206 PowerMock.replayAll();
207 PolicyMsg actual = Whitebox
208 .invokeMethod(DmaapService.class, "getEnrichedPolicyMsg", vmEntity, vesAlarm);
209 PowerMock.verifyAll();
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(""));