X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=holmes-actions%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fholmes%2Fcommon%2Fdmaap%2FDmaapServiceTest.java;h=76c4a45530daeb3f74a49823127e5688f09fff39;hb=490fc3c1fafe50d5fb0e23db5cfd10730be96866;hp=72a0bc8892b164e5355b4645b16e3ca903c3aac3;hpb=b4e2bd572489d6fce49571b4c3e681d267ffcc57;p=holmes%2Fcommon.git diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java index 72a0bc8..76c4a45 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2020 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,11 @@ */ package org.onap.holmes.common.dmaap; -import static org.easymock.EasyMock.anyObject; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; - -import java.util.ArrayList; -import java.util.List; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.onap.holmes.common.aai.AaiQuery; import org.onap.holmes.common.aai.entity.RelationshipList.Relationship; @@ -28,18 +27,30 @@ import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData; import org.onap.holmes.common.aai.entity.VmEntity; import org.onap.holmes.common.aai.entity.VnfEntity; import org.onap.holmes.common.api.stat.VesAlarm; -import org.onap.holmes.common.exception.CorrelationException; -import org.powermock.core.classloader.annotations.PrepareForTest; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.onap.holmes.common.dcae.DcaeConfigurationsCache; +import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser; import org.onap.holmes.common.dmaap.entity.PolicyMsg; +import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS; +import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache; +import org.onap.holmes.common.dmaap.store.UniqueRequestIdCache; +import org.onap.holmes.common.exception.CorrelationException; import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import java.io.*; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import static org.easymock.EasyMock.anyObject; +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + @PrepareForTest({DmaapService.class, Publisher.class, AaiQuery.class}) @RunWith(PowerMockRunner.class) public class DmaapServiceTest { @@ -47,55 +58,52 @@ public class DmaapServiceTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private Publisher publisher; - private AaiQuery aaiQuery; private DmaapService dmaapService; + private ClosedLoopControlNameCache closedLoopControlNameCache = new ClosedLoopControlNameCache(); + + private UniqueRequestIdCache uniqueRequestIdCache = new UniqueRequestIdCache(); + @Before public void setUp() { - dmaapService = new DmaapService(); - publisher = PowerMock.createMock(Publisher.class); - Whitebox.setInternalState(dmaapService, "publisher", publisher); aaiQuery = PowerMock.createMock(AaiQuery.class); - Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery); - } - @Test - public void testDmaapService_publish_ok() throws Exception { - PowerMock.resetAll(); - PolicyMsg policyMsg = new PolicyMsg(); - PowerMock.expectPrivate(publisher, "publish", anyObject(PolicyMsg.class)).andReturn(true) - .anyTimes(); - PowerMock.replayAll(); - Whitebox.invokeMethod(dmaapService, "publishPolicyMsg", policyMsg); - PowerMock.verifyAll(); - } - - @Test - public void testDmaapService_publish_exception() throws Exception { - PowerMock.resetAll(); - final PolicyMsg policyMsg = new PolicyMsg(); - PowerMock.expectPrivate(publisher, "publish", policyMsg) - .andThrow(new CorrelationException("")).anyTimes(); - PowerMock.replayAll(); - Whitebox.invokeMethod(dmaapService, "publishPolicyMsg", policyMsg); - PowerMock.verifyAll(); + dmaapService = new DmaapService(); + dmaapService.setClosedLoopControlNameCache(closedLoopControlNameCache); + dmaapService.setUniqueRequestIdCache(uniqueRequestIdCache); + dmaapService.setAaiQuery(aaiQuery); } @Test public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception { + String packageName = "org.onap.holmes.rule"; + closedLoopControlNameCache.put(packageName, "Control-loop-VoLTE"); + long startTime = System.currentTimeMillis(); + long endTime = System.currentTimeMillis() + 1000000; + VesAlarm vesAlarm = new VesAlarm(); + vesAlarm.setStartEpochMicrosec(startTime); + vesAlarm.setLastEpochMicrosec(endTime); + vesAlarm.setAlarmIsCleared(1); + vesAlarm.setSourceName("test"); + vesAlarm.setSourceId("782d-4dfa-88ef"); + vesAlarm.setEventName("alarmCleared"); PowerMock.resetAll(); PowerMock.replayAll(); PolicyMsg policyMsg = Whitebox - .invokeMethod(dmaapService, "getDefaultPolicyMsg", "tetss"); + .invokeMethod(dmaapService, "getDefaultPolicyMsg", vesAlarm, packageName); PowerMock.verifyAll(); assertThat(policyMsg.getTarget(), equalTo("vserver.vserver-name")); assertThat(policyMsg.getTargetType(), equalTo("VM")); - assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("tetss")); + assertThat(policyMsg.getAai().get("vserver.vserver-name"), equalTo("test")); + assertThat(policyMsg.getClosedLoopEventStatus(), is(EVENT_STATUS.ABATED)); + assertThat(policyMsg.getClosedLoopControlName(), equalTo("Control-loop-VoLTE")); + assertThat(policyMsg.getClosedLoopAlarmStart(), is(startTime)); + assertThat(policyMsg.getClosedLoopAlarmEnd(), is(endTime)); + assertThat(policyMsg.getRequestID(), notNullValue()); } @Test @@ -191,7 +199,7 @@ public class DmaapServiceTest { } @Test - public void testDmaapService_getEnrichedPolicyMsg_ok() throws Exception { + public void testDmaapService_getEnrichedPolicyMsg_onset() throws Exception { PowerMock.resetAll(); VmEntity vmEntity = new VmEntity(); vmEntity.setInMaint(false); @@ -201,18 +209,105 @@ public class DmaapServiceTest { VesAlarm vesAlarm = new VesAlarm(); vesAlarm.setEventId("11111"); vesAlarm.setEventName("3333"); + vesAlarm.setSourceId("111"); + vesAlarm.setStartEpochMicrosec(11111L); PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class), anyObject(String.class)).andReturn(null).anyTimes(); PowerMock.replayAll(); PolicyMsg actual = Whitebox - .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, "loopName"); + .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName"); PowerMock.verifyAll(); - assertThat(actual.getClosedLoopControlName(), equalTo(null)); + assertThat(actual.getClosedLoopControlName(), nullValue()); assertThat(actual.getAai().get("vserver.prov-status"), equalTo("prov")); - assertThat(actual.getAai().get("vserver.vserver-name2") == null, equalTo(true)); + assertThat(actual.getAai().get("vserver.vserver-name2"), nullValue()); assertThat(actual.getAai().get("generic-vnf.service-instance-id"), equalTo("")); } + + @Test + public void testDmaapService_getEnrichedPolicyMsg_abated() throws Exception { + PowerMock.resetAll(); + VmEntity vmEntity = new VmEntity(); + vmEntity.setInMaint(false); + vmEntity.setClosedLoopDisable(true); + vmEntity.setProvStatus("prov"); + vmEntity.setResourceVersion("kkkk"); + VesAlarm vesAlarm = new VesAlarm(); + vesAlarm.setEventId("11111"); + vesAlarm.setEventName("3333"); + vesAlarm.setSourceId("111"); + vesAlarm.setStartEpochMicrosec(11111L); + vesAlarm.setLastEpochMicrosec(21111L); + vesAlarm.setAlarmIsCleared(PolicyMassgeConstant.POLICY_MESSAGE_ABATED); + + PowerMock.expectPrivate(dmaapService, "getVnfEntity", anyObject(String.class), + anyObject(String.class)).andReturn(null).anyTimes(); + + PowerMock.replayAll(); + PolicyMsg actual = Whitebox + .invokeMethod(dmaapService, "getEnrichedPolicyMsg", vmEntity, vesAlarm, vesAlarm, "loopName"); + PowerMock.verifyAll(); + + assertThat(actual.getClosedLoopControlName(), nullValue()); + assertThat(actual.getAai().get("vserver.prov-status"), nullValue()); + assertThat(actual.getAai().get("vserver.vserver-name2"), nullValue()); + assertThat(actual.getAai().get("generic-vnf.service-instance-id"), nullValue()); + } + + @Test + public void testPublishPolicyMsg_onset() throws Exception { + PowerMock.resetAll(); + Publisher publisher = PowerMock.createPartialMock(Publisher.class, "publish", PolicyMsg.class); + PolicyMsg policyMsg = new PolicyMsg(); + policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET); + PowerMock.expectNew(Publisher.class).andReturn(publisher); + EasyMock.expect(publisher.publish(policyMsg)).andReturn(true); + + DcaeConfigurationsCache.setDcaeConfigurations( + DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json"))); + + PowerMock.replayAll(); + dmaapService.publishPolicyMsg(policyMsg, "sec_fault_unsecure"); + PowerMock.verifyAll(); + + } + + @Test + public void testPublishPolicyMsg_abated() throws Exception { + PowerMock.resetAll(); + Publisher publisher = PowerMock.createPartialMock(Publisher.class, "publish", PolicyMsg.class); + PolicyMsg policyMsg = new PolicyMsg(); + policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED); + policyMsg.setRequestID("testRequestid"); + uniqueRequestIdCache.put("testAlarmId", "testRequestid"); + PowerMock.expectNew(Publisher.class).andReturn(publisher); + EasyMock.expect(publisher.publish(policyMsg)).andReturn(true); + + DcaeConfigurationsCache.setDcaeConfigurations( + DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json"))); + + PowerMock.replayAll(); + dmaapService.publishPolicyMsg(policyMsg, "sec_fault_unsecure"); + PowerMock.verifyAll(); + + } + + private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException { + URL url = DmaapServiceTest.class.getClassLoader().getResource(fileName); + File configFile = new File(new URI(url.toString()).getPath()); + BufferedReader br = new BufferedReader(new FileReader(configFile)); + + final StringBuilder sb = new StringBuilder(); + br.lines().forEach(line -> { + sb.append(line); + }); + try { + br.close(); + } catch (IOException e) { + // Do nothing + } + return sb.toString(); + } } \ No newline at end of file