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;
54 private DmaapService dmaapService;
58 dmaapService = new DmaapService();
59 publisher = PowerMock.createMock(Publisher.class);
60 Whitebox.setInternalState(dmaapService, "publisher", publisher);
61 aaiQuery = PowerMock.createMock(AaiQuery.class);
62 Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery);
66 public void testDmaapService_publish_ok() throws Exception {
68 PolicyMsg policyMsg = new PolicyMsg();
69 PowerMock.expectPrivate(publisher, "publish", anyObject(PolicyMsg.class)).andReturn(true)
71 PowerMock.replayAll();
72 Whitebox.invokeMethod(dmaapService, "publishPolicyMsg", policyMsg);
73 PowerMock.verifyAll();
77 public void testDmaapService_publish_exception() throws Exception {
79 final PolicyMsg policyMsg = new PolicyMsg();
80 PowerMock.expectPrivate(publisher, "publish", policyMsg)
81 .andThrow(new CorrelationException("")).anyTimes();
82 PowerMock.replayAll();
83 Whitebox.invokeMethod(dmaapService, "publishPolicyMsg", policyMsg);
84 PowerMock.verifyAll();
88 public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception {
91 PowerMock.replayAll();
92 PolicyMsg policyMsg = Whitebox
93 .invokeMethod(dmaapService, "getDefaultPolicyMsg", "tetss");
94 PowerMock.verifyAll();
96 assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
97 assertThat(policyMsg.getTargetType(), equalTo("VM"));
98 assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("tetss"));
102 public void testDmaapService_getVnfEntity_ok() throws Exception {
103 PowerMock.resetAll();
104 VnfEntity expect = new VnfEntity();
105 expect.setVnfName("test");
106 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
107 anyObject(String.class)).andReturn(expect).anyTimes();
108 PowerMock.replayAll();
109 VnfEntity actual = Whitebox
110 .invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
111 PowerMock.verifyAll();
113 assertThat(actual.getVnfName(), equalTo("test"));
117 public void testDmaapService_getVnfEntity_exception() throws Exception {
118 PowerMock.resetAll();
119 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
120 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
121 PowerMock.replayAll();
122 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
123 PowerMock.verifyAll();
125 assertThat(actual == null, equalTo(true));
129 public void testDmaapService_getVmEntity_ok() throws Exception {
130 PowerMock.resetAll();
131 VmEntity expect = new VmEntity();
132 expect.setVserverId("11111");
133 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
134 anyObject(String.class)).andReturn(expect).anyTimes();
135 PowerMock.replayAll();
136 VmEntity actual = Whitebox
137 .invokeMethod(dmaapService, "getVmEntity", "tset", "test");
138 PowerMock.verifyAll();
140 assertThat(actual.getVserverId(), equalTo("11111"));
144 public void testDmaapService_getVmEntity_exception() throws Exception {
145 PowerMock.resetAll();
146 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
147 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
148 PowerMock.replayAll();
149 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVmEntity", "tset", "test");
150 PowerMock.verifyAll();
152 assertThat(actual == null, equalTo(true));
156 public void testDmaapService_getVserverInstanceId_ok() throws Exception {
157 PowerMock.resetAll();
158 VnfEntity vnfEntity = new VnfEntity();
159 Relationship relationship = new Relationship();
160 relationship.setRelatedTo("service-instance");
162 List<RelationshipData> relationshipDataList = new ArrayList<>();
164 RelationshipData relationshipData = new RelationshipData();
165 relationshipData.setRelationshipKey("service-instance.service-instance-id");
166 relationshipData.setRelationshipValue("USUCP0PCOIL0110UJZZ01");
167 relationshipDataList.add(relationshipData);
168 relationship.setRelationshipDataList(relationshipDataList);
170 List<Relationship> relationships = new ArrayList<>();
171 relationships.add(relationship);
172 vnfEntity.getRelationshipList().setRelationships(relationships);
174 PowerMock.replayAll();
175 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
176 PowerMock.verifyAll();
178 assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
182 public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
183 PowerMock.resetAll();
184 VnfEntity vnfEntity = null;
186 PowerMock.replayAll();
187 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
188 PowerMock.verifyAll();
190 assertThat(actual, equalTo(""));
194 public void testDmaapService_getEnrichedPolicyMsg_ok() throws Exception {
195 PowerMock.resetAll();
196 VmEntity vmEntity = new VmEntity();
197 vmEntity.setInMaint(false);
198 vmEntity.setClosedLoopDisable(true);
199 vmEntity.setProvStatus("prov");
200 vmEntity.setResourceVersion("kkkk");
201 VesAlarm vesAlarm = new VesAlarm();
202 vesAlarm.setEventId("11111");
203 vesAlarm.setEventName("3333");
205 PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
206 anyObject(String.class)).andReturn(null).anyTimes();
208 PowerMock.replayAll();
209 PolicyMsg actual = Whitebox
210 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm);
211 PowerMock.verifyAll();
213 assertThat(actual.getPolicyName(), equalTo("vLoadBalancer"));
214 assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov"));
215 assertThat(actual.getAai().get("vserver.vserver-name2") == null, equalTo(true));
216 assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo(""));