Add engine junit test
authorXuanweiLiao <liao.xuanwei@zte.com.cn>
Thu, 23 Feb 2017 07:13:34 +0000 (15:13 +0800)
committerXuanweiLiao <liao.xuanwei@zte.com.cn>
Thu, 23 Feb 2017 08:45:26 +0000 (16:45 +0800)
Issue-ID:HOLMES-24

Change-Id: Idf62760a2925f26ec239aaff79271b789d740079
Signed-off-by: XuanweiLiao <liao.xuanwei@zte.com.cn>
14 files changed:
engine-d/pom.xml
engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java
engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java
engine-d/src/test/java/org/openo/holmes/engine/EngineDActiveAppTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/EnginedAppConfigTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/db/CorrelationRuleDaoTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapperTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/manager/DroolsEngineTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/request/CompileRuleRequestTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/request/DeployRuleRequestTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/resources/EngineResourcesTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/response/CorrelationRuleResponseTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/utils/AlarmUtilTest.java [new file with mode: 0644]
engine-d/src/test/java/org/openo/holmes/engine/wrapper/RuleMgtWrapperTest.java [new file with mode: 0644]

index bb055bc..727f5dc 100644 (file)
         <dependency>
             <groupId>io.dropwizard</groupId>
             <artifactId>dropwizard-core</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>ch.qos.logback</groupId>
+                    <artifactId>logback</artifactId>
+                </exclusion>
+                <exclusion>
+                    <artifactId>log4j-over-slf4j</artifactId>
+                    <groupId>org.slf4j</groupId>
+                </exclusion>
+            </exclusions>
         </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <scope>provided</scope>
         </dependency>
 
+        <dependency>
+            <groupId>org.powermock</groupId>
+            <artifactId>powermock-api-easymock</artifactId>
+            <version>1.4.10</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
index 473b929..16ae49c 100644 (file)
@@ -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) {
index 34dab1f..59110d8 100644 (file)
@@ -39,8 +39,8 @@ public class RuleMgtWrapper {
 
         List<CorrelationRule> 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 (file)
index 0000000..af79cc6
--- /dev/null
@@ -0,0 +1,27 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+\r
+public class EngineDActiveAppTest {\r
+    public static void main(String[] args) throws Exception {\r
+        String filePath = "C:\\Users\\Administrator\\AppData\\Roaming\\IM\\6092002109\\RecvFiles\\correlation-engine.yml";\r
+        new EngineDActiveApp().run(new String[]{"server", filePath});\r
+    }\r
+}\r
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 (file)
index 0000000..c24894c
--- /dev/null
@@ -0,0 +1,76 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine;\r
+\r
+import io.dropwizard.db.DataSourceFactory;\r
+import org.hamcrest.core.IsEqual;\r
+import org.hamcrest.core.IsNull;\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.openo.holmes.common.config.MQConfig;\r
+import org.powermock.api.easymock.PowerMock;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+public class EnginedAppConfigTest {\r
+    private EngineDAppConfig engineAppConfig;\r
+\r
+    @Before\r
+    public void setUp() {\r
+        engineAppConfig = new EngineDAppConfig();\r
+    }\r
+\r
+    @Test\r
+    public void getMqConfig() {\r
+        MQConfig mqConfig = PowerMock.createMock(MQConfig.class);\r
+        engineAppConfig.setMqConfig(mqConfig);\r
+        Assert.assertThat(engineAppConfig.getMqConfig(), IsNull.notNullValue());\r
+    }\r
+\r
+    @Test\r
+    public void setMqConfig() {\r
+        MQConfig mqConfig = PowerMock.createMock(MQConfig.class);\r
+        engineAppConfig.setMqConfig(mqConfig);\r
+        Assert.assertThat(engineAppConfig.getMqConfig(), IsEqual.equalTo(mqConfig));\r
+    }\r
+\r
+    @Test\r
+    public void getDataSourceFactory() {\r
+        Assert.assertThat(engineAppConfig.getDataSourceFactory(), IsNull.<DataSourceFactory>notNullValue());\r
+    }\r
+\r
+    @Test\r
+    public void setDataSourceFactory() {\r
+        DataSourceFactory database = new DataSourceFactory();\r
+        engineAppConfig.setDataSourceFactory(database);\r
+        Assert.assertThat(engineAppConfig.getDataSourceFactory(), IsEqual.equalTo(database));\r
+    }\r
+\r
+    @Test\r
+    public void getApidescription() {\r
+        final String apidescription = "Holmes rule management rest API";\r
+        Assert.assertThat(engineAppConfig.getApidescription(),IsEqual.equalTo(apidescription));\r
+    }\r
+\r
+    @Test\r
+    public void setApidescription() {\r
+        final String apidescription = "set api description";\r
+        engineAppConfig.setApidescription(apidescription);\r
+        Assert.assertThat(engineAppConfig.getApidescription(),IsEqual.equalTo(apidescription));\r
+    }\r
+}\r
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 (file)
index 0000000..3352b08
--- /dev/null
@@ -0,0 +1,50 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine.db;\r
+\r
+import org.easymock.EasyMock;\r
+import org.hamcrest.core.IsNull;\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.openo.holmes.common.api.entity.CorrelationRule;\r
+import org.powermock.api.easymock.PowerMock;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+public class CorrelationRuleDaoTest {\r
+    private CorrelationRuleDao correlationRuleDao;\r
+\r
+    @Before\r
+    public void setUp() {\r
+        correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class);\r
+    }\r
+\r
+    @Test\r
+    public void queryRuleByEnable() {\r
+        int enable = 0;\r
+        EasyMock.expect(correlationRuleDao.queryRuleByEnable(EasyMock.anyInt())).andReturn(new ArrayList<CorrelationRule>());\r
+        PowerMock.replayAll();\r
+        List<CorrelationRule> correlationRules = correlationRuleDao.queryRuleByEnable(enable);\r
+        PowerMock.verifyAll();\r
+        Assert.assertThat(correlationRules, IsNull.<List<CorrelationRule>>notNullValue());\r
+    }\r
+\r
+}\r
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 (file)
index 0000000..ce8f9bf
--- /dev/null
@@ -0,0 +1,59 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine.db.mapper;\r
+\r
+import org.junit.Test;\r
+import org.powermock.api.easymock.PowerMock;\r
+\r
+import java.sql.Date;\r
+import java.sql.ResultSet;\r
+import java.util.Properties;\r
+\r
+import static org.easymock.EasyMock.expect;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+public class CorrelationRuleMapperTest {\r
+\r
+    @Test\r
+    public void map() throws Exception {\r
+        CorrelationRuleMapper mapper = new CorrelationRuleMapper();\r
+        ResultSet resultSet = PowerMock.createMock(ResultSet.class);\r
+        expect(resultSet.getString("name")).andReturn("");\r
+        expect(resultSet.getString("rid")).andReturn("");\r
+        expect(resultSet.getString("description")).andReturn("");\r
+        expect(resultSet.getInt("enable")).andReturn(0);\r
+        expect(resultSet.getInt("templateID")).andReturn(0);\r
+        expect(resultSet.getString("engineID")).andReturn("");\r
+        expect(resultSet.getString("engineType")).andReturn("");\r
+        expect(resultSet.getString("creator")).andReturn("");\r
+        expect(resultSet.getDate("createTime")).andReturn(new Date(System.currentTimeMillis()));\r
+        expect(resultSet.getString("updator")).andReturn("");\r
+        expect(resultSet.getDate("updateTime")).andReturn(new Date(System.currentTimeMillis()));\r
+        Properties propMock = PowerMock.createMock(Properties.class);\r
+        expect(resultSet.getObject("params")).andReturn(propMock);\r
+        expect(resultSet.getString("domain")).andReturn("");\r
+        expect(resultSet.getString("content")).andReturn("");\r
+        expect(resultSet.getInt("isManual")).andReturn(0);\r
+        expect(resultSet.getString("vendor")).andReturn("");\r
+        expect(resultSet.getString("package")).andReturn("");\r
+\r
+        PowerMock.replayAll();\r
+        mapper.map(0, resultSet, null);\r
+        PowerMock.verify();\r
+    }\r
+}\r
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 (file)
index 0000000..3877e3f
--- /dev/null
@@ -0,0 +1,421 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine.manager;\r
+\r
+import org.drools.KnowledgeBase;\r
+import org.drools.KnowledgeBaseConfiguration;\r
+import org.drools.builder.KnowledgeBuilder;\r
+import org.drools.builder.KnowledgeBuilderErrors;\r
+import org.drools.builder.ResourceType;\r
+import org.drools.definition.KnowledgePackage;\r
+import org.drools.io.Resource;\r
+import org.drools.runtime.StatefulKnowledgeSession;\r
+import org.drools.runtime.rule.FactHandle;\r
+import org.easymock.EasyMock;\r
+import org.glassfish.hk2.api.IterableProvider;\r
+import org.hamcrest.core.IsEqual;\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Rule;\r
+import org.junit.Test;\r
+import org.junit.rules.ExpectedException;\r
+import org.junit.runner.RunWith;\r
+import org.openo.holmes.common.api.entity.CorrelationRule;\r
+import org.openo.holmes.common.api.stat.Alarm;\r
+import org.openo.holmes.common.config.MQConfig;\r
+import org.openo.holmes.common.exception.CorrelationException;\r
+import org.openo.holmes.common.exception.EngineException;\r
+import org.openo.holmes.engine.request.DeployRuleRequest;\r
+import org.openo.holmes.engine.wrapper.RuleMgtWrapper;\r
+import org.powermock.api.easymock.PowerMock;\r
+import org.powermock.core.classloader.annotations.PrepareForTest;\r
+import org.powermock.modules.junit4.PowerMockRunner;\r
+import org.powermock.reflect.Whitebox;\r
+\r
+import javax.jms.*;\r
+import java.lang.reflect.Method;\r
+import java.util.*;\r
+\r
+import static org.easymock.EasyMock.*;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+@RunWith(PowerMockRunner.class)\r
+@PrepareForTest(DroolsEngine.class)\r
+public class DroolsEngineTest {\r
+    @Rule\r
+    public ExpectedException thrown = ExpectedException.none();\r
+\r
+    private RuleMgtWrapper ruleMgtWrapper;\r
+\r
+    private KnowledgeBase kbase;\r
+\r
+    private KnowledgeBaseConfiguration kconf;\r
+\r
+    private StatefulKnowledgeSession ksession;\r
+\r
+    private KnowledgeBuilder kbuilder;\r
+\r
+    private IterableProvider<MQConfig> mqConfigProvider;\r
+\r
+    private ConnectionFactory connectionFactory;\r
+\r
+    private DroolsEngine droolsEngine;\r
+\r
+    @Before\r
+    public void setUp() {\r
+        droolsEngine =  new DroolsEngine();\r
+\r
+        ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class);\r
+        kbase = PowerMock.createMock(KnowledgeBase.class);\r
+        kconf = PowerMock.createMock(KnowledgeBaseConfiguration.class);\r
+        ksession = PowerMock.createMock(StatefulKnowledgeSession.class);\r
+        kbuilder = PowerMock.createMock(KnowledgeBuilder.class);\r
+        mqConfigProvider = PowerMock.createMock(IterableProvider.class);\r
+        connectionFactory = PowerMock.createMock(ConnectionFactory.class);\r
+\r
+        Whitebox.setInternalState(droolsEngine,"ruleMgtWrapper",ruleMgtWrapper);\r
+        Whitebox.setInternalState(droolsEngine,"kbase",kbase);\r
+        Whitebox.setInternalState(droolsEngine,"kconf",kconf);\r
+        Whitebox.setInternalState(droolsEngine,"ksession",ksession);\r
+        Whitebox.setInternalState(droolsEngine,"kbuilder",kbuilder);\r
+        Whitebox.setInternalState(droolsEngine,"mqConfigProvider",mqConfigProvider);\r
+        Whitebox.setInternalState(droolsEngine,"connectionFactory",connectionFactory);\r
+\r
+        PowerMock.resetAll();\r
+    }\r
+\r
+    @Test\r
+    public void init() throws Exception {\r
+        MQConfig mqConfig = new MQConfig();\r
+        mqConfig.brokerIp = "127.0.0.1";\r
+        mqConfig.brokerPort = 4567;\r
+        mqConfig.brokerUsername = "admin";\r
+        mqConfig.brokerPassword = "admin";\r
+        expect(mqConfigProvider.get()).andReturn(mqConfig).anyTimes();\r
+        PowerMock.replayAll();\r
+\r
+        Method method = DroolsEngine.class.getDeclaredMethod("init");\r
+        method.setAccessible(true);\r
+        method.invoke(droolsEngine);\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void initDeployRule_exception() throws Exception {\r
+        thrown.expect(EngineException.class);\r
+\r
+        List<CorrelationRule> rules = new ArrayList<CorrelationRule>();\r
+        CorrelationRule rule = new CorrelationRule();\r
+        rule.setContent("content");\r
+        rules.add(rule);\r
+        expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules);\r
+        kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));\r
+        expect(kbuilder.getKnowledgePackages()).andReturn(new ArrayList<KnowledgePackage>());\r
+        kbase.addKnowledgePackages(anyObject(Collection.class));\r
+        expectLastCall().andThrow(new RuntimeException(""));\r
+        PowerMock.replayAll();\r
+\r
+        Method method = DroolsEngine.class.getDeclaredMethod("initDeployRule");\r
+        method.setAccessible(true);\r
+        method.invoke(droolsEngine);\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void initDeployRule_normal() throws Exception {\r
+        List<CorrelationRule> rules = new ArrayList<CorrelationRule>();\r
+        CorrelationRule rule = new CorrelationRule();\r
+        rule.setContent("content");\r
+        rules.add(rule);\r
+        expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules);\r
+        kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));\r
+        expect(kbuilder.getKnowledgePackages()).andReturn(new ArrayList<KnowledgePackage>());\r
+        kbase.addKnowledgePackages(anyObject(Collection.class));\r
+        expect(ksession.fireAllRules()).andReturn(1);\r
+        PowerMock.replayAll();\r
+\r
+        Method method = DroolsEngine.class.getDeclaredMethod("initDeployRule");\r
+        method.setAccessible(true);\r
+        method.invoke(droolsEngine);\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deployRule_rull_is_null() throws CorrelationException {\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        thrown.expect(NullPointerException.class);\r
+\r
+        droolsEngine.deployRule(null, locale);\r
+    }\r
+\r
+    @Test\r
+    public void deployRule_kbuilder_has_errors() throws CorrelationException {\r
+        DeployRuleRequest rule = PowerMock.createMock(DeployRuleRequest.class);\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        thrown.expect(CorrelationException.class);\r
+\r
+        KnowledgeBuilderErrors errors = PowerMock.createMock(KnowledgeBuilderErrors.class);\r
+        expect(rule.getContent()).andReturn("rule");\r
+        kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));\r
+        expect(kbuilder.hasErrors()).andReturn(true);\r
+        expect(kbuilder.getErrors()).andReturn(errors);\r
+        PowerMock.replayAll();\r
+        droolsEngine.deployRule(rule, locale);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deployRule_kbase_knowledgePackages_contains_package() throws CorrelationException {\r
+        DeployRuleRequest rule = PowerMock.createMock(DeployRuleRequest.class);\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        thrown.expect(CorrelationException.class);\r
+\r
+        KnowledgePackage kPackage = PowerMock.createMock(KnowledgePackage.class);\r
+        Collection<KnowledgePackage> builderColl = PowerMock.createMock(Collection.class);\r
+        Iterator<KnowledgePackage> iterator = PowerMock.createMock(Iterator.class);\r
+        Collection<KnowledgePackage> baseColl = new ArrayList<KnowledgePackage>();\r
+        baseColl.add(kPackage);\r
+        expect(rule.getContent()).andReturn("rule");\r
+        expect(kbuilder.hasErrors()).andReturn(false);\r
+        kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));\r
+        expect(kbuilder.getKnowledgePackages()).andReturn(builderColl);\r
+        expect(builderColl.iterator()).andReturn(iterator);\r
+        expect(iterator.next()).andReturn(kPackage);\r
+        expect(kbase.getKnowledgePackages()).andReturn(baseColl);\r
+        PowerMock.replayAll();\r
+        droolsEngine.deployRule(rule, locale);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deployRule_add_knowledge_packages_exception() throws CorrelationException {\r
+        DeployRuleRequest rule = PowerMock.createMock(DeployRuleRequest.class);\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        thrown.expect(CorrelationException.class);\r
+\r
+        KnowledgePackage kPackage = PowerMock.createMock(KnowledgePackage.class);\r
+        Collection<KnowledgePackage> builderColl = PowerMock.createMock(Collection.class);\r
+        Iterator<KnowledgePackage> iterator = PowerMock.createMock(Iterator.class);\r
+        Collection<KnowledgePackage> baseColl = new ArrayList<KnowledgePackage>();\r
+        expect(rule.getContent()).andReturn("rule");\r
+        expect(kbuilder.hasErrors()).andReturn(false);\r
+        kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));\r
+        expect(kbuilder.getKnowledgePackages()).andReturn(builderColl).times(2);\r
+        expect(builderColl.iterator()).andReturn(iterator);\r
+        expect(iterator.next()).andReturn(kPackage);\r
+        expect(kbase.getKnowledgePackages()).andReturn(baseColl);\r
+        kbase.addKnowledgePackages(anyObject(Collection.class));\r
+        EasyMock.expectLastCall().andThrow(new RuntimeException(""));\r
+        PowerMock.replayAll();\r
+        droolsEngine.deployRule(rule, locale);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deployRule_normal() throws CorrelationException {\r
+        DeployRuleRequest rule = PowerMock.createMock(DeployRuleRequest.class);\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        final String pkgName = "pkgName";\r
+        KnowledgePackage kPackage = PowerMock.createMock(KnowledgePackage.class);\r
+        Collection<KnowledgePackage> builderColl = PowerMock.createMock(Collection.class);\r
+        Iterator<KnowledgePackage> iterator = PowerMock.createMock(Iterator.class);\r
+        Collection<KnowledgePackage> baseColl = new ArrayList<KnowledgePackage>();\r
+        expect(rule.getContent()).andReturn("rule");\r
+        expect(kbuilder.hasErrors()).andReturn(false);\r
+        kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));\r
+        expect(kbuilder.getKnowledgePackages()).andReturn(builderColl).times(2);\r
+        expect(builderColl.iterator()).andReturn(iterator);\r
+        expect(iterator.next()).andReturn(kPackage);\r
+        expect(kbase.getKnowledgePackages()).andReturn(baseColl);\r
+        kbase.addKnowledgePackages(anyObject(Collection.class));\r
+        expect(ksession.fireAllRules()).andReturn(1);\r
+        expect(kPackage.getName()).andReturn(pkgName);\r
+\r
+        PowerMock.replayAll();\r
+        String resultPkgName = droolsEngine.deployRule(rule, locale);\r
+        PowerMock.verifyAll();\r
+        Assert.assertThat(resultPkgName, IsEqual.equalTo(pkgName));\r
+    }\r
+\r
+    @Test\r
+    public void undeployRule_knowledgepackage_is_null() throws CorrelationException {\r
+        String packageName = "packageName";\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        thrown.expect(CorrelationException.class);\r
+\r
+        expect(kbase.getKnowledgePackage(anyObject(String.class))).andReturn(null);\r
+        PowerMock.replayAll();\r
+        droolsEngine.undeployRule(packageName,locale);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void undeployRule_remove_knowledge_package_exception() throws CorrelationException {\r
+        String packageName = "packageName";\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        thrown.expect(CorrelationException.class);\r
+\r
+        KnowledgePackage pkg = PowerMock.createMock(KnowledgePackage.class);\r
+        expect(kbase.getKnowledgePackage(anyObject(String.class))).andReturn(pkg);\r
+        expect(pkg.getName()).andReturn("");\r
+        kbase.removeKnowledgePackage(anyObject(String.class));\r
+        EasyMock.expectLastCall().andThrow(new RuntimeException(""));\r
+        PowerMock.replayAll();\r
+        droolsEngine.undeployRule(packageName,locale);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void undeployRule_normal() throws CorrelationException {\r
+        String packageName = "packageName";\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        KnowledgePackage pkg = PowerMock.createMock(KnowledgePackage.class);\r
+        expect(kbase.getKnowledgePackage(anyObject(String.class))).andReturn(pkg);\r
+        expect(pkg.getName()).andReturn("");\r
+        kbase.removeKnowledgePackage(anyObject(String.class));\r
+        PowerMock.replayAll();\r
+        droolsEngine.undeployRule(packageName,locale);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void compileRule_kbuilder_has_errors() throws CorrelationException {\r
+        String content = "content";\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        thrown.expect(CorrelationException.class);\r
+\r
+        KnowledgeBuilderErrors errors = PowerMock.createMock(KnowledgeBuilderErrors.class);\r
+        kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));\r
+        expect(kbuilder.hasErrors()).andReturn(true);\r
+        expect(kbuilder.getErrors()).andReturn(errors);\r
+        PowerMock.replayAll();\r
+        droolsEngine.compileRule(content,locale);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void compileRule_normal() throws CorrelationException {\r
+        String content = "content";\r
+        Locale locale = PowerMock.createMock(Locale.class);\r
+\r
+        kbuilder.add(anyObject(Resource.class), anyObject(ResourceType.class));\r
+        expect(kbuilder.hasErrors()).andReturn(false);\r
+        PowerMock.replayAll();\r
+        droolsEngine.compileRule(content,locale);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void putRaisedIntoStream_facthandle_is_null() {\r
+        Alarm raiseAlarm = new Alarm();\r
+\r
+        expect(ksession.getFactHandle(anyObject(Alarm.class))).andReturn(null);\r
+        expect(ksession.insert(anyObject(Alarm.class))).andReturn(null);\r
+        expect(ksession.fireAllRules()).andReturn(0);\r
+        PowerMock.replayAll();\r
+        droolsEngine.putRaisedIntoStream(raiseAlarm);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void putRaisedIntoStream_factHandle_is_not_null() {\r
+        Alarm raiseAlarm = new Alarm();\r
+        FactHandle factHandle = PowerMock.createMock(FactHandle.class);\r
+        expect(ksession.getFactHandle(anyObject(Alarm.class))).andReturn(factHandle);\r
+        ksession.retract(anyObject(FactHandle.class));\r
+        expect(ksession.insert(anyObject(Alarm.class))).andReturn(null);\r
+        expect(ksession.fireAllRules()).andReturn(0);\r
+        PowerMock.replayAll();\r
+        droolsEngine.putRaisedIntoStream(raiseAlarm);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void listener_run_objmessage_is_null() throws JMSException {\r
+        DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener();\r
+\r
+        Connection connection = PowerMock.createMock(Connection.class);\r
+        Session session = PowerMock.createMock(Session.class);\r
+        Destination destination = PowerMock.createMock(Topic.class);\r
+        MessageConsumer messageConsumer = PowerMock.createMock(MessageConsumer.class);\r
+\r
+        expect(connectionFactory.createConnection()).andReturn(connection);\r
+        connection.start();\r
+        expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);\r
+        expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);\r
+        expect(session.createConsumer(anyObject(Destination.class))).andReturn(messageConsumer);\r
+        expect(messageConsumer.receive(anyLong())).andReturn(null);\r
+\r
+        PowerMock.replayAll();\r
+        listener.run();\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void listener_run_objmessage_is_not_null() throws JMSException {\r
+        DroolsEngine.AlarmMqMessageListener listener = droolsEngine.new AlarmMqMessageListener();\r
+\r
+        Connection connection = PowerMock.createMock(Connection.class);\r
+        Session session = PowerMock.createMock(Session.class);\r
+        Destination destination = PowerMock.createMock(Topic.class);\r
+        MessageConsumer messageConsumer = PowerMock.createMock(MessageConsumer.class);\r
+        ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class);\r
+        Alarm raiseAlarm = new Alarm();\r
+\r
+        FactHandle factHandle = PowerMock.createMock(FactHandle.class);\r
+\r
+        expect(connectionFactory.createConnection()).andReturn(connection);\r
+        connection.start();\r
+        expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);\r
+        expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);\r
+        expect(session.createConsumer(anyObject(Destination.class))).andReturn(messageConsumer);\r
+        expect(messageConsumer.receive(anyLong())).andReturn(objMessage);\r
+        expect(objMessage.getObject()).andReturn(raiseAlarm);\r
+\r
+        expect(ksession.getFactHandle(anyObject(Alarm.class))).andReturn(factHandle);\r
+        ksession.retract(anyObject(FactHandle.class));\r
+        expect(ksession.insert(anyObject(Alarm.class))).andReturn(null);\r
+        expect(ksession.fireAllRules()).andReturn(0);\r
+\r
+        expect(messageConsumer.receive(anyLong())).andReturn(null);\r
+\r
+        PowerMock.replayAll();\r
+        listener.run();\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void stop() throws Exception {\r
+        ksession.dispose();\r
+        PowerMock.replayAll();\r
+        droolsEngine.stop();\r
+        PowerMock.verifyAll();\r
+    }\r
+}\r
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 (file)
index 0000000..dfb49b9
--- /dev/null
@@ -0,0 +1,34 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine.request;\r
+\r
+import org.junit.Test;\r
+\r
+import static org.hamcrest.core.IsEqual.equalTo;\r
+import static org.junit.Assert.assertThat;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+public class CompileRuleRequestTest {\r
+    @Test\r
+    public void getterAndSetter4Content(){\r
+        final String content = "content";\r
+        CompileRuleRequest request = new CompileRuleRequest();\r
+        request.setContent(content);\r
+        assertThat(request.getContent(), equalTo(content));\r
+    }\r
+}\r
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 (file)
index 0000000..131330e
--- /dev/null
@@ -0,0 +1,42 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine.request;\r
+\r
+import org.junit.Test;\r
+\r
+import static org.hamcrest.core.IsEqual.equalTo;\r
+import static org.junit.Assert.assertThat;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+public class DeployRuleRequestTest {\r
+    @Test\r
+    public void getterAndSetter4Content(){\r
+        final String content = "content";\r
+        DeployRuleRequest request = new DeployRuleRequest();\r
+        request.setContent(content);\r
+        assertThat(request.getContent(), equalTo(content));\r
+    }\r
+\r
+    @Test\r
+    public void getterAndSetter4Engineid(){\r
+        final String engineid = "engineid";\r
+        DeployRuleRequest request = new DeployRuleRequest();\r
+        request.setEngineId(engineid);\r
+        assertThat(request.getEngineId(), equalTo(engineid));\r
+    }\r
+}\r
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 (file)
index 0000000..2a40336
--- /dev/null
@@ -0,0 +1,134 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine.resources;\r
+\r
+import org.junit.Before;\r
+import org.junit.Rule;\r
+import org.junit.Test;\r
+import org.junit.rules.ExpectedException;\r
+import org.openo.holmes.common.exception.CorrelationException;\r
+import org.openo.holmes.engine.manager.DroolsEngine;\r
+import org.openo.holmes.engine.request.CompileRuleRequest;\r
+import org.openo.holmes.engine.request.DeployRuleRequest;\r
+import org.powermock.api.easymock.PowerMock;\r
+import org.powermock.reflect.Whitebox;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.ws.rs.WebApplicationException;\r
+import java.util.Locale;\r
+\r
+import static org.easymock.EasyMock.*;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+public class EngineResourcesTest {\r
+    @Rule\r
+    public ExpectedException thrown = ExpectedException.none();\r
+    DroolsEngine droolsEngine;\r
+    private EngineResources engineResources;\r
+\r
+    @Before\r
+    public void setUp() {\r
+        droolsEngine = PowerMock.createMock(DroolsEngine.class);\r
+        engineResources = new EngineResources();\r
+\r
+        Whitebox.setInternalState(engineResources,"droolsEngine",droolsEngine);\r
+        PowerMock.resetAll();\r
+    }\r
+\r
+    @Test\r
+    public void deployRule_exception() throws CorrelationException {\r
+        DeployRuleRequest deployRuleRequest = new DeployRuleRequest();\r
+        HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+        thrown.expect(WebApplicationException.class);\r
+\r
+        expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+        expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class), anyObject(Locale.class))).\r
+                andThrow(new CorrelationException(""));\r
+        PowerMock.replayAll();\r
+        engineResources.deployRule(deployRuleRequest, httpRequest);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void deployRule_normal() throws CorrelationException {\r
+        DeployRuleRequest deployRuleRequest = new DeployRuleRequest();\r
+        HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+        expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+        expect(droolsEngine.deployRule(anyObject(DeployRuleRequest.class),\r
+                anyObject(Locale.class))).andReturn("packageName");\r
+        PowerMock.replayAll();\r
+        engineResources.deployRule(deployRuleRequest, httpRequest);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void undeployRule_exception() throws CorrelationException {\r
+        String packageName = "packageName";\r
+        HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+        thrown.expect(WebApplicationException.class);\r
+\r
+        expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+        droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class));\r
+        expectLastCall().andThrow(new CorrelationException(""));\r
+        PowerMock.replayAll();\r
+        engineResources.undeployRule(packageName, httpRequest);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void undeployRule_normal() throws CorrelationException {\r
+        String packageName = "packageName";\r
+        HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+        expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+        droolsEngine.undeployRule(anyObject(String.class), anyObject(Locale.class));\r
+        PowerMock.replayAll();\r
+        engineResources.undeployRule(packageName, httpRequest);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void compileRule_exception() throws CorrelationException {\r
+        CompileRuleRequest compileRuleRequest = new CompileRuleRequest();\r
+        HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+        thrown.expect(WebApplicationException.class);\r
+\r
+        expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+        droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class));\r
+        expectLastCall().andThrow(new CorrelationException(""));\r
+        PowerMock.replayAll();\r
+        engineResources.compileRule(compileRuleRequest, httpRequest);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void compileRule_normal() throws CorrelationException {\r
+        CompileRuleRequest compileRuleRequest = new CompileRuleRequest();\r
+        HttpServletRequest httpRequest = PowerMock.createMock(HttpServletRequest.class);\r
+\r
+        expect(httpRequest.getHeader("language-option")).andReturn("en_US");\r
+        droolsEngine.compileRule(anyObject(String.class),anyObject(Locale.class));\r
+        PowerMock.replayAll();\r
+        engineResources.compileRule(compileRuleRequest, httpRequest);\r
+        PowerMock.verifyAll();\r
+    }\r
+}\r
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 (file)
index 0000000..45cd19b
--- /dev/null
@@ -0,0 +1,35 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine.response;\r
+\r
+import org.junit.Test;\r
+import org.openo.holmes.engine.request.DeployRuleRequest;\r
+\r
+import static org.hamcrest.core.IsEqual.equalTo;\r
+import static org.junit.Assert.assertThat;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+public class CorrelationRuleResponseTest {\r
+    @Test\r
+    public void getterAndSetter4RuleId(){\r
+        final String packageName = "package";\r
+        CorrelationRuleResponse request = new CorrelationRuleResponse();\r
+        request.setPackageName(packageName);\r
+        assertThat(request.getPackageName(), equalTo(packageName));\r
+    }\r
+}\r
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 (file)
index 0000000..d5ef771
--- /dev/null
@@ -0,0 +1,138 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine.utils;\r
+\r
+import org.hamcrest.core.IsEqual;\r
+import org.hamcrest.core.IsNull;\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.openo.holmes.common.api.stat.Alarm;\r
+import org.openo.holmes.common.producer.MQProducer;\r
+import org.powermock.api.easymock.PowerMock;\r
+import org.powermock.reflect.Whitebox;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+public class AlarmUtilTest {\r
+    private AlarmUtil alarmUtil;\r
+    private final Map<String, Map<String, Integer>> rootPriorityMap = new HashMap<String, Map<String, Integer>>();\r
+    private final Map<String, String> saveRuleMsg = new HashMap<String, String>();\r
+\r
+    @Before\r
+    public void setUp() {\r
+        alarmUtil = AlarmUtil.getInstance();\r
+        Whitebox.setInternalState(alarmUtil,"rootPriorityMap",rootPriorityMap);\r
+        Whitebox.setInternalState(alarmUtil,"saveRuleMsg",saveRuleMsg);\r
+        PowerMock.resetAll();\r
+    }\r
+\r
+    @Test\r
+    public void getInstance() {\r
+        AlarmUtil instance = AlarmUtil.getInstance();\r
+        Assert.assertThat(instance,IsNull.<AlarmUtil>notNullValue());\r
+    }\r
+\r
+    @Test\r
+    public void equipTypeFilter_is_nullstr() {\r
+        String probableCauseStr = "null";\r
+        String equipType = "equipType";\r
+        Alarm alarm = new Alarm();\r
+        boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm);\r
+        Assert.assertThat(filter, IsEqual.equalTo(true));\r
+    }\r
+\r
+    @Test\r
+    public void equipTypeFilter_equals_alarm() {\r
+        String probableCauseStr = "11,4567";\r
+        String equipType = "ee,equipType";\r
+        Alarm alarm = new Alarm();\r
+        alarm.setProbableCause(4567);\r
+        alarm.setEquipType("equipType");\r
+        boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm);\r
+        Assert.assertThat(filter, IsEqual.equalTo(true));\r
+    }\r
+\r
+    @Test\r
+    public void equipTypeFilter_not_equals_alarm() {\r
+        String probableCauseStr = "11,45";\r
+        String equipType = "ee,equipType";\r
+        Alarm alarm = new Alarm();\r
+        alarm.setProbableCause(4567);\r
+        alarm.setEquipType("equipType");\r
+        boolean filter = alarmUtil.equipTypeFilter(probableCauseStr, equipType, alarm);\r
+        Assert.assertThat(filter, IsEqual.equalTo(false));\r
+    }\r
+\r
+    @Test\r
+    public void getPriority_rootprioritymap_containskey_ruleid() {\r
+        String ruleId = "1";\r
+        String probableCauseStr = "11,4567";\r
+        String rootAlarmFeatureStr = "0,1";\r
+        String equipTypeStr = "ee,equipType";\r
+        Alarm alarm = new Alarm();\r
+\r
+        Map<String, Integer> map = new HashMap<String, Integer>();\r
+        map.put("11-ee", 0);\r
+        map.put("4567-equipType", 1);\r
+        rootPriorityMap.put(ruleId, map);\r
+\r
+        saveRuleMsg.put(ruleId, "11ee0");\r
+\r
+        Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm);\r
+        Assert.assertThat(priority,IsEqual.equalTo(0));\r
+    }\r
+\r
+    @Test\r
+    public void getPriority_rootprioritymap_not_containskey_ruleid() {\r
+        String ruleId = "1";\r
+        String probableCauseStr = "11,4567";\r
+        String rootAlarmFeatureStr = "0,1";\r
+        String equipTypeStr = "ee,equipType";\r
+        Alarm alarm = new Alarm();\r
+\r
+        saveRuleMsg.put(ruleId, "11ee0");\r
+\r
+        Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm);\r
+        Assert.assertThat(priority,IsEqual.equalTo(0));\r
+    }\r
+\r
+    @Test\r
+    public void getPriority_priority_is_not_null() {\r
+        String ruleId = "1";\r
+        String probableCauseStr = "11,4567";\r
+        String rootAlarmFeatureStr = "1,1";\r
+        String equipTypeStr = "ee,equipType";\r
+        Alarm alarm = new Alarm();\r
+        alarm.setProbableCause(11);\r
+        alarm.setEquipType("ee");\r
+\r
+        saveRuleMsg.put(ruleId, "11ee0");\r
+\r
+        Integer priority = alarmUtil.getPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr, alarm);\r
+        Assert.assertThat(priority,IsEqual.equalTo(1));\r
+    }\r
+\r
+    @Test\r
+    public void getMqProducer() {\r
+        MQProducer mqProducer = alarmUtil.getMqProducer();\r
+        Assert.assertThat(mqProducer, IsNull.<MQProducer>notNullValue());\r
+    }\r
+}\r
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 (file)
index 0000000..e8c2e7e
--- /dev/null
@@ -0,0 +1,76 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.holmes.engine.wrapper;\r
+\r
+import org.junit.Before;\r
+import org.junit.Rule;\r
+import org.junit.Test;\r
+import org.junit.rules.ExpectedException;\r
+import org.openo.holmes.common.api.entity.CorrelationRule;\r
+import org.openo.holmes.common.exception.DbException;\r
+import org.openo.holmes.common.utils.DbDaoUtil;\r
+import org.openo.holmes.engine.db.CorrelationRuleDao;\r
+import org.powermock.api.easymock.PowerMock;\r
+import org.powermock.reflect.Whitebox;\r
+\r
+import java.util.ArrayList;\r
+\r
+import static org.easymock.EasyMock.*;\r
+\r
+/**\r
+ * Created by Administrator on 2017/2/20.\r
+ */\r
+public class RuleMgtWrapperTest {\r
+    @Rule\r
+    public ExpectedException thrown = ExpectedException.none();\r
+    private DbDaoUtil daoUtil;\r
+    private RuleMgtWrapper ruleMgtWrapper;\r
+\r
+    @Before\r
+    public void setUp() {\r
+        daoUtil = PowerMock.createMock(DbDaoUtil.class);\r
+        ruleMgtWrapper = new RuleMgtWrapper();\r
+\r
+        Whitebox.setInternalState(ruleMgtWrapper,"daoUtil",daoUtil);\r
+        PowerMock.resetAll();\r
+    }\r
+\r
+    @Test\r
+    public void queryRuleByEnable_ruletemp_is_null() throws DbException {\r
+        int enable = 3;\r
+\r
+        thrown.expect(DbException.class);\r
+\r
+        CorrelationRuleDao correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class);\r
+        expect(daoUtil.getJdbiDaoByOnDemand(anyObject(Class.class))).andReturn(correlationRuleDao);\r
+        expect(correlationRuleDao.queryRuleByRuleEnable(anyInt())).andReturn(null);\r
+        PowerMock.replayAll();\r
+        ruleMgtWrapper.queryRuleByEnable(enable);\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    @Test\r
+    public void queryRuleByEnable_normal() throws DbException {\r
+        int enable = 3;\r
+\r
+        CorrelationRuleDao correlationRuleDao = PowerMock.createMock(CorrelationRuleDao.class);\r
+        expect(daoUtil.getJdbiDaoByOnDemand(anyObject(Class.class))).andReturn(correlationRuleDao);\r
+        expect(correlationRuleDao.queryRuleByRuleEnable(anyInt())).andReturn(new ArrayList<CorrelationRule>());\r
+        PowerMock.replayAll();\r
+        ruleMgtWrapper.queryRuleByEnable(enable);\r
+        PowerMock.verifyAll();\r
+    }\r
+}\r