summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFengLiang <feng.liang1@zte.com.cn>2017-03-15 12:06:57 +0800
committerFengLiang <feng.liang1@zte.com.cn>2017-03-15 12:06:57 +0800
commit972fddd339d102b237b0647f2514cbc974414489 (patch)
tree5e6de0ff497a6cbe423266d218b1b16e29d13a19
parente16b222eaaf94b75d669863f93ab48c509eae7ad (diff)
Fix test and restructure
Change-Id: Ia971f87d86f5b849aca68ac96606beec6091e2fb Issue-ID: HOLMES-47 Signed-off-by: FengLiang <feng.liang1@zte.com.cn>
-rw-r--r--engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java79
-rw-r--r--engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java185
2 files changed, 68 insertions, 196 deletions
diff --git a/engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java b/engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java
index ad1b4e5..a97cae9 100644
--- a/engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java
+++ b/engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java
@@ -18,8 +18,10 @@ package org.openo.holmes.engine.manager;
import java.io.Serializable;
import java.io.StringReader;
+import java.util.HashSet;
import java.util.List;
import java.util.Locale;
+import java.util.Set;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jms.Connection;
@@ -62,21 +64,14 @@ import org.openo.holmes.engine.wrapper.RuleMgtWrapper;
public class DroolsEngine {
private final static int ENABLE = 1;
-
+ private final Set<String> packageNames = new HashSet<String>();
@Inject
private RuleMgtWrapper ruleMgtWrapper;
-
private KnowledgeBase kbase;
-
private KnowledgeBaseConfiguration kconf;
-
private StatefulKnowledgeSession ksession;
-
- private KnowledgeBuilder kbuilder;
-
@Inject
private IterableProvider<MQConfig> mqConfigProvider;
-
private ConnectionFactory connectionFactory;
@PostConstruct
@@ -94,9 +89,9 @@ public class DroolsEngine {
private void registerAlarmTopicListener() {
String brokerURL =
- "tcp://" + mqConfigProvider.get().brokerIp + ":" + mqConfigProvider.get().brokerPort;
+ "tcp://" + mqConfigProvider.get().brokerIp + ":" + mqConfigProvider.get().brokerPort;
connectionFactory = new ActiveMQConnectionFactory(mqConfigProvider.get().brokerUsername,
- mqConfigProvider.get().brokerPassword, brokerURL);
+ mqConfigProvider.get().brokerPassword, brokerURL);
AlarmMqMessageListener listener = new AlarmMqMessageListener();
listener.receive();
@@ -123,8 +118,6 @@ public class DroolsEngine {
this.kconf.setProperty("drools.assertBehaviour", "equality");
- this.kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
-
this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf);
this.ksession = kbase.newStatefulKnowledgeSession();
@@ -147,7 +140,7 @@ public class DroolsEngine {
StringReader reader = new StringReader(ruleContent);
Resource res = ResourceFactory.newReaderResource(reader);
- kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(res, ResourceType.DRL);
@@ -161,55 +154,40 @@ public class DroolsEngine {
}
public synchronized String deployRule(DeployRuleRequest rule, Locale locale)
- throws CorrelationException {
+ throws CorrelationException {
StringReader reader = new StringReader(rule.getContent());
Resource res = ResourceFactory.newReaderResource(reader);
- kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(res, ResourceType.DRL);
- if (kbuilder.hasErrors()) {
-
- String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
- I18nProxy.ENGINE_CONTENT_ILLEGALITY,
- new String[]{kbuilder.getErrors().toString()});
- throw new CorrelationException(errorMsg);
- }
-
- KnowledgePackage kpackage = kbuilder.getKnowledgePackages().iterator().next();
+ judgeRuleContent(locale, kbuilder);
- if (kbase.getKnowledgePackages().contains(kpackage)) {
-
- String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
- I18nProxy.ENGINE_CONTENT_ILLEGALITY, new String[]{
- I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_CONTAINS_PACKAGE)});
-
- throw new CorrelationException(errorMsg);
- }
+ String packageName = kbuilder.getKnowledgePackages().iterator().next().getName();
try {
-
+ packageNames.add(packageName);
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
} catch (Exception e) {
String errorMsg =
- I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_DEPLOY_RULE_FAILED);
+ I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_DEPLOY_RULE_FAILED);
throw new CorrelationException(errorMsg, e);
}
ksession.fireAllRules();
- return kpackage.getName();
+ return packageName;
}
public synchronized void undeployRule(String packageName, Locale locale)
- throws CorrelationException {
+ throws CorrelationException {
KnowledgePackage pkg = kbase.getKnowledgePackage(packageName);
if (null == pkg) {
String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
- I18nProxy.ENGINE_DELETE_RULE_NULL,
- new String[]{packageName});
+ I18nProxy.ENGINE_DELETE_RULE_NULL,
+ new String[]{packageName});
throw new CorrelationException(errorMsg);
}
@@ -218,27 +196,42 @@ public class DroolsEngine {
kbase.removeKnowledgePackage(pkg.getName());
} catch (Exception e) {
String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
- I18nProxy.ENGINE_DELETE_RULE_FAILED, new String[]{packageName});
+ I18nProxy.ENGINE_DELETE_RULE_FAILED, new String[]{packageName});
throw new CorrelationException(errorMsg, e);
}
}
public void compileRule(String content, Locale locale)
- throws CorrelationException {
+ throws CorrelationException {
StringReader reader = new StringReader(content);
Resource res = ResourceFactory.newReaderResource(reader);
- kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(res, ResourceType.DRL);
+ judgeRuleContent(locale, kbuilder);
+ }
+
+ private void judgeRuleContent(Locale locale, KnowledgeBuilder kbuilder)
+ throws CorrelationException {
if (kbuilder.hasErrors()) {
String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
- I18nProxy.ENGINE_CONTENT_ILLEGALITY,
- new String[]{kbuilder.getErrors().toString()});
+ I18nProxy.ENGINE_CONTENT_ILLEGALITY,
+ new String[]{kbuilder.getErrors().toString()});
log.error(errorMsg);
throw new CorrelationException(errorMsg);
}
+
+ String packageName = kbuilder.getKnowledgePackages().iterator().next().getName();
+
+ if (packageNames.contains(packageName)) {
+ String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
+ I18nProxy.ENGINE_CONTENT_ILLEGALITY, new String[]{
+ I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_CONTAINS_PACKAGE)});
+
+ throw new CorrelationException(errorMsg);
+ }
}
public void putRaisedIntoStream(Alarm raiseAlarm) {
diff --git a/engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java b/engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java
index 44de609..54d9bf5 100644
--- a/engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java
+++ b/engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java
@@ -23,8 +23,6 @@ import static org.easymock.EasyMock.expect;
import java.lang.reflect.Method;
import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.jms.Connection;
@@ -37,13 +35,9 @@ import javax.jms.Topic;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
-import org.drools.builder.KnowledgeBuilder;
-import org.drools.builder.KnowledgeBuilderErrors;
-import org.drools.builder.ResourceType;
-import org.drools.definition.KnowledgePackage;
-import org.drools.io.Resource;
+import org.drools.KnowledgeBaseFactory;
+import org.drools.conf.EventProcessingOption;
import org.drools.runtime.StatefulKnowledgeSession;
-import org.drools.runtime.rule.FactHandle;
import org.easymock.EasyMock;
import org.glassfish.hk2.api.IterableProvider;
import org.junit.Before;
@@ -53,6 +47,7 @@ import org.junit.rules.ExpectedException;
import org.openo.holmes.common.api.entity.CorrelationRule;
import org.openo.holmes.common.api.stat.Alarm;
import org.openo.holmes.common.config.MQConfig;
+import org.openo.holmes.common.constant.AlarmConst;
import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.engine.request.DeployRuleRequest;
import org.openo.holmes.engine.wrapper.RuleMgtWrapper;
@@ -76,8 +71,6 @@ public class DroolsEngineTest {
private StatefulKnowledgeSession ksession;
- private KnowledgeBuilder kbuilder;
-
private IterableProvider<MQConfig> mqConfigProvider;
private ConnectionFactory connectionFactory;
@@ -88,20 +81,21 @@ public class DroolsEngineTest {
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);
- kbase = PowerMock.createMock(KnowledgeBase.class);
- kconf = PowerMock.createMock(KnowledgeBaseConfiguration.class);
- ksession = PowerMock.createMock(StatefulKnowledgeSession.class);
- kbuilder = PowerMock.createMock(KnowledgeBuilder.class);
mqConfigProvider = PowerMock.createMock(IterableProvider.class);
connectionFactory = PowerMock.createMock(ConnectionFactory.class);
Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper);
- Whitebox.setInternalState(droolsEngine, "kbase", kbase);
+ Whitebox.setInternalState(droolsEngine, "mqConfigProvider", mqConfigProvider);
Whitebox.setInternalState(droolsEngine, "kconf", kconf);
+ Whitebox.setInternalState(droolsEngine, "kbase", kbase);
Whitebox.setInternalState(droolsEngine, "ksession", ksession);
- Whitebox.setInternalState(droolsEngine, "kbuilder", kbuilder);
- Whitebox.setInternalState(droolsEngine, "mqConfigProvider", mqConfigProvider);
Whitebox.setInternalState(droolsEngine, "connectionFactory", connectionFactory);
PowerMock.resetAll();
@@ -141,177 +135,71 @@ public class DroolsEngineTest {
@Test
public void deployRule_kbuilder_has_errors() throws CorrelationException {
- DeployRuleRequest rule = PowerMock.createMock(DeployRuleRequest.class);
- Locale locale = PowerMock.createMock(Locale.class);
+ DeployRuleRequest rule = new DeployRuleRequest();
+ rule.setContent("rule123");
+ Locale locale = new Locale(AlarmConst.I18N_EN);
thrown.expect(CorrelationException.class);
- KnowledgeBuilderErrors errors = PowerMock.createMock(KnowledgeBuilderErrors.class);
- expect(rule.getContent()).andReturn("rule");
- kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));
- expect(kbuilder.hasErrors()).andReturn(true);
- expect(kbuilder.getErrors()).andReturn(errors);
-
- PowerMock.replayAll();
-
droolsEngine.deployRule(rule, locale);
-
- PowerMock.verifyAll();
}
@Test
- public void deployRule_kbase_knowledgePackages_contains_package() throws CorrelationException {
- DeployRuleRequest rule = PowerMock.createMock(DeployRuleRequest.class);
- Locale locale = PowerMock.createMock(Locale.class);
+ 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);
- KnowledgePackage kPackage = PowerMock.createMock(KnowledgePackage.class);
- Collection<KnowledgePackage> builderColl = PowerMock.createMock(Collection.class);
- Iterator<KnowledgePackage> iterator = PowerMock.createMock(Iterator.class);
- Collection<KnowledgePackage> baseColl = new ArrayList<KnowledgePackage>();
- baseColl.add(kPackage);
- expect(rule.getContent()).andReturn("rule");
- expect(kbuilder.hasErrors()).andReturn(false);
- kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));
- expect(kbuilder.getKnowledgePackages()).andReturn(builderColl);
- expect(builderColl.iterator()).andReturn(iterator);
- expect(iterator.next()).andReturn(kPackage);
- expect(kbase.getKnowledgePackages()).andReturn(baseColl);
-
- PowerMock.replayAll();
-
droolsEngine.deployRule(rule, locale);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void deployRule_add_knowledge_packages_exception() throws CorrelationException {
- DeployRuleRequest rule = PowerMock.createMock(DeployRuleRequest.class);
- Locale locale = PowerMock.createMock(Locale.class);
-
- thrown.expect(CorrelationException.class);
-
- KnowledgePackage kPackage = PowerMock.createMock(KnowledgePackage.class);
- Collection<KnowledgePackage> builderColl = PowerMock.createMock(Collection.class);
- Iterator<KnowledgePackage> iterator = PowerMock.createMock(Iterator.class);
- Collection<KnowledgePackage> baseColl = new ArrayList<KnowledgePackage>();
- expect(rule.getContent()).andReturn("rule");
- expect(kbuilder.hasErrors()).andReturn(false);
- kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));
- expect(kbuilder.getKnowledgePackages()).andReturn(builderColl).times(2);
- expect(builderColl.iterator()).andReturn(iterator);
- expect(iterator.next()).andReturn(kPackage);
- expect(kbase.getKnowledgePackages()).andReturn(baseColl);
- kbase.addKnowledgePackages(anyObject(Collection.class));
- EasyMock.expectLastCall().andThrow(new RuntimeException(""));
-
- PowerMock.replayAll();
-
droolsEngine.deployRule(rule, locale);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void undeployRule_knowledgepackage_is_null() throws CorrelationException {
- String packageName = "packageName";
- Locale locale = PowerMock.createMock(Locale.class);
-
- thrown.expect(CorrelationException.class);
-
- expect(kbase.getKnowledgePackage(anyObject(String.class))).andReturn(null);
-
- PowerMock.replayAll();
-
- droolsEngine.undeployRule(packageName, locale);
-
- PowerMock.verifyAll();
}
@Test
- public void undeployRule_remove_knowledge_package_exception() throws CorrelationException {
- String packageName = "packageName";
- Locale locale = PowerMock.createMock(Locale.class);
+ public void undeployRule_package_name_is_null() throws CorrelationException {
+ String packageName = null;
+ Locale locale = new Locale(AlarmConst.I18N_EN);
thrown.expect(CorrelationException.class);
- KnowledgePackage pkg = PowerMock.createMock(KnowledgePackage.class);
- expect(kbase.getKnowledgePackage(anyObject(String.class))).andReturn(pkg);
- expect(pkg.getName()).andReturn("");
- kbase.removeKnowledgePackage(anyObject(String.class));
- EasyMock.expectLastCall().andThrow(new RuntimeException(""));
-
- PowerMock.replayAll();
-
droolsEngine.undeployRule(packageName, locale);
-
- PowerMock.verifyAll();
}
@Test
public void undeployRule_normal() throws CorrelationException {
- String packageName = "packageName";
- Locale locale = PowerMock.createMock(Locale.class);
+ Locale locale = new Locale(AlarmConst.I18N_EN);
- KnowledgePackage pkg = PowerMock.createMock(KnowledgePackage.class);
- expect(kbase.getKnowledgePackage(anyObject(String.class))).andReturn(pkg);
- expect(pkg.getName()).andReturn("");
- kbase.removeKnowledgePackage(anyObject(String.class));
+ DeployRuleRequest rule = new DeployRuleRequest();
+ rule.setContent("package rule123");
+ droolsEngine.deployRule(rule, locale);
- PowerMock.replayAll();
+ String packageName = "rule123";
droolsEngine.undeployRule(packageName, locale);
-
- PowerMock.verifyAll();
}
@Test
public void compileRule_kbuilder_has_errors() throws CorrelationException {
- String content = "content";
- Locale locale = PowerMock.createMock(Locale.class);
+ String content = "have error content";
+ Locale locale = new Locale(AlarmConst.I18N_EN);
thrown.expect(CorrelationException.class);
- KnowledgeBuilderErrors errors = PowerMock.createMock(KnowledgeBuilderErrors.class);
- kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));
- expect(kbuilder.hasErrors()).andReturn(true);
- expect(kbuilder.getErrors()).andReturn(errors);
-
- PowerMock.replayAll();
-
droolsEngine.compileRule(content, locale);
-
- PowerMock.verifyAll();
}
+
@Test
public void putRaisedIntoStream_facthandle_is_null() {
- expect(ksession.getFactHandle(anyObject(Alarm.class))).andReturn(null);
- expect(ksession.insert(anyObject(Alarm.class))).andReturn(null);
- expect(ksession.fireAllRules()).andReturn(0);
-
- PowerMock.replayAll();
-
- droolsEngine.putRaisedIntoStream(new Alarm());
-
- PowerMock.verifyAll();
+ Alarm raiseAlarm = new Alarm();
+ droolsEngine.putRaisedIntoStream(raiseAlarm);
+ droolsEngine.putRaisedIntoStream(raiseAlarm);
}
@Test
public void putRaisedIntoStream_factHandle_is_not_null() {
- FactHandle factHandle = PowerMock.createMock(FactHandle.class);
- expect(ksession.getFactHandle(anyObject(Alarm.class))).andReturn(factHandle);
- ksession.retract(anyObject(FactHandle.class));
- expect(ksession.insert(anyObject(Alarm.class))).andReturn(null);
- expect(ksession.fireAllRules()).andReturn(0);
-
- PowerMock.replayAll();
-
droolsEngine.putRaisedIntoStream(new Alarm());
-
- PowerMock.verifyAll();
}
@@ -422,16 +310,7 @@ public class DroolsEngineTest {
ActiveMQObjectMessage objectMessage = new ActiveMQObjectMessage();
objectMessage.setObject(alarm);
- expect(ksession.getFactHandle(anyObject(Alarm.class))).andReturn(null);
-
- expect(ksession.insert(anyObject(Alarm.class))).andReturn(null);
- expect(ksession.fireAllRules()).andReturn(1);
-
- PowerMock.replayAll();
-
listener.onMessage(objectMessage);
-
- PowerMock.verifyAll();
}
@Test