diff options
Diffstat (limited to 'rulemgt/src')
5 files changed, 1246 insertions, 0 deletions
diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java new file mode 100644 index 0000000..69910c6 --- /dev/null +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java @@ -0,0 +1,25 @@ +/** + * 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; + +public class RuleActiveAppTest { + + public static void main(String[] args) throws Exception { + String filePath = "E:\\code\\OES_Analytics_FM_Relation\\correlation-mgt\\rulemgt-standalone\\src\\assembly\\resource\\conf\\correlation-rule.yml"; + new RuleActiveApp().run(new String[]{"server", filePath}); + } +}
\ 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 new file mode 100644 index 0000000..29e7f2b --- /dev/null +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDaoTest.java @@ -0,0 +1,147 @@ +/**
+ * 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.db;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.openo.holmes.common.api.entity.CorrelationRule;
+import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.utils.DbDaoUtil;
+import org.openo.holmes.common.utils.I18nProxy;
+import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+import org.skife.jdbi.v2.Handle;
+import org.skife.jdbi.v2.Query;
+
+
+@RunWith(PowerMockRunner.class)
+@SuppressStaticInitializationFor("org.openo.holmes.common.utils.DbDaoUtil")
+public class CorrelationRuleQueryDaoTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private DbDaoUtil dbDaoUtil;
+
+ private Handle handle;
+
+ private Query query;
+
+ private CorrelationRuleQueryDao correlationRuleQueryDao;
+ private RuleQueryCondition ruleQueryCondition;
+
+ @Before
+ public void setUp() throws Exception {
+ correlationRuleQueryDao = new CorrelationRuleQueryDao();
+
+ dbDaoUtil = PowerMock.createMock(DbDaoUtil.class);
+ handle = PowerMock.createMock(Handle.class);
+ query = PowerMock.createMock(Query.class);
+
+ Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil);
+
+ ruleQueryCondition = createRuleQueryCondition();
+ }
+
+
+
+ @Test
+ public void getCorrelationRulesByCondition_db_exception() throws Exception {
+
+ thrown.expect(DbException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED);
+
+ EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
+ EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
+ EasyMock.expect(query.list()).andThrow(new RuntimeException()).anyTimes();
+ dbDaoUtil.close(handle);
+ EasyMock.expectLastCall();
+
+ PowerMock.replayAll();
+
+ correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void getCorrelationRulesByCondition_normal() throws Exception {
+ 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();
+
+ List<CorrelationRule> result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
+ assertThat(result.size(), is(1));
+
+ PowerMock.verifyAll();
+ }
+
+ private List<Map<String, Object>> createQueryResult() {
+ List<Map<String, Object>> list = new ArrayList<>();
+ Map<String, Object> value = new HashMap<>();
+ value.put("name", "Rule-001");
+ value.put("rid", "rule_" + System.currentTimeMillis());
+ value.put("description", "desc");
+ value.put("enable", 0);
+ value.put("templateID", 1);
+ value.put("engineId", "engine-001");
+ value.put("engineType", "engineType-001");
+ value.put("creator", "admin");
+ value.put("createTime", new Date());
+ value.put("updator", "admin");
+ value.put("updateTime", new Date());
+ value.put("params", new Properties());
+ value.put("domain", "Domain");
+ value.put("isManual", 0);
+ value.put("vendor", "Vendor");
+ value.put("content", "Contents");
+ value.put("package", "package");
+ list.add(value);
+ return list;
+ }
+
+ private RuleQueryCondition createRuleQueryCondition() {
+ RuleQueryCondition ruleQueryCondition = new RuleQueryCondition();
+ ruleQueryCondition.setRid("rule_" + System.currentTimeMillis());
+ ruleQueryCondition.setName("Rule-001");
+ ruleQueryCondition.setEnabled(0);
+ ruleQueryCondition.setCreator("admin");
+ ruleQueryCondition.setModifier("admin");
+ return ruleQueryCondition;
+ }
+
+}
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 new file mode 100644 index 0000000..b69a1f7 --- /dev/null +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapperTest.java @@ -0,0 +1,54 @@ +/**
+ * 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.db.mapper;
+
+import static org.easymock.EasyMock.expect;
+
+import java.sql.Date;
+import java.sql.ResultSet;
+import java.util.Properties;
+import org.junit.Test;
+import org.powermock.api.easymock.PowerMock;
+
+public class CorrelationRuleMapperTest {
+
+ @Test
+ public void map() throws Exception {
+ CorrelationRuleMapper mapper = new CorrelationRuleMapper();
+ ResultSet resultSet = PowerMock.createMock(ResultSet.class);
+ expect(resultSet.getString("name")).andReturn("");
+ expect(resultSet.getString("rid")).andReturn("");
+ expect(resultSet.getString("description")).andReturn("");
+ expect(resultSet.getInt("enable")).andReturn(0);
+ expect(resultSet.getInt("templateID")).andReturn(1);
+ expect(resultSet.getString("engineID")).andReturn("");
+ expect(resultSet.getString("engineType")).andReturn("");
+ expect(resultSet.getString("creator")).andReturn("");
+ expect(resultSet.getDate("createTime")).andReturn(new Date(System.currentTimeMillis()));
+ expect(resultSet.getString("updator")).andReturn("");
+ expect(resultSet.getDate("updateTime")).andReturn(new Date(System.currentTimeMillis()));
+ 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);
+ mapper.map(0, resultSet, null);
+ PowerMock.verify(resultSet);
+ }
+}
\ No newline at end of file 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 new file mode 100644 index 0000000..1f6f331 --- /dev/null +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/resources/RuleMgtResourcesTest.java @@ -0,0 +1,318 @@ +/**
+ * 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.resources;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.WebApplicationException;
+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.CallException;
+import org.openo.holmes.common.exception.DataFormatException;
+import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest;
+import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest;
+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.wrapper.RuleMgtWrapper;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.reflect.Whitebox;
+
+public class RuleMgtResourcesTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private HttpServletRequest request = PowerMock.createMock(HttpServletRequest.class);
+
+ private RuleMgtWrapper ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class);
+
+ private RuleMgtResources ruleMgtResources = new RuleMgtResources();
+
+ @Before
+ public void setUp() throws Exception {
+ Whitebox.setInternalState(ruleMgtResources, "ruleMgtWrapper", ruleMgtWrapper);
+ PowerMock.resetAll();
+ }
+
+ @Test
+ public void addCorrelationRule_call_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
+ EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest))
+ .andThrow(new CallException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.addCorrelationRule(request, ruleCreateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void addCorrelationRule_db_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
+ EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest)).andThrow(new DbException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.addCorrelationRule(request, ruleCreateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void addCorrelationRule_data_format_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
+ EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest))
+ .andThrow(new DataFormatException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.addCorrelationRule(request, ruleCreateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void addCorrelationRule_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
+ EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest))
+ .andThrow(new RuntimeException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.addCorrelationRule(request, ruleCreateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void addCorrelationRule_normal() throws Exception {
+ final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
+ EasyMock.expect(ruleMgtWrapper.addCorrelationRule("admin",
+ ruleCreateRequest)).andReturn(new RuleAddAndUpdateResponse());
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.addCorrelationRule(request, ruleCreateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void updateCorrelationRule_call_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
+ EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest))
+ .andThrow(new CallException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void updateCorrelationRule_data_format_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
+ EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest))
+ .andThrow(new DataFormatException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void updateCorrelationRule_db_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
+ EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest))
+ .andThrow(new DbException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void updateCorrelationRule_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
+ EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin", ruleUpdateRequest))
+ .andThrow(new RuntimeException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void updateCorrelationRule_normal() throws Exception {
+ final RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
+ EasyMock.expect(ruleMgtWrapper.updateCorrelationRule("admin",
+ ruleUpdateRequest)).andReturn(new RuleAddAndUpdateResponse());
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ EasyMock.expect(request.getHeader("username")).andReturn("admin");
+ PowerMock.replayAll();
+ ruleMgtResources.updateCorrelationRule(request, ruleUpdateRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void deleteCorrelationRule_call_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
+ ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+ EasyMock.expectLastCall().andThrow(new CallException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ PowerMock.replayAll();
+ ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void deleteCorrelationRule_data_format_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
+ ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+ EasyMock.expectLastCall().andThrow(new DataFormatException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ PowerMock.replayAll();
+ ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void deleteCorrelationRule_db_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
+ ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+ EasyMock.expectLastCall().andThrow(new DbException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ PowerMock.replayAll();
+ ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void deleteCorrelationRule_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
+ ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+ EasyMock.expectLastCall().andThrow(new RuntimeException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ PowerMock.replayAll();
+ ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void deleteCorrelationRule_normal() throws Exception {
+ final RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
+ ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+ EasyMock.expectLastCall();
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US");
+ PowerMock.replayAll();
+ ruleMgtResources.deleteCorrelationRule(request, ruleDeleteRequest);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void getCorrelationRules_data_format_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\","
+ + "\"enabled\":0,\"creator\":\"admin\"}";
+ EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
+ .andThrow(new DataFormatException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2);
+ PowerMock.replayAll();
+ ruleMgtResources.getCorrelationRules(request, requestStr);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void getCorrelationRules_db_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\","
+ + "\"enabled\":0,\"creator\":\"admin\"}";
+ EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
+ .andThrow(new DbException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2);
+ PowerMock.replayAll();
+ ruleMgtResources.getCorrelationRules(request, requestStr);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void getCorrelationRules_exception() throws Exception {
+ thrown.expect(WebApplicationException.class);
+
+ final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\","
+ + "\"enabled\":0,\"creator\":\"admin\"}";
+ EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
+ .andThrow(new RuntimeException(""));
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2);
+ PowerMock.replayAll();
+ ruleMgtResources.getCorrelationRules(request, requestStr);
+ PowerMock.verifyAll();
+ }
+
+
+ @Test
+ public void getCorrelationRules_normal_request_string_null() throws Exception {
+ EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
+ .andReturn(new RuleQueryListResponse());
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2);
+ PowerMock.replayAll();
+ ruleMgtResources.getCorrelationRules(request, null);
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void getCorrelationRules_normal_request_string_enabled_missing() throws Exception {
+ final String requestStr = "{\"ruleid\":\"rule_001\",\"rulename\":\"Rule-001\","
+ + "\"creator\":\"admin\"}";
+ EasyMock.expect(ruleMgtWrapper.getCorrelationRuleByCondition(EasyMock.anyObject(RuleQueryCondition.class)))
+ .andReturn(new RuleQueryListResponse());
+ EasyMock.expect(request.getHeader("language-option")).andReturn("en_US").times(2);
+ PowerMock.replayAll();
+ ruleMgtResources.getCorrelationRules(request, requestStr);
+ PowerMock.verifyAll();
+ }
+}
\ No newline at end of file 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 new file mode 100644 index 0000000..41cb494 --- /dev/null +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapperTest.java @@ -0,0 +1,702 @@ +/**
+ * 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.wrapper;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.openo.holmes.common.api.entity.CorrelationRule;
+import org.openo.holmes.common.exception.CallException;
+import org.openo.holmes.common.exception.DataFormatException;
+import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.utils.DbDaoUtil;
+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.bean.request.RuleCreateRequest;
+import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest;
+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.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.reflect.Whitebox;
+
+@RunWith(PowerMockRunner.class)
+@SuppressStaticInitializationFor("org.openo.holmes.common.utils.DbDaoUtil")
+public class RuleMgtWrapperTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private RuleMgtWrapper ruleMgtWrapper;
+
+ private EngineWrapper engineWrapperMock;
+
+ private DbDaoUtil dbDaoUtilMock;
+
+ private CorrelationRuleQueryDao correlationRuleQueryDaoMock;
+
+ private CorrelationRuleDao correlationRuleDaoMock;
+
+ private static final String USER_NAME = "admin";
+
+ @Before
+ public void setUp() throws Exception {
+
+ ruleMgtWrapper = new RuleMgtWrapper();
+
+ engineWrapperMock = PowerMock.createMock(EngineWrapper.class);
+ correlationRuleQueryDaoMock = PowerMock.createMock(CorrelationRuleQueryDao.class);
+ dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class);
+ correlationRuleDaoMock = PowerMock.createMock(CorrelationRuleDao.class);
+
+ Whitebox.setInternalState(ruleMgtWrapper, "daoUtil", dbDaoUtilMock);
+ Whitebox.setInternalState(ruleMgtWrapper, "correlationRuleQueryDao", correlationRuleQueryDaoMock);
+ Whitebox.setInternalState(ruleMgtWrapper, "engineWarpper", engineWrapperMock);
+
+ PowerMock.resetAll();
+ }
+
+ @Test
+ public void addCorrelationRule_name_is_null() throws Exception {
+ thrown.expect(DbException.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));
+ }
+
+ @Test
+ public void addCorrelationRule_name_is_empty() throws Exception {
+ thrown.expect(DbException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_RULE_NAME_IS_EMPTY);
+
+ ruleMgtWrapper.addCorrelationRule("admin", createRuleCreateRequest("", "This is a rule for testing.",
+ "Mocked contents.", 0));
+ }
+
+ @Test
+ public void addCorrelationRule_duplicated_rule() throws Exception {
+
+ final String ruleName = "Rule-001";
+
+ RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
+ "Mocked contents.", 0);
+ CorrelationRule correlationRule = convertCreateRequest2CorrelationRule(ruleCreateRequest);
+
+ thrown.expect(DbException.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);
+
+ PowerMock.verifyAll();
+ }
+
+ @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);
+
+ thrown.expect(CallException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED);
+
+ EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
+ correlationRuleDaoMock);
+ EasyMock.expect(correlationRuleDaoMock.queryRuleByRuleName(ruleName)).andReturn(null);
+ EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
+ .andThrow(new CallException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED));
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
+
+ PowerMock.verifyAll();
+ }
+
+ @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);
+
+ thrown.expect(CallException.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 CallException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED));
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void addCorrelationRule_rule_save_failure() throws Exception {
+ final String ruleName = "Rule-001";
+
+ RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
+ "Mocked contents.", 1);
+
+ thrown.expect(DbException.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"));
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void addCorrelationRule_normal() throws Exception {
+ final String ruleName = "Rule-001";
+
+ RuleCreateRequest ruleCreateRequest = createRuleCreateRequest(ruleName, "This is a rule for testing.",
+ "Mocked contents.", 1);
+
+ 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);
+ EasyMock.expect(engineWrapperMock.deployEngine(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
+ .andReturn("package-001");
+ EasyMock.expect(correlationRuleDaoMock.saveRule(EasyMock.anyObject(CorrelationRule.class)))
+ .andReturn(correlationRuleRet);
+
+ PowerMock.replayAll();
+
+ RuleAddAndUpdateResponse response = ruleMgtWrapper.addCorrelationRule("admin", ruleCreateRequest);
+ assertThat(response.getRuleId(), equalTo(correlationRuleRet.getRid()));
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void updateCorrelationRule_param_null() throws Exception {
+ thrown.expect(DataFormatException.class);
+ thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+
+ ruleMgtWrapper.updateCorrelationRule(USER_NAME, null);
+ }
+
+ @Test
+ public void updateCorrelationRule_rule_not_exist() throws Exception {
+ thrown.expect(DbException.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(DataFormatException.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(DataFormatException.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(DbException.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(CallException.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);
+ correlationRuleDaoMock.updateRule(EasyMock.anyObject(CorrelationRule.class));
+ EasyMock.expectLastCall();
+ EasyMock.expect(engineWrapperMock.checkRuleFromEngine(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
+ .andThrow(new CallException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED));
+
+ 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();
+ }
+
+ @Test
+ public void updateCorrelationRule_rule_enabled_engine_delete_failure() throws Exception {
+ thrown.expect(DbException.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."));
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void updateCorrelationRule_rule_enabled_engine_deploy_failure() throws Exception {
+ thrown.expect(CallException.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 CallException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED));
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
+
+ PowerMock.verifyAll();
+ }
+
+ @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");
+
+ 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");
+
+ PowerMock.replayAll();
+
+ RuleAddAndUpdateResponse response = ruleMgtWrapper.updateCorrelationRule(USER_NAME, ruleUpdateRequest);
+
+ assertThat(response.getRuleId(), equalTo(ruleUpdateRequest.getRuleId()));
+
+ 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(DataFormatException.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(DataFormatException.class);
+ thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY));
+
+ ruleMgtWrapper.deleteCorrelationRule(null);
+ }
+
+ @Test
+ public void deleteCorrelationRule_rule_not_exit() throws Exception {
+ thrown.expect(DbException.class);
+ thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE));
+
+ RuleDeleteRequest ruleDeleteRequest = createRuleDeleteRequest("rule_" + System.currentTimeMillis());
+
+ EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(CorrelationRuleDao.class)).andReturn(
+ correlationRuleDaoMock).anyTimes();
+ EasyMock.expect(correlationRuleDaoMock.getRuleByRid(ruleDeleteRequest.getRuleId()))
+ .andReturn(null);
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void deleteCorrelationRule_rule_enabled_engine_delete_exception() throws Exception {
+ thrown.expect(DbException.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(DbException.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()))
+ .andReturn(correlationRule);
+ EasyMock.expect(engineWrapperMock.deleteRuleFromEngine(correlationRule.getPackageName())).andReturn(true);
+ correlationRuleDaoMock.deleteRule(correlationRule);
+ EasyMock.expectLastCall();
+
+ PowerMock.replayAll();
+
+ ruleMgtWrapper.deleteCorrelationRule(ruleDeleteRequest);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void getCorrelationRuleByCondition_data_format_exception() throws Exception {
+ thrown.expect(DataFormatException.class);
+ thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED));
+
+ EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
+ RuleQueryCondition.class)))
+ .andThrow(new DataFormatException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED));
+
+ PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+
+ ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());
+
+ PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+ }
+
+ @Test
+ public void getCorrelationRuleByCondition_db_exception() throws Exception {
+ thrown.expect(DbException.class);
+ thrown.expectMessage((I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED));
+
+ EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
+ RuleQueryCondition.class)))
+ .andThrow(new DbException(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED));
+
+ PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+
+ ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());
+
+ PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+ }
+
+ @Test
+ public void getCorrelationRuleByCondition_normal() throws Exception {
+ List<CorrelationRule> correlationRuleList = new ArrayList<CorrelationRule>(10);
+ for (int i = 0; i < 10; ++i) {
+ CorrelationRule correlationRule = new CorrelationRule();
+ correlationRule.setContent("content" + i);
+ correlationRule.setName("name" + i);
+ correlationRule.setRid("rule_" + i);
+ correlationRule.setIsManual(i % 2);
+ correlationRule.setEngineType("engineType" + (i % 2 + 1));
+ correlationRule.setEngineId("engineId" + i);
+ correlationRule.setCreateTime(new Date());
+ correlationRule.setCreator(USER_NAME);
+ correlationRule.setDescription("description" + i);
+ correlationRule.setPackageName("package" + i);
+ correlationRuleList.add(correlationRule);
+ }
+
+ EasyMock.expect(correlationRuleQueryDaoMock.getCorrelationRulesByCondition(EasyMock.anyObject(
+ RuleQueryCondition.class))).andReturn(correlationRuleList);
+
+ PowerMock.replay(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+
+ RuleQueryListResponse response = ruleMgtWrapper.getCorrelationRuleByCondition(new RuleQueryCondition());
+
+ PowerMock.verify(correlationRuleQueryDaoMock, CorrelationRuleQueryDao.class);
+
+ assertThat(response.getTotalCount(), is(10));
+
+ for (int i = 0; i < 10; ++i) {
+ assertThat(response.getCorrelationRules().get(i).getRuleId(),
+ equalTo(correlationRuleList.get(i).getRid()));
+ }
+ }
+
+ private RuleCreateRequest createRuleCreateRequest(String ruleName, String description, String content,
+ int enabled) {
+ RuleCreateRequest rcr;
+ rcr = new RuleCreateRequest();
+ rcr.setRuleName(ruleName);
+ rcr.setDescription(description);
+ rcr.setContent(content);
+ rcr.setEnabled(enabled);
+ return rcr;
+ }
+
+ private RuleUpdateRequest createRuleUpdateRequest(String ruleId, String description, String content, int enabled) {
+ RuleUpdateRequest ruleUpdateRequest = new RuleUpdateRequest();
+ ruleUpdateRequest.setRuleId(ruleId);
+ ruleUpdateRequest.setDescription(description);
+ ruleUpdateRequest.setContent(content);
+ ruleUpdateRequest.setEnabled(enabled);
+ return ruleUpdateRequest;
+ }
+
+ private RuleDeleteRequest createRuleDeleteRequest(String ruleId) {
+ RuleDeleteRequest ruleDeleteRequest = new RuleDeleteRequest();
+ ruleDeleteRequest.setRuleId(ruleId);
+ return ruleDeleteRequest;
+ }
+
+ private CorrelationRule convertCreateRequest2CorrelationRule(RuleCreateRequest ruleCreateRequest) {
+ CorrelationRule correlationRule = new CorrelationRule();
+ correlationRule.setContent(ruleCreateRequest.getContent());
+ correlationRule.setDescription(ruleCreateRequest.getDescription());
+ correlationRule.setName(ruleCreateRequest.getRuleName());
+ correlationRule.setCreator(USER_NAME);
+ correlationRule.setModifier(USER_NAME);
+ correlationRule.setEnabled(ruleCreateRequest.getEnabled());
+ return correlationRule;
+ }
+
+ private CorrelationRule convertUpdateRequest2CorrelationRule(RuleUpdateRequest ruleUpdateRequest) {
+ CorrelationRule correlationRule = new CorrelationRule();
+ correlationRule.setRid(ruleUpdateRequest.getRuleId());
+ correlationRule.setContent(ruleUpdateRequest.getContent());
+ correlationRule.setDescription(ruleUpdateRequest.getDescription());
+ correlationRule.setEnabled(ruleUpdateRequest.getEnabled());
+ correlationRule.setUpdateTime(new Date());
+ correlationRule.setModifier(USER_NAME);
+ return correlationRule;
+ }
+}
\ No newline at end of file |