Improved the UT coverage
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / dmaap / DmaapServiceTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.holmes.common.dmaap;
17
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.CoreMatchers.nullValue;
22 import static org.hamcrest.core.Is.is;
23 import static org.junit.Assert.assertThat;
24
25 import java.io.*;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.easymock.EasyMock;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.junit.runner.RunWith;
38 import org.onap.holmes.common.aai.AaiQuery;
39 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
40 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
41 import org.onap.holmes.common.aai.entity.VmEntity;
42 import org.onap.holmes.common.aai.entity.VnfEntity;
43 import org.onap.holmes.common.api.stat.VesAlarm;
44 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
45 import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser;
46 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
47 import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS;
48 import org.onap.holmes.common.exception.CorrelationException;
49 import org.powermock.api.easymock.PowerMock;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
52 import org.powermock.reflect.Whitebox;
53
54 @PrepareForTest({DmaapService.class, Publisher.class, AaiQuery.class})
55 @RunWith(PowerMockRunner.class)
56 public class DmaapServiceTest {
57
58     @Rule
59     public ExpectedException thrown = ExpectedException.none();
60
61     private AaiQuery aaiQuery;
62
63     private DmaapService dmaapService;
64
65     @Before
66     public void setUp() {
67         dmaapService = new DmaapService();
68         aaiQuery = PowerMock.createMock(AaiQuery.class);
69         Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery);
70     }
71
72     @Test
73     public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception {
74         String packageName = "org.onap.holmes.rule";
75         DmaapService.loopControlNames.put(packageName, "Control-loop-VoLTE");
76         long startTime = System.currentTimeMillis();
77         long endTime = System.currentTimeMillis() + 1000000;
78         VesAlarm vesAlarm = new VesAlarm();
79         vesAlarm.setStartEpochMicrosec(startTime);
80         vesAlarm.setLastEpochMicrosec(endTime);
81         vesAlarm.setAlarmIsCleared(1);
82         vesAlarm.setSourceName("test");
83         vesAlarm.setSourceId("782d-4dfa-88ef");
84         vesAlarm.setEventName("alarmCleared");
85         PowerMock.resetAll();
86
87         PowerMock.replayAll();
88         PolicyMsg policyMsg = Whitebox
89                 .invokeMethod(dmaapService, "getDefaultPolicyMsg", vesAlarm, packageName);
90         PowerMock.verifyAll();
91
92         assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name"));
93         assertThat(policyMsg.getTargetType(), equalTo("VM"));
94         assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("test"));
95         assertThat(policyMsg.getClosedLoopEventStatus(), is(EVENT_STATUS.ABATED));
96         assertThat(policyMsg.getClosedLoopControlName(), equalTo("Control-loop-VoLTE"));
97         assertThat(policyMsg.getClosedLoopAlarmStart(), is(startTime));
98         assertThat(policyMsg.getClosedLoopAlarmEnd(), is(endTime));
99         assertThat(policyMsg.getRequestID(), notNullValue());
100     }
101
102     @Test
103     public void testDmaapService_getVnfEntity_ok() throws Exception {
104         PowerMock.resetAll();
105         VnfEntity expect = new VnfEntity();
106         expect.setVnfName("test");
107         PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
108                 anyObject(String.class)).andReturn(expect).anyTimes();
109         PowerMock.replayAll();
110         VnfEntity actual = Whitebox
111                 .invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
112         PowerMock.verifyAll();
113
114         assertThat(actual.getVnfName(), equalTo("test"));
115     }
116
117     @Test
118     public void testDmaapService_getVnfEntity_exception() throws Exception {
119         PowerMock.resetAll();
120         PowerMock.expectPrivate(aaiQuery, "getAaiVnfData", anyObject(String.class),
121                 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
122         PowerMock.replayAll();
123         VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVnfEntity", "tset", "test");
124         PowerMock.verifyAll();
125
126         assertThat(actual == null, equalTo(true));
127     }
128
129     @Test
130     public void testDmaapService_getVmEntity_ok() throws Exception {
131         PowerMock.resetAll();
132         VmEntity expect = new VmEntity();
133         expect.setVserverId("11111");
134         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
135                 anyObject(String.class)).andReturn(expect).anyTimes();
136         PowerMock.replayAll();
137         VmEntity actual = Whitebox
138                 .invokeMethod(dmaapService, "getVmEntity", "tset", "test");
139         PowerMock.verifyAll();
140
141         assertThat(actual.getVserverId(), equalTo("11111"));
142     }
143
144     @Test
145     public void testDmaapService_getVmEntity_exception() throws Exception {
146         PowerMock.resetAll();
147         PowerMock.expectPrivate(aaiQuery, "getAaiVmData", anyObject(String.class),
148                 anyObject(String.class)).andThrow(new CorrelationException("")).anyTimes();
149         PowerMock.replayAll();
150         VnfEntity actual = Whitebox.invokeMethod(dmaapService, "getVmEntity", "tset", "test");
151         PowerMock.verifyAll();
152
153         assertThat(actual == null, equalTo(true));
154     }
155
156     @Test
157     public void testDmaapService_getVserverInstanceId_ok() throws Exception {
158         PowerMock.resetAll();
159         VnfEntity vnfEntity = new VnfEntity();
160         Relationship relationship = new Relationship();
161         relationship.setRelatedTo("service-instance");
162
163         List<RelationshipData> relationshipDataList = new ArrayList<>();
164
165         RelationshipData relationshipData = new RelationshipData();
166         relationshipData.setRelationshipKey("service-instance.service-instance-id");
167         relationshipData.setRelationshipValue("USUCP0PCOIL0110UJZZ01");
168         relationshipDataList.add(relationshipData);
169         relationship.setRelationshipDataList(relationshipDataList);
170
171         List<Relationship> relationships = new ArrayList<>();
172         relationships.add(relationship);
173         vnfEntity.getRelationshipList().setRelationships(relationships);
174
175         PowerMock.replayAll();
176         String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
177         PowerMock.verifyAll();
178
179         assertThat(actual, equalTo("USUCP0PCOIL0110UJZZ01"));
180     }
181
182     @Test
183     public void testDmaapService_getVserverInstanceId_input_null() throws Exception {
184         PowerMock.resetAll();
185         VnfEntity vnfEntity = null;
186
187         PowerMock.replayAll();
188         String actual = Whitebox.invokeMethod(dmaapService, "getVserverInstanceId", vnfEntity);
189         PowerMock.verifyAll();
190
191         assertThat(actual, equalTo(""));
192     }
193
194     @Test
195     public void testDmaapService_getEnrichedPolicyMsg_onset() throws Exception {
196         PowerMock.resetAll();
197         VmEntity vmEntity = new VmEntity();
198         vmEntity.setInMaint(false);
199         vmEntity.setClosedLoopDisable(true);
200         vmEntity.setProvStatus("prov");
201         vmEntity.setResourceVersion("kkkk");
202         VesAlarm vesAlarm = new VesAlarm();
203         vesAlarm.setEventId("11111");
204         vesAlarm.setEventName("3333");
205         vesAlarm.setSourceId("111");
206         vesAlarm.setStartEpochMicrosec(11111L);
207
208         PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
209                 anyObject(String.class)).andReturn(null).anyTimes();
210
211         PowerMock.replayAll();
212         PolicyMsg actual = Whitebox
213                 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName");
214         PowerMock.verifyAll();
215
216         assertThat(actual.getClosedLoopControlName(), nullValue());
217         assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov"));
218         assertThat(actual.getAai().get("vserver.vserver-name2"), nullValue());
219         assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo(""));
220     }
221
222     @Test
223     public void testDmaapService_getEnrichedPolicyMsg_abated() throws Exception {
224         PowerMock.resetAll();
225         VmEntity vmEntity = new VmEntity();
226         vmEntity.setInMaint(false);
227         vmEntity.setClosedLoopDisable(true);
228         vmEntity.setProvStatus("prov");
229         vmEntity.setResourceVersion("kkkk");
230         VesAlarm vesAlarm = new VesAlarm();
231         vesAlarm.setEventId("11111");
232         vesAlarm.setEventName("3333");
233         vesAlarm.setSourceId("111");
234         vesAlarm.setStartEpochMicrosec(11111L);
235         vesAlarm.setLastEpochMicrosec(21111L);
236         vesAlarm.setAlarmIsCleared(PolicyMassgeConstant.POLICY_MESSAGE_ABATED);
237
238         PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class),
239                 anyObject(String.class)).andReturn(null).anyTimes();
240
241         PowerMock.replayAll();
242         PolicyMsg actual = Whitebox
243                 .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName");
244         PowerMock.verifyAll();
245
246         assertThat(actual.getClosedLoopControlName(), nullValue());
247         assertThat(actual.getAai().get("vserver.prov-status"), nullValue());
248         assertThat(actual.getAai().get("vserver.vserver-name2"), nullValue());
249         assertThat(actual.getAai().get("generic-vnf.service-instance-id"), nullValue());
250     }
251
252     @Test
253     public void testPublishPolicyMsg_onset() throws Exception {
254         PowerMock.resetAll();
255         Publisher publisher = PowerMock.createPartialMock(Publisher.class, "publish", PolicyMsg.class);
256         PolicyMsg policyMsg = new PolicyMsg();
257         policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET);
258         PowerMock.expectNew(Publisher.class).andReturn(publisher);
259         EasyMock.expect(publisher.publish(policyMsg)).andReturn(true);
260
261         DcaeConfigurationsCache.setDcaeConfigurations(
262                 DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json")));
263
264         PowerMock.replayAll();
265         dmaapService.publishPolicyMsg(policyMsg, "sec_fault_unsecure");
266         PowerMock.verifyAll();
267
268     }
269
270     @Test
271     public void testPublishPolicyMsg_abated() throws Exception {
272         PowerMock.resetAll();
273         Publisher publisher = PowerMock.createPartialMock(Publisher.class, "publish", PolicyMsg.class);
274         PolicyMsg policyMsg = new PolicyMsg();
275         policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED);
276         policyMsg.setRequestID("testRequestid");
277         DmaapService.alarmUniqueRequestID.put("testAlarmId", "testRequestid");
278         PowerMock.expectNew(Publisher.class).andReturn(publisher);
279         EasyMock.expect(publisher.publish(policyMsg)).andReturn(true);
280
281         DcaeConfigurationsCache.setDcaeConfigurations(
282                 DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json")));
283
284         PowerMock.replayAll();
285         dmaapService.publishPolicyMsg(policyMsg, "sec_fault_unsecure");
286         PowerMock.verifyAll();
287
288     }
289
290     private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException {
291         URL url = DmaapServiceTest.class.getClassLoader().getResource(fileName);
292         File configFile = new File(new URI(url.toString()).getPath());
293         BufferedReader br = new BufferedReader(new FileReader(configFile));
294
295         final StringBuilder sb = new StringBuilder();
296         br.lines().forEach(line -> {
297             sb.append(line);
298         });
299         try {
300             br.close();
301         } catch (IOException e) {
302             // Do nothing
303         }
304         return sb.toString();
305     }
306 }