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.easymock.EasyMock;
25 import org.junit.runner.RunWith;
26 import org.omg.CORBA.Any;
27 import org.onap.holmes.common.aai.AaiQuery;
28 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
29 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
30 import org.onap.holmes.common.aai.entity.VmEntity;
31 import org.onap.holmes.common.aai.entity.VnfEntity;
32 import org.onap.holmes.common.api.stat.VesAlarm;
33 import org.onap.holmes.common.exception.CorrelationException;
34 import org.powermock.api.mockito.PowerMockito;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
42 import org.powermock.api.easymock.PowerMock;
43 import org.powermock.modules.junit4.PowerMockRunner;
44 import org.powermock.reflect.Whitebox;
46 @PrepareForTest({DmaapService.class, Publisher.class, AaiQuery.class})
47 @RunWith(PowerMockRunner.class)
48 public class DmaapServiceTest {
51 public ExpectedException thrown = ExpectedException.none();
53 private AaiQuery aaiQuery;
55 private DmaapService dmaapService;
59 dmaapService = new DmaapService();
60 aaiQuery = PowerMock.createMock(AaiQuery.class);
61 Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery);
65 public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception {
68 PowerMock.replayAll();
69 PolicyMsg policyMsg = Whitebox
70 .invokeMethod(dmaapService, "getDefaultPolicyMsg", "tetss");
71 PowerMock.verifyAll();
73 assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-id"));
74 assertThat(policyMsg.getTargetType(), equalTo("VM"));
75 assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("tetss"));
79 public void testDmaapService_getVnfEntity_ok() throws Exception {
81 VnfEntity expect = new VnfEntity();
82 expect.setVnfName("test");
83 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
84 anyObject(String.class)).andReturn(expect).anyTimes();
85 PowerMock.replayAll();
86 VnfEntity actual = Whitebox
87 .invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
88 PowerMock.verifyAll();
90 assertThat(actual.getVnfName(), equalTo("test"));
94 public void testDmaapService_getVnfEntity_exception() throws Exception {
96 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
97 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
98 PowerMock.replayAll();
99 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
100 PowerMock.verifyAll();
102 assertThat(actual == null, equalTo(true));
106 public void testDmaapService_getVmEntity_ok() throws Exception {
107 PowerMock.resetAll();
108 VmEntity expect = new VmEntity();
109 expect.setVserverId("11111");
110 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
111 anyObject(String.class)).andReturn(expect).anyTimes();
112 PowerMock.replayAll();
113 VmEntity actual = Whitebox
114 .invokeMethod(dmaapService, "getVmEntity", "tset", "test");
115 PowerMock.verifyAll();
117 assertThat(actual.getVserverId(), equalTo("11111"));
121 public void testDmaapService_getVmEntity_exception() throws Exception {
122 PowerMock.resetAll();
123 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
124 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
125 PowerMock.replayAll();
126 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVmEntity", "tset", "test");
127 PowerMock.verifyAll();
129 assertThat(actual == null, equalTo(true));
133 public void testDmaapService_getVserverInstanceId_ok() throws Exception {
134 PowerMock.resetAll();
135 VnfEntity vnfEntity = new VnfEntity();
136 Relationship relationship = new Relationship();
137 relationship.setRelatedTo("service-instance");
139 List<RelationshipData> relationshipDataList = new ArrayList<>();
141 RelationshipData relationshipData = new RelationshipData();
142 relationshipData.setRelationshipKey("service-instance.service-instance-id");
143 relationshipData.setRelationshipValue("USUCP0PCOIL0110UJZZ01");
144 relationshipDataList.add(relationshipData);
145 relationship.setRelationshipDataList(relationshipDataList);
147 List<Relationship> relationships = new ArrayList<>();
148 relationships.add(relationship);
149 vnfEntity.getRelationshipList().setRelationships(relationships);
151 PowerMock.replayAll();
152 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
153 PowerMock.verifyAll();
155 assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
159 public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
160 PowerMock.resetAll();
161 VnfEntity vnfEntity = null;
163 PowerMock.replayAll();
164 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
165 PowerMock.verifyAll();
167 assertThat(actual, equalTo(""));
171 public void testDmaapService_getEnrichedPolicyMsg_ok() throws Exception {
172 PowerMock.resetAll();
173 VmEntity vmEntity = new VmEntity();
174 vmEntity.setInMaint(false);
175 vmEntity.setClosedLoopDisable(true);
176 vmEntity.setProvStatus("prov");
177 vmEntity.setResourceVersion("kkkk");
178 VesAlarm vesAlarm = new VesAlarm();
179 vesAlarm.setEventId("11111");
180 vesAlarm.setEventName("3333");
181 vesAlarm.setSourceId("111");
182 vesAlarm.setStartEpochMicrosec(11111L);
184 PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
185 anyObject(String.class)).andReturn(null).anyTimes();
187 PowerMock.replayAll();
188 PolicyMsg actual = Whitebox
189 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName");
190 PowerMock.verifyAll();
192 assertThat(actual.getClosedLoopControlName(), equalTo(null));
193 assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov"));
194 assertThat(actual.getAai().get("vserver.vserver-name2") == null, equalTo(true));
195 assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo(""));