2 * Copyright 2017 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.holmes.rulemgt.db;
\r
19 import static org.hamcrest.MatcherAssert.assertThat;
\r
20 import static org.hamcrest.Matchers.is;
\r
22 import java.util.ArrayList;
\r
23 import java.util.Date;
\r
24 import java.util.HashMap;
\r
25 import java.util.List;
\r
26 import java.util.Map;
\r
27 import java.util.Properties;
\r
28 import org.easymock.EasyMock;
\r
29 import org.junit.Before;
\r
30 import org.junit.Rule;
\r
31 import org.junit.Test;
\r
32 import org.junit.rules.ExpectedException;
\r
33 import org.openo.holmes.common.api.entity.CorrelationRule;
\r
34 import org.openo.holmes.common.exception.CorrelationException;
\r
35 import org.openo.holmes.common.utils.DbDaoUtil;
\r
36 import org.openo.holmes.common.utils.I18nProxy;
\r
37 import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;
\r
38 import org.powermock.api.easymock.PowerMock;
\r
39 import org.powermock.modules.junit4.rule.PowerMockRule;
\r
40 import org.powermock.reflect.Whitebox;
\r
41 import org.skife.jdbi.v2.Handle;
\r
42 import org.skife.jdbi.v2.Query;
\r
45 public class CorrelationRuleQueryDaoTest {
\r
48 public ExpectedException thrown = ExpectedException.none();
\r
51 public PowerMockRule powerMockRule = new PowerMockRule();
\r
52 private DbDaoUtil dbDaoUtil;
\r
54 private Handle handle;
\r
56 private Query query;
\r
58 private CorrelationRuleQueryDao correlationRuleQueryDao;
\r
59 private RuleQueryCondition ruleQueryCondition;
\r
62 public void setUp() throws Exception {
\r
63 correlationRuleQueryDao = new CorrelationRuleQueryDao();
\r
65 dbDaoUtil = PowerMock.createMock(DbDaoUtil.class);
\r
66 handle = PowerMock.createMock(Handle.class);
\r
67 query = PowerMock.createMock(Query.class);
\r
69 Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil);
\r
71 ruleQueryCondition = createRuleQueryCondition();
\r
76 public void getCorrelationRulesByCondition_db_exception() throws Exception {
\r
78 thrown.expect(CorrelationException.class);
\r
79 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED);
\r
81 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
82 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
83 EasyMock.expect(query.list()).andThrow(new RuntimeException()).anyTimes();
\r
84 dbDaoUtil.close(handle);
\r
85 EasyMock.expectLastCall();
\r
87 PowerMock.replayAll();
\r
89 correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
\r
91 PowerMock.verifyAll();
\r
95 public void getCorrelationRulesByCondition_normal() throws Exception {
\r
96 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
97 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
98 EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();
\r
99 dbDaoUtil.close(handle);
\r
100 EasyMock.expectLastCall();
\r
102 PowerMock.replayAll();
\r
104 List<CorrelationRule> result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
\r
105 assertThat(result.size(), is(1));
\r
107 PowerMock.verifyAll();
\r
111 public void getCorrelationRulesByCondition_get_where_sql_exception() throws Exception {
\r
112 thrown.expect(CorrelationException.class);
\r
113 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED);
\r
115 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
116 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
117 EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();
\r
118 dbDaoUtil.close(handle);
\r
119 EasyMock.expectLastCall();
\r
121 PowerMock.replayAll();
\r
123 correlationRuleQueryDao.getCorrelationRulesByCondition(null);
\r
125 PowerMock.verifyAll();
\r
128 private List<Map<String, Object>> createQueryResult() {
\r
129 List<Map<String, Object>> list = new ArrayList<>();
\r
130 Map<String, Object> value = new HashMap<>();
\r
131 value.put("name", "Rule-001");
\r
132 value.put("rid", "rule_" + System.currentTimeMillis());
\r
133 value.put("description", "desc");
\r
134 value.put("enable", 0);
\r
135 value.put("templateID", 1);
\r
136 value.put("engineId", "engine-001");
\r
137 value.put("engineType", "engineType-001");
\r
138 value.put("creator", "admin");
\r
139 value.put("createTime", new Date());
\r
140 value.put("updator", "admin");
\r
141 value.put("updateTime", new Date());
\r
142 value.put("params", new Properties());
\r
143 value.put("domain", "Domain");
\r
144 value.put("isManual", 0);
\r
145 value.put("vendor", "Vendor");
\r
146 value.put("content", "Contents");
\r
147 value.put("package", "package");
\r
152 private RuleQueryCondition createRuleQueryCondition() {
\r
153 RuleQueryCondition ruleQueryCondition = new RuleQueryCondition();
\r
154 ruleQueryCondition.setRid("rule_" + System.currentTimeMillis());
\r
155 ruleQueryCondition.setName("Rule-001");
\r
156 ruleQueryCondition.setEnabled(0);
\r
157 ruleQueryCondition.setCreator("admin");
\r
158 ruleQueryCondition.setModifier("admin");
\r
159 return ruleQueryCondition;
\r