summaryrefslogtreecommitdiffstats
path: root/rulemgt/src/test/java
diff options
context:
space:
mode:
authoryoubowu <wu.youbo@zte.com.cn>2017-02-27 09:21:05 +0800
committer6092002067 <wu.youbo@zte.com.cn>2017-02-27 10:15:46 +0800
commite87f8243439fb68d1308a5132ae624942d77776d (patch)
tree2ca350893d9f1bb2a69827fa1aa1deac15038801 /rulemgt/src/test/java
parent27e71c9c0f9ec16e377a5f21ec4f72bbd5c1efc0 (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/src/test/java')
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java13
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequestTest.java34
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java64
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java177
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java26
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java1
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java17
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java482
8 files changed, 385 insertions, 429 deletions
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);