diff options
author | GuangrongFu <fu.guangrong@zte.com.cn> | 2017-02-18 14:33:57 +0800 |
---|---|---|
committer | GuangrongFu <fu.guangrong@zte.com.cn> | 2017-02-18 15:52:31 +0800 |
commit | 5947322b7253ba4eff59ee38951114e77050c8c7 (patch) | |
tree | 730de9b7cc1ee3820d3cf96a44204db86955fbd8 /rulemgt/src | |
parent | cbe87c3d24a9a3fdb3843e44cd686dd78de1debf (diff) |
Modify the UT Due to the Code Modification
Change-Id: I5b787bb2f64d05050921ded375438d507efbfd61
Issue-ID:HOLMES-12
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Diffstat (limited to 'rulemgt/src')
10 files changed, 221 insertions, 355 deletions
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java index 5c5a30c..3433678 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java @@ -22,7 +22,6 @@ import javax.validation.constraints.NotNull; import org.hibernate.validator.constraints.NotEmpty; import com.fasterxml.jackson.annotation.JsonProperty; import io.dropwizard.Configuration; -import lombok.Getter; public class RuleAppConfig extends Configuration { diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java index e2f4fdd..329b9c9 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java @@ -17,7 +17,7 @@ package org.openo.holmes.rulemgt.bolt.enginebolt; import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
-import org.openo.holmes.common.exception.CallException;
+import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
@@ -26,16 +26,16 @@ import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; public class EngineWrapper {
- public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CallException {
+ public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException {
return "";
}
- public boolean deleteRuleFromEngine(String packageName) throws CallException {
+ public boolean deleteRuleFromEngine(String packageName) throws CorrelationException {
return true;
}
public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine)
- throws CallException {
+ throws CorrelationException {
return true;
}
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java index 55c3cd8..e086829 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java @@ -16,8 +16,13 @@ package org.openo.holmes.rulemgt.constant;
public class RuleMgtConstant {
+
public static final int STATUS_RULE_OPEN = 1;
public static final int STATUS_RULE_CLOSE = 0;
public static final int STATUS_RULE_ALL = 2;
public static final String PACKAGE_NAME = "packagename";
+
+ private RuleMgtConstant() {
+
+ }
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java index 7a7a0eb..a136d12 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleDao.java @@ -53,9 +53,9 @@ public abstract class CorrelationRuleDao { private void deleteRule2DbInner(CorrelationRule correlationRule) {
String name = correlationRule.getName() != null ? correlationRule.getName().trim() : "";
String rid = correlationRule.getRid() != null ? correlationRule.getRid().trim() : "";
- if (!name.equals("") && !rid.equals("")) {
+ if (!"".equals(name) && !"".equals(rid)) {
deleteRuleByRidAndName(rid, name);
- } else if (!rid.equals("")) {
+ } else if (!"".equals(rid)) {
deleteRuleByRid(rid);
}
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java index 9b66ae5..62f8bad 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java @@ -15,19 +15,6 @@ */
package org.openo.holmes.rulemgt.db;
-import org.openo.holmes.common.api.entity.CorrelationRule;
-import org.openo.holmes.common.exception.DataFormatException;
-import org.openo.holmes.common.exception.DbException;
-import org.openo.holmes.common.utils.DbDaoUtil;
-import org.openo.holmes.common.utils.I18nProxy;
-import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;
-import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
-import lombok.extern.slf4j.Slf4j;
-import org.jvnet.hk2.annotations.Service;
-import org.skife.jdbi.v2.Handle;
-import org.skife.jdbi.v2.Query;
-
-import javax.inject.Inject;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -36,14 +23,27 @@ import java.util.Date; import java.util.List;
import java.util.Map;
import java.util.Properties;
+import javax.inject.Inject;
+import lombok.extern.slf4j.Slf4j;
+import org.jvnet.hk2.annotations.Service;
+import org.openo.holmes.common.api.entity.CorrelationRule;
+import org.openo.holmes.common.exception.CorrelationException;
+import org.openo.holmes.common.utils.DbDaoUtil;
+import org.openo.holmes.common.utils.I18nProxy;
+import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;
+import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
+import org.skife.jdbi.v2.Handle;
+import org.skife.jdbi.v2.Query;
@Service
@Slf4j
public class CorrelationRuleQueryDao {
+
@Inject
private DbDaoUtil dbDaoUtil;
- public List<CorrelationRule> getCorrelationRulesByCondition(RuleQueryCondition ruleQueryCondition) throws DataFormatException, DbException {
+ public List<CorrelationRule> getCorrelationRulesByCondition(RuleQueryCondition ruleQueryCondition)
+ throws CorrelationException {
List<CorrelationRule> correlationRules = new ArrayList<CorrelationRule>();
Handle handle = null;
String whereStr = getWhereStrByRequestEntity(ruleQueryCondition);
@@ -58,7 +58,7 @@ public class CorrelationRuleQueryDao { return correlationRules;
} catch (Exception e) {
log.warn("Query rule: rule id =" + ruleQueryCondition.getRid() + " failed");
- throw new DbException(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED, e);
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED, e);
} finally {
dbDaoUtil.close(handle);
}
@@ -86,7 +86,7 @@ public class CorrelationRuleQueryDao { return correlationRule;
}
- private String getWhereStrByRequestEntity(RuleQueryCondition ruleQueryCondition) throws DataFormatException {
+ private String getWhereStrByRequestEntity(RuleQueryCondition ruleQueryCondition) throws CorrelationException {
try {
Class clazz = ruleQueryCondition.getClass();
Field[] fields = clazz.getDeclaredFields();
@@ -95,23 +95,17 @@ public class CorrelationRuleQueryDao { for (Field field : fields) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(),
clazz);
- Method getMethod = pd.getReadMethod();//获得get方法
- Object o = getMethod.invoke(ruleQueryCondition);//执行get方法返回一个Object
+ Method getMethod = pd.getReadMethod();
+ Object o = getMethod.invoke(ruleQueryCondition);
if (o != null) {
- if (field.getName().equals("enabled")) {
- int enabled = (int) o;
- if (enabled != RuleMgtConstant.STATUS_RULE_ALL) {
- whereSql = whereSql + "enable =" + enabled;
- whereSql += " AND ";
- }
- } else if (field.getName().equals("name")) {
- if (!"".equals(o.toString().trim())) {
- whereSql = whereSql + field.getName() + " like '%" + o + "%' AND ";
- }
- } else {
- if (!"".equals(o.toString().trim())) {
- whereSql = whereSql + field.getName() + "='" + o + "' AND ";
- }
+ String tempName = field.getName();
+ if ("enabled".equals(tempName) && (int) o != RuleMgtConstant.STATUS_RULE_ALL) {
+ whereSql = whereSql + "enable =" + (int) o;
+ whereSql += " AND ";
+ } else if ("name".equals(tempName) && !"".equals(o.toString().trim())) {
+ whereSql = whereSql + field.getName() + " like '%" + o + "%' AND ";
+ } else if (!"".equals(o.toString().trim())) {
+ whereSql = whereSql + field.getName() + "='" + o + "' AND ";
}
}
}
@@ -121,7 +115,7 @@ public class CorrelationRuleQueryDao { }
return "";
} catch (Exception e) {
- throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED, e);
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED, e);
}
}
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java index 8395df7..c2d95f9 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java @@ -35,9 +35,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType;
import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
-import org.openo.holmes.common.exception.CallException;
-import org.openo.holmes.common.exception.DataFormatException;
-import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.common.utils.ExceptionUtil;
import org.openo.holmes.common.utils.I18nProxy;
import org.openo.holmes.common.utils.JacksonUtil;
@@ -67,30 +65,18 @@ public class RuleMgtResources { @ApiOperation(value = "Save the alarm+ rule to the database, and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)
@Timed
public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request,
- @ApiParam(value = "alarm+ rule create request.<br>[rulename]:<font color=\"red\">required</font><br>[content]:<font color=\"red\">required</font><br>[enabled]:<font color=\"red\">required</font>", required = true) RuleCreateRequest ruleCreateRequest) {
+ @ApiParam(value = "alarm+ rule create request.<br>[rulename]:<font color=\"red\">required</font><br>[content]:<font color=\"red\">required</font><br>[enabled]:<font color=\"red\">required</font>", required = true) RuleCreateRequest ruleCreateRequest) {
Locale locale = LanguageUtil.getLocale(request);
RuleAddAndUpdateResponse ruleChangeResponse;
try {
ruleChangeResponse = ruleMgtWrapper
- .addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest);
+ .addCorrelationRule(UserUtil.getUserName(request), ruleCreateRequest);
log.info("create rule:" + ruleCreateRequest.getRuleName() + " success.");
return ruleChangeResponse;
- } catch (CallException e) {
+ } catch (CorrelationException e) {
log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);
throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (DbException e) {
- log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (DataFormatException e) {
- log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (Exception e) {
- log.error("create rule:" + ruleCreateRequest.getRuleName() + " failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- I18nProxy.RULE_MANAGEMENT_UNKNOWN_EXCEPTION));
+ e.getMessage()));
}
}
@@ -99,29 +85,17 @@ public class RuleMgtResources { @ApiOperation(value = "Update the alarm+ rule and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class)
@Timed
public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request,
- @ApiParam(value = "alarm+ rule update request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleUpdateRequest ruleUpdateRequest) {
+ @ApiParam(value = "alarm+ rule update request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleUpdateRequest ruleUpdateRequest) {
Locale locale = LanguageUtil.getLocale(request);
RuleAddAndUpdateResponse ruleChangeResponse;
try {
ruleChangeResponse = ruleMgtWrapper
- .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);
+ .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);
return ruleChangeResponse;
- } catch (CallException e) {
+ } catch (CorrelationException e) {
log.error("create rule:" + ruleUpdateRequest.getContent() + " failed", e);
throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (DataFormatException e) {
- log.error("update alarm plus rule:" + ruleUpdateRequest.getContent() + " failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (DbException e) {
- log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (Exception e) {
- log.error("update rule:" + ruleUpdateRequest.getRuleId() + " failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- I18nProxy.RULE_MANAGEMENT_UNKNOWN_EXCEPTION));
+ e.getMessage()));
}
}
@@ -130,28 +104,16 @@ public class RuleMgtResources { @ApiOperation(value = "Delete the alarm+ rule,and when the enable is open also removed from the engine.")
@Timed
public boolean deleteCorrelationRule(@Context HttpServletRequest request,
- @ApiParam(value = "alarm+ rule delete request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleDeleteRequest ruleDeleteRequest) {
+ @ApiParam(value = "alarm+ rule delete request.<br>[ruleid]:<font color=\"red\">required</font>", required = true) RuleDeleteRequest ruleDeleteRequest) {
Locale locale = LanguageUtil.getLocale(request);
try {
ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
log.info("delete rule:" + ruleDeleteRequest.getRuleId() + " successful");
return true;
- } catch (DataFormatException e) {
+ } catch (CorrelationException e) {
log.error("delete rule:" + ruleDeleteRequest.getRuleId() + " failed", e);
throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (DbException e) {
- log.error("delete rule:" + ruleDeleteRequest.getRuleId() + " failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (CallException e) {
- log.error("delete rule:" + ruleDeleteRequest.getRuleId() + " failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (Exception e) {
- log.error("delete rule:" + ruleDeleteRequest.getRuleId() + " failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- I18nProxy.RULE_MANAGEMENT_UNKNOWN_EXCEPTION));
+ e.getMessage()));
}
}
@@ -160,38 +122,30 @@ public class RuleMgtResources { @ApiOperation(value = "According to the conditions query the alarm + rules", response = RuleQueryListResponse.class)
@Timed
public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request,
- @ApiParam(value = "query condition:<br>" + " <b>[ruleid]</b>:Rule ID;<br>"
- + "<b>[rulename]</b>:Rule name;<br>" + "<b>[creator]</b>:creator of the rule;<br>"
- + "<b>[modifier]</b>:modifier of the rule;<br>"
- + "<b>[enabled]</b>: 0 is Enabled,1 is disabled;<br><font color=\"red\">for example:</font><br>{\"ruleid\":\"rule_1484727187317\"}", required = false) @QueryParam("queryrequest") String ruleQueryRequest) {
+ @ApiParam(value = "query condition:<br>" + " <b>[ruleid]</b>:Rule ID;<br>"
+ + "<b>[rulename]</b>:Rule name;<br>" + "<b>[creator]</b>:creator of the rule;<br>"
+ + "<b>[modifier]</b>:modifier of the rule;<br>"
+ + "<b>[enabled]</b>: 0 is Enabled,1 is disabled;<br><font color=\"red\">for example:</font><br>{\"ruleid\":\"rule_1484727187317\"}", required = false) @QueryParam("queryrequest") String ruleQueryRequest) {
Locale locale = LanguageUtil.getLocale(request);
RuleQueryListResponse ruleQueryListResponse;
RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request);
try {
ruleQueryListResponse = ruleMgtWrapper
- .getCorrelationRuleByCondition(ruleQueryCondition);
+ .getCorrelationRuleByCondition(ruleQueryCondition);
return ruleQueryListResponse;
- } catch (DataFormatException e) {
+ } catch (CorrelationException e) {
log.error("query rule failed,cause query condition conversion failure", e);
throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (DbException e) {
- log.error("query rule failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- e.getMessage()));
- } catch (Exception e) {
- log.error("query rule failed", e);
- throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- I18nProxy.RULE_MANAGEMENT_UNKNOWN_EXCEPTION));
+ e.getMessage()));
}
}
private RuleQueryCondition getRuleQueryCondition(String queryRequest,
- HttpServletRequest request) {
+ HttpServletRequest request) {
Locale locale = LanguageUtil.getLocale(request);
try {
RuleQueryCondition ruleQueryCondition = JacksonUtil
- .jsonToBean(queryRequest, RuleQueryCondition.class);
+ .jsonToBean(queryRequest, RuleQueryCondition.class);
if (queryRequest == null) {
ruleQueryCondition.setEnabled(2);
} else if (queryRequest.indexOf("enabled") == -1) {
@@ -201,7 +155,7 @@ public class RuleMgtResources { } catch (IOException e) {
log.warn("queryRequest convert to json failed", e);
throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
- I18nProxy.RULE_MANAGEMENT_DATA_FORMAT_ERROR));
+ I18nProxy.RULE_MANAGEMENT_DATA_FORMAT_ERROR));
}
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java index 60bce08..67183b7 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java @@ -23,9 +23,7 @@ import javax.inject.Singleton; import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
import org.openo.holmes.common.api.entity.CorrelationRule;
-import org.openo.holmes.common.exception.CallException;
-import org.openo.holmes.common.exception.DataFormatException;
-import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.common.utils.DbDaoUtil;
import org.openo.holmes.common.utils.I18nProxy;
import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
@@ -56,24 +54,29 @@ public class RuleMgtWrapper { private DbDaoUtil daoUtil;
public RuleAddAndUpdateResponse addCorrelationRule(String creator,
- RuleCreateRequest ruleCreateRequest)
- throws DataFormatException, CallException, DbException {
+ RuleCreateRequest ruleCreateRequest)
+ throws CorrelationException {
CorrelationRule correlationRule = convertRuleCreateRequest2CorrelationRule(creator,
- ruleCreateRequest);
+ ruleCreateRequest);
if (correlationRule.getName() == null || "".equals(correlationRule.getName().trim())) {
- throw new DbException(I18nProxy.RULE_MANAGEMENT_RULE_NAME_IS_EMPTY);
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NAME_IS_EMPTY);
+ }
+ CorrelationRule ruleTemp;
+ try {
+ ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .queryRuleByRuleName(correlationRule.getName());
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR, e);
}
- CorrelationRule ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .queryRuleByRuleName(correlationRule.getName());
if (ruleTemp != null) {
- throw new DbException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);
}
correlationRule.setPackageName(deployRule2Engine(correlationRule));
try {
correlationRule = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .saveRule(correlationRule);
- } catch (RuntimeException e) {
- throw new DbException(I18nProxy.RULE_MANAGEMENT_CREATE_RULE_FAILED, e);
+ .saveRule(correlationRule);
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_RULE_FAILED, e);
}
RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();
ruleAddAndUpdateResponse.setRuleId(correlationRule.getRid());
@@ -81,17 +84,22 @@ public class RuleMgtWrapper { }
public RuleAddAndUpdateResponse updateCorrelationRule(String modifier,
- RuleUpdateRequest ruleUpdateRequest)
- throws DataFormatException, DbException, CallException {
+ RuleUpdateRequest ruleUpdateRequest)
+ throws CorrelationException {
if (ruleUpdateRequest != null) {
- CorrelationRule oldCorrelationRule = daoUtil
- .getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .getRuleByRid(ruleUpdateRequest.getRuleId());
+ CorrelationRule oldCorrelationRule;
+ try {
+ oldCorrelationRule = daoUtil
+ .getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .getRuleByRid(ruleUpdateRequest.getRuleId());
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR, e);
+ }
if (oldCorrelationRule == null) {
- throw new DbException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
}
CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,
- ruleUpdateRequest);
+ ruleUpdateRequest);
checkCorrelation(newCorrelationRule, oldCorrelationRule);
RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();
try {
@@ -99,65 +107,68 @@ public class RuleMgtWrapper { engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName());
}
daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .updateRule(newCorrelationRule);
- } catch (RuntimeException e) {
- throw new DbException(I18nProxy.RULE_MANAGEMENT_UPDATE_RULE_FAILED, e);
+ .updateRule(newCorrelationRule);
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_UPDATE_RULE_FAILED, e);
}
ruleChangeResponse.setRuleId(newCorrelationRule.getRid());
deployRule2Engine(newCorrelationRule);
return ruleChangeResponse;
} else {
- throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
}
public void checkCorrelation(CorrelationRule newCorrelationRule,
- CorrelationRule oldCorrelationRule) throws DataFormatException {
+ CorrelationRule oldCorrelationRule) throws CorrelationException {
int newEnabled = newCorrelationRule.getEnabled();
if (newCorrelationRule.getContent() == null) {
newCorrelationRule.setContent(oldCorrelationRule.getContent());
}
if (newEnabled != RuleMgtConstant.STATUS_RULE_CLOSE
- && newEnabled != RuleMgtConstant.STATUS_RULE_OPEN) {
- throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
+ && newEnabled != RuleMgtConstant.STATUS_RULE_OPEN) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
}
}
public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)
- throws DbException, DataFormatException, CallException {
+ throws CorrelationException {
if (ruleDeleteRequest != null) {
- CorrelationRule correlationRule = daoUtil
- .getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .getRuleByRid(ruleDeleteRequest.getRuleId());
+ CorrelationRule correlationRule;
+ try {
+ correlationRule = daoUtil
+ .getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .getRuleByRid(ruleDeleteRequest.getRuleId());
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR);
+ }
if (correlationRule == null) {
log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId()
- + " does not exist the database.");
- throw new DbException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
+ + " does not exist the database.");
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
}
try {
if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName());
}
daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .deleteRule(correlationRule);
- } catch (RuntimeException e) {
- throw new DbException(I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED, e);
+ .deleteRule(correlationRule);
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED, e);
}
- } else
-
- {
- throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ } else {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
}
public CorrelationRule convertRuleCreateRequest2CorrelationRule(String userName,
- RuleCreateRequest ruleCreateRequest) throws DataFormatException {
+ RuleCreateRequest ruleCreateRequest) throws CorrelationException {
if (ruleCreateRequest != null) {
if (ruleCreateRequest.getEnabled() != RuleMgtConstant.STATUS_RULE_OPEN
- && ruleCreateRequest.getEnabled() != RuleMgtConstant.STATUS_RULE_CLOSE) {
- throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ && ruleCreateRequest.getEnabled() != RuleMgtConstant.STATUS_RULE_CLOSE) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
CorrelationRule correlationRule = new CorrelationRule();
String ruleId = "rule_" + System.currentTimeMillis();
@@ -177,13 +188,13 @@ public class RuleMgtWrapper { correlationRule.setEnabled(ruleCreateRequest.getEnabled());
return correlationRule;
} else {
- throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
}
private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,
- RuleUpdateRequest ruleUpdateRequest) throws DataFormatException {
+ RuleUpdateRequest ruleUpdateRequest) throws CorrelationException {
if (ruleUpdateRequest != null) {
CorrelationRule correlationRule = new CorrelationRule();
correlationRule.setRid(ruleUpdateRequest.getRuleId());
@@ -194,12 +205,12 @@ public class RuleMgtWrapper { correlationRule.setModifier(modifier);
return correlationRule;
} else {
- throw new DataFormatException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
}
- private String deployRule2Engine(CorrelationRule correlationRule) throws CallException {
+ private String deployRule2Engine(CorrelationRule correlationRule) throws CorrelationException {
if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule))) {
if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule));
@@ -209,18 +220,18 @@ public class RuleMgtWrapper { }
public RuleQueryListResponse getCorrelationRuleByCondition(
- RuleQueryCondition ruleQueryCondition) throws DataFormatException, DbException {
+ RuleQueryCondition ruleQueryCondition) throws CorrelationException {
List<CorrelationRule> correlationRule = correlationRuleQueryDao
- .getCorrelationRulesByCondition(ruleQueryCondition);
+ .getCorrelationRulesByCondition(ruleQueryCondition);
RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();
ruleQueryListResponse.setTotalCount(correlationRule.size());
ruleQueryListResponse
- .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule));
+ .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule));
return ruleQueryListResponse;
}
private List<RuleResult4API> correlationRules2RuleResult4APIs(
- List<CorrelationRule> correlationRules) {
+ List<CorrelationRule> correlationRules) {
List<RuleResult4API> ruleResult4APIs = new ArrayList<RuleResult4API>();
for (CorrelationRule correlationRule : correlationRules) {
RuleResult4API ruleResult4API = new RuleResult4API();
@@ -239,7 +250,7 @@ public class RuleMgtWrapper { }
private CorrelationDeployRule4Engine correlationRules2DeployRule(
- CorrelationRule correlationRule) {
+ CorrelationRule correlationRule) {
CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();
correlationDeployRule4Engine.setContent(correlationRule.getContent());
correlationDeployRule4Engine.setEngineId(correlationRule.getEngineId());
@@ -247,7 +258,7 @@ public class RuleMgtWrapper { }
private CorrelationCheckRule4Engine correlationRules2CheckRule(
- CorrelationRule correlationRule) {
+ CorrelationRule correlationRule) {
CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine();
correlationCheckRule4Engine.setContent(correlationRule.getContent());
return correlationCheckRule4Engine;
diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java index 29e7f2b..2e7cc1c 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java @@ -32,7 +32,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.openo.holmes.common.api.entity.CorrelationRule;
-import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.common.utils.DbDaoUtil;
import org.openo.holmes.common.utils.I18nProxy;
import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;
@@ -78,7 +78,7 @@ public class CorrelationRuleQueryDaoTest { @Test
public void getCorrelationRulesByCondition_db_exception() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED);
EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java index 1f6f331..e25c5b3 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java @@ -23,9 +23,7 @@ import org.junit.Before; import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.openo.holmes.common.exception.CallException;
-import org.openo.holmes.common.exception.DataFormatException;
-import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest;
import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest;
import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;
@@ -54,53 +52,12 @@ public class RuleMgtResourcesTest { }
@Test
- public void addCorrelationRule_call_exception() throws Exception {
+ public void addCorrelationRule_correlation_exception() throws Exception {
thrown.expect(WebApplicationException.class);
final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest))
- .andThrow(new CallException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
- EasyMock.expect(request.getHeader("username")).andReturn("admin");
- PowerMock.replayAll();
- ruleMgtResources.addCorrelationRule(request, ruleCreateRequest);
- PowerMock.verifyAll();
- }
-
- @Test
- public void addCorrelationRule_db_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
- EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest)).andThrow(new DbException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
- EasyMock.expect(request.getHeader("username")).andReturn("admin");
- PowerMock.replayAll();
- ruleMgtResources.addCorrelationRule(request, ruleCreateRequest);
- PowerMock.verifyAll();
- }
-
- @Test
- public void addCorrelationRule_data_format_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
- EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest))
- .andThrow(new DataFormatException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
- EasyMock.expect(request.getHeader("username")).andReturn("admin");
- PowerMock.replayAll();
- ruleMgtResources.addCorrelationRule(request, ruleCreateRequest);
- PowerMock.verifyAll();
- }
-
- @Test
- public void addCorrelationRule_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
- EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest))
- .andThrow(new RuntimeException(""));
+ .andThrow(new CorrelationException(EasyMock.anyObject(String.class)));
EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
EasyMock.expect(request.getHeader("username")).andReturn("admin");
PowerMock.replayAll();
@@ -121,54 +78,12 @@ public class RuleMgtResourcesTest { }
@Test
- public void updateCorrelationRule_call_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
- EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest))
- .andThrow(new CallException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
- EasyMock.expect(request.getHeader("username")).andReturn("admin");
- PowerMock.replayAll();
- ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest);
- PowerMock.verifyAll();
- }
-
- @Test
- public void updateCorrelationRule_data_format_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
- EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest))
- .andThrow(new DataFormatException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
- EasyMock.expect(request.getHeader("username")).andReturn("admin");
- PowerMock.replayAll();
- ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest);
- PowerMock.verifyAll();
- }
-
- @Test
- public void updateCorrelationRule_db_exception() throws Exception {
+ public void updateCorrelationRule_correlation_exception() throws Exception {
thrown.expect(WebApplicationException.class);
final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest))
- .andThrow(new DbException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
- EasyMock.expect(request.getHeader("username")).andReturn("admin");
- PowerMock.replayAll();
- ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest);
- PowerMock.verifyAll();
- }
-
- @Test
- public void updateCorrelationRule_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
- EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest))
- .andThrow(new RuntimeException(""));
+ .andThrow(new CorrelationException(EasyMock.anyObject(String.class)));
EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
EasyMock.expect(request.getHeader("username")).andReturn("admin");
PowerMock.replayAll();
@@ -189,51 +104,12 @@ public class RuleMgtResourcesTest { }
@Test
- public void deleteCorrelationRule_call_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
- ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
- EasyMock.expectLastCall().andThrow(new CallException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
- PowerMock.replayAll();
- ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest);
- PowerMock.verifyAll();
- }
-
- @Test
- public void deleteCorrelationRule_data_format_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
- ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
- EasyMock.expectLastCall().andThrow(new DataFormatException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
- PowerMock.replayAll();
- ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest);
- PowerMock.verifyAll();
- }
-
- @Test
- public void deleteCorrelationRule_db_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
- ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
- EasyMock.expectLastCall().andThrow(new DbException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
- PowerMock.replayAll();
- ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest);
- PowerMock.verifyAll();
- }
-
- @Test
- public void deleteCorrelationRule_exception() throws Exception {
+ public void deleteCorrelationRule_correlation_exception() throws Exception {
thrown.expect(WebApplicationException.class);
final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
- EasyMock.expectLastCall().andThrow(new RuntimeException(""));
+ EasyMock.expectLastCall().andThrow(new CorrelationException(EasyMock.anyObject(String.class)));
EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
PowerMock.replayAll();
ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest);
@@ -258,42 +134,13 @@ public class RuleMgtResourcesTest { final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\","
+ "\"enabled\":0,\"creator\":\"admin\"}";
EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
- .andThrow(new DataFormatException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2);
- PowerMock.replayAll();
- ruleMgtResources.getCorrelationRules(request, requestStr);
- PowerMock.verifyAll();
- }
-
- @Test
- public void getCorrelationRules_db_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\","
- + "\"enabled\":0,\"creator\":\"admin\"}";
- EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
- .andThrow(new DbException(""));
- EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2);
- PowerMock.replayAll();
- ruleMgtResources.getCorrelationRules(request, requestStr);
- PowerMock.verifyAll();
- }
-
- @Test
- public void getCorrelationRules_exception() throws Exception {
- thrown.expect(WebApplicationException.class);
-
- final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\","
- + "\"enabled\":0,\"creator\":\"admin\"}";
- EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
- .andThrow(new RuntimeException(""));
+ .andThrow(new CorrelationException(EasyMock.anyObject(String.class)));
EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2);
PowerMock.replayAll();
ruleMgtResources.getCorrelationRules(request, requestStr);
PowerMock.verifyAll();
}
-
@Test
public void getCorrelationRules_normal_request_string_null() throws Exception {
EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java index 41cb494..1cb63cd 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java @@ -20,7 +20,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
-
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -31,9 +30,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.openo.holmes.common.api.entity.CorrelationRule;
-import org.openo.holmes.common.exception.CallException;
-import org.openo.holmes.common.exception.DataFormatException;
-import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.common.utils.DbDaoUtil;
import org.openo.holmes.common.utils.I18nProxy;
import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
@@ -90,7 +87,7 @@ public class RuleMgtWrapperTest { @Test
public void addCorrelationRule_name_is_null() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NAME_IS_EMPTY);
ruleMgtWrapper.addCorrelationRule(USER_NAME, createRuleCreateRequest(null, "This is a rule for testing.",
@@ -99,7 +96,7 @@ public class RuleMgtWrapperTest { @Test
public void addCorrelationRule_name_is_empty() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NAME_IS_EMPTY);
ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.",
@@ -107,6 +104,29 @@ public class RuleMgtWrapperTest { }
@Test
+ public void addCorrelationRule_rule_query_exception() throws Exception {
+
+ final String ruleName = "Rule-001";
+
+ RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
+ "Mocked contents.", 0);
+
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_DB_ERROR);
+
+ EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
+ correlationRuleDaoMock);
+ EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName))
+ .andThrow(new RuntimeException(""));
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
public void addCorrelationRule_duplicated_rule() throws Exception {
final String ruleName = "Rule-001";
@@ -115,7 +135,7 @@ public class RuleMgtWrapperTest { "Mocked contents.", 0);
CorrelationRule correlationRule = convertCreateRequest2CorrelationRule(ruleCreateRequest);
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);
EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
@@ -137,14 +157,14 @@ public class RuleMgtWrapperTest { RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
"Mocked contents.", 0);
- thrown.expect(CallException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED);
EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
correlationRuleDaoMock);
EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null);
EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
- .andThrow(new CallException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED));
+ .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED));
PowerMock.replayAll();
@@ -161,7 +181,7 @@ public class RuleMgtWrapperTest { RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
"Mocked contents.", 1);
- thrown.expect(CallException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED);
EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
@@ -170,7 +190,7 @@ public class RuleMgtWrapperTest { EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
.andReturn(true);
EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andThrow(new CallException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED));
+ .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED));
PowerMock.replayAll();
@@ -186,7 +206,7 @@ public class RuleMgtWrapperTest { RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
"Mocked contents.", 1);
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CREATE_RULE_FAILED);
EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
@@ -236,15 +256,32 @@ public class RuleMgtWrapperTest { @Test
public void updateCorrelationRule_param_null() throws Exception {
- thrown.expect(DataFormatException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
ruleMgtWrapper.updateCorrelationRule(USER_NAME, null);
}
@Test
+ public void updateCorrelationRule_rule_query_exception() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_DB_ERROR);
+
+ EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
+ correlationRuleDaoMock);
+ EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class)))
+ .andThrow(new RuntimeException(""));
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.updateCorrelationRule(USER_NAME, new RuleUpdateRequest());
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
public void updateCorrelationRule_rule_not_exist() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
@@ -260,7 +297,7 @@ public class RuleMgtWrapperTest { @Test
public void updateCorrelationRule_rule_status_illegal_with_contents() throws Exception {
- thrown.expect(DataFormatException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
@@ -282,7 +319,7 @@ public class RuleMgtWrapperTest { @Test
public void updateCorrelationRule_rule_status_illegal_with_null_contents() throws Exception {
- thrown.expect(DataFormatException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
@@ -304,7 +341,7 @@ public class RuleMgtWrapperTest { @Test
public void updateCorrelationRule_rule_disabled_update_failure() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_UPDATE_RULE_FAILED);
RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
@@ -328,7 +365,7 @@ public class RuleMgtWrapperTest { @Test
public void updateCorrelationRule_rule_disabled_deploy_verify_exception() throws Exception {
- thrown.expect(CallException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED);
RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
@@ -343,7 +380,7 @@ public class RuleMgtWrapperTest { correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));
EasyMock.expectLastCall();
EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
- .andThrow(new CallException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED));
+ .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED));
PowerMock.replayAll();
@@ -380,7 +417,7 @@ public class RuleMgtWrapperTest { @Test
public void updateCorrelationRule_rule_enabled_engine_delete_failure() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_UPDATE_RULE_FAILED);
RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
@@ -404,7 +441,7 @@ public class RuleMgtWrapperTest { @Test
public void updateCorrelationRule_rule_enabled_engine_deploy_failure() throws Exception {
- thrown.expect(CallException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED);
RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
@@ -422,7 +459,7 @@ public class RuleMgtWrapperTest { EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
.andReturn(true);
EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andThrow(new CallException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED));
+ .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED));
PowerMock.replayAll();
@@ -475,7 +512,7 @@ public class RuleMgtWrapperTest { @Test
public void checkCorrelation_illegal_status() throws Exception {
- thrown.expect(DataFormatException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
RuleUpdateRequest ruleCreateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
@@ -490,15 +527,34 @@ public class RuleMgtWrapperTest { @Test
public void deleteCorrelationRule_request_null() throws Exception {
- thrown.expect(DataFormatException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY));
ruleMgtWrapper.deleteCorrelationRule(null);
}
@Test
+ public void deleteCorrelationRule_rule_query_exception() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_DB_ERROR));
+
+ RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
+
+ EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
+ correlationRuleDaoMock).anyTimes();
+ EasyMock.expect(correlationRuleDaoMock.getRuleByRid(ruleDeleteRequest.getRuleId()))
+ .andThrow(new RuntimeException(""));
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
public void deleteCorrelationRule_rule_not_exit() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE));
RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
@@ -517,7 +573,7 @@ public class RuleMgtWrapperTest { @Test
public void deleteCorrelationRule_rule_enabled_engine_delete_exception() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED));
RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
@@ -541,7 +597,7 @@ public class RuleMgtWrapperTest { @Test
public void deleteCorrelationRule_rule_disabled_delete_exception() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED));
RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
@@ -587,12 +643,12 @@ public class RuleMgtWrapperTest { @Test
public void getCorrelationRuleByCondition_data_format_exception() throws Exception {
- thrown.expect(DataFormatException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED));
EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
RuleQueryCondition.class)))
- .andThrow(new DataFormatException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED));
+ .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED));
PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
@@ -603,12 +659,12 @@ public class RuleMgtWrapperTest { @Test
public void getCorrelationRuleByCondition_db_exception() throws Exception {
- thrown.expect(DbException.class);
+ thrown.expect(CorrelationException.class);
thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED));
EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
RuleQueryCondition.class)))
- .andThrow(new DbException(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED));
+ .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED));
PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
|