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.omg.CORBA.Any;
26 import org.onap.holmes.common.aai.AaiQuery;
27 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
28 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
29 import org.onap.holmes.common.aai.entity.VmEntity;
30 import org.onap.holmes.common.aai.entity.VnfEntity;
31 import org.onap.holmes.common.api.stat.VesAlarm;
32 import org.onap.holmes.common.exception.CorrelationException;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
41 import org.powermock.api.easymock.PowerMock;
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 {
67 PowerMock.replayAll();
68 PolicyMsg policyMsg = Whitebox
69 .invokeMethod(dmaapService, "getDefaultPolicyMsg", "tetss");
70 PowerMock.verifyAll();
72 assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
73 assertThat(policyMsg.getTargetType(), equalTo("VM"));
74 assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("tetss"));
78 public void testDmaapService_getVnfEntity_ok() throws Exception {
80 VnfEntity expect = new VnfEntity();
81 expect.setVnfName("test");
82 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
83 anyObject(String.class)).andReturn(expect).anyTimes();
84 PowerMock.replayAll();
85 VnfEntity actual = Whitebox
86 .invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
87 PowerMock.verifyAll();
89 assertThat(actual.getVnfName(), equalTo("test"));
93 public void testDmaapService_getVnfEntity_exception() throws Exception {
95 PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
96 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
97 PowerMock.replayAll();
98 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
99 PowerMock.verifyAll();
101 assertThat(actual == null, equalTo(true));
105 public void testDmaapService_getVmEntity_ok() throws Exception {
106 PowerMock.resetAll();
107 VmEntity expect = new VmEntity();
108 expect.setVserverId("11111");
109 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
110 anyObject(String.class)).andReturn(expect).anyTimes();
111 PowerMock.replayAll();
112 VmEntity actual = Whitebox
113 .invokeMethod(dmaapService, "getVmEntity", "tset", "test");
114 PowerMock.verifyAll();
116 assertThat(actual.getVserverId(), equalTo("11111"));
120 public void testDmaapService_getVmEntity_exception() throws Exception {
121 PowerMock.resetAll();
122 PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
123 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
124 PowerMock.replayAll();
125 VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVmEntity", "tset", "test");
126 PowerMock.verifyAll();
128 assertThat(actual == null, equalTo(true));
132 public void testDmaapService_getVserverInstanceId_ok() throws Exception {
133 PowerMock.resetAll();
134 VnfEntity vnfEntity = new VnfEntity();
135 Relationship relationship = new Relationship();
136 relationship.setRelatedTo("service-instance");
138 List<RelationshipData> relationshipDataList = new ArrayList<>();
140 RelationshipData relationshipData = new RelationshipData();
141 relationshipData.setRelationshipKey("service-instance.service-instance-id");
142 relationshipData.setRelationshipValue("USUCP0PCOIL0110UJZZ01");
143 relationshipDataList.add(relationshipData);
144 relationship.setRelationshipDataList(relationshipDataList);
146 List<Relationship> relationships = new ArrayList<>();
147 relationships.add(relationship);
148 vnfEntity.getRelationshipList().setRelationships(relationships);
150 PowerMock.replayAll();
151 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
152 PowerMock.verifyAll();
154 assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
158 public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
159 PowerMock.resetAll();
160 VnfEntity vnfEntity = null;
162 PowerMock.replayAll();
163 String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
164 PowerMock.verifyAll();
166 assertThat(actual, equalTo(""));
170 public void testDmaapService_getEnrichedPolicyMsg_ok() throws Exception {
171 PowerMock.resetAll();
172 VmEntity vmEntity = new VmEntity();
173 vmEntity.setInMaint(false);
174 vmEntity.setClosedLoopDisable(true);
175 vmEntity.setProvStatus("prov");
176 vmEntity.setResourceVersion("kkkk");
177 VesAlarm vesAlarm = new VesAlarm();
178 vesAlarm.setEventId("11111");
179 vesAlarm.setEventName("3333");
181 PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
182 anyObject(String.class)).andReturn(null).anyTimes();
184 PowerMock.replayAll();
185 PolicyMsg actual = Whitebox
186 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, "loopName");
187 PowerMock.verifyAll();
189 assertThat(actual.getClosedLoopControlName(), equalTo(null));
190 assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov"));
191 assertThat(actual.getAai().get("vserver.vserver-name2") == null, equalTo(true));
192 assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo(""));