X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=engine-d%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fholmes%2Fengine%2Fmanager%2FDroolsEngineTest.java;h=4ad910780c4c4f694c7996478f7e821b58c16e28;hb=163dd74935802f562ef7272cadf0f88b6a63960a;hp=885b5e44515b0e7200c6eb446674e941fd3cb295;hpb=3c2a7c4614ed0b925a7ac9d0c2ff4907c89e6b51;p=holmes%2Fengine-management.git
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 885b5e4..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.
- *
+ *
* 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
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* 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,167 +16,157 @@
package org.onap.holmes.engine.manager;
-import static org.easymock.EasyMock.anyInt;
-import static org.easymock.EasyMock.expect;
-
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Locale;
-import org.drools.KnowledgeBase;
-import org.drools.KnowledgeBaseConfiguration;
-import org.drools.KnowledgeBaseFactory;
-import org.drools.conf.EventProcessingOption;
-import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.onap.holmes.common.api.stat.VesAlarm;
-import org.onap.holmes.engine.request.DeployRuleRequest;
+import org.onap.holmes.common.api.entity.AlarmInfo;
import org.onap.holmes.common.api.entity.CorrelationRule;
-import org.onap.holmes.common.constant.AlarmConst;
+import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.DbDaoUtil;
+import org.onap.holmes.engine.db.AlarmInfoDao;
+import org.onap.holmes.engine.request.DeployRuleRequest;
import org.onap.holmes.engine.wrapper.RuleMgtWrapper;
import org.powermock.api.easymock.PowerMock;
-import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.reflect.Whitebox;
+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;
+
public class DroolsEngineTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
- @Rule
- public PowerMockRule powerMockRule = new PowerMockRule();
-
private RuleMgtWrapper ruleMgtWrapper;
- private KnowledgeBase kbase;
-
- private KnowledgeBaseConfiguration kconf;
-
- private StatefulKnowledgeSession ksession;
-
+ private AlarmInfoDao alarmInfoDaoMock;
private DroolsEngine droolsEngine;
- @Before
- public void setUp() {
- droolsEngine = new DroolsEngine();
-
- this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
- this.kconf.setOption(EventProcessingOption.STREAM);
- this.kconf.setProperty("drools.assertBehaviour", "equality");
- this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf);
- this.ksession = kbase.newStatefulKnowledgeSession();
-
- ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class);
+ private DbDaoUtil dbDaoUtilStub;
+ 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");
+ }
- Whitebox.setInternalState(droolsEngine, "kconf", kconf);
- Whitebox.setInternalState(droolsEngine, "kbase", kbase);
- Whitebox.setInternalState(droolsEngine, "ksession", ksession);
-
+ @Before
+ public void setUp() throws Exception {
PowerMock.resetAll();
}
@Test
- public void init() throws Exception {
-
- List rules = new ArrayList();
- CorrelationRule rule = new CorrelationRule();
- rule.setContent("content");
- rules.add(rule);
-
- expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules);
- PowerMock.replayAll();
-
- Method method = DroolsEngine.class.getDeclaredMethod("init");
- method.setAccessible(true);
- method.invoke(droolsEngine);
-
- 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";
+
+ thrown.expect(CorrelationException.class);
- droolsEngine.undeployRule(packageName, locale);
+ 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 putRaisedIntoStream_facthandle_is_null() {
+ 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
+ public void putRaisedIntoStream_facthandle_is_not_null() {
VesAlarm raiseAlarm = new VesAlarm();
- raiseAlarm.setVersion((long) 245235);
+ raiseAlarm.setSourceId("11111");
+ raiseAlarm.setEventName("alarm");
droolsEngine.putRaisedIntoStream(raiseAlarm);
droolsEngine.putRaisedIntoStream(raiseAlarm);
}
@Test
- public void putRaisedIntoStream_factHandle_is_not_null() {
+ public void putRaisedIntoStream_factHandle_is_null() {
VesAlarm raiseAlarm = new VesAlarm();
- raiseAlarm.setVersion((long) 245235);
+ raiseAlarm.setSourceId("11111");
+ raiseAlarm.setEventName("alarm");
droolsEngine.putRaisedIntoStream(raiseAlarm);
}
@@ -184,4 +174,138 @@ public class DroolsEngineTest {
public void stop() throws Exception {
droolsEngine.stop();
}
+
+ @Test
+ public void testConvertAlarmInfo2VesAlarm() throws Exception {
+ AlarmInfo alarmInfo = new AlarmInfo();
+ alarmInfo.setEventId("eventId");
+ alarmInfo.setEventName("eventName");
+ alarmInfo.setStartEpochMicroSec(1L);
+ alarmInfo.setLastEpochMicroSec(1L);
+ alarmInfo.setSourceId("sourceId");
+ alarmInfo.setSourceName("sourceName");
+ alarmInfo.setRootFlag(0);
+ alarmInfo.setAlarmIsCleared(1);
+
+ VesAlarm vesAlarm = Whitebox.invokeMethod(droolsEngine, "convertAlarmInfo2VesAlarm", alarmInfo);
+
+ assertThat(vesAlarm.getAlarmIsCleared(), is(1));
+ assertThat(vesAlarm.getSourceName(), equalTo("sourceName"));
+ assertThat(vesAlarm.getSourceId(), equalTo("sourceId"));
+ assertThat(vesAlarm.getStartEpochMicrosec(), is(1L));
+ assertThat(vesAlarm.getLastEpochMicrosec(), is(1L));
+ assertThat(vesAlarm.getEventName(), equalTo("eventName"));
+ 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 packages = droolsEngine.queryPackagesFromEngine();
+
+ assertThat(packages.contains("packageCheck"), is(true));
+ }
+}
+
+class RuleMgtWrapperStub extends RuleMgtWrapper {
+ private List 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 getRules() {
+ return rules;
+ }
+
+ public void setRules(List rules) {
+ this.rules = rules;
+ }
+
+ @Override
+ public List queryRuleByEnable(int enabled) throws CorrelationException {
+ return rules.stream().filter(rule -> rule.getEnabled() == enabled).collect(Collectors.toList());
+ }
+}
+
+class AlarmInfoDaoStub extends AlarmInfoDao {
+
+ private List 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 queryAlarm() {
+ return alarms;
+ }
+
+ @Override
+ protected int deleteAlarmByAlarmIsCleared(int alarmIsCleared) {
+ return 1;
+ }
+}
+
+class DbDaoUtilStub extends DbDaoUtil {
+ private AlarmInfoDao dao = new AlarmInfoDaoStub();
+ @Override
+ public T getJdbiDaoByOnDemand(Class daoClazz) {
+
+ return (T)dao;
+
+ }
}