bugfix - rule sync issues when engine redeployed 28/123428/1
authorGuangrongFu <fu.guangrong@zte.com.cn>
Sat, 21 Aug 2021 08:58:53 +0000 (16:58 +0800)
committerGuangrongFu <fu.guangrong@zte.com.cn>
Sat, 21 Aug 2021 09:00:42 +0000 (17:00 +0800)
Issue-ID: HOLMES-462
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Change-Id: I082b0df4215fd13b7313b3093494c34c7daa7c9c

rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleAllocator.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapper.java
rulemgt/src/test/java/org/onap/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java

index 2dc05ee..31c200a 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright 2017-2020 ZTE Corporation.
+ * Copyright 2017-2021 ZTE Corporation.
  * <p>
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
 
@@ -41,6 +42,8 @@ public class RuleAllocator {
     private static final Logger LOGGER = LoggerFactory.getLogger(RuleAllocator.class);
 
     public final static int ENABLE = 1;
+    public final static int RETRY_TIMES = 5;
+    public final static long RETRY_INTERVAL_SEC = 15;
     private RuleMgtWrapper ruleMgtWrapper;
     private RuleQueryWrapper ruleQueryWrapper;
     private EngineWrapper engineWrapper;
@@ -194,7 +197,7 @@ public class RuleAllocator {
 
     // Sorted by the number of rules each engine contains, in a descending order.
     private List<String> sortIpByRuleNumDesc(List<String> ips) {
-        List<CorrelationRule> rules = null;
+        List<CorrelationRule> rules;
         Map<String, Integer> ruleNumOfEngines = new HashMap();
 
         try {
@@ -219,12 +222,27 @@ public class RuleAllocator {
     }
 
     private void allocateRule(CorrelationRule rule, String ip) throws CorrelationException {
-        try {
-            ruleMgtWrapper.deployRule2Engine(rule, ip);
-            correlationRuleDao.updateRule(rule);
-        } catch (CorrelationException e) {
-            throw new CorrelationException(String.format("Failed to allocate rule <%s> to <%s>",
-                    rule.getName(), ip), e);
+        // Retry for a couple of times in case of deployment failure
+        // due to unfinished initialization procedures of engine instances.
+        for (int i = 0; i <= RETRY_TIMES; ++i) {
+            try {
+                ruleMgtWrapper.deployRule2Engine(rule, ip);
+                correlationRuleDao.updateRule(rule);
+                // If the codes reach here, it means everything's okay. There's no need to run the loop more.
+                break;
+            } catch (CorrelationException e) {
+                LOGGER.warn(String.format("Failed to allocate rule <%s> to <%s>. Retry: %d.",
+                        rule.getName(), ip, i), e);
+                if (i == RETRY_TIMES) {
+                    throw new CorrelationException(String.format("Failed to allocate rule <%s> to <%s>",
+                            rule.getName(), ip), e);
+                }
+                try {
+                    SECONDS.sleep(RETRY_INTERVAL_SEC * (i + 1));
+                } catch (InterruptedException interruptedException) {
+                    LOGGER.info(interruptedException.getMessage(), interruptedException);
+                }
+            }
         }
     }
 
index 5ac4fe6..73a2f2b 100644 (file)
@@ -1,29 +1,29 @@
-/**\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.onap.holmes.rulemgt.constant;\r
-\r
-public class RuleMgtConstant {\r
-\r
-    private RuleMgtConstant() {\r
-\r
-    }\r
-    public static final int STATUS_RULE_OPEN = 1;\r
-    public static final int STATUS_RULE_CLOSE = 0;\r
-    public static final int STATUS_RULE_ALL = 2;\r
-    public static final String PACKAGE = "packageName";\r
-    public static final String ENGINE_PATH = "/api/holmes-engine-mgmt/v1/rule";\r
-    public static final int RESPONSE_STATUS_OK = 200;\r
-}\r
+/**
+ * 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.onap.holmes.rulemgt.constant;
+
+public class RuleMgtConstant {
+
+    private RuleMgtConstant() {
+
+    }
+    public static final int STATUS_ENABLED = 1;
+    public static final int STATUS_DISABLED = 0;
+    public static final int STATUS_RULE_ALL = 2;
+    public static final String PACKAGE = "packageName";
+    public static final String ENGINE_PATH = "/api/holmes-engine-mgmt/v1/rule";
+    public static final int RESPONSE_STATUS_OK = 200;
+}
index 22982da..7fdae27 100644 (file)
-/**\r
- * Copyright 2017 ZTE Corporation.\r
- * <p>\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
- * <p>\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- * <p>\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.onap.holmes.rulemgt.wrapper;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Date;\r
-import java.util.List;\r
-import javax.annotation.PostConstruct;\r
-import javax.inject.Inject;\r
-import javax.inject.Singleton;\r
-\r
-import lombok.extern.slf4j.Slf4j;\r
-import org.jvnet.hk2.annotations.Service;\r
-import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
-import org.onap.holmes.rulemgt.bean.response.RuleResult4API;\r
-import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
-import org.onap.holmes.rulemgt.db.CorrelationRuleDao;\r
-import org.onap.holmes.common.api.entity.CorrelationRule;\r
-import org.onap.holmes.common.exception.CorrelationException;\r
-import org.onap.holmes.common.utils.DbDaoUtil;\r
-import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
-import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;\r
-import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest;\r
-import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;\r
-import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;\r
-import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
-import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
-import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;\r
-import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;\r
-import org.onap.holmes.rulemgt.tools.EngineTools;\r
-\r
-\r
-@Service\r
-@Singleton\r
-@Slf4j\r
-public class RuleMgtWrapper {\r
-\r
-    @Inject\r
-    private EngineTools engineTools;\r
-\r
-    @Inject\r
-    private RuleQueryWrapper ruleQueryWrapper;\r
-\r
-    @Inject\r
-    private CorrelationRuleQueryDao correlationRuleQueryDao;\r
-    @Inject\r
-    private EngineWrapper engineWarpper;\r
-    @Inject\r
-    private DbDaoUtil daoUtil;\r
-\r
-    private CorrelationRuleDao correlationRuleDao;\r
-\r
-    @PostConstruct\r
-    public void initDaoUtil() {\r
-        correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);\r
-    }\r
-\r
-    public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest)\r
-            throws CorrelationException {\r
-        if (ruleCreateRequest == null) {\r
-            throw new CorrelationException("The request object can not be empty!");\r
-        }\r
-        CorrelationRule correlationRule = convertCreateRequest2Rule(creator,\r
-                ruleCreateRequest);\r
-        checkCorrelation(correlationRule);\r
-        CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName());\r
-        if (ruleTemp != null) {\r
-            throw new CorrelationException("A rule with the same name already exists.");\r
-        }\r
-        String ip = "";\r
-        try {\r
-            ip = engineTools.getEngineWithLeastRules();\r
-        } catch (Exception e) {\r
-            log.error("When adding rules, can not get engine instance ip");\r
-        }\r
-        String packageName = deployRule2Engine(correlationRule, ip);\r
-        correlationRule.setPackageName(packageName);\r
-        correlationRule.setEngineInstance(ip);\r
-        CorrelationRule result = null;\r
-        try {\r
-            result = correlationRuleDao.saveRule(correlationRule);\r
-        } catch (CorrelationException e) {\r
-            engineWarpper.deleteRuleFromEngine(packageName, ip);\r
-            throw new CorrelationException(e.getMessage(), e);\r
-        }\r
-        RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();\r
-        ruleAddAndUpdateResponse.setRuleId(result.getRid());\r
-        return ruleAddAndUpdateResponse;\r
-    }\r
-\r
-    public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest)\r
-            throws CorrelationException {\r
-        if (ruleUpdateRequest == null) {\r
-            throw new CorrelationException("The request object can not be empty!");\r
-        }\r
-        CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId());\r
-        if (oldCorrelationRule == null) {\r
-            throw new CorrelationException("You're trying to update a rule which does not exist in the system.");\r
-        }\r
-        String updateIp = "";\r
-        updateIp = oldCorrelationRule.getEngineInstance();\r
-        CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,\r
-                ruleUpdateRequest, oldCorrelationRule.getName());\r
-        newCorrelationRule.setEngineInstance(updateIp);\r
-        checkCorrelation(newCorrelationRule);\r
-        RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();\r
-        ruleChangeResponse.setRuleId(newCorrelationRule.getRid());\r
-\r
-        if (!haveChange(newCorrelationRule, oldCorrelationRule)) {\r
-            return ruleChangeResponse;\r
-        }\r
-        if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
-            String oldRuleEngineInstance = oldCorrelationRule.getEngineInstance();\r
-            engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName(), oldRuleEngineInstance);\r
-        }\r
-        newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule, updateIp));\r
-        correlationRuleDao.updateRule(newCorrelationRule);\r
-        return ruleChangeResponse;\r
-    }\r
-\r
-    private void checkCorrelation(CorrelationRule correlationRule) throws CorrelationException {\r
-        int enabled = correlationRule.getEnabled();\r
-        String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim();\r
-        String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim();\r
-        if ("".equals(content)) {\r
-            throw new CorrelationException("The contents of the rule can not be empty!");\r
-        }\r
-        if (enabled != RuleMgtConstant.STATUS_RULE_CLOSE\r
-                && enabled != RuleMgtConstant.STATUS_RULE_OPEN) {\r
-            throw new CorrelationException("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed.");\r
-        }\r
-        if ("".equals(ruleName)) {\r
-            throw new CorrelationException("The name of the rule can not be empty.");\r
-        }\r
-    }\r
-\r
-    private boolean haveChange(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) {\r
-        String newContent = newCorrelationRule.getContent();\r
-        String oldContent = oldCorrelationRule.getContent();\r
-        int newEnabled = newCorrelationRule.getEnabled();\r
-        int oldEnabled = oldCorrelationRule.getEnabled();\r
-        String newDes = newCorrelationRule.getDescription();\r
-        String oldDes = oldCorrelationRule.getDescription();\r
-        String oldControlLoop = oldCorrelationRule.getClosedControlLoopName();\r
-        String newControlLoop = newCorrelationRule.getClosedControlLoopName();\r
-        if (newContent.equals(oldContent) && newEnabled == oldEnabled\r
-                && newDes.equals(oldDes) && newControlLoop.equals(oldControlLoop)) {\r
-            return false;\r
-        }\r
-        return true;\r
-    }\r
-\r
-    public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)\r
-            throws CorrelationException {\r
-        if (ruleDeleteRequest == null) {\r
-            throw new CorrelationException("The request object can not be empty!");\r
-        }\r
-        CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId());\r
-        if (correlationRule == null) {\r
-            log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database.");\r
-            throw new CorrelationException("You're trying to delete a rule which does not exist in the system.");\r
-        }\r
-        if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
-            String ip = correlationRule.getEngineInstance();\r
-            engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName(), ip);\r
-        }\r
-        correlationRuleDao.deleteRule(correlationRule);\r
-    }\r
-\r
-    private CorrelationRule convertCreateRequest2Rule(String userName,\r
-                                                      RuleCreateRequest ruleCreateRequest) throws CorrelationException {\r
-        String tempContent = ruleCreateRequest.getContent();\r
-        CorrelationRule correlationRule = new CorrelationRule();\r
-        String ruleId = "rule_" + System.currentTimeMillis();\r
-        String description = ruleCreateRequest.getDescription() == null ? "" : ruleCreateRequest.getDescription();\r
-        correlationRule.setRid(ruleId);\r
-        if (tempContent != null) {\r
-            correlationRule.setContent(tempContent.trim());\r
-        }\r
-        correlationRule.setDescription(description);\r
-        correlationRule.setCreateTime(new Date());\r
-        correlationRule.setUpdateTime(new Date());\r
-        correlationRule.setName(ruleCreateRequest.getRuleName());\r
-        correlationRule.setEngineID("correlation-d");\r
-        correlationRule.setEngineType("");\r
-        correlationRule.setTemplateID(0);\r
-        correlationRule.setVendor("");\r
-        correlationRule.setCreator(userName);\r
-        correlationRule.setModifier(userName);\r
-        correlationRule.setEnabled(ruleCreateRequest.getEnabled());\r
-        correlationRule.setClosedControlLoopName(ruleCreateRequest.getLoopControlName());\r
-        return correlationRule;\r
-    }\r
-\r
-    private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,\r
-                                                                     RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException {\r
-        CorrelationRule correlationRule = new CorrelationRule();\r
-        String description = ruleUpdateRequest.getDescription() == null ? "" : ruleUpdateRequest.getDescription();\r
-        correlationRule.setRid(ruleUpdateRequest.getRuleId());\r
-        correlationRule.setContent(ruleUpdateRequest.getContent());\r
-        correlationRule.setDescription(description);\r
-        correlationRule.setEnabled(ruleUpdateRequest.getEnabled());\r
-        correlationRule.setUpdateTime(new Date());\r
-        correlationRule.setModifier(modifier);\r
-        correlationRule.setName(ruleName);\r
-        correlationRule.setClosedControlLoopName(ruleUpdateRequest.getLoopControlName());\r
-        return correlationRule;\r
-    }\r
-\r
-    public String deployRule2Engine(CorrelationRule correlationRule, String ip)\r
-            throws CorrelationException {\r
-        if (engineWarpper.checkRuleFromEngine(toCorrelationCheckRule(correlationRule), ip) && (\r
-                correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) {\r
-            return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule), ip);\r
-        }\r
-        return "";\r
-    }\r
-\r
-    public RuleQueryListResponse getCorrelationRuleByCondition(\r
-            RuleQueryCondition ruleQueryCondition) throws CorrelationException {\r
-        List<CorrelationRule> correlationRule = correlationRuleQueryDao\r
-                .getCorrelationRulesByCondition(ruleQueryCondition);\r
-        RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();\r
-        ruleQueryListResponse.setTotalCount(correlationRule.size());\r
-        ruleQueryListResponse\r
-                .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule));\r
-        return ruleQueryListResponse;\r
-    }\r
-\r
-    private List<RuleResult4API> correlationRules2RuleResult4APIs(\r
-            List<CorrelationRule> correlationRules) {\r
-        List<RuleResult4API> ruleResult4APIs = new ArrayList<RuleResult4API>();\r
-        for (CorrelationRule correlationRule : correlationRules) {\r
-            RuleResult4API ruleResult4API = new RuleResult4API();\r
-            String description = correlationRule.getDescription() == null ? "" : correlationRule.getDescription();\r
-            ruleResult4API.setRuleId(correlationRule.getRid());\r
-            ruleResult4API.setRuleName(correlationRule.getName());\r
-            ruleResult4API.setDescription(description);\r
-            ruleResult4API.setContent(correlationRule.getContent());\r
-            ruleResult4API.setCreateTime(correlationRule.getCreateTime());\r
-            ruleResult4API.setCreator(correlationRule.getCreator());\r
-            ruleResult4API.setUpdateTime(correlationRule.getUpdateTime());\r
-            ruleResult4API.setModifier(correlationRule.getModifier());\r
-            ruleResult4API.setEnabled(correlationRule.getEnabled());\r
-            ruleResult4API.setLoopControlName(correlationRule.getClosedControlLoopName());\r
-            ruleResult4APIs.add(ruleResult4API);\r
-        }\r
-        return ruleResult4APIs;\r
-    }\r
-\r
-    private CorrelationDeployRule4Engine correlationRules2DeployRule(\r
-            CorrelationRule correlationRule) {\r
-        CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();\r
-        correlationDeployRule4Engine.setContent(correlationRule.getContent());\r
-        correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID());\r
-        correlationDeployRule4Engine.setLoopControlName(correlationRule.getClosedControlLoopName());\r
-        return correlationDeployRule4Engine;\r
-    }\r
-\r
-    private CorrelationCheckRule4Engine toCorrelationCheckRule(\r
-            CorrelationRule correlationRule) {\r
-        CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine();\r
-        correlationCheckRule4Engine.setContent(correlationRule.getContent());\r
-        return correlationCheckRule4Engine;\r
-    }\r
-}\r
+/**
+ * Copyright 2017 ZTE Corporation.
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.rulemgt.wrapper;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import lombok.extern.slf4j.Slf4j;
+import org.jvnet.hk2.annotations.Service;
+import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
+import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
+import org.onap.holmes.rulemgt.constant.RuleMgtConstant;
+import org.onap.holmes.rulemgt.db.CorrelationRuleDao;
+import org.onap.holmes.common.api.entity.CorrelationRule;
+import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.DbDaoUtil;
+import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
+import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
+import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest;
+import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;
+import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;
+import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
+import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
+import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;
+import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;
+import org.onap.holmes.rulemgt.tools.EngineTools;
+
+
+@Service
+@Singleton
+@Slf4j
+public class RuleMgtWrapper {
+
+    @Inject
+    private EngineTools engineTools;
+
+    @Inject
+    private RuleQueryWrapper ruleQueryWrapper;
+
+    @Inject
+    private CorrelationRuleQueryDao correlationRuleQueryDao;
+    @Inject
+    private EngineWrapper engineWarpper;
+    @Inject
+    private DbDaoUtil daoUtil;
+
+    private CorrelationRuleDao correlationRuleDao;
+
+    @PostConstruct
+    public void initDaoUtil() {
+        correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);
+    }
+
+    public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest)
+            throws CorrelationException {
+        if (ruleCreateRequest == null) {
+            throw new CorrelationException("The request object can not be empty!");
+        }
+        CorrelationRule correlationRule = convertCreateRequest2Rule(creator,
+                ruleCreateRequest);
+        validateCorrelationRule(correlationRule);
+        CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName());
+        if (ruleTemp != null) {
+            throw new CorrelationException("A rule with the same name already exists.");
+        }
+        String ip = "";
+        try {
+            ip = engineTools.getEngineWithLeastRules();
+        } catch (Exception e) {
+            log.error("When adding rules, can not get engine instance ip");
+        }
+        String packageName = deployRule2Engine(correlationRule, ip);
+        correlationRule.setPackageName(packageName);
+        correlationRule.setEngineInstance(ip);
+        CorrelationRule result = null;
+        try {
+            result = correlationRuleDao.saveRule(correlationRule);
+        } catch (CorrelationException e) {
+            engineWarpper.deleteRuleFromEngine(packageName, ip);
+            throw new CorrelationException(e.getMessage(), e);
+        }
+        RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();
+        ruleAddAndUpdateResponse.setRuleId(result.getRid());
+        return ruleAddAndUpdateResponse;
+    }
+
+    public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest)
+            throws CorrelationException {
+        if (ruleUpdateRequest == null) {
+            throw new CorrelationException("The request object can not be empty!");
+        }
+        CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId());
+        if (oldCorrelationRule == null) {
+            throw new CorrelationException("You're trying to update a rule which does not exist in the system.");
+        }
+
+        String updateIp = oldCorrelationRule.getEngineInstance();
+        if (!checkIfEngineExists(updateIp)) {
+            updateIp = engineTools.getEngineWithLeastRules();
+        }
+        CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,
+                ruleUpdateRequest, oldCorrelationRule.getName());
+        newCorrelationRule.setEngineInstance(updateIp);
+        validateCorrelationRule(newCorrelationRule);
+        RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();
+        ruleChangeResponse.setRuleId(newCorrelationRule.getRid());
+
+        if (!checkIfRuleChanged(newCorrelationRule, oldCorrelationRule)) {
+            return ruleChangeResponse;
+        }
+        String engineInstance = oldCorrelationRule.getEngineInstance();
+        if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_ENABLED
+                && checkIfEngineExists(engineInstance)) {
+            engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName(), engineInstance);
+        }
+        newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule, updateIp));
+        correlationRuleDao.updateRule(newCorrelationRule);
+        return ruleChangeResponse;
+    }
+
+    private void validateCorrelationRule(CorrelationRule correlationRule) throws CorrelationException {
+        int enabled = correlationRule.getEnabled();
+        String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim();
+        String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim();
+        if ("".equals(content)) {
+            throw new CorrelationException("The contents of the rule can not be empty!");
+        }
+        if (enabled != RuleMgtConstant.STATUS_DISABLED
+                && enabled != RuleMgtConstant.STATUS_ENABLED) {
+            throw new CorrelationException("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed.");
+        }
+        if ("".equals(ruleName)) {
+            throw new CorrelationException("The name of the rule can not be empty.");
+        }
+    }
+
+    private boolean checkIfRuleChanged(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) {
+        String newContent = newCorrelationRule.getContent();
+        String oldContent = oldCorrelationRule.getContent();
+        int newEnabled = newCorrelationRule.getEnabled();
+        int oldEnabled = oldCorrelationRule.getEnabled();
+        String newDes = newCorrelationRule.getDescription();
+        String oldDes = oldCorrelationRule.getDescription();
+        String oldControlLoop = oldCorrelationRule.getClosedControlLoopName();
+        String newControlLoop = newCorrelationRule.getClosedControlLoopName();
+        if (newContent.equals(oldContent) && newEnabled == oldEnabled
+                && newDes.equals(oldDes) && newControlLoop.equals(oldControlLoop)) {
+            return false;
+        }
+        return true;
+    }
+
+    public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)
+            throws CorrelationException {
+        if (ruleDeleteRequest == null) {
+            throw new CorrelationException("The request object can not be empty!");
+        }
+        CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId());
+        if (correlationRule == null) {
+            log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database.");
+            throw new CorrelationException("You're trying to delete a rule which does not exist in the system.");
+        }
+        if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_ENABLED) {
+            String ip = correlationRule.getEngineInstance();
+            engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName(), ip);
+        }
+        correlationRuleDao.deleteRule(correlationRule);
+    }
+
+    private CorrelationRule convertCreateRequest2Rule(String userName,
+                                                      RuleCreateRequest ruleCreateRequest) throws CorrelationException {
+        String tempContent = ruleCreateRequest.getContent();
+        CorrelationRule correlationRule = new CorrelationRule();
+        String ruleId = "rule_" + System.currentTimeMillis();
+        String description = ruleCreateRequest.getDescription() == null ? "" : ruleCreateRequest.getDescription();
+        correlationRule.setRid(ruleId);
+        if (tempContent != null) {
+            correlationRule.setContent(tempContent.trim());
+        }
+        correlationRule.setDescription(description);
+        correlationRule.setCreateTime(new Date());
+        correlationRule.setUpdateTime(new Date());
+        correlationRule.setName(ruleCreateRequest.getRuleName());
+        correlationRule.setEngineID("correlation-d");
+        correlationRule.setEngineType("");
+        correlationRule.setTemplateID(0);
+        correlationRule.setVendor("");
+        correlationRule.setCreator(userName);
+        correlationRule.setModifier(userName);
+        correlationRule.setEnabled(ruleCreateRequest.getEnabled());
+        correlationRule.setClosedControlLoopName(ruleCreateRequest.getLoopControlName());
+        return correlationRule;
+    }
+
+    private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,
+                                                                     RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException {
+        CorrelationRule correlationRule = new CorrelationRule();
+        String description = ruleUpdateRequest.getDescription() == null ? "" : ruleUpdateRequest.getDescription();
+        correlationRule.setRid(ruleUpdateRequest.getRuleId());
+        correlationRule.setContent(ruleUpdateRequest.getContent());
+        correlationRule.setDescription(description);
+        correlationRule.setEnabled(ruleUpdateRequest.getEnabled());
+        correlationRule.setUpdateTime(new Date());
+        correlationRule.setModifier(modifier);
+        correlationRule.setName(ruleName);
+        correlationRule.setClosedControlLoopName(ruleUpdateRequest.getLoopControlName());
+        return correlationRule;
+    }
+
+    public String deployRule2Engine(CorrelationRule correlationRule, String ip)
+            throws CorrelationException {
+        if (engineWarpper.checkRuleFromEngine(toCorrelationCheckRule(correlationRule), ip) && (
+                correlationRule.getEnabled() == RuleMgtConstant.STATUS_ENABLED)) {
+            return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule), ip);
+        }
+        return "";
+    }
+
+    public RuleQueryListResponse getCorrelationRuleByCondition(
+            RuleQueryCondition ruleQueryCondition) throws CorrelationException {
+        List<CorrelationRule> correlationRule = correlationRuleQueryDao
+                .getCorrelationRulesByCondition(ruleQueryCondition);
+        RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();
+        ruleQueryListResponse.setTotalCount(correlationRule.size());
+        ruleQueryListResponse
+                .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule));
+        return ruleQueryListResponse;
+    }
+
+    private List<RuleResult4API> correlationRules2RuleResult4APIs(
+            List<CorrelationRule> correlationRules) {
+        List<RuleResult4API> ruleResult4APIs = new ArrayList<RuleResult4API>();
+        for (CorrelationRule correlationRule : correlationRules) {
+            RuleResult4API ruleResult4API = new RuleResult4API();
+            String description = correlationRule.getDescription() == null ? "" : correlationRule.getDescription();
+            ruleResult4API.setRuleId(correlationRule.getRid());
+            ruleResult4API.setRuleName(correlationRule.getName());
+            ruleResult4API.setDescription(description);
+            ruleResult4API.setContent(correlationRule.getContent());
+            ruleResult4API.setCreateTime(correlationRule.getCreateTime());
+            ruleResult4API.setCreator(correlationRule.getCreator());
+            ruleResult4API.setUpdateTime(correlationRule.getUpdateTime());
+            ruleResult4API.setModifier(correlationRule.getModifier());
+            ruleResult4API.setEnabled(correlationRule.getEnabled());
+            ruleResult4API.setLoopControlName(correlationRule.getClosedControlLoopName());
+            ruleResult4APIs.add(ruleResult4API);
+        }
+        return ruleResult4APIs;
+    }
+
+    private CorrelationDeployRule4Engine correlationRules2DeployRule(
+            CorrelationRule correlationRule) {
+        CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();
+        correlationDeployRule4Engine.setContent(correlationRule.getContent());
+        correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID());
+        correlationDeployRule4Engine.setLoopControlName(correlationRule.getClosedControlLoopName());
+        return correlationDeployRule4Engine;
+    }
+
+    private CorrelationCheckRule4Engine toCorrelationCheckRule(
+            CorrelationRule correlationRule) {
+        CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine();
+        correlationCheckRule4Engine.setContent(correlationRule.getContent());
+        return correlationCheckRule4Engine;
+    }
+
+    private boolean checkIfEngineExists(String ip) {
+        List engineList = engineTools.getInstanceList();
+        return engineList.contains(ip);
+    }
+}
index 2a8357c..626b91d 100644 (file)
-/**\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
-\r
-package org.onap.holmes.rulemgt.wrapper;\r
-\r
-\r
-import org.easymock.EasyMock;\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.onap.holmes.common.api.entity.CorrelationRule;\r
-import org.onap.holmes.common.exception.CorrelationException;\r
-import org.onap.holmes.common.utils.DbDaoUtil;\r
-import org.onap.holmes.rulemgt.bean.request.*;\r
-import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
-import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
-import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;\r
-import org.onap.holmes.rulemgt.db.CorrelationRuleDao;\r
-import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;\r
-import org.onap.holmes.rulemgt.tools.EngineTools;\r
-import org.powermock.api.easymock.PowerMock;\r
-import org.powermock.modules.junit4.PowerMockRunner;\r
-import org.powermock.reflect.Whitebox;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Date;\r
-import java.util.List;\r
-\r
-import static org.hamcrest.MatcherAssert.assertThat;\r
-import static org.hamcrest.Matchers.equalTo;\r
-import static org.hamcrest.Matchers.is;\r
-\r
-@RunWith(PowerMockRunner.class)\r
-public class RuleMgtWrapperTest {\r
-\r
-    @Rule\r
-    public ExpectedException thrown = ExpectedException.none();\r
-\r
-    private RuleMgtWrapper ruleMgtWrapper;\r
-\r
-    private EngineWrapper engineWrapperMock;\r
-\r
-    private DbDaoUtil dbDaoUtilMock;\r
-\r
-    private CorrelationRuleQueryDao correlationRuleQueryDaoMock;\r
-\r
-    private CorrelationRuleDao correlationRuleDaoMock;\r
-\r
-    private EngineTools engineToolsMock;\r
-\r
-    private static final String USER_NAME = "admin";\r
-\r
-    @Before\r
-    public void setUp() throws Exception {\r
-\r
-        ruleMgtWrapper = new RuleMgtWrapper();\r
-\r
-        engineWrapperMock = PowerMock.createMock(EngineWrapper.class);\r
-        correlationRuleQueryDaoMock = PowerMock.createMock(CorrelationRuleQueryDao.class);\r
-        dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class);\r
-        correlationRuleDaoMock = PowerMock.createMock(CorrelationRuleDao.class);\r
-        engineToolsMock = PowerMock.createMock(EngineTools.class);\r
-\r
-        Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock);\r
-        Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock);\r
-        Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock);\r
-        Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleDao", correlationRuleDaoMock);\r
-        Whitebox.setInternalState(ruleMgtWrapper,"engineTools", engineToolsMock);\r
-\r
-        PowerMock.resetAll();\r
-    }\r
-\r
-    @Test\r
-    public void initDaoUtil_normal() {\r
-        ruleMgtWrapper.initDaoUtil();\r
-    }\r
-\r
-    @Test\r
-    public void addCorrelationRule_name_is_null() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("The name of the rule can not be empty.");\r
-\r
-        ruleMgtWrapper.addCorrelationRule(USER_NAME, createRuleCreateRequest(null, "This is a rule for testing.",\r
-                "Mocked contents.", 0));\r
-    }\r
-\r
-    @Test\r
-    public void addCorrelationRule_request_null() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("The request object can not be empty!");\r
-\r
-        ruleMgtWrapper.addCorrelationRule(USER_NAME, null);\r
-    }\r
-\r
-    @Test\r
-    public void addCorrelationRule_name_is_empty() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("The name of the rule can not be empty.");\r
-\r
-        ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.",\r
-                "Mocked contents.", 0));\r
-    }\r
-\r
-    @Test\r
-    public void addCorrelationRule_content_is_empty() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("The contents of the rule can not be empty!");\r
-\r
-        ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.",\r
-                "", 0));\r
-    }\r
-\r
-    @Test\r
-    public void addCorrelationRule_enabled_is_off_limit() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed.");\r
-\r
-        ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.",\r
-                "Mocked contents.", 3));\r
-    }\r
-\r
-    @Test\r
-    public void addCorrelationRule_duplicated_rule() throws Exception {\r
-\r
-        final String ruleName = "Rule-001";\r
-\r
-        RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",\r
-                "Mocked contents.", 0);\r
-        CorrelationRule correlationRule = convertCreateRequest2CorrelationRule(ruleCreateRequest);\r
-\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("A rule with the same name already exists.");\r
-\r
-        EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(correlationRule);\r
-        PowerMock.replayAll();\r
-\r
-        ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);\r
-\r
-        PowerMock.verifyAll();\r
-    }\r
-\r
-    @Test\r
-    public void addCorrelationRule_normal() throws Exception {\r
-        final String ruleName = "Rule-001";\r
-\r
-        RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",\r
-                "Mocked contents.", 1);\r
-        ruleCreateRequest.setLoopControlName("loopName");\r
-\r
-        CorrelationRule correlationRuleRet = new CorrelationRule();\r
-        correlationRuleRet.setRid("rule_" + System.currentTimeMillis());\r
-\r
-        EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null);\r
-        EasyMock.expect(engineToolsMock.getEngineWithLeastRules()).andReturn("127.0.0.1");\r
-        EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)\r
-                , EasyMock.anyObject(String.class)))\r
-                .andReturn(true);\r
-        EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)\r
-                , EasyMock.anyObject(String.class)))\r
-                .andReturn("package-001");\r
-        EasyMock.expect(correlationRuleDaoMock.saveRule(EasyMock.anyObject(CorrelationRule.class)))\r
-                .andReturn(correlationRuleRet);\r
-\r
-        PowerMock.replayAll();\r
-\r
-        RuleAddAndUpdateResponse response = ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);\r
-        PowerMock.verifyAll();\r
-\r
-        assertThat(response.getRuleId(), equalTo(correlationRuleRet.getRid()));\r
-    }\r
-\r
-    @Test\r
-    public void updateCorrelationRule_param_null() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("The request object can not be empty!");\r
-\r
-        ruleMgtWrapper.updateCorrelationRule(USER_NAME, null);\r
-    }\r
-\r
-    @Test\r
-    public void updateCorrelationRule_normal() throws Exception {\r
-        CorrelationRule oldCorrelationRule = new CorrelationRule();\r
-        oldCorrelationRule.setRid("rule_1");\r
-        oldCorrelationRule.setName("name");\r
-        oldCorrelationRule.setDescription("des1");\r
-        oldCorrelationRule.setContent("content");\r
-        oldCorrelationRule.setPackageName("testName");\r
-        oldCorrelationRule.setEnabled(1);\r
-        oldCorrelationRule.setClosedControlLoopName("cl-name");\r
-        oldCorrelationRule.setEngineInstance("127.0.0.1");\r
-        RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des2", "contetnt2", 1);\r
-\r
-        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule);\r
-        EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName", "127.0.0.1")).andReturn(true);\r
-        correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));\r
-        EasyMock.expectLastCall();\r
-        EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)\r
-                , EasyMock.anyObject(String.class)))\r
-                .andReturn(true);\r
-        EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)\r
-                , EasyMock.anyObject(String.class)))\r
-                .andReturn("packageName1");\r
-        PowerMock.replayAll();\r
-\r
-        ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);\r
-\r
-        PowerMock.verifyAll();\r
-\r
-        assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId()));\r
-    }\r
-\r
-    @Test\r
-    public void updateCorrelationRule_param_no_change() throws Exception {\r
-        CorrelationRule oldCorrelationRule = new CorrelationRule();\r
-        oldCorrelationRule.setRid("rule_1");\r
-        oldCorrelationRule.setName("name");\r
-        oldCorrelationRule.setDescription("des1");\r
-        oldCorrelationRule.setContent("content");\r
-        oldCorrelationRule.setPackageName("testName");\r
-        oldCorrelationRule.setEnabled(1);\r
-        oldCorrelationRule.setClosedControlLoopName("cl-name");\r
-        RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des1", "content", 1);\r
-\r
-        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule);\r
-\r
-        PowerMock.replayAll();\r
-\r
-        ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);\r
-\r
-        PowerMock.verifyAll();\r
-\r
-        assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId()));\r
-    }\r
-\r
-    @Test\r
-    public void updateCorrelationRule_rule_not_exist() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("You're trying to update a rule which does not exist in the system.");\r
-\r
-        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(EasyMock.anyObject(String.class))).andReturn(null);\r
-\r
-        PowerMock.replayAll();\r
-\r
-        ruleMgtWrapper.updateCorrelationRule(USER_NAME, new RuleUpdateRequest());\r
-\r
-        PowerMock.verifyAll();\r
-    }\r
-\r
-    @Test\r
-    public void deleteCorrelationRule_request_null() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("The request object can not be empty!");\r
-\r
-        ruleMgtWrapper.deleteCorrelationRule(null);\r
-    }\r
-\r
-    @Test\r
-    public void deleteCorrelationRule_rule_not_exit() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("You're trying to delete a rule which does not exist in the system.");\r
-\r
-        RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());\r
-\r
-        EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(\r
-                correlationRuleDaoMock).anyTimes();\r
-        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId()))\r
-                .andReturn(null);\r
-\r
-        PowerMock.replayAll();\r
-\r
-        ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);\r
-\r
-        PowerMock.verifyAll();\r
-    }\r
-\r
-    @Test\r
-    public void deleteCorrelationRule_normal() throws Exception {\r
-        RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());\r
-        CorrelationRule correlationRule = new CorrelationRule();\r
-        correlationRule.setEnabled(1);\r
-        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId()))\r
-                .andReturn(correlationRule);\r
-        EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class)\r
-                , EasyMock.anyObject(String.class))).andReturn(true);\r
-        correlationRuleDaoMock.deleteRule(EasyMock.anyObject(CorrelationRule.class));\r
-        EasyMock.expectLastCall();\r
-        PowerMock.replayAll();\r
-\r
-        ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);\r
-\r
-        PowerMock.verifyAll();\r
-    }\r
-\r
-    @Test\r
-    public void getCorrelationRuleByCondition_data_format_exception() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("An error occurred while building the query SQL.");\r
-\r
-        EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(\r
-                RuleQueryCondition.class)))\r
-                .andThrow(new CorrelationException("An error occurred while building the query SQL."));\r
-\r
-        PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);\r
-\r
-        ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());\r
-\r
-        PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);\r
-    }\r
-\r
-    @Test\r
-    public void getCorrelationRuleByCondition_db_exception() throws Exception {\r
-        thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("Failed to query the rule.");\r
-\r
-        EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(\r
-                RuleQueryCondition.class)))\r
-                .andThrow(new CorrelationException("Failed to query the rule."));\r
-\r
-        PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);\r
-\r
-        ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());\r
-\r
-        PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);\r
-    }\r
-\r
-    @Test\r
-    public void getCorrelationRuleByCondition_normal() throws Exception {\r
-        List<CorrelationRule> correlationRuleList = new ArrayList<CorrelationRule>(10);\r
-        for (int i = 0; i < 10; ++i) {\r
-            CorrelationRule correlationRule = new CorrelationRule();\r
-            correlationRule.setContent("content" + i);\r
-            correlationRule.setName("name" + i);\r
-            correlationRule.setRid("rule_" + i);\r
-            correlationRule.setEngineType("engineType" + (i % 2 + 1));\r
-            correlationRule.setEngineID("engineId" + i);\r
-            correlationRule.setCreateTime(new Date());\r
-            correlationRule.setCreator(USER_NAME);\r
-            correlationRule.setDescription("description" + i);\r
-            correlationRule.setPackageName("package" + i);\r
-            correlationRuleList.add(correlationRule);\r
-        }\r
-\r
-        EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(\r
-                RuleQueryCondition.class))).andReturn(correlationRuleList);\r
-\r
-        PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);\r
-\r
-        RuleQueryListResponse response = ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());\r
-\r
-        PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);\r
-\r
-        assertThat(response.getTotalCount(), is(10));\r
-\r
-        for (int i = 0; i < 10; ++i) {\r
-            assertThat(response.getCorrelationRules().get(i).getRuleId(),\r
-                    equalTo(correlationRuleList.get(i).getRid()));\r
-        }\r
-    }\r
-\r
-    private RuleCreateRequest createRuleCreateRequest(String ruleName, String description, String content,\r
-            int enabled) {\r
-        RuleCreateRequest rcr;\r
-        rcr = new RuleCreateRequest();\r
-        rcr.setRuleName(ruleName);\r
-        rcr.setDescription(description);\r
-        rcr.setContent(content);\r
-        rcr.setEnabled(enabled);\r
-        return rcr;\r
-    }\r
-\r
-    private RuleUpdateRequest createRuleUpdateRequest(String ruleId, String clName, String description,\r
-            String content, int enabled) {\r
-        RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();\r
-        ruleUpdateRequest.setRuleId(ruleId);\r
-        ruleUpdateRequest.setDescription(description);\r
-        ruleUpdateRequest.setContent(content);\r
-        ruleUpdateRequest.setEnabled(enabled);\r
-        ruleUpdateRequest.setLoopControlName(clName);\r
-        return ruleUpdateRequest;\r
-    }\r
-\r
-    private RuleDeleteRequest createRuleDeleteRequest(String ruleId) {\r
-        RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();\r
-        ruleDeleteRequest.setRuleId(ruleId);\r
-        return ruleDeleteRequest;\r
-    }\r
-\r
-    private CorrelationRule convertCreateRequest2CorrelationRule(RuleCreateRequest ruleCreateRequest) {\r
-        CorrelationRule correlationRule = new CorrelationRule();\r
-        correlationRule.setContent(ruleCreateRequest.getContent());\r
-        correlationRule.setDescription(ruleCreateRequest.getDescription());\r
-        correlationRule.setName(ruleCreateRequest.getRuleName());\r
-        correlationRule.setCreator(USER_NAME);\r
-        correlationRule.setModifier(USER_NAME);\r
-        correlationRule.setEnabled(ruleCreateRequest.getEnabled());\r
-        return correlationRule;\r
-    }\r
-\r
-    private CorrelationRule convertUpdateRequest2CorrelationRule(RuleUpdateRequest ruleUpdateRequest) {\r
-        CorrelationRule correlationRule = new CorrelationRule();\r
-        correlationRule.setRid(ruleUpdateRequest.getRuleId());\r
-        correlationRule.setContent(ruleUpdateRequest.getContent());\r
-        correlationRule.setDescription(ruleUpdateRequest.getDescription());\r
-        correlationRule.setEnabled(ruleUpdateRequest.getEnabled());\r
-        correlationRule.setUpdateTime(new Date());\r
-        correlationRule.setModifier(USER_NAME);\r
-        return correlationRule;\r
-    }\r
+/**
+ * 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.onap.holmes.rulemgt.wrapper;
+
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.onap.holmes.common.api.entity.CorrelationRule;
+import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.DbDaoUtil;
+import org.onap.holmes.rulemgt.bean.request.*;
+import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
+import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
+import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;
+import org.onap.holmes.rulemgt.db.CorrelationRuleDao;
+import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;
+import org.onap.holmes.rulemgt.tools.EngineTools;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+
+@RunWith(PowerMockRunner.class)
+public class RuleMgtWrapperTest {
+
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+
+    private RuleMgtWrapper ruleMgtWrapper;
+
+    private EngineWrapper engineWrapperMock;
+
+    private DbDaoUtil dbDaoUtilMock;
+
+    private CorrelationRuleQueryDao correlationRuleQueryDaoMock;
+
+    private CorrelationRuleDao correlationRuleDaoMock;
+
+    private EngineTools engineToolsMock;
+
+    private static final String USER_NAME = "admin";
+
+    @Before
+    public void setUp() throws Exception {
+
+        ruleMgtWrapper = new RuleMgtWrapper();
+
+        engineWrapperMock = PowerMock.createMock(EngineWrapper.class);
+        correlationRuleQueryDaoMock = PowerMock.createMock(CorrelationRuleQueryDao.class);
+        dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class);
+        correlationRuleDaoMock = PowerMock.createMock(CorrelationRuleDao.class);
+        engineToolsMock = PowerMock.createMock(EngineTools.class);
+
+        Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock);
+        Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock);
+        Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock);
+        Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleDao", correlationRuleDaoMock);
+        Whitebox.setInternalState(ruleMgtWrapper,"engineTools", engineToolsMock);
+
+        PowerMock.resetAll();
+    }
+
+    @Test
+    public void initDaoUtil_normal() {
+        ruleMgtWrapper.initDaoUtil();
+    }
+
+    @Test
+    public void addCorrelationRule_name_is_null() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("The name of the rule can not be empty.");
+
+        ruleMgtWrapper.addCorrelationRule(USER_NAME, createRuleCreateRequest(null, "This is a rule for testing.",
+                "Mocked contents.", 0));
+    }
+
+    @Test
+    public void addCorrelationRule_request_null() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("The request object can not be empty!");
+
+        ruleMgtWrapper.addCorrelationRule(USER_NAME, null);
+    }
+
+    @Test
+    public void addCorrelationRule_name_is_empty() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("The name of the rule can not be empty.");
+
+        ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.",
+                "Mocked contents.", 0));
+    }
+
+    @Test
+    public void addCorrelationRule_content_is_empty() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("The contents of the rule can not be empty!");
+
+        ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.",
+                "", 0));
+    }
+
+    @Test
+    public void addCorrelationRule_enabled_is_off_limit() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed.");
+
+        ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.",
+                "Mocked contents.", 3));
+    }
+
+    @Test
+    public void addCorrelationRule_duplicated_rule() throws Exception {
+
+        final String ruleName = "Rule-001";
+
+        RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
+                "Mocked contents.", 0);
+        CorrelationRule correlationRule = convertCreateRequest2CorrelationRule(ruleCreateRequest);
+
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("A rule with the same name already exists.");
+
+        EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(correlationRule);
+        PowerMock.replayAll();
+
+        ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
+
+        PowerMock.verifyAll();
+    }
+
+    @Test
+    public void addCorrelationRule_normal() throws Exception {
+        final String ruleName = "Rule-001";
+
+        RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
+                "Mocked contents.", 1);
+        ruleCreateRequest.setLoopControlName("loopName");
+
+        CorrelationRule correlationRuleRet = new CorrelationRule();
+        correlationRuleRet.setRid("rule_" + System.currentTimeMillis());
+
+        EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null);
+        EasyMock.expect(engineToolsMock.getEngineWithLeastRules()).andReturn("127.0.0.1");
+        EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)
+                , EasyMock.anyObject(String.class)))
+                .andReturn(true);
+        EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)
+                , EasyMock.anyObject(String.class)))
+                .andReturn("package-001");
+        EasyMock.expect(correlationRuleDaoMock.saveRule(EasyMock.anyObject(CorrelationRule.class)))
+                .andReturn(correlationRuleRet);
+
+        PowerMock.replayAll();
+
+        RuleAddAndUpdateResponse response = ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
+        PowerMock.verifyAll();
+
+        assertThat(response.getRuleId(), equalTo(correlationRuleRet.getRid()));
+    }
+
+    @Test
+    public void updateCorrelationRule_param_null() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("The request object can not be empty!");
+
+        ruleMgtWrapper.updateCorrelationRule(USER_NAME, null);
+    }
+
+    @Test
+    public void updateCorrelationRule_normal() throws Exception {
+        CorrelationRule oldCorrelationRule = new CorrelationRule();
+        oldCorrelationRule.setRid("rule_1");
+        oldCorrelationRule.setName("name");
+        oldCorrelationRule.setDescription("des1");
+        oldCorrelationRule.setContent("content");
+        oldCorrelationRule.setPackageName("testName");
+        oldCorrelationRule.setEnabled(1);
+        oldCorrelationRule.setClosedControlLoopName("cl-name");
+        oldCorrelationRule.setEngineInstance("127.0.0.1");
+        RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des2", "contetnt2", 1);
+
+        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule);
+        EasyMock.expect(engineToolsMock.getInstanceList()).andReturn(Arrays.asList("127.0.0.1", "127.0.0.2")).times(2);
+        EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName", "127.0.0.1")).andReturn(true);
+        correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));
+        EasyMock.expectLastCall();
+        EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)
+                , EasyMock.anyObject(String.class)))
+                .andReturn(true);
+        EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)
+                , EasyMock.anyObject(String.class)))
+                .andReturn("packageName1");
+        PowerMock.replayAll();
+
+        ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
+
+        PowerMock.verifyAll();
+
+        assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId()));
+    }
+
+    @Test
+    public void updateCorrelationRule_param_no_change() throws Exception {
+        CorrelationRule oldCorrelationRule = new CorrelationRule();
+        oldCorrelationRule.setRid("rule_1");
+        oldCorrelationRule.setName("name");
+        oldCorrelationRule.setDescription("des1");
+        oldCorrelationRule.setContent("content");
+        oldCorrelationRule.setPackageName("testName");
+        oldCorrelationRule.setEnabled(1);
+        oldCorrelationRule.setClosedControlLoopName("cl-name");
+        oldCorrelationRule.setEngineInstance("127.0.0.1");
+        RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "cl-name", "des1", "content", 1);
+
+        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule);
+        EasyMock.expect(engineToolsMock.getInstanceList()).andReturn(Arrays.asList("127.0.0.1", "127.0.0.2"));
+
+        PowerMock.replayAll();
+
+        ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
+
+        PowerMock.verifyAll();
+
+        assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId()));
+    }
+
+    @Test
+    public void updateCorrelationRule_rule_not_exist() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("You're trying to update a rule which does not exist in the system.");
+
+        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(EasyMock.anyObject(String.class))).andReturn(null);
+
+        PowerMock.replayAll();
+
+        ruleMgtWrapper.updateCorrelationRule(USER_NAME, new RuleUpdateRequest());
+
+        PowerMock.verifyAll();
+    }
+
+    @Test
+    public void deleteCorrelationRule_request_null() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("The request object can not be empty!");
+
+        ruleMgtWrapper.deleteCorrelationRule(null);
+    }
+
+    @Test
+    public void deleteCorrelationRule_rule_not_exit() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("You're trying to delete a rule which does not exist in the system.");
+
+        RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
+
+        EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
+                correlationRuleDaoMock).anyTimes();
+        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId()))
+                .andReturn(null);
+
+        PowerMock.replayAll();
+
+        ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+
+        PowerMock.verifyAll();
+    }
+
+    @Test
+    public void deleteCorrelationRule_normal() throws Exception {
+        RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
+        CorrelationRule correlationRule = new CorrelationRule();
+        correlationRule.setEnabled(1);
+        EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId()))
+                .andReturn(correlationRule);
+        EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class)
+                , EasyMock.anyObject(String.class))).andReturn(true);
+        correlationRuleDaoMock.deleteRule(EasyMock.anyObject(CorrelationRule.class));
+        EasyMock.expectLastCall();
+        PowerMock.replayAll();
+
+        ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+
+        PowerMock.verifyAll();
+    }
+
+    @Test
+    public void getCorrelationRuleByCondition_data_format_exception() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("An error occurred while building the query SQL.");
+
+        EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
+                RuleQueryCondition.class)))
+                .andThrow(new CorrelationException("An error occurred while building the query SQL."));
+
+        PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+
+        ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());
+
+        PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+    }
+
+    @Test
+    public void getCorrelationRuleByCondition_db_exception() throws Exception {
+        thrown.expect(CorrelationException.class);
+        thrown.expectMessage("Failed to query the rule.");
+
+        EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
+                RuleQueryCondition.class)))
+                .andThrow(new CorrelationException("Failed to query the rule."));
+
+        PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+
+        ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());
+
+        PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+    }
+
+    @Test
+    public void getCorrelationRuleByCondition_normal() throws Exception {
+        List<CorrelationRule> correlationRuleList = new ArrayList<CorrelationRule>(10);
+        for (int i = 0; i < 10; ++i) {
+            CorrelationRule correlationRule = new CorrelationRule();
+            correlationRule.setContent("content" + i);
+            correlationRule.setName("name" + i);
+            correlationRule.setRid("rule_" + i);
+            correlationRule.setEngineType("engineType" + (i % 2 + 1));
+            correlationRule.setEngineID("engineId" + i);
+            correlationRule.setCreateTime(new Date());
+            correlationRule.setCreator(USER_NAME);
+            correlationRule.setDescription("description" + i);
+            correlationRule.setPackageName("package" + i);
+            correlationRuleList.add(correlationRule);
+        }
+
+        EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
+                RuleQueryCondition.class))).andReturn(correlationRuleList);
+
+        PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+
+        RuleQueryListResponse response = ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());
+
+        PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+
+        assertThat(response.getTotalCount(), is(10));
+
+        for (int i = 0; i < 10; ++i) {
+            assertThat(response.getCorrelationRules().get(i).getRuleId(),
+                    equalTo(correlationRuleList.get(i).getRid()));
+        }
+    }
+
+    private RuleCreateRequest createRuleCreateRequest(String ruleName, String description, String content,
+            int enabled) {
+        RuleCreateRequest rcr;
+        rcr = new RuleCreateRequest();
+        rcr.setRuleName(ruleName);
+        rcr.setDescription(description);
+        rcr.setContent(content);
+        rcr.setEnabled(enabled);
+        return rcr;
+    }
+
+    private RuleUpdateRequest createRuleUpdateRequest(String ruleId, String clName, String description,
+            String content, int enabled) {
+        RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
+        ruleUpdateRequest.setRuleId(ruleId);
+        ruleUpdateRequest.setDescription(description);
+        ruleUpdateRequest.setContent(content);
+        ruleUpdateRequest.setEnabled(enabled);
+        ruleUpdateRequest.setLoopControlName(clName);
+        return ruleUpdateRequest;
+    }
+
+    private RuleDeleteRequest createRuleDeleteRequest(String ruleId) {
+        RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
+        ruleDeleteRequest.setRuleId(ruleId);
+        return ruleDeleteRequest;
+    }
+
+    private CorrelationRule convertCreateRequest2CorrelationRule(RuleCreateRequest ruleCreateRequest) {
+        CorrelationRule correlationRule = new CorrelationRule();
+        correlationRule.setContent(ruleCreateRequest.getContent());
+        correlationRule.setDescription(ruleCreateRequest.getDescription());
+        correlationRule.setName(ruleCreateRequest.getRuleName());
+        correlationRule.setCreator(USER_NAME);
+        correlationRule.setModifier(USER_NAME);
+        correlationRule.setEnabled(ruleCreateRequest.getEnabled());
+        return correlationRule;
+    }
+
+    private CorrelationRule convertUpdateRequest2CorrelationRule(RuleUpdateRequest ruleUpdateRequest) {
+        CorrelationRule correlationRule = new CorrelationRule();
+        correlationRule.setRid(ruleUpdateRequest.getRuleId());
+        correlationRule.setContent(ruleUpdateRequest.getContent());
+        correlationRule.setDescription(ruleUpdateRequest.getDescription());
+        correlationRule.setEnabled(ruleUpdateRequest.getEnabled());
+        correlationRule.setUpdateTime(new Date());
+        correlationRule.setModifier(USER_NAME);
+        return correlationRule;
+    }
 }
\ No newline at end of file