diff options
author | GuangrongFu <fu.guangrong@zte.com.cn> | 2019-02-23 15:12:48 +0800 |
---|---|---|
committer | GuangrongFu <fu.guangrong@zte.com.cn> | 2019-02-23 15:12:48 +0800 |
commit | 953aee4dbbed4ce9edae8ca9914290b3164b077a (patch) | |
tree | 86626a68cb0ca74cf7e2d07977326242f51af420 /holmes-actions/src/test/java | |
parent | 76e5d677724b13160bb3360b734239192b7be20e (diff) |
Improved the UT coverage
Change-Id: I97c82fac9a9e11095c7d0eab4339ec9633a1090c
Issue-ID: HOLMES-203
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/test/java')
5 files changed, 134 insertions, 15 deletions
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/api/entity/AlarmsCorrelationTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/api/entity/AlarmsCorrelationTest.java index dd940c4..822d74c 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/api/entity/AlarmsCorrelationTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/api/entity/AlarmsCorrelationTest.java @@ -110,7 +110,6 @@ public class AlarmsCorrelationTest { assertThat(alarmsCorrelation.getReserve3(), equalTo(reserve3)); } - @Test public void testToString() throws Exception { final AlarmsCorrelation alarmsCorrelationTemp = new AlarmsCorrelation(); @@ -119,14 +118,4 @@ public class AlarmsCorrelationTest { alarmsCorrelation.setRuleId(ruleId); assertThat(alarmsCorrelation.toString(), equalTo(alarmsCorrelationTemp.toString())); } - - - @Test - public void testEqualsAndHashCode() throws Exception { - final AlarmsCorrelation alarmsCorrelationTemp = new AlarmsCorrelation(); - String ruleId = "ruleId"; - alarmsCorrelationTemp.setRuleId(ruleId); - alarmsCorrelation.setRuleId(ruleId); - assertThat(alarmsCorrelation, equalTo(alarmsCorrelationTemp)); - } -} +} diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/api/entity/CorrelationRuleTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/api/entity/CorrelationRuleTest.java index 1a25a35..c4b605a 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/api/entity/CorrelationRuleTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/api/entity/CorrelationRuleTest.java @@ -150,4 +150,19 @@ public class CorrelationRuleTest { correlationRule.setEngineInstance(engineInstance); assertThat(correlationRule.getEngineInstance(), equalTo(engineInstance)); } + + @Test + public void getterAndSetter4ClosedControlLoopName(){ + final String closedControlLoopName = "closedControlLoopName"; + correlationRule.setClosedControlLoopName(closedControlLoopName); + assertThat(correlationRule.getClosedControlLoopName(), equalTo(closedControlLoopName)); + } + + @Test + public void testClone() { + final String rid = "rid"; + correlationRule.setRid(rid); + CorrelationRule rule = (CorrelationRule) correlationRule.clone(); + assertThat(rule.getRid(), equalTo(rid)); + } } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java index 827220c..c441942 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java @@ -27,12 +27,19 @@ import java.io.IOException; import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
+
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
import org.onap.holmes.common.dcae.entity.SecurityInfo;
+import org.onap.holmes.common.exception.CorrelationException;
public class DcaeConfigurationParserTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Test
public void parse() throws Exception {
DcaeConfigurations obj = DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json"));
@@ -43,6 +50,19 @@ public class DcaeConfigurationParserTest { assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getDmaapInfo().getLocation(), equalTo("mtl5"));
}
+ @Test
+ public void parse_with_empty_contents_excption() throws CorrelationException {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage("Can not resolve configurations from DCAE. The configuration string is empty.");
+ DcaeConfigurationParser.parse("");
+ }
+
+ @Test
+ public void parse_with_illegal_dcae_response_excption() throws CorrelationException {
+ thrown.expect(CorrelationException.class);
+ DcaeConfigurationParser.parse("This is an ordinary string.");
+ }
+
private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException {
URL url = DcaeConfigurationParserTest.class.getClassLoader().getResource(fileName);
File configFile = new File(new URI(url.toString()).getPath());
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 b47a16f..6494cd8 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 @@ -18,11 +18,18 @@ package org.onap.holmes.common.dmaap; import static org.easymock.EasyMock.anyObject; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; +import java.io.*; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.util.ArrayList; import java.util.List; + +import org.easymock.EasyMock; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -34,6 +41,8 @@ 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.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.exception.CorrelationException; @@ -183,7 +192,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); @@ -204,9 +213,94 @@ public class DmaapServiceTest { .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"); + DmaapService.alarmUniqueRequestID.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 diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MSBRegisterUtilTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/MSBRegisterUtilTest.java index 76aad81..ab75e06 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MSBRegisterUtilTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/MSBRegisterUtilTest.java @@ -35,6 +35,7 @@ public class MSBRegisterUtilTest { @Rule
public PowerMockRule powerMockRule = new PowerMockRule();
+
private MSBRegisterUtil msbRegisterUtil = new MSBRegisterUtil();
@Test
|