summaryrefslogtreecommitdiffstats
path: root/engine-d/src/test
diff options
context:
space:
mode:
authorGuangrongFu <fu.guangrong@zte.com.cn>2018-08-14 17:52:44 +0800
committerGuangrongFu <fu.guangrong@zte.com.cn>2018-08-14 18:08:54 +0800
commit163dd74935802f562ef7272cadf0f88b6a63960a (patch)
treed11148f48d7020129d98a4a464100339aa9b3a5a /engine-d/src/test
parent43ea5d47cc50cfc3200d4f7a1b9748fab3426bf2 (diff)
Optimized Rule Deployment Logic
Change-Id: Iff47a4d028aaad96ac8d7517d721ae43b50a9b89 Issue-ID: HOLMES-156 Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Diffstat (limited to 'engine-d/src/test')
-rw-r--r--engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java272
-rw-r--r--engine-d/src/test/java/org/onap/holmes/engine/resources/EngineResourcesTest.java15
2 files changed, 180 insertions, 107 deletions
diff --git a/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java b/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java
index 71ef172..4ad9107 100644
--- a/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java
+++ b/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java
@@ -1,12 +1,12 @@
/**
* Copyright 2017 ZTE Corporation.
- *
+ * <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ * <p>
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ * <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,23 +16,14 @@
package org.onap.holmes.engine.manager;
-import org.easymock.EasyMock;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.kie.api.KieBase;
-import org.kie.api.KieServices;
-import org.kie.api.builder.KieBuilder;
-import org.kie.api.builder.KieFileSystem;
-import org.kie.api.builder.KieRepository;
-import org.kie.api.io.KieResources;
-import org.kie.api.runtime.KieContainer;
-import org.kie.api.runtime.KieSession;
import org.onap.holmes.common.api.entity.AlarmInfo;
import org.onap.holmes.common.api.entity.CorrelationRule;
import org.onap.holmes.common.api.stat.VesAlarm;
-import org.onap.holmes.common.constant.AlarmConst;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.common.utils.DbDaoUtil;
import org.onap.holmes.engine.db.AlarmInfoDao;
@@ -41,10 +32,12 @@ import org.onap.holmes.engine.wrapper.RuleMgtWrapper;
import org.powermock.api.easymock.PowerMock;
import org.powermock.reflect.Whitebox;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Locale;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.*;
+import java.util.stream.Collectors;
+import static org.easymock.EasyMock.anyObject;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
@@ -56,135 +49,108 @@ public class DroolsEngineTest {
private RuleMgtWrapper ruleMgtWrapper;
- private KieBase kieBase;
- private KieSession kieSession;
- private KieContainer kieContainer;
- private KieFileSystem kfs;
- private KieServices ks;
- private KieBuilder kieBuilder;
- private KieResources resources;
- private KieRepository kieRepository;
-
private AlarmInfoDao alarmInfoDaoMock;
private DroolsEngine droolsEngine;
- private DbDaoUtil dbDaoUtilMock;
+ private DbDaoUtil dbDaoUtilStub;
- @Before
- public void setUp() throws Exception {
+ public DroolsEngineTest() throws Exception {
droolsEngine = new DroolsEngine();
+ ruleMgtWrapper = new RuleMgtWrapperStub();
+ dbDaoUtilStub = new DbDaoUtilStub();
+ Whitebox.setInternalState(droolsEngine, "daoUtil", dbDaoUtilStub);
+ Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper);
+ Whitebox.invokeMethod(droolsEngine, "init");
+ }
- ks = KieServices.Factory.get();
- resources = ks.getResources();
- kieRepository = ks.getRepository();
- kfs = Whitebox.invokeMethod(droolsEngine, "createKieFileSystemWithKProject", ks);
- kieBuilder = ks.newKieBuilder(kfs).buildAll();
- kieContainer = ks.newKieContainer(kieRepository.getDefaultReleaseId());
- kieBase = kieContainer.getKieBase();
- kieSession = kieContainer.newKieSession();
-
- alarmInfoDaoMock = PowerMock.createMock(AlarmInfoDao.class);
- dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class);
-
-
- Whitebox.setInternalState(droolsEngine, "kieBase", kieBase);
- Whitebox.setInternalState(droolsEngine, "kieSession", kieSession);
- Whitebox.setInternalState(droolsEngine, "kieContainer", kieContainer);
- Whitebox.setInternalState(droolsEngine, "kfs", kfs);
- Whitebox.setInternalState(droolsEngine, "ks", ks);
- Whitebox.setInternalState(droolsEngine, "kieBuilder", kieBuilder);
- Whitebox.setInternalState(droolsEngine, "resources", resources);
- Whitebox.setInternalState(droolsEngine, "kieRepository", kieRepository);
-
- Whitebox.setInternalState(droolsEngine, "daoUtil", dbDaoUtilMock);
- Whitebox.setInternalState(droolsEngine, "alarmInfoDao", alarmInfoDaoMock);
-
+ @Before
+ public void setUp() throws Exception {
PowerMock.resetAll();
}
@Test
- public void init() throws Exception {
-
- List<CorrelationRule> rules = new ArrayList<CorrelationRule>();
- CorrelationRule rule = new CorrelationRule();
- rule.setContent("package content");
- rule.setClosedControlLoopName("test");
- rule.setPackageName("org.onap.holmes");
- rules.add(rule);
-
-// expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules);
- List<AlarmInfo> alarmInfoList = new ArrayList<AlarmInfo>();
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(AlarmInfoDao.class)).andReturn(alarmInfoDaoMock).anyTimes();
- EasyMock.expect(alarmInfoDaoMock.queryAllAlarm()).andReturn(alarmInfoList).anyTimes();
- PowerMock.replayAll();
-
- PowerMock.verifyAll();
+ public void deployRule_rule_is_null() throws CorrelationException {
+ thrown.expect(NullPointerException.class);
+ droolsEngine.deployRule(null);
}
@Test
- public void deployRule_rule_is_null() throws CorrelationException {
- Locale locale = PowerMock.createMock(Locale.class);
-
- thrown.expect(NullPointerException.class);
+ public void deployRule_invalid_rule_no_pkg_name() throws CorrelationException {
+ DeployRuleRequest rule = new DeployRuleRequest();
+ rule.setContent("rule123;");
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage("The package name can not be empty.");
- droolsEngine.deployRule(null, locale);
+ droolsEngine.deployRule(rule);
}
@Test
- public void deployRule_kbuilder_has_errors() throws CorrelationException {
+ public void deployRule_invalid_rule_illegal_contents() throws CorrelationException {
DeployRuleRequest rule = new DeployRuleRequest();
- rule.setContent("rule123");
- Locale locale = new Locale(AlarmConst.I18N_EN);
-
+ rule.setContent("package rule123; a random string");
thrown.expect(CorrelationException.class);
- droolsEngine.deployRule(rule, locale);
+ droolsEngine.deployRule(rule);
}
@Test
public void deployRule_package_name_repeat() throws CorrelationException {
DeployRuleRequest rule = new DeployRuleRequest();
rule.setContent("package rule123");
- Locale locale = new Locale(AlarmConst.I18N_EN);
thrown.expect(CorrelationException.class);
-
- droolsEngine.deployRule(rule, locale);
- droolsEngine.deployRule(rule, locale);
+ thrown.expectMessage("A rule with the same package name already exists in the system.");
+ droolsEngine.deployRule(rule);
+ droolsEngine.deployRule(rule);
}
@Test
public void undeployRule_package_name_is_null() throws CorrelationException {
String packageName = null;
- Locale locale = new Locale(AlarmConst.I18N_EN);
-
thrown.expect(CorrelationException.class);
+ thrown.expectMessage("The package name should not be null.");
- droolsEngine.undeployRule(packageName, locale);
+ droolsEngine.undeployRule(packageName);
}
@Test
public void undeployRule_normal() throws CorrelationException {
- Locale locale = new Locale(AlarmConst.I18N_EN);
-
DeployRuleRequest rule = new DeployRuleRequest();
rule.setContent("package rule123");
- droolsEngine.deployRule(rule, locale);
+ droolsEngine.deployRule(rule);
+ droolsEngine.undeployRule("rule123");
+ }
- String packageName = "rule123";
+ @Test
+ public void compileRule_compilation_failure() throws CorrelationException {
+ String content = "invalid contents";
- droolsEngine.undeployRule(packageName, locale);
+ thrown.expect(CorrelationException.class);
+
+ droolsEngine.compileRule(content);
}
@Test
- public void compileRule_kbuilder_has_errors() throws CorrelationException {
- String content = "have error content";
- Locale locale = new Locale(AlarmConst.I18N_EN);
-
+ public void compileRule_compilation_deployed_rule() throws CorrelationException {
+ String content = "package deployed;";
+ DeployRuleRequest rule = new DeployRuleRequest();
+ rule.setContent(content);
+ rule.setLoopControlName(UUID.randomUUID().toString());
thrown.expect(CorrelationException.class);
- droolsEngine.compileRule(content, locale);
+ droolsEngine.deployRule(rule);
+ droolsEngine.compileRule(content);
+ }
+
+ @Test
+ public void compileRule_compilation_normal() throws CorrelationException {
+ String content = "package deployed;";
+ DeployRuleRequest rule = new DeployRuleRequest();
+ rule.setContent(content);
+ rule.setLoopControlName(UUID.randomUUID().toString());
+
+ droolsEngine.compileRule(content);
}
@Test
@@ -221,9 +187,7 @@ public class DroolsEngineTest {
alarmInfo.setRootFlag(0);
alarmInfo.setAlarmIsCleared(1);
- PowerMock.replayAll();
- VesAlarm vesAlarm = Whitebox.invokeMethod(droolsEngine,"convertAlarmInfo2VesAlarm",alarmInfo);
- PowerMock.verifyAll();
+ VesAlarm vesAlarm = Whitebox.invokeMethod(droolsEngine, "convertAlarmInfo2VesAlarm", alarmInfo);
assertThat(vesAlarm.getAlarmIsCleared(), is(1));
assertThat(vesAlarm.getSourceName(), equalTo("sourceName"));
@@ -234,4 +198,114 @@ public class DroolsEngineTest {
assertThat(vesAlarm.getEventId(), equalTo("eventId"));
assertThat(vesAlarm.getRootFlag(), is(0));
}
+
+ @Test
+ public void testConvertVesAlarm2AlarmInfo() throws Exception {
+ VesAlarm vesAlarm = new VesAlarm();
+ vesAlarm.setEventId("eventId");
+ vesAlarm.setEventName("eventName");
+ vesAlarm.setStartEpochMicrosec(1L);
+ vesAlarm.setLastEpochMicrosec(1L);
+ vesAlarm.setSourceId("sourceId");
+ vesAlarm.setSourceName("sourceName");
+ vesAlarm.setRootFlag(0);
+ vesAlarm.setAlarmIsCleared(1);
+
+ AlarmInfo alarmInfo = Whitebox.invokeMethod(droolsEngine, "convertVesAlarm2AlarmInfo", vesAlarm);
+
+ assertThat(alarmInfo.getAlarmIsCleared(), is(1));
+ assertThat(alarmInfo.getSourceName(), equalTo("sourceName"));
+ assertThat(alarmInfo.getSourceId(), equalTo("sourceId"));
+ assertThat(alarmInfo.getStartEpochMicroSec(), is(1L));
+ assertThat(alarmInfo.getLastEpochMicroSec(), is(1L));
+ assertThat(alarmInfo.getEventName(), equalTo("eventName"));
+ assertThat(alarmInfo.getEventId(), equalTo("eventId"));
+ assertThat(alarmInfo.getRootFlag(), is(0));
+ }
+
+ @Test
+ public void testQueryPackagesFromEngine() throws CorrelationException {
+
+ DeployRuleRequest rule = new DeployRuleRequest();
+ rule.setContent("package packageCheck; rule \"test\" when eval(1==1) then System.out.println(1); end");
+ rule.setLoopControlName(UUID.randomUUID().toString());
+
+ droolsEngine.deployRule(rule);
+
+ List<String> packages = droolsEngine.queryPackagesFromEngine();
+
+ assertThat(packages.contains("packageCheck"), is(true));
+ }
+}
+
+class RuleMgtWrapperStub extends RuleMgtWrapper {
+ private List<CorrelationRule> rules;
+
+ public RuleMgtWrapperStub() {
+ rules = new ArrayList<>();
+ CorrelationRule rule = new CorrelationRule();
+ rule.setEnabled(1);
+ rule.setContent("package org.onap.holmes;");
+ rule.setPackageName("UT");
+ rule.setClosedControlLoopName(UUID.randomUUID().toString());
+ rules.add(rule);
+ }
+
+ public List<CorrelationRule> getRules() {
+ return rules;
+ }
+
+ public void setRules(List<CorrelationRule> rules) {
+ this.rules = rules;
+ }
+
+ @Override
+ public List<CorrelationRule> queryRuleByEnable(int enabled) throws CorrelationException {
+ return rules.stream().filter(rule -> rule.getEnabled() == enabled).collect(Collectors.toList());
+ }
+}
+
+class AlarmInfoDaoStub extends AlarmInfoDao {
+
+ private List<AlarmInfo> alarms;
+
+ public AlarmInfoDaoStub() {
+ alarms = new ArrayList<>();
+ AlarmInfo info = new AlarmInfo();
+ info.setEventId("eventId");
+ info.setEventName("eventName");
+ info.setStartEpochMicroSec(1L);
+ info.setLastEpochMicroSec(1L);
+ info.setSourceId("sourceId");
+ info.setSourceName("sourceName");
+ info.setRootFlag(0);
+ info.setAlarmIsCleared(1);
+ alarms.add(info);
+ }
+
+ @Override
+ protected String addAlarm(AlarmInfo alarmInfo) {
+ alarms.add(alarmInfo);
+ return null;
+ }
+
+ @Override
+ protected List<AlarmInfo> queryAlarm() {
+ return alarms;
+ }
+
+ @Override
+ protected int deleteAlarmByAlarmIsCleared(int alarmIsCleared) {
+ return 1;
+ }
+}
+
+class DbDaoUtilStub extends DbDaoUtil {
+ private AlarmInfoDao dao = new AlarmInfoDaoStub();
+ @Override
+ public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {
+
+ return (T)dao;
+
+ }
}
diff --git a/engine-d/src/test/java/org/onap/holmes/engine/resources/EngineResourcesTest.java b/engine-d/src/test/java/org/onap/holmes/engine/resources/EngineResourcesTest.java
index 6be232f..a09eb3a 100644
--- a/engine-d/src/test/java/org/onap/holmes/engine/resources/EngineResourcesTest.java
+++ b/engine-d/src/test/java/org/onap/holmes/engine/resources/EngineResourcesTest.java
@@ -44,7 +44,7 @@ public class EngineResourcesTest {
droolsEngine = PowerMock.createMock(DroolsEngine.class);
engineResources = new EngineResources();
- Whitebox.setInternalState(engineResources,"droolsEngine",droolsEngine);
+ Whitebox.setInternalState(engineResources,"droolsEngine", droolsEngine);
PowerMock.resetAll();
}
@@ -56,7 +56,7 @@ public class EngineResourcesTest {
thrown.expect(WebApplicationException.class);
expect(httpRequest.getHeader("language-option")).andReturn("en_US");
- expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class), anyObject(Locale.class))).
+ expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class))).
andThrow(new CorrelationException(""));
PowerMock.replayAll();
engineResources.deployRule(deployRuleRequest, httpRequest);
@@ -71,8 +71,7 @@ public class EngineResourcesTest {
HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
expect(httpRequest.getHeader("language-option")).andReturn("en_US");
- expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class),
- anyObject(Locale.class))).andReturn("packageName");
+ expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class))).andReturn("packageName");
PowerMock.replayAll();
engineResources.deployRule(deployRuleRequest, httpRequest);
PowerMock.verifyAll();
@@ -86,7 +85,7 @@ public class EngineResourcesTest {
thrown.expect(WebApplicationException.class);
expect(httpRequest.getHeader("language-option")).andReturn("en_US");
- droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class));
+ droolsEngine.undeployRule(anyObject(String.class));
expectLastCall().andThrow(new CorrelationException(""));
PowerMock.replayAll();
engineResources.undeployRule(packageName, httpRequest);
@@ -99,7 +98,7 @@ public class EngineResourcesTest {
HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
expect(httpRequest.getHeader("language-option")).andReturn("en_US");
- droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class));
+ droolsEngine.undeployRule(anyObject(String.class));
PowerMock.replayAll();
engineResources.undeployRule(packageName, httpRequest);
PowerMock.verifyAll();
@@ -113,7 +112,7 @@ public class EngineResourcesTest {
thrown.expect(WebApplicationException.class);
expect(httpRequest.getHeader("language-option")).andReturn("en_US");
- droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class));
+ droolsEngine.compileRule(anyObject(String.class));
expectLastCall().andThrow(new CorrelationException(""));
PowerMock.replayAll();
engineResources.compileRule(compileRuleRequest, httpRequest);
@@ -126,7 +125,7 @@ public class EngineResourcesTest {
HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);
expect(httpRequest.getHeader("language-option")).andReturn("en_US");
- droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class));
+ droolsEngine.compileRule(anyObject(String.class));
PowerMock.replayAll();
engineResources.compileRule(compileRuleRequest, httpRequest);
PowerMock.verifyAll();