Added some codes for UT 00/79000/1
authortangpeng <tang.peng5@zte.com.cn>
Fri, 22 Feb 2019 09:47:23 +0000 (17:47 +0800)
committertangpeng <tang.peng5@zte.com.cn>
Fri, 22 Feb 2019 09:47:23 +0000 (17:47 +0800)
Change-Id: Iaf47ab320511538618c031913dc349679b4ed6b8
Issue-ID: HOLMES-203
Signed-off-by: tangpeng <tang.peng5@zte.com.cn>
holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java
holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java
holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java
holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java

index a8045ec..4b90dd7 100644 (file)
@@ -21,7 +21,6 @@ import java.util.HashMap;
 import java.util.List;\r
 import java.util.Map;\r
 import java.util.Set;\r
-import lombok.NoArgsConstructor;\r
 \r
 public class DcaeConfigurations extends HashMap<String, Object>{\r
 \r
index e3f6e58..ba52262 100644 (file)
@@ -70,19 +70,9 @@ public class DmaapService {
         if (rootAlarm.getAlarmIsCleared() == PolicyMassgeConstant.POLICY_MESSAGE_ONSET) {
             enrichVnfInfo(vmEntity, childAlarm, policyMsg);
             policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET);
-            try {
-                policyMsg.getAai().put("vserver.in-maint", vmEntity.getInMaint());
-            } catch (Exception e) {
-                log.error("Failed to parse the field \"in-maint\". A boolean string (\"true\"/\"false\")"
-                        + " is expected but the actual value is " + vmEntity.getInMaint() + ".", e);
-            }
-            try {
-                policyMsg.getAai().put("vserver.is-closed-loop-disabled",
+            policyMsg.getAai().put("vserver.in-maint", vmEntity.getInMaint());
+            policyMsg.getAai().put("vserver.is-closed-loop-disabled",
                         vmEntity.getClosedLoopDisable());
-            } catch (Exception e) {
-                log.error("Failed to parse the field \"is-closed-loop-disabled\". A boolean string (\"true\"/\"false\")"
-                        + " is expected but the actual value is " + vmEntity.getClosedLoopDisable() + ".", e);
-            }
             policyMsg.getAai().put("vserver.prov-status", vmEntity.getProvStatus());
             policyMsg.getAai().put("vserver.resource-version", vmEntity.getResourceVersion());
         } else {
@@ -196,7 +186,7 @@ public class DmaapService {
                     break;
                 }
             }
-            log.info("Clear alarm, requestId deleted successful");
+            log.info("An alarm is cleared and the corresponding requestId is deleted successfully");
         }
     }
 }
index 827220c..c441942 100644 (file)
@@ -27,12 +27,19 @@ import java.io.IOException;
 import java.net.URI;\r
 import java.net.URISyntaxException;\r
 import java.net.URL;\r
+\r
+import org.junit.Rule;\r
 import org.junit.Test;\r
+import org.junit.rules.ExpectedException;\r
 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;\r
 import org.onap.holmes.common.dcae.entity.SecurityInfo;\r
+import org.onap.holmes.common.exception.CorrelationException;\r
 \r
 public class DcaeConfigurationParserTest {\r
 \r
+    @Rule\r
+    public ExpectedException thrown = ExpectedException.none();\r
+\r
     @Test\r
     public void parse() throws Exception {\r
         DcaeConfigurations obj = DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json"));\r
@@ -43,6 +50,19 @@ public class DcaeConfigurationParserTest {
         assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getDmaapInfo().getLocation(), equalTo("mtl5"));\r
     }\r
 \r
+    @Test\r
+    public void parse_with_empty_contents_excption() throws CorrelationException {\r
+        thrown.expect(CorrelationException.class);\r
+        thrown.expectMessage("Can not resolve configurations from DCAE. The configuration string is empty.");\r
+        DcaeConfigurationParser.parse("");\r
+    }\r
+\r
+    @Test\r
+    public void parse_with_illegal_dcae_response_excption() throws CorrelationException {\r
+        thrown.expect(CorrelationException.class);\r
+        DcaeConfigurationParser.parse("This is an ordinary string.");\r
+    }\r
+\r
     private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException {\r
         URL url = DcaeConfigurationParserTest.class.getClassLoader().getResource(fileName);\r
         File configFile = new File(new URI(url.toString()).getPath());\r
index b47a16f..6494cd8 100644 (file)
@@ -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