diff options
author | youbowu <wu.youbo@zte.com.cn> | 2017-02-27 09:21:05 +0800 |
---|---|---|
committer | 6092002067 <wu.youbo@zte.com.cn> | 2017-02-27 10:15:46 +0800 |
commit | e87f8243439fb68d1308a5132ae624942d77776d (patch) | |
tree | 2ca350893d9f1bb2a69827fa1aa1deac15038801 /rulemgt | |
parent | 27e71c9c0f9ec16e377a5f21ec4f72bbd5c1efc0 (diff) |
Add rule management module unit tests
Issue-ID:HOLMES-50
Change-Id: Ie21a9046cb3d237aa0c52c9c8ccb77123da2de1d
Signed-off-by: youbowu <wu.youbo@zte.com.cn>
Diffstat (limited to 'rulemgt')
18 files changed, 760 insertions, 610 deletions
diff --git a/rulemgt/pom.xml b/rulemgt/pom.xml index 7159332..c4f1ecf 100644 --- a/rulemgt/pom.xml +++ b/rulemgt/pom.xml @@ -34,14 +34,14 @@ <artifactId>holmes-actions</artifactId> <exclusions> <exclusion> - <groupId>org.glassfish.jersey.core</groupId> - <artifactId>jersey-common</artifactId> + <groupId>org.glassfish.jersey.containers</groupId> + <artifactId>jersey-container-servlet-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> - <groupId>org.glassfish.jersey.core</groupId> - <artifactId>jersey-common</artifactId> + <groupId>org.glassfish.jersey.containers</groupId> + <artifactId>jersey-container-servlet-core</artifactId> <version>2.16</version> </dependency> <dependency> @@ -53,10 +53,6 @@ <artifactId>dropwizard-ioc-container</artifactId> </dependency> <dependency> - <groupId>io.dropwizard</groupId> - <artifactId>dropwizard-core</artifactId> - </dependency> - <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> @@ -106,6 +102,22 @@ <version>3.15.0-GA</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.3.6</version> + </dependency> + <dependency> + <groupId>com.googlecode.json-simple</groupId> + <artifactId>json-simple</artifactId> + <version>1.1.1</version> + </dependency> + <dependency> + <groupId>net.sf.json-lib</groupId> + <artifactId>json-lib</artifactId> + <version>2.4</version> + <classifier>jdk15</classifier> + </dependency> </dependencies> <build> <plugins> 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 3433678..57ae0c5 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java @@ -15,14 +15,15 @@ */ package org.openo.holmes.rulemgt; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.dropwizard.Configuration; import io.dropwizard.db.DataSourceFactory; import javax.validation.Valid; import javax.validation.constraints.NotNull; - import org.hibernate.validator.constraints.NotEmpty; -import com.fasterxml.jackson.annotation.JsonProperty; -import io.dropwizard.Configuration; +import org.jvnet.hk2.annotations.Service; +@Service public class RuleAppConfig extends Configuration { @NotEmpty @@ -31,6 +32,8 @@ public class RuleAppConfig extends Configuration { @NotEmpty private String apidescription = "Holmes rule management rest API"; + @NotEmpty + private String msbServerAddr; @Valid @NotNull @@ -51,8 +54,17 @@ public class RuleAppConfig extends Configuration { return apidescription; } - public void setApidescription( String apidescription ) { + public void setApidescription(String apidescription) { this.apidescription = apidescription; } + @JsonProperty + public String getMsbServerAddr() { + return msbServerAddr; + } + + @JsonProperty + public void setMsbServerAddr(String msbServerAddr) { + this.msbServerAddr = msbServerAddr; + } } diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java new file mode 100644 index 0000000..463eb2a --- /dev/null +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java @@ -0,0 +1,27 @@ +/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.holmes.rulemgt.bean.request;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class CorrelationRestRequest {
+
+ private String rootURL;
+}
\ No newline at end of file diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java new file mode 100644 index 0000000..6267cff --- /dev/null +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java @@ -0,0 +1,129 @@ +/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openo.holmes.rulemgt.bolt.enginebolt;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.ByteArrayOutputStream;
+import java.io.UnsupportedEncodingException;
+import javax.inject.Inject;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.entity.BufferedHttpEntity;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.impl.client.HttpClients;
+import org.jvnet.hk2.annotations.Service;
+import org.openo.holmes.common.exception.CorrelationException;
+import org.openo.holmes.common.utils.I18nProxy;
+import org.openo.holmes.rulemgt.RuleAppConfig;
+import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
+import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
+import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
+
+@Service
+public class EngineService {
+
+ @Inject
+ private RuleAppConfig ruleAppConfig;
+
+ protected HttpResponse delete(String packageName) throws Exception {
+ return deleteRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName);
+ }
+
+ protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine) throws Exception {
+ ObjectMapper mapper = new ObjectMapper();
+ String content = mapper.writeValueAsString(correlationCheckRule4Engine);
+ return postRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);
+ }
+
+ protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws Exception {
+ ObjectMapper mapper = new ObjectMapper();
+ String content = mapper.writeValueAsString(correlationDeployRule4Engine);
+ return putRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);
+ }
+
+ private HttpResponse postRequest(String url, String content) throws Exception {
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost(url);
+ setHeader(httpPost);
+ if (StringUtils.isNotEmpty(content)) {
+ httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
+ }
+ return httpClient.execute(httpPost);
+ }
+
+ private HttpResponse putRequest(String url, String content)
+ throws Exception {
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpPut httpPut = new HttpPut(url);
+ setHeader(httpPut);
+ if (StringUtils.isNotEmpty(content)) {
+ httpPut.setEntity(new ByteArrayEntity(content.getBytes()));
+ }
+ return httpClient.execute(httpPut);
+ }
+
+ private HttpResponse deleteRequest(String url) throws Exception {
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpDelete httpDelete = new HttpDelete(url);
+ setHeader(httpDelete);
+ return httpClient.execute(httpDelete);
+ }
+
+ private void setHeader(HttpRequestBase httpRequestBase) {
+ httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");
+ httpRequestBase.setHeader("Accept", "application/json");
+ httpRequestBase.setHeader("Content-Type", "application/json");
+ }
+
+ public byte[] getData(HttpEntity httpEntity) throws Exception {
+ BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ bufferedHttpEntity.writeTo(byteArrayOutputStream);
+ byte[] responseBytes = byteArrayOutputStream.toByteArray();
+ return responseBytes;
+ }
+
+ public String getResponseContent(HttpResponse httpResponse) throws CorrelationException {
+ byte[] dataByte;
+ String result = null;
+ try {
+ HttpEntity httpEntity = httpResponse.getEntity();
+ if (httpEntity != null) {
+ byte[] responseBytes = getData(httpEntity);
+ dataByte = responseBytes;
+ result = bytesToString(dataByte);
+ }
+ return result;
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);
+ }
+ }
+
+ private String bytesToString(byte[] bytes) throws UnsupportedEncodingException {
+ if (bytes != null) {
+ String returnStr = new String(bytes, "utf-8");
+ returnStr = StringUtils.trim(returnStr);
+ return returnStr;
+ }
+ return null;
+ }
+}
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 329b9c9..b69e66b 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 @@ -15,27 +15,73 @@ */
package org.openo.holmes.rulemgt.bolt.enginebolt;
+import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
+import net.sf.json.JSONObject;
+import org.apache.http.HttpResponse;
import org.jvnet.hk2.annotations.Service;
import org.openo.holmes.common.exception.CorrelationException;
+import org.openo.holmes.common.utils.I18nProxy;
import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
+import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
@Service
@Slf4j
public class EngineWrapper {
+ @Inject
+ private EngineService engineService;
public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException {
- return "";
+ HttpResponse httpResponse;
+ try {
+ httpResponse = engineService.deploy(correlationRule);
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED,e);
+ }
+ if (httpResponse.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ log.info("Call deploy rule rest interface in engine successfully.");
+ String content = engineService.getResponseContent(httpResponse);
+ try {
+ JSONObject json = JSONObject.fromObject(content);
+ return json.get(RuleMgtConstant.PACKAGE).toString();
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR,e);
+ }
+ } else {
+ throw new CorrelationException(I18nProxy.ENGINE_DEPLOY_RULE_FAILED);
+ }
}
public boolean deleteRuleFromEngine(String packageName) throws CorrelationException {
- return true;
+ HttpResponse httpResponse;
+ try {
+ httpResponse = engineService.delete(packageName);
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED,e);
+ }
+ if (httpResponse.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ log.info("Call delete rule rest interface in engine successfully.");
+ return true;
+ } else {
+ throw new CorrelationException(I18nProxy.ENGINE_DELETE_RULE_FAILED);
+ }
}
public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine)
throws CorrelationException {
- return true;
+ HttpResponse httpResponse;
+ try {
+ httpResponse = engineService.check(correlationCheckRule4Engine);
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED,e);
+ }
+ if (httpResponse.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ log.info("Call check rule rest interface in engine successfully.");
+ return true;
+ } else {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CHECK_NO_PASS);
+ }
}
}
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 e086829..348056d 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 @@ -17,12 +17,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() {
}
+ 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 = "package";
+ public static final String ENGINE_PATH = "/api/correlation-engine/v1/rule";
+ public static final int RESPONSE_STATUS_OK = 200;
}
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 5096db3..224f861 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 @@ -71,7 +71,7 @@ public class CorrelationRuleQueryDao { correlationRule.setDescription((String) value.get("description"));
correlationRule.setEnabled((Integer) value.get("enable"));
correlationRule.setTemplateID((Integer) value.get("templateID"));
- correlationRule.setEngineId((String) value.get("engineID"));
+ correlationRule.setEngineID((String) value.get("engineID"));
correlationRule.setEngineType((String) value.get("engineType"));
correlationRule.setCreator((String) value.get("creator"));
correlationRule.setCreateTime((Date) value.get("createTime"));
@@ -80,7 +80,6 @@ public class CorrelationRuleQueryDao { correlationRule.setParams((Properties) value.get("params"));
correlationRule.setDomain((String) value.get("domain"));
correlationRule.setContent((String) value.get("content"));
- correlationRule.setIsManual((Integer) value.get("isManual"));
correlationRule.setVendor((String) value.get("vendor"));
correlationRule.setPackageName((String) value.get("package"));
return correlationRule;
@@ -95,21 +94,25 @@ public class CorrelationRuleQueryDao { for (Field field : fields) {
// Jacoco will cause an exception when calculating the coverage of the UT
// Remove this if jacoco solves this problem in the future
- if (field.getName().contains("jacoco")){
+ if (field.getName().contains("jacoco")) {
continue;
}
-
PropertyDescriptor pd = new PropertyDescriptor(field.getName(),
clazz);
Method getMethod = pd.getReadMethod();
Object o = getMethod.invoke(ruleQueryCondition);
if (o != null) {
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 ";
+ if ("enabled".equals(tempName)) {
+ int enabled = (int) o;
+ if (enabled != RuleMgtConstant.STATUS_RULE_ALL) {
+ whereSql = whereSql + "enable =" + enabled;
+ whereSql += " AND ";
+ }
+ } else if ("name".equals(tempName)) {
+ if (!"".equals(o.toString().trim())) {
+ whereSql = whereSql + field.getName() + " like '%" + o + "%' AND ";
+ }
} else if (!"".equals(o.toString().trim())) {
whereSql = whereSql + field.getName() + "='" + o + "' AND ";
}
@@ -124,17 +127,4 @@ public class CorrelationRuleQueryDao { throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED, e);
}
}
-
- private String getMethodName(Field field){
- String ret = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);;
- Class clazz = field.getDeclaringClass();
-
- if (clazz.equals(Boolean.class)){
- ret = "is" + ret;
- }else{
- ret = "get" + ret;
- }
-
- return ret;
- }
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java index d318de9..a8b114d 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java @@ -32,7 +32,7 @@ public class CorrelationRuleMapper implements ResultSetMapper<CorrelationRule> { correlationRule.setDescription(resultSet.getString("description"));
correlationRule.setEnabled(resultSet.getInt("enable"));
correlationRule.setTemplateID(resultSet.getInt("templateID"));
- correlationRule.setEngineId(resultSet.getString("engineID"));
+ correlationRule.setEngineID(resultSet.getString("engineID"));
correlationRule.setEngineType(resultSet.getString("engineType"));
correlationRule.setCreator(resultSet.getString("creator"));
correlationRule.setCreateTime(resultSet.getDate("createTime"));
@@ -41,7 +41,6 @@ public class CorrelationRuleMapper implements ResultSetMapper<CorrelationRule> { correlationRule.setParams((Properties)resultSet.getObject("params"));
correlationRule.setDomain(resultSet.getString("domain"));
correlationRule.setContent(resultSet.getString("content"));
- correlationRule.setIsManual(resultSet.getInt("isManual"));
correlationRule.setVendor(resultSet.getString("vendor"));
correlationRule.setPackageName(resultSet.getString("package"));
return correlationRule;
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 c2d95f9..682faf0 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 @@ -34,6 +34,7 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import lombok.extern.slf4j.Slf4j;
+import net.sf.json.JSONObject;
import org.jvnet.hk2.annotations.Service;
import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.common.utils.ExceptionUtil;
@@ -47,10 +48,11 @@ import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition; import org.openo.holmes.rulemgt.bean.request.RuleUpdateRequest;
import org.openo.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
import org.openo.holmes.rulemgt.bean.response.RuleQueryListResponse;
+import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
import org.openo.holmes.rulemgt.wrapper.RuleMgtWrapper;
-@SwaggerDefinition
@Service
+@SwaggerDefinition
@Path("/rule")
@Api(tags = {"CorrelationRules"})
@Produces(MediaType.APPLICATION_JSON)
@@ -89,11 +91,11 @@ public class RuleMgtResources { Locale locale = LanguageUtil.getLocale(request);
RuleAddAndUpdateResponse ruleChangeResponse;
try {
- ruleChangeResponse = ruleMgtWrapper
- .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);
+ ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);
+ log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");
return ruleChangeResponse;
} catch (CorrelationException e) {
- log.error("create rule:" + ruleUpdateRequest.getContent() + " failed", e);
+ log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e);
throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
e.getMessage()));
}
@@ -132,6 +134,7 @@ public class RuleMgtResources { try {
ruleQueryListResponse = ruleMgtWrapper
.getCorrelationRuleByCondition(ruleQueryCondition);
+ log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition));
return ruleQueryListResponse;
} catch (CorrelationException e) {
log.error("query rule failed,cause query condition conversion failure", e);
@@ -147,9 +150,9 @@ public class RuleMgtResources { RuleQueryCondition ruleQueryCondition = JacksonUtil
.jsonToBean(queryRequest, RuleQueryCondition.class);
if (queryRequest == null) {
- ruleQueryCondition.setEnabled(2);
+ ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);
} else if (queryRequest.indexOf("enabled") == -1) {
- ruleQueryCondition.setEnabled(2);
+ ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);
}
return ruleQueryCondition;
} catch (IOException e) {
@@ -158,5 +161,4 @@ public class RuleMgtResources { 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 b1ed44e..afd80b1 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 @@ -18,6 +18,7 @@ package org.openo.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;
@@ -53,168 +54,143 @@ public class RuleMgtWrapper { @Inject
private DbDaoUtil daoUtil;
- public RuleAddAndUpdateResponse addCorrelationRule(String creator,
- RuleCreateRequest ruleCreateRequest)
+ private CorrelationRuleDao correlationRuleDao;
+
+ @PostConstruct
+ public void initDaoUtil() {
+ correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);
+ }
+
+ public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest)
throws CorrelationException {
- CorrelationRule correlationRule = convertRuleCreateRequest2CorrelationRule(creator,
- ruleCreateRequest);
- if (correlationRule.getName() == null || "".equals(correlationRule.getName().trim())) {
- 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);
+ if (ruleCreateRequest == null) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
+ CorrelationRule correlationRule = convertCreateRequest2Rule(creator,
+ ruleCreateRequest);
+ checkCorrelation(correlationRule);
+ CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName());
if (ruleTemp != null) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);
}
correlationRule.setPackageName(deployRule2Engine(correlationRule));
- try {
- correlationRule = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .saveRule(correlationRule);
- } catch (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_RULE_FAILED, e);
- }
+ CorrelationRule result = correlationRuleDao.saveRule(correlationRule);
RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();
- ruleAddAndUpdateResponse.setRuleId(correlationRule.getRid());
+ ruleAddAndUpdateResponse.setRuleId(result.getRid());
return ruleAddAndUpdateResponse;
}
- public RuleAddAndUpdateResponse updateCorrelationRule(String modifier,
- RuleUpdateRequest ruleUpdateRequest)
+ public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest)
throws CorrelationException {
- if (ruleUpdateRequest != null) {
- 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 CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
- }
- CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,
- ruleUpdateRequest);
- checkCorrelation(newCorrelationRule, oldCorrelationRule);
- RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();
- try {
- if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
- engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName());
- }
- daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .updateRule(newCorrelationRule);
- } catch (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_UPDATE_RULE_FAILED, e);
- }
- ruleChangeResponse.setRuleId(newCorrelationRule.getRid());
- deployRule2Engine(newCorrelationRule);
- return ruleChangeResponse;
- } else {
+ if (ruleUpdateRequest == null) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
-
+ CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId());
+ if (oldCorrelationRule == null) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
+ }
+ CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,
+ ruleUpdateRequest, oldCorrelationRule.getName());
+ checkCorrelation(newCorrelationRule);
+ RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();
+ ruleChangeResponse.setRuleId(newCorrelationRule.getRid());
+ if (!haveChange(newCorrelationRule, oldCorrelationRule)) {
+ return ruleChangeResponse;
+ }
+ if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
+ engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName());
+ }
+ correlationRuleDao.updateRule(newCorrelationRule);
+ deployRule2Engine(newCorrelationRule);
+ return ruleChangeResponse;
}
- public void checkCorrelation(CorrelationRule newCorrelationRule,
- CorrelationRule oldCorrelationRule) throws CorrelationException {
- int newEnabled = newCorrelationRule.getEnabled();
- if (newCorrelationRule.getContent() == null) {
- newCorrelationRule.setContent(oldCorrelationRule.getContent());
+ private void checkCorrelation(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(I18nProxy.RULE_MANAGEMENT_CONTENT_CANNOT_BE_EMPTY);
}
- if (newEnabled != RuleMgtConstant.STATUS_RULE_CLOSE
- && newEnabled != RuleMgtConstant.STATUS_RULE_OPEN) {
+ if (enabled != RuleMgtConstant.STATUS_RULE_CLOSE
+ && enabled != RuleMgtConstant.STATUS_RULE_OPEN) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
}
+ if ("".equals(ruleName)) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NAME_CANNOT_BE_EMPTY);
+ }
+ }
+
+ private boolean haveChange(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();
+ if (newContent.equals(oldContent) && newEnabled == oldEnabled && newDes.equals(oldDes)) {
+ return false;
+ }
+ return true;
}
public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)
throws CorrelationException {
- if (ruleDeleteRequest != null) {
- CorrelationRule correlationRule;
- try {
- correlationRule = daoUtil
- .getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .getRuleByRid(ruleDeleteRequest.getRuleId());
- } catch (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR, e);
- }
- if (correlationRule == null) {
- log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId()
- + " 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 (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED, e);
- }
- } else {
+ if (ruleDeleteRequest == null) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_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(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
+ }
+ if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
+ engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName());
+ }
+ correlationRuleDao.deleteRule(correlationRule);
}
- public CorrelationRule convertRuleCreateRequest2CorrelationRule(String userName,
+ private CorrelationRule convertCreateRequest2Rule(String userName,
RuleCreateRequest ruleCreateRequest) throws CorrelationException {
- if (ruleCreateRequest != null) {
- if (ruleCreateRequest.getEnabled() != RuleMgtConstant.STATUS_RULE_OPEN
- && 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();
- correlationRule.setRid(ruleId);
- correlationRule.setContent(ruleCreateRequest.getContent());
- correlationRule.setDescription(ruleCreateRequest.getDescription());
- correlationRule.setCreateTime(new Date());
- correlationRule.setUpdateTime(new Date());
- correlationRule.setName(ruleCreateRequest.getRuleName());
- correlationRule.setEngineId("correlation-d");
- correlationRule.setEngineType("");
- correlationRule.setIsManual(0);
- correlationRule.setTemplateID(0);
- correlationRule.setVendor("");
- correlationRule.setCreator(userName);
- correlationRule.setModifier(userName);
- correlationRule.setEnabled(ruleCreateRequest.getEnabled());
- return correlationRule;
- } else {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ String tempContent = ruleCreateRequest.getContent();
+ CorrelationRule correlationRule = new CorrelationRule();
+ String ruleId = "rule_" + System.currentTimeMillis();
+ correlationRule.setRid(ruleId);
+ if (tempContent != null) {
+ correlationRule.setContent(tempContent.trim());
}
-
+ correlationRule.setDescription(ruleCreateRequest.getDescription());
+ 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());
+ return correlationRule;
}
private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,
- RuleUpdateRequest ruleUpdateRequest) throws CorrelationException {
- if (ruleUpdateRequest != null) {
- 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(modifier);
- return correlationRule;
- } else {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
- }
-
+ RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException {
+ 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(modifier);
+ correlationRule.setName(ruleName);
+ return correlationRule;
}
- private String deployRule2Engine(CorrelationRule correlationRule) throws CorrelationException {
- if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule))) {
- if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
- return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule));
- }
+ private String deployRule2Engine(CorrelationRule correlationRule)
+ throws CorrelationException {
+ if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule)) && (
+ correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) {
+ return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule));
}
return "";
}
@@ -253,7 +229,7 @@ public class RuleMgtWrapper { CorrelationRule correlationRule) {
CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();
correlationDeployRule4Engine.setContent(correlationRule.getContent());
- correlationDeployRule4Engine.setEngineId(correlationRule.getEngineId());
+ correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID());
return correlationDeployRule4Engine;
}
diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java index 996f80e..e451410 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java @@ -22,34 +22,45 @@ import static org.junit.Assert.assertThat; import io.dropwizard.db.DataSourceFactory;
import org.junit.Before;
+import org.junit.Test;
public class RuleAppConfigTest {
private RuleAppConfig ruleAppConfig;
@Before
- public void setUp(){
+ public void setUp() {
ruleAppConfig = new RuleAppConfig();
}
+ @Test
public void getDataSourceFactory() throws Exception {
assertThat(ruleAppConfig.getDataSourceFactory(), notNullValue());
}
+ @Test
public void setDataSourceFactory() throws Exception {
final DataSourceFactory factory = new DataSourceFactory();
ruleAppConfig.setDataSourceFactory(factory);
assertThat(ruleAppConfig.getDataSourceFactory(), equalTo(factory));
}
+ @Test
public void getApidescription() throws Exception {
assertThat(ruleAppConfig.getApidescription(), equalTo("Holmes rule management rest API"));
}
+ @Test
public void setApidescription() throws Exception {
final String value = "desc";
ruleAppConfig.setApidescription(value);
assertThat(ruleAppConfig.getApidescription(), equalTo(value));
}
+ @Test
+ public void getterAndSetter4MsbServerAddr() throws Exception {
+ final String value = "msbServerAddr";
+ ruleAppConfig.setMsbServerAddr(value);
+ assertThat(ruleAppConfig.getMsbServerAddr(), equalTo(value));
+ }
}
\ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java new file mode 100644 index 0000000..5f3c5c1 --- /dev/null +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java @@ -0,0 +1,34 @@ +/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.openo.holmes.rulemgt.bean.request;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class CorrelationRestRequestTest {
+
+ @Test
+ public void getterAndSetter4RootURL() {
+ final String rootURL = "rootURL";
+ CorrelationRestRequest correlationRestRequest = new CorrelationRestRequest();
+ correlationRestRequest.setRootURL(rootURL);
+ assertThat(correlationRestRequest.getRootURL(), equalTo(rootURL));
+ }
+}
\ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java new file mode 100644 index 0000000..d911f9c --- /dev/null +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java @@ -0,0 +1,64 @@ +/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.openo.holmes.rulemgt.bolt.enginebolt;
+
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.openo.holmes.rulemgt.RuleAppConfig;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.modules.junit4.rule.PowerMockRule;
+import org.powermock.reflect.Whitebox;
+
+public class EngineServiceTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+ @Rule
+ public PowerMockRule powerMockRule = new PowerMockRule();
+ private EngineService engineService;
+ private HttpEntity httpEntityMock;
+ private HttpResponse httpResponseMock;
+ private HttpClient httpClient;
+ private RuleAppConfig ruleAppConfig = new RuleAppConfig();
+
+ @Before
+ public void setUp() {
+ engineService = new EngineService();
+ httpEntityMock = PowerMock.createMock(HttpEntity.class);
+ httpResponseMock = PowerMock.createMock(HttpResponse.class);
+ httpClient = PowerMock.createMock(HttpClient.class);
+ Whitebox.setInternalState(engineService, "ruleAppConfig", ruleAppConfig);
+ }
+
+ @Test
+ public void getResponseContent_http_entity_is_null() throws Exception {
+ EasyMock.expect(httpResponseMock.getEntity()).andReturn(null);
+ PowerMock.replayAll();
+
+ engineService.getResponseContent(httpResponseMock);
+
+ PowerMock.verifyAll();
+ }
+}
\ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java index b729def..24839e8 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java @@ -16,37 +16,196 @@ package org.openo.holmes.rulemgt.bolt.enginebolt;
-import static org.hamcrest.core.Is.is;
-import static org.hamcrest.core.IsEqual.equalTo;
-import static org.junit.Assert.*;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.easymock.EasyMock;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.openo.holmes.common.exception.CorrelationException;
+import org.openo.holmes.common.utils.I18nProxy;
import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.reflect.Whitebox;
public class EngineWrapperTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
private EngineWrapper engineWrapper = new EngineWrapper();
+ private EngineService engineServiceMock;
+ private HttpResponse httpResponseMock;
+ private StatusLine statusLineMock;
@Before
public void setUp() throws Exception {
+ engineServiceMock = PowerMock.createMock(EngineService.class);
+ httpResponseMock = PowerMock.createMock(HttpResponse.class);
+ statusLineMock = PowerMock.createMock(StatusLine.class);
+ Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock);
+ }
+
+ @Test
+ public void deployEngine_invoke_rule_deploy_exception() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED);
+
+ EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow(
+ new RuntimeException(""));
+ PowerMock.replayAll();
+
+ engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void deployEngine_http_status_not_ok() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.ENGINE_DEPLOY_RULE_FAILED);
+
+ EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
+ .andReturn(httpResponseMock);
+ EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
+
+ PowerMock.replayAll();
+
+ engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void deployEngine_parse_content_exception() throws Exception {
+ String content = "";
+
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR);
+ EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
+ .andReturn(httpResponseMock);
+ EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
+ EasyMock.expect(engineServiceMock.getResponseContent(EasyMock.anyObject(HttpResponse.class)))
+ .andReturn(content);
+
+ PowerMock.replayAll();
+
+ engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void deployEngine_success() throws Exception {
+ String content = "{\"package\":\"test\"}";
+ EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
+ .andReturn(httpResponseMock);
+ EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
+ EasyMock.expect(engineServiceMock.getResponseContent(EasyMock.anyObject(HttpResponse.class)))
+ .andReturn(content);
+ PowerMock.replayAll();
+
+ String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
+
+ assertThat(result, equalTo("test"));
+
+ }
+
+ @Test
+ public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED);
+
+ EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class))).andThrow(
+ new RuntimeException(""));
+ PowerMock.replayAll();
+ engineWrapper.deleteRuleFromEngine("");
+
+ PowerMock.verifyAll();
}
@Test
- public void deployEngine() throws Exception {
- assertThat(engineWrapper.deployEngine(new CorrelationDeployRule4Engine()), equalTo(""));
+ public void deleteRuleFromEngine_http_status_not_ok() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.ENGINE_DELETE_RULE_FAILED);
+
+ EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
+ .andReturn(httpResponseMock);
+ EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
+
+ PowerMock.replayAll();
+
+ engineWrapper.deleteRuleFromEngine("");
+
+ PowerMock.verifyAll();
}
@Test
- public void deleteRuleFromEngine() throws Exception {
- assertThat(engineWrapper.deleteRuleFromEngine(""), is(true));
+ public void deleteRuleFromEngine_success() throws Exception {
+ EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
+ .andReturn(httpResponseMock);
+ EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
+
+ PowerMock.replayAll();
+
+ boolean result = engineWrapper.deleteRuleFromEngine("");
+
+ assertThat(result, equalTo(true));
}
@Test
- public void checkRuleFromEngine() throws Exception {
- assertThat(engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine()), is(true));
+ public void checkRuleFromEngine_invoke_rule_delete_exception() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED);
+
+ EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow(
+ new RuntimeException(""));
+ PowerMock.replayAll();
+
+ engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
+
+ PowerMock.verifyAll();
}
+ @Test
+ public void checkRuleFromEngine_http_status_not_ok() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CHECK_NO_PASS);
+
+ EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
+ .andReturn(httpResponseMock);
+ EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
+
+ PowerMock.replayAll();
+
+ engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void checkRuleFromEngine_success() throws Exception {
+ EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
+ .andReturn(httpResponseMock);
+ EasyMock.expect(httpResponseMock.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
+
+ PowerMock.replayAll();
+
+ boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
+
+ assertThat(result, equalTo(true));
+ }
}
\ No newline at end of file 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 0b1d151..8df2074 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 @@ -30,26 +30,29 @@ import org.junit.Before; import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
import org.openo.holmes.common.api.entity.CorrelationRule;
import org.openo.holmes.common.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.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
+import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.reflect.Whitebox;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.Query;
+
public class CorrelationRuleQueryDaoTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
- @Rule
+ @Rule
public PowerMockRule powerMockRule = new PowerMockRule();
-
private DbDaoUtil dbDaoUtil;
private Handle handle;
@@ -73,7 +76,6 @@ public class CorrelationRuleQueryDaoTest { }
-
@Test
public void getCorrelationRulesByCondition_db_exception() throws Exception {
@@ -109,6 +111,24 @@ public class CorrelationRuleQueryDaoTest { PowerMock.verifyAll();
}
+ @Test
+ public void getCorrelationRulesByCondition_get_where_sql_exception() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED);
+
+ EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
+ EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
+ EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();
+ dbDaoUtil.close(handle);
+ EasyMock.expectLastCall();
+
+ PowerMock.replayAll();
+
+ correlationRuleQueryDao.getCorrelationRulesByCondition(null);
+
+ PowerMock.verifyAll();
+ }
+
private List<Map<String, Object>> createQueryResult() {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> value = new HashMap<>();
diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java index b69a1f7..2a6b5f4 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java @@ -44,7 +44,6 @@ public class CorrelationRuleMapperTest { expect(resultSet.getObject("params")).andReturn(new Properties());
expect(resultSet.getString("domain")).andReturn("");
expect(resultSet.getString("content")).andReturn("");
- expect(resultSet.getInt("isManual")).andReturn(0);
expect(resultSet.getString("vendor")).andReturn("");
expect(resultSet.getString("package")).andReturn("");
PowerMock.replay(resultSet);
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 e25c5b3..16a547b 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 @@ -19,6 +19,7 @@ package org.openo.holmes.rulemgt.resources; import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException;
import org.easymock.EasyMock;
+import org.eclipse.jetty.server.Request;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -45,6 +46,8 @@ public class RuleMgtResourcesTest { private RuleMgtResources ruleMgtResources = new RuleMgtResources();
+ private Request requestMock = PowerMock.createMock(Request.class);
+
@Before
public void setUp() throws Exception {
Whitebox.setInternalState(ruleMgtResources, "ruleMgtWrapper", ruleMgtWrapper);
@@ -67,6 +70,7 @@ public class RuleMgtResourcesTest { @Test
public void addCorrelationRule_normal() throws Exception {
+ StringBuilder stringBuilder = new StringBuilder("http://localhost");
final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin",
ruleCreateRequest)).andReturn(new RuleAddAndUpdateResponse());
@@ -142,6 +146,19 @@ public class RuleMgtResourcesTest { }
@Test
+ public void getCorrelationRules_param_translate_exception() {
+ thrown.expect(WebApplicationException.class);
+
+ String queryRequest = "this is error param";
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2);
+
+ PowerMock.replayAll();
+ ruleMgtResources.getCorrelationRules(request, queryRequest);
+ PowerMock.verifyAll();
+
+ }
+
+ @Test
public void getCorrelationRules_normal_request_string_null() throws Exception {
EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
.andReturn(new RuleQueryListResponse());
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 db13d17..f6d0544 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 @@ -45,8 +45,6 @@ import org.openo.holmes.rulemgt.bolt.enginebolt.EngineWrapper; import org.openo.holmes.rulemgt.db.CorrelationRuleDao;
import org.openo.holmes.rulemgt.db.CorrelationRuleQueryDao;
import org.powermock.api.easymock.PowerMock;
-import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
-import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.reflect.Whitebox;
@@ -54,7 +52,6 @@ public class RuleMgtWrapperTest { @Rule
public ExpectedException thrown = ExpectedException.none();
-
@Rule
public PowerMockRule powerMockRule = new PowerMockRule();
@@ -83,144 +80,73 @@ public class RuleMgtWrapperTest { Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock);
Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock);
Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock);
+ Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleDao", correlationRuleDaoMock);
PowerMock.resetAll();
}
@Test
- public void addCorrelationRule_name_is_null() throws Exception {
- 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.",
- "Mocked contents.", 0));
+ public void initDaoUtil_normal() {
+ ruleMgtWrapper.initDaoUtil();
}
@Test
- public void addCorrelationRule_name_is_empty() throws Exception {
+ public void addCorrelationRule_name_is_null() throws Exception {
thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NAME_IS_EMPTY);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NAME_CANNOT_BE_EMPTY);
- ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.",
+ ruleMgtWrapper.addCorrelationRule(USER_NAME, createRuleCreateRequest(null, "This is a rule for testing.",
"Mocked contents.", 0));
}
@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);
-
+ public void addCorrelationRule_request_null() throws Exception {
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);
+ thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY));
- PowerMock.verifyAll();
+ ruleMgtWrapper.addCorrelationRule(USER_NAME, null);
}
@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);
-
+ public void addCorrelationRule_name_is_empty() throws Exception {
thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock);
- EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(correlationRule);
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NAME_CANNOT_BE_EMPTY);
- PowerMock.verifyAll();
+ ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.",
+ "Mocked contents.", 0));
}
@Test
- public void addCorrelationRule_rule_deploy_verify_failure() throws Exception {
-
- final String ruleName = "Rule-001";
-
- RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
- "Mocked contents.", 0);
-
+ public void addCorrelationRule_content_is_empty() throws Exception {
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 CorrelationException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED));
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CONTENT_CANNOT_BE_EMPTY);
- PowerMock.replayAll();
-
- ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
-
- PowerMock.verifyAll();
+ ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.",
+ "", 0));
}
@Test
- public void addCorrelationRule_rule_deploy_rule_enabled_failure() throws Exception {
-
- final String ruleName = "Rule-001";
-
- RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
- "Mocked contents.", 1);
-
+ public void addCorrelationRule_enabled_is_off_limit() throws Exception {
thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_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)))
- .andReturn(true);
- EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED));
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
- PowerMock.verifyAll();
+ ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("test", "This is a rule for testing.",
+ "Mocked contents.", 3));
}
@Test
- public void addCorrelationRule_rule_save_failure() throws Exception {
+ public void addCorrelationRule_duplicated_rule() throws Exception {
+
final String ruleName = "Rule-001";
RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
- "Mocked contents.", 1);
+ "Mocked contents.", 0);
+ CorrelationRule correlationRule = convertCreateRequest2CorrelationRule(ruleCreateRequest);
thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CREATE_RULE_FAILED);
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).times(2);
- EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null);
- EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
- .andReturn(true);
- EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andReturn("package-001");
- EasyMock.expect(correlationRuleDaoMock.saveRule(EasyMock.anyObject(CorrelationRule.class)))
- .andThrow(new RuntimeException("any message"));
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);
+ EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(correlationRule);
PowerMock.replayAll();
ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
@@ -238,8 +164,6 @@ public class RuleMgtWrapperTest { CorrelationRule correlationRuleRet = new CorrelationRule();
correlationRuleRet.setRid("rule_" + System.currentTimeMillis());
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).times(2);
EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null);
EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
.andReturn(true);
@@ -265,269 +189,70 @@ public class RuleMgtWrapperTest { }
@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(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock);
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class))).andReturn(null);
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.updateCorrelationRule(USER_NAME, new RuleUpdateRequest());
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void updateCorrelationRule_rule_status_illegal_with_contents() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
-
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", "contents", 3);
- CorrelationRule correlationRule = convertUpdateRequest2CorrelationRule(ruleUpdateRequest);
- correlationRule.setContent("previous contents");
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock);
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class)))
- .andReturn(correlationRule);
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void updateCorrelationRule_rule_status_illegal_with_null_contents() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
-
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", null, 3);
- CorrelationRule correlationRule = convertUpdateRequest2CorrelationRule(ruleUpdateRequest);
- correlationRule.setContent("previous contents");
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock);
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class)))
- .andReturn(correlationRule);
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void updateCorrelationRule_rule_disabled_update_failure() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_UPDATE_RULE_FAILED);
-
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", "contents", 0);
- CorrelationRule correlationRuleOld = convertUpdateRequest2CorrelationRule(ruleUpdateRequest);
- correlationRuleOld.setContent("previous contents");
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class)))
- .andReturn(correlationRuleOld);
- correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));
- EasyMock.expectLastCall().andThrow(new RuntimeException("Failed to update the rule."));
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void updateCorrelationRule_rule_disabled_deploy_verify_exception() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED);
-
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", null, 0);
- CorrelationRule correlationRuleOld = convertUpdateRequest2CorrelationRule(ruleUpdateRequest);
- correlationRuleOld.setContent("previous contents");
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class)))
- .andReturn(correlationRuleOld);
+ 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);
+ RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "des2", "contetnt2", 1);
+
+ EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule);
+ EasyMock.expect(engineWrapperMock.deleteRuleFromEngine("testName")).andReturn(true);
correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));
EasyMock.expectLastCall();
EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
- .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED));
-
+ .andReturn(true);
+ EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
+ .andReturn("packageName1");
PowerMock.replayAll();
ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
PowerMock.verifyAll();
- }
-
- @Test
- public void updateCorrelationRule_rule_disabled_deploy_verify_false() throws Exception {
-
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", null, 0);
- CorrelationRule correlationRuleOld = convertUpdateRequest2CorrelationRule(ruleUpdateRequest);
- correlationRuleOld.setContent("previous contents");
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class)))
- .andReturn(correlationRuleOld);
- correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));
- EasyMock.expectLastCall();
- EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
- .andReturn(false);
-
- PowerMock.replayAll();
-
- RuleAddAndUpdateResponse response = ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
- assertThat(response.getRuleId(), equalTo(ruleUpdateRequest.getRuleId()));
-
- PowerMock.verifyAll();
+ assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId()));
}
@Test
- public void updateCorrelationRule_rule_enabled_engine_delete_failure() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_UPDATE_RULE_FAILED);
-
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", null, 1);
- CorrelationRule correlationRuleOld = convertUpdateRequest2CorrelationRule(ruleUpdateRequest);
- correlationRuleOld.setContent("previous contents");
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class)))
- .andReturn(correlationRuleOld);
- engineWrapperMock.deleteRuleFromEngine(correlationRuleOld.getPackageName());
- EasyMock.expectLastCall().andThrow(new RuntimeException("Failed to delete the rule from the engine."));
+ 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);
+ RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_1", "des1", "content", 1);
+
+ EasyMock.expect(correlationRuleDaoMock.queryRuleByRid("rule_1")).andReturn(oldCorrelationRule);
PowerMock.replayAll();
ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
PowerMock.verifyAll();
- }
-
- @Test
- public void updateCorrelationRule_rule_enabled_engine_deploy_failure() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED);
-
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", null, 1);
- CorrelationRule correlationRuleOld = convertUpdateRequest2CorrelationRule(ruleUpdateRequest);
- correlationRuleOld.setContent("previous contents");
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class)))
- .andReturn(correlationRuleOld);
- EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(correlationRuleOld.getPackageName())).andReturn(true);
- correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));
- EasyMock.expectLastCall();
- EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
- .andReturn(true);
- EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andThrow(new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED));
-
- PowerMock.replayAll();
- ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
-
- PowerMock.verifyAll();
+ assertThat(oldCorrelationRule.getRid(), equalTo(ruleUpdateRequest.getRuleId()));
}
@Test
- public void updateCorrelationRule_rule_enabled_deploy_rule_enabled() throws Exception {
-
- RuleUpdateRequest ruleUpdateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", null, 1);
- CorrelationRule correlationRuleOld = convertUpdateRequest2CorrelationRule(ruleUpdateRequest);
- correlationRuleOld.setContent("previous contents");
+ public void updateCorrelationRule_rule_not_exist() throws Exception {
+ thrown.expect(CorrelationException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(EasyMock.anyObject(String.class)))
- .andReturn(correlationRuleOld);
- EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(correlationRuleOld.getPackageName())).andReturn(true);
- correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));
- EasyMock.expectLastCall();
- EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
- .andReturn(true);
- EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andReturn("package-name");
+ EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(EasyMock.anyObject(String.class))).andReturn(null);
PowerMock.replayAll();
- RuleAddAndUpdateResponse response = ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
-
- assertThat(response.getRuleId(), equalTo(ruleUpdateRequest.getRuleId()));
+ ruleMgtWrapper.updateCorrelationRule(USER_NAME, new RuleUpdateRequest());
PowerMock.verifyAll();
}
@Test
- public void checkCorrelation_content_null() throws Exception {
- RuleUpdateRequest ruleCreateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", null, 0);
- CorrelationRule correlationRuleOld = convertUpdateRequest2CorrelationRule(ruleCreateRequest);
- CorrelationRule correlationRuleNew = convertUpdateRequest2CorrelationRule(ruleCreateRequest);
- correlationRuleOld.setContent("previous contents");
-
- ruleMgtWrapper.checkCorrelation(correlationRuleNew, correlationRuleOld);
-
- assertThat(correlationRuleNew.getContent(), equalTo(correlationRuleOld.getContent()));
- }
-
- @Test
- public void checkCorrelation_illegal_status() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
-
- RuleUpdateRequest ruleCreateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", null, 2);
- CorrelationRule correlationRuleOld = convertUpdateRequest2CorrelationRule(ruleCreateRequest);
- CorrelationRule correlationRuleNew = convertUpdateRequest2CorrelationRule(ruleCreateRequest);
- correlationRuleOld.setContent("previous contents");
-
- ruleMgtWrapper.checkCorrelation(correlationRuleNew, correlationRuleOld);
- }
-
-
- @Test
public void deleteCorrelationRule_request_null() throws Exception {
thrown.expect(CorrelationException.class);
thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY));
@@ -536,25 +261,6 @@ public class RuleMgtWrapperTest { }
@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(CorrelationException.class);
thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE));
@@ -563,7 +269,7 @@ public class RuleMgtWrapperTest { EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(ruleDeleteRequest.getRuleId()))
+ EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId()))
.andReturn(null);
PowerMock.replayAll();
@@ -574,68 +280,15 @@ public class RuleMgtWrapperTest { }
@Test
- public void deleteCorrelationRule_rule_enabled_engine_delete_exception() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED));
-
- RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
- RuleUpdateRequest ruleCreateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", "contents", 1);
- CorrelationRule correlationRule = convertUpdateRequest2CorrelationRule(ruleCreateRequest);
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(ruleDeleteRequest.getRuleId()))
- .andReturn(correlationRule);
- EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(correlationRule.getPackageName()))
- .andThrow(new RuntimeException("Failed to delete the rule from the engine"));
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
-
- PowerMock.verifyAll();
- }
-
- @Test
- public void deleteCorrelationRule_rule_disabled_delete_exception() throws Exception {
- thrown.expect(CorrelationException.class);
- thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED));
-
- RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
- RuleUpdateRequest ruleCreateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", "contents", 0);
- CorrelationRule correlationRule = convertUpdateRequest2CorrelationRule(ruleCreateRequest);
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(ruleDeleteRequest.getRuleId()))
- .andReturn(correlationRule);
- correlationRuleDaoMock.deleteRule(correlationRule);
- EasyMock.expectLastCall().andThrow(new RuntimeException("Failed to delete the rule from the DB"));
-
- PowerMock.replayAll();
-
- ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
-
- PowerMock.verifyAll();
- }
-
- @Test
public void deleteCorrelationRule_normal() throws Exception {
RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
- RuleUpdateRequest ruleCreateRequest = createRuleUpdateRequest("rule_" + System.currentTimeMillis(),
- "desc", "contents", 1);
- CorrelationRule correlationRule = convertUpdateRequest2CorrelationRule(ruleCreateRequest);
-
- EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
- correlationRuleDaoMock).anyTimes();
- EasyMock.expect(correlationRuleDaoMock.getRuleByRid(ruleDeleteRequest.getRuleId()))
+ CorrelationRule correlationRule = new CorrelationRule();
+ correlationRule.setEnabled(1);
+ EasyMock.expect(correlationRuleDaoMock.queryRuleByRid(ruleDeleteRequest.getRuleId()))
.andReturn(correlationRule);
- EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(correlationRule.getPackageName())).andReturn(true);
- correlationRuleDaoMock.deleteRule(correlationRule);
+ EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(EasyMock.anyObject(String.class))).andReturn(true);
+ correlationRuleDaoMock.deleteRule(EasyMock.anyObject(CorrelationRule.class));
EasyMock.expectLastCall();
-
PowerMock.replayAll();
ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
@@ -683,9 +336,8 @@ public class RuleMgtWrapperTest { correlationRule.setContent("content" + i);
correlationRule.setName("name" + i);
correlationRule.setRid("rule_" + i);
- correlationRule.setIsManual(i % 2);
correlationRule.setEngineType("engineType" + (i % 2 + 1));
- correlationRule.setEngineId("engineId" + i);
+ correlationRule.setEngineID("engineId" + i);
correlationRule.setCreateTime(new Date());
correlationRule.setCreator(USER_NAME);
correlationRule.setDescription("description" + i);
|