From 724a1dcd96ea33a16c0b88446fbc2d7c7d0d5c7d Mon Sep 17 00:00:00 2001 From: XuanweiLiao Date: Thu, 23 Feb 2017 15:13:34 +0800 Subject: Add engine junit test Issue-ID:HOLMES-24 Change-Id: Idf62760a2925f26ec239aaff79271b789d740079 Signed-off-by: XuanweiLiao --- engine-d/pom.xml | 16 + .../openo/holmes/engine/manager/DroolsEngine.java | 24 +- .../holmes/engine/wrapper/RuleMgtWrapper.java | 4 +- .../openo/holmes/engine/EngineDActiveAppTest.java | 27 ++ .../openo/holmes/engine/EnginedAppConfigTest.java | 76 ++++ .../holmes/engine/db/CorrelationRuleDaoTest.java | 50 +++ .../db/mapper/CorrelationRuleMapperTest.java | 59 +++ .../holmes/engine/manager/DroolsEngineTest.java | 421 +++++++++++++++++++++ .../engine/request/CompileRuleRequestTest.java | 34 ++ .../engine/request/DeployRuleRequestTest.java | 42 ++ .../engine/resources/EngineResourcesTest.java | 134 +++++++ .../response/CorrelationRuleResponseTest.java | 35 ++ .../openo/holmes/engine/utils/AlarmUtilTest.java | 138 +++++++ .../holmes/engine/wrapper/RuleMgtWrapperTest.java | 76 ++++ 14 files changed, 1122 insertions(+), 14 deletions(-) create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/EngineDActiveAppTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/EnginedAppConfigTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/db/CorrelationRuleDaoTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapperTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/request/CompileRuleRequestTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/request/DeployRuleRequestTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/resources/EngineResourcesTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/response/CorrelationRuleResponseTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/utils/AlarmUtilTest.java create mode 100644 engine-d/src/test/java/org/openo/holmes/engine/wrapper/RuleMgtWrapperTest.java (limited to 'engine-d') diff --git a/engine-d/pom.xml b/engine-d/pom.xml index bb055bc..727f5dc 100644 --- a/engine-d/pom.xml +++ b/engine-d/pom.xml @@ -81,6 +81,16 @@ io.dropwizard dropwizard-core + + + ch.qos.logback + logback + + + log4j-over-slf4j + org.slf4j + + org.projectlombok @@ -120,6 +130,12 @@ provided + + org.powermock + powermock-api-easymock + 1.4.10 + test + 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 473b929..16ae49c 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 @@ -150,7 +150,9 @@ public class DroolsEngine { StringReader reader = new StringReader(ruleContent); Resource res = ResourceFactory.newReaderResource(reader); - kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + if (kbuilder == null) { + kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + } kbuilder.add(res, ResourceType.DRL); @@ -161,8 +163,6 @@ public class DroolsEngine { throw new EngineException(e); } - kbuilder = null; - ksession.fireAllRules(); } @@ -171,7 +171,9 @@ public class DroolsEngine { StringReader reader = new StringReader(rule.getContent()); Resource res = ResourceFactory.newReaderResource(reader); - kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + if (kbuilder == null) { + kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + } kbuilder.add(res, ResourceType.DRL); @@ -183,9 +185,9 @@ public class DroolsEngine { throw new CorrelationException(errorMsg); } - String packageName = kbuilder.getKnowledgePackages().iterator().next().getName(); + KnowledgePackage kpackage = kbuilder.getKnowledgePackages().iterator().next(); - if (kbase.getKnowledgePackages().contains(packageName)) { + if (kbase.getKnowledgePackages().contains(kpackage)) { String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, I18nProxy.ENGINE_CONTENT_ILLEGALITY,new String[]{ @@ -203,13 +205,10 @@ public class DroolsEngine { throw new CorrelationException(errorMsg, e); } - kbuilder = null; - ksession.fireAllRules(); - return packageName; + return kpackage.getName(); } - public synchronized void undeployRule(String packageName, Locale locale) throws CorrelationException { @@ -237,7 +236,9 @@ public class DroolsEngine { StringReader reader = new StringReader(content); Resource res = ResourceFactory.newReaderResource(reader); - kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + if (kbuilder == null) { + kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + } kbuilder.add(res, ResourceType.DRL); @@ -248,7 +249,6 @@ public class DroolsEngine { log.error(errorMsg); throw new CorrelationException(errorMsg); } - kbuilder = null; } public void putRaisedIntoStream(Alarm raiseAlarm) { diff --git a/engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java b/engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java index 34dab1f..59110d8 100644 --- a/engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java +++ b/engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java @@ -39,8 +39,8 @@ public class RuleMgtWrapper { List ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class) .queryRuleByRuleEnable(enable); - if (ruleTemp != null) { - throw new DbException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME); + if (ruleTemp == null) { + throw new DbException(I18nProxy.RULE_MANAGEMENT_DB_ERROR); } return ruleTemp; } diff --git a/engine-d/src/test/java/org/openo/holmes/engine/EngineDActiveAppTest.java b/engine-d/src/test/java/org/openo/holmes/engine/EngineDActiveAppTest.java new file mode 100644 index 0000000..af79cc6 --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/EngineDActiveAppTest.java @@ -0,0 +1,27 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine; + +/** + * Created by Administrator on 2017/2/20. + */ + +public class EngineDActiveAppTest { + public static void main(String[] args) throws Exception { + String filePath = "C:\\Users\\Administrator\\AppData\\Roaming\\IM\\6092002109\\RecvFiles\\correlation-engine.yml"; + new EngineDActiveApp().run(new String[]{"server", filePath}); + } +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/EnginedAppConfigTest.java b/engine-d/src/test/java/org/openo/holmes/engine/EnginedAppConfigTest.java new file mode 100644 index 0000000..c24894c --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/EnginedAppConfigTest.java @@ -0,0 +1,76 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine; + +import io.dropwizard.db.DataSourceFactory; +import org.hamcrest.core.IsEqual; +import org.hamcrest.core.IsNull; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openo.holmes.common.config.MQConfig; +import org.powermock.api.easymock.PowerMock; + +/** + * Created by Administrator on 2017/2/20. + */ +public class EnginedAppConfigTest { + private EngineDAppConfig engineAppConfig; + + @Before + public void setUp() { + engineAppConfig = new EngineDAppConfig(); + } + + @Test + public void getMqConfig() { + MQConfig mqConfig = PowerMock.createMock(MQConfig.class); + engineAppConfig.setMqConfig(mqConfig); + Assert.assertThat(engineAppConfig.getMqConfig(), IsNull.notNullValue()); + } + + @Test + public void setMqConfig() { + MQConfig mqConfig = PowerMock.createMock(MQConfig.class); + engineAppConfig.setMqConfig(mqConfig); + Assert.assertThat(engineAppConfig.getMqConfig(), IsEqual.equalTo(mqConfig)); + } + + @Test + public void getDataSourceFactory() { + Assert.assertThat(engineAppConfig.getDataSourceFactory(), IsNull.notNullValue()); + } + + @Test + public void setDataSourceFactory() { + DataSourceFactory database = new DataSourceFactory(); + engineAppConfig.setDataSourceFactory(database); + Assert.assertThat(engineAppConfig.getDataSourceFactory(), IsEqual.equalTo(database)); + } + + @Test + public void getApidescription() { + final String apidescription = "Holmes rule management rest API"; + Assert.assertThat(engineAppConfig.getApidescription(),IsEqual.equalTo(apidescription)); + } + + @Test + public void setApidescription() { + final String apidescription = "set api description"; + engineAppConfig.setApidescription(apidescription); + Assert.assertThat(engineAppConfig.getApidescription(),IsEqual.equalTo(apidescription)); + } +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/db/CorrelationRuleDaoTest.java b/engine-d/src/test/java/org/openo/holmes/engine/db/CorrelationRuleDaoTest.java new file mode 100644 index 0000000..3352b08 --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/db/CorrelationRuleDaoTest.java @@ -0,0 +1,50 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine.db; + +import org.easymock.EasyMock; +import org.hamcrest.core.IsNull; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openo.holmes.common.api.entity.CorrelationRule; +import org.powermock.api.easymock.PowerMock; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Administrator on 2017/2/20. + */ +public class CorrelationRuleDaoTest { + private CorrelationRuleDao correlationRuleDao; + + @Before + public void setUp() { + correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class); + } + + @Test + public void queryRuleByEnable() { + int enable = 0; + EasyMock.expect(correlationRuleDao.queryRuleByEnable(EasyMock.anyInt())).andReturn(new ArrayList()); + PowerMock.replayAll(); + List correlationRules = correlationRuleDao.queryRuleByEnable(enable); + PowerMock.verifyAll(); + Assert.assertThat(correlationRules, IsNull.>notNullValue()); + } + +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapperTest.java b/engine-d/src/test/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapperTest.java new file mode 100644 index 0000000..ce8f9bf --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapperTest.java @@ -0,0 +1,59 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine.db.mapper; + +import org.junit.Test; +import org.powermock.api.easymock.PowerMock; + +import java.sql.Date; +import java.sql.ResultSet; +import java.util.Properties; + +import static org.easymock.EasyMock.expect; + +/** + * Created by Administrator on 2017/2/20. + */ +public class CorrelationRuleMapperTest { + + @Test + public void map() throws Exception { + CorrelationRuleMapper mapper = new CorrelationRuleMapper(); + ResultSet resultSet = PowerMock.createMock(ResultSet.class); + expect(resultSet.getString("name")).andReturn(""); + expect(resultSet.getString("rid")).andReturn(""); + expect(resultSet.getString("description")).andReturn(""); + expect(resultSet.getInt("enable")).andReturn(0); + expect(resultSet.getInt("templateID")).andReturn(0); + expect(resultSet.getString("engineID")).andReturn(""); + expect(resultSet.getString("engineType")).andReturn(""); + expect(resultSet.getString("creator")).andReturn(""); + expect(resultSet.getDate("createTime")).andReturn(new Date(System.currentTimeMillis())); + expect(resultSet.getString("updator")).andReturn(""); + expect(resultSet.getDate("updateTime")).andReturn(new Date(System.currentTimeMillis())); + Properties propMock = PowerMock.createMock(Properties.class); + expect(resultSet.getObject("params")).andReturn(propMock); + expect(resultSet.getString("domain")).andReturn(""); + expect(resultSet.getString("content")).andReturn(""); + expect(resultSet.getInt("isManual")).andReturn(0); + expect(resultSet.getString("vendor")).andReturn(""); + expect(resultSet.getString("package")).andReturn(""); + + PowerMock.replayAll(); + mapper.map(0, resultSet, null); + PowerMock.verify(); + } +} 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 new file mode 100644 index 0000000..3877e3f --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java @@ -0,0 +1,421 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine.manager; + +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.runtime.StatefulKnowledgeSession; +import org.drools.runtime.rule.FactHandle; +import org.easymock.EasyMock; +import org.glassfish.hk2.api.IterableProvider; +import org.hamcrest.core.IsEqual; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +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.exception.CorrelationException; +import org.openo.holmes.common.exception.EngineException; +import org.openo.holmes.engine.request.DeployRuleRequest; +import org.openo.holmes.engine.wrapper.RuleMgtWrapper; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import javax.jms.*; +import java.lang.reflect.Method; +import java.util.*; + +import static org.easymock.EasyMock.*; + +/** + * Created by Administrator on 2017/2/20. + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest(DroolsEngine.class) +public class DroolsEngineTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private RuleMgtWrapper ruleMgtWrapper; + + private KnowledgeBase kbase; + + private KnowledgeBaseConfiguration kconf; + + private StatefulKnowledgeSession ksession; + + private KnowledgeBuilder kbuilder; + + private IterableProvider mqConfigProvider; + + private ConnectionFactory connectionFactory; + + private DroolsEngine droolsEngine; + + @Before + public void setUp() { + droolsEngine = new DroolsEngine(); + + 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,"kconf",kconf); + Whitebox.setInternalState(droolsEngine,"ksession",ksession); + Whitebox.setInternalState(droolsEngine,"kbuilder",kbuilder); + Whitebox.setInternalState(droolsEngine,"mqConfigProvider",mqConfigProvider); + Whitebox.setInternalState(droolsEngine,"connectionFactory",connectionFactory); + + PowerMock.resetAll(); + } + + @Test + public void init() throws Exception { + MQConfig mqConfig = new MQConfig(); + mqConfig.brokerIp = "127.0.0.1"; + mqConfig.brokerPort = 4567; + mqConfig.brokerUsername = "admin"; + mqConfig.brokerPassword = "admin"; + expect(mqConfigProvider.get()).andReturn(mqConfig).anyTimes(); + PowerMock.replayAll(); + + Method method = DroolsEngine.class.getDeclaredMethod("init"); + method.setAccessible(true); + method.invoke(droolsEngine); + + PowerMock.verifyAll(); + } + + @Test + public void initDeployRule_exception() throws Exception { + thrown.expect(EngineException.class); + + List rules = new ArrayList(); + CorrelationRule rule = new CorrelationRule(); + rule.setContent("content"); + rules.add(rule); + expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules); + kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class)); + expect(kbuilder.getKnowledgePackages()).andReturn(new ArrayList()); + kbase.addKnowledgePackages(anyObject(Collection.class)); + expectLastCall().andThrow(new RuntimeException("")); + PowerMock.replayAll(); + + Method method = DroolsEngine.class.getDeclaredMethod("initDeployRule"); + method.setAccessible(true); + method.invoke(droolsEngine); + + PowerMock.verifyAll(); + } + + @Test + public void initDeployRule_normal() throws Exception { + List rules = new ArrayList(); + CorrelationRule rule = new CorrelationRule(); + rule.setContent("content"); + rules.add(rule); + expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules); + kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class)); + expect(kbuilder.getKnowledgePackages()).andReturn(new ArrayList()); + kbase.addKnowledgePackages(anyObject(Collection.class)); + expect(ksession.fireAllRules()).andReturn(1); + PowerMock.replayAll(); + + Method method = DroolsEngine.class.getDeclaredMethod("initDeployRule"); + method.setAccessible(true); + method.invoke(droolsEngine); + + PowerMock.verifyAll(); + } + + @Test + public void deployRule_rull_is_null() throws CorrelationException { + Locale locale = PowerMock.createMock(Locale.class); + + thrown.expect(NullPointerException.class); + + droolsEngine.deployRule(null, locale); + } + + @Test + public void deployRule_kbuilder_has_errors() throws CorrelationException { + DeployRuleRequest rule = PowerMock.createMock(DeployRuleRequest.class); + Locale locale = PowerMock.createMock(Locale.class); + + 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); + + thrown.expect(CorrelationException.class); + + KnowledgePackage kPackage = PowerMock.createMock(KnowledgePackage.class); + Collection builderColl = PowerMock.createMock(Collection.class); + Iterator iterator = PowerMock.createMock(Iterator.class); + Collection baseColl = new ArrayList(); + 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 builderColl = PowerMock.createMock(Collection.class); + Iterator iterator = PowerMock.createMock(Iterator.class); + Collection baseColl = new ArrayList(); + 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 deployRule_normal() throws CorrelationException { + DeployRuleRequest rule = PowerMock.createMock(DeployRuleRequest.class); + Locale locale = PowerMock.createMock(Locale.class); + + final String pkgName = "pkgName"; + KnowledgePackage kPackage = PowerMock.createMock(KnowledgePackage.class); + Collection builderColl = PowerMock.createMock(Collection.class); + Iterator iterator = PowerMock.createMock(Iterator.class); + Collection baseColl = new ArrayList(); + 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)); + expect(ksession.fireAllRules()).andReturn(1); + expect(kPackage.getName()).andReturn(pkgName); + + PowerMock.replayAll(); + String resultPkgName = droolsEngine.deployRule(rule, locale); + PowerMock.verifyAll(); + Assert.assertThat(resultPkgName, IsEqual.equalTo(pkgName)); + } + + @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); + + 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); + + KnowledgePackage pkg = PowerMock.createMock(KnowledgePackage.class); + expect(kbase.getKnowledgePackage(anyObject(String.class))).andReturn(pkg); + expect(pkg.getName()).andReturn(""); + kbase.removeKnowledgePackage(anyObject(String.class)); + PowerMock.replayAll(); + droolsEngine.undeployRule(packageName,locale); + PowerMock.verifyAll(); + } + + @Test + public void compileRule_kbuilder_has_errors() throws CorrelationException { + String content = "content"; + Locale locale = PowerMock.createMock(Locale.class); + + 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 compileRule_normal() throws CorrelationException { + String content = "content"; + Locale locale = PowerMock.createMock(Locale.class); + + kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class)); + expect(kbuilder.hasErrors()).andReturn(false); + PowerMock.replayAll(); + droolsEngine.compileRule(content,locale); + PowerMock.verifyAll(); + } + + @Test + public void putRaisedIntoStream_facthandle_is_null() { + Alarm raiseAlarm = new Alarm(); + + 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(raiseAlarm); + PowerMock.verifyAll(); + } + + @Test + public void putRaisedIntoStream_factHandle_is_not_null() { + Alarm raiseAlarm = new Alarm(); + 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(raiseAlarm); + PowerMock.verifyAll(); + } + + @Test + public void listener_run_objmessage_is_null() throws JMSException { + DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); + + Connection connection = PowerMock.createMock(Connection.class); + Session session = PowerMock.createMock(Session.class); + Destination destination = PowerMock.createMock(Topic.class); + MessageConsumer messageConsumer = PowerMock.createMock(MessageConsumer.class); + + expect(connectionFactory.createConnection()).andReturn(connection); + connection.start(); + expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session); + expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination); + expect(session.createConsumer(anyObject(Destination.class))).andReturn(messageConsumer); + expect(messageConsumer.receive(anyLong())).andReturn(null); + + PowerMock.replayAll(); + listener.run(); + PowerMock.verifyAll(); + } + + @Test + public void listener_run_objmessage_is_not_null() throws JMSException { + DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener(); + + Connection connection = PowerMock.createMock(Connection.class); + Session session = PowerMock.createMock(Session.class); + Destination destination = PowerMock.createMock(Topic.class); + MessageConsumer messageConsumer = PowerMock.createMock(MessageConsumer.class); + ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class); + Alarm raiseAlarm = new Alarm(); + + FactHandle factHandle = PowerMock.createMock(FactHandle.class); + + expect(connectionFactory.createConnection()).andReturn(connection); + connection.start(); + expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session); + expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination); + expect(session.createConsumer(anyObject(Destination.class))).andReturn(messageConsumer); + expect(messageConsumer.receive(anyLong())).andReturn(objMessage); + expect(objMessage.getObject()).andReturn(raiseAlarm); + + 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); + + expect(messageConsumer.receive(anyLong())).andReturn(null); + + PowerMock.replayAll(); + listener.run(); + PowerMock.verifyAll(); + } + + @Test + public void stop() throws Exception { + ksession.dispose(); + PowerMock.replayAll(); + droolsEngine.stop(); + PowerMock.verifyAll(); + } +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/request/CompileRuleRequestTest.java b/engine-d/src/test/java/org/openo/holmes/engine/request/CompileRuleRequestTest.java new file mode 100644 index 0000000..dfb49b9 --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/request/CompileRuleRequestTest.java @@ -0,0 +1,34 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine.request; + +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Created by Administrator on 2017/2/20. + */ +public class CompileRuleRequestTest { + @Test + public void getterAndSetter4Content(){ + final String content = "content"; + CompileRuleRequest request = new CompileRuleRequest(); + request.setContent(content); + assertThat(request.getContent(), equalTo(content)); + } +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/request/DeployRuleRequestTest.java b/engine-d/src/test/java/org/openo/holmes/engine/request/DeployRuleRequestTest.java new file mode 100644 index 0000000..131330e --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/request/DeployRuleRequestTest.java @@ -0,0 +1,42 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine.request; + +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Created by Administrator on 2017/2/20. + */ +public class DeployRuleRequestTest { + @Test + public void getterAndSetter4Content(){ + final String content = "content"; + DeployRuleRequest request = new DeployRuleRequest(); + request.setContent(content); + assertThat(request.getContent(), equalTo(content)); + } + + @Test + public void getterAndSetter4Engineid(){ + final String engineid = "engineid"; + DeployRuleRequest request = new DeployRuleRequest(); + request.setEngineId(engineid); + assertThat(request.getEngineId(), equalTo(engineid)); + } +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/resources/EngineResourcesTest.java b/engine-d/src/test/java/org/openo/holmes/engine/resources/EngineResourcesTest.java new file mode 100644 index 0000000..2a40336 --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/resources/EngineResourcesTest.java @@ -0,0 +1,134 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine.resources; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.openo.holmes.common.exception.CorrelationException; +import org.openo.holmes.engine.manager.DroolsEngine; +import org.openo.holmes.engine.request.CompileRuleRequest; +import org.openo.holmes.engine.request.DeployRuleRequest; +import org.powermock.api.easymock.PowerMock; +import org.powermock.reflect.Whitebox; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.WebApplicationException; +import java.util.Locale; + +import static org.easymock.EasyMock.*; + +/** + * Created by Administrator on 2017/2/20. + */ +public class EngineResourcesTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + DroolsEngine droolsEngine; + private EngineResources engineResources; + + @Before + public void setUp() { + droolsEngine = PowerMock.createMock(DroolsEngine.class); + engineResources = new EngineResources(); + + Whitebox.setInternalState(engineResources,"droolsEngine",droolsEngine); + PowerMock.resetAll(); + } + + @Test + public void deployRule_exception() throws CorrelationException { + DeployRuleRequest deployRuleRequest = new DeployRuleRequest(); + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + thrown.expect(WebApplicationException.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class), anyObject(Locale.class))). + andThrow(new CorrelationException("")); + PowerMock.replayAll(); + engineResources.deployRule(deployRuleRequest, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void deployRule_normal() throws CorrelationException { + DeployRuleRequest deployRuleRequest = new DeployRuleRequest(); + 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"); + PowerMock.replayAll(); + engineResources.deployRule(deployRuleRequest, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void undeployRule_exception() throws CorrelationException { + String packageName = "packageName"; + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + thrown.expect(WebApplicationException.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class)); + expectLastCall().andThrow(new CorrelationException("")); + PowerMock.replayAll(); + engineResources.undeployRule(packageName, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void undeployRule_normal() throws CorrelationException { + String packageName = "packageName"; + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class)); + PowerMock.replayAll(); + engineResources.undeployRule(packageName, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void compileRule_exception() throws CorrelationException { + CompileRuleRequest compileRuleRequest = new CompileRuleRequest(); + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + thrown.expect(WebApplicationException.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class)); + expectLastCall().andThrow(new CorrelationException("")); + PowerMock.replayAll(); + engineResources.compileRule(compileRuleRequest, httpRequest); + PowerMock.verifyAll(); + } + + @Test + public void compileRule_normal() throws CorrelationException { + CompileRuleRequest compileRuleRequest = new CompileRuleRequest(); + HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class); + + expect(httpRequest.getHeader("language-option")).andReturn("en_US"); + droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class)); + PowerMock.replayAll(); + engineResources.compileRule(compileRuleRequest, httpRequest); + PowerMock.verifyAll(); + } +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/response/CorrelationRuleResponseTest.java b/engine-d/src/test/java/org/openo/holmes/engine/response/CorrelationRuleResponseTest.java new file mode 100644 index 0000000..45cd19b --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/response/CorrelationRuleResponseTest.java @@ -0,0 +1,35 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine.response; + +import org.junit.Test; +import org.openo.holmes.engine.request.DeployRuleRequest; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Created by Administrator on 2017/2/20. + */ +public class CorrelationRuleResponseTest { + @Test + public void getterAndSetter4RuleId(){ + final String packageName = "package"; + CorrelationRuleResponse request = new CorrelationRuleResponse(); + request.setPackageName(packageName); + assertThat(request.getPackageName(), equalTo(packageName)); + } +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/utils/AlarmUtilTest.java b/engine-d/src/test/java/org/openo/holmes/engine/utils/AlarmUtilTest.java new file mode 100644 index 0000000..d5ef771 --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/utils/AlarmUtilTest.java @@ -0,0 +1,138 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine.utils; + +import org.hamcrest.core.IsEqual; +import org.hamcrest.core.IsNull; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openo.holmes.common.api.stat.Alarm; +import org.openo.holmes.common.producer.MQProducer; +import org.powermock.api.easymock.PowerMock; +import org.powermock.reflect.Whitebox; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Administrator on 2017/2/20. + */ +public class AlarmUtilTest { + private AlarmUtil alarmUtil; + private final Map> rootPriorityMap = new HashMap>(); + private final Map saveRuleMsg = new HashMap(); + + @Before + public void setUp() { + alarmUtil = AlarmUtil.getInstance(); + Whitebox.setInternalState(alarmUtil,"rootPriorityMap",rootPriorityMap); + Whitebox.setInternalState(alarmUtil,"saveRuleMsg",saveRuleMsg); + PowerMock.resetAll(); + } + + @Test + public void getInstance() { + AlarmUtil instance = AlarmUtil.getInstance(); + Assert.assertThat(instance,IsNull.notNullValue()); + } + + @Test + public void equipTypeFilter_is_nullstr() { + String probableCauseStr = "null"; + String equipType = "equipType"; + Alarm alarm = new Alarm(); + boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm); + Assert.assertThat(filter, IsEqual.equalTo(true)); + } + + @Test + public void equipTypeFilter_equals_alarm() { + String probableCauseStr = "11,4567"; + String equipType = "ee,equipType"; + Alarm alarm = new Alarm(); + alarm.setProbableCause(4567); + alarm.setEquipType("equipType"); + boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm); + Assert.assertThat(filter, IsEqual.equalTo(true)); + } + + @Test + public void equipTypeFilter_not_equals_alarm() { + String probableCauseStr = "11,45"; + String equipType = "ee,equipType"; + Alarm alarm = new Alarm(); + alarm.setProbableCause(4567); + alarm.setEquipType("equipType"); + boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm); + Assert.assertThat(filter, IsEqual.equalTo(false)); + } + + @Test + public void getPriority_rootprioritymap_containskey_ruleid() { + String ruleId = "1"; + String probableCauseStr = "11,4567"; + String rootAlarmFeatureStr = "0,1"; + String equipTypeStr = "ee,equipType"; + Alarm alarm = new Alarm(); + + Map map = new HashMap(); + map.put("11-ee", 0); + map.put("4567-equipType", 1); + rootPriorityMap.put(ruleId, map); + + saveRuleMsg.put(ruleId, "11ee0"); + + Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm); + Assert.assertThat(priority,IsEqual.equalTo(0)); + } + + @Test + public void getPriority_rootprioritymap_not_containskey_ruleid() { + String ruleId = "1"; + String probableCauseStr = "11,4567"; + String rootAlarmFeatureStr = "0,1"; + String equipTypeStr = "ee,equipType"; + Alarm alarm = new Alarm(); + + saveRuleMsg.put(ruleId, "11ee0"); + + Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm); + Assert.assertThat(priority,IsEqual.equalTo(0)); + } + + @Test + public void getPriority_priority_is_not_null() { + String ruleId = "1"; + String probableCauseStr = "11,4567"; + String rootAlarmFeatureStr = "1,1"; + String equipTypeStr = "ee,equipType"; + Alarm alarm = new Alarm(); + alarm.setProbableCause(11); + alarm.setEquipType("ee"); + + saveRuleMsg.put(ruleId, "11ee0"); + + Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm); + Assert.assertThat(priority,IsEqual.equalTo(1)); + } + + @Test + public void getMqProducer() { + MQProducer mqProducer = alarmUtil.getMqProducer(); + Assert.assertThat(mqProducer, IsNull.notNullValue()); + } +} diff --git a/engine-d/src/test/java/org/openo/holmes/engine/wrapper/RuleMgtWrapperTest.java b/engine-d/src/test/java/org/openo/holmes/engine/wrapper/RuleMgtWrapperTest.java new file mode 100644 index 0000000..e8c2e7e --- /dev/null +++ b/engine-d/src/test/java/org/openo/holmes/engine/wrapper/RuleMgtWrapperTest.java @@ -0,0 +1,76 @@ +/** + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.engine.wrapper; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.openo.holmes.common.api.entity.CorrelationRule; +import org.openo.holmes.common.exception.DbException; +import org.openo.holmes.common.utils.DbDaoUtil; +import org.openo.holmes.engine.db.CorrelationRuleDao; +import org.powermock.api.easymock.PowerMock; +import org.powermock.reflect.Whitebox; + +import java.util.ArrayList; + +import static org.easymock.EasyMock.*; + +/** + * Created by Administrator on 2017/2/20. + */ +public class RuleMgtWrapperTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private DbDaoUtil daoUtil; + private RuleMgtWrapper ruleMgtWrapper; + + @Before + public void setUp() { + daoUtil = PowerMock.createMock(DbDaoUtil.class); + ruleMgtWrapper = new RuleMgtWrapper(); + + Whitebox.setInternalState(ruleMgtWrapper,"daoUtil",daoUtil); + PowerMock.resetAll(); + } + + @Test + public void queryRuleByEnable_ruletemp_is_null() throws DbException { + int enable = 3; + + thrown.expect(DbException.class); + + CorrelationRuleDao correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class); + expect(daoUtil.getJdbiDaoByOnDemand(anyObject(Class.class))).andReturn(correlationRuleDao); + expect(correlationRuleDao.queryRuleByRuleEnable(anyInt())).andReturn(null); + PowerMock.replayAll(); + ruleMgtWrapper.queryRuleByEnable(enable); + PowerMock.verifyAll(); + } + + @Test + public void queryRuleByEnable_normal() throws DbException { + int enable = 3; + + CorrelationRuleDao correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class); + expect(daoUtil.getJdbiDaoByOnDemand(anyObject(Class.class))).andReturn(correlationRuleDao); + expect(correlationRuleDao.queryRuleByRuleEnable(anyInt())).andReturn(new ArrayList()); + PowerMock.replayAll(); + ruleMgtWrapper.queryRuleByEnable(enable); + PowerMock.verifyAll(); + } +} -- cgit 1.2.3-korg