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.junit.runner.RunWith;
\r
34 import org.openo.holmes.common.api.entity.CorrelationRule;
\r
35 import org.openo.holmes.common.exception.CorrelationException;
\r
36 import org.openo.holmes.common.utils.DbDaoUtil;
\r
37 import org.openo.holmes.common.utils.I18nProxy;
\r
38 import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;
\r
39 import org.powermock.api.easymock.PowerMock;
\r
40 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
\r
41 import org.powermock.modules.junit4.PowerMockRunner;
\r
42 import org.powermock.modules.junit4.rule.PowerMockRule;
\r
43 import org.powermock.reflect.Whitebox;
\r
44 import org.skife.jdbi.v2.Handle;
\r
45 import org.skife.jdbi.v2.Query;
\r
49 public class CorrelationRuleQueryDaoTest {
\r
52 public ExpectedException thrown = ExpectedException.none();
\r
55 public PowerMockRule powerMockRule = new PowerMockRule();
\r
56 private DbDaoUtil dbDaoUtil;
\r
58 private Handle handle;
\r
60 private Query query;
\r
62 private CorrelationRuleQueryDao correlationRuleQueryDao;
\r
63 private RuleQueryCondition ruleQueryCondition;
\r
66 public void setUp() throws Exception {
\r
67 correlationRuleQueryDao = new CorrelationRuleQueryDao();
\r
69 dbDaoUtil = PowerMock.createMock(DbDaoUtil.class);
\r
70 handle = PowerMock.createMock(Handle.class);
\r
71 query = PowerMock.createMock(Query.class);
\r
73 Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil);
\r
75 ruleQueryCondition = createRuleQueryCondition();
\r
80 public void getCorrelationRulesByCondition_db_exception() throws Exception {
\r
82 thrown.expect(CorrelationException.class);
\r
83 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_QUERY_RULE_FAILED);
\r
85 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
86 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
87 EasyMock.expect(query.list()).andThrow(new RuntimeException()).anyTimes();
\r
88 dbDaoUtil.close(handle);
\r
89 EasyMock.expectLastCall();
\r
91 PowerMock.replayAll();
\r
93 correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
\r
95 PowerMock.verifyAll();
\r
99 public void getCorrelationRulesByCondition_normal() throws Exception {
\r
100 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
101 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
102 EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();
\r
103 dbDaoUtil.close(handle);
\r
104 EasyMock.expectLastCall();
\r
106 PowerMock.replayAll();
\r
108 List<CorrelationRule> result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
\r
109 assertThat(result.size(), is(1));
\r
111 PowerMock.verifyAll();
\r
115 public void getCorrelationRulesByCondition_get_where_sql_exception() throws Exception {
\r
116 thrown.expect(CorrelationException.class);
\r
117 thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED);
\r
119 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
120 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
121 EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();
\r
122 dbDaoUtil.close(handle);
\r
123 EasyMock.expectLastCall();
\r
125 PowerMock.replayAll();
\r
127 correlationRuleQueryDao.getCorrelationRulesByCondition(null);
\r
129 PowerMock.verifyAll();
\r
132 private List<Map<String, Object>> createQueryResult() {
\r
133 List<Map<String, Object>> list = new ArrayList<>();
\r
134 Map<String, Object> value = new HashMap<>();
\r
135 value.put("name", "Rule-001");
\r
136 value.put("rid", "rule_" + System.currentTimeMillis());
\r
137 value.put("description", "desc");
\r
138 value.put("enable", 0);
\r
139 value.put("templateID", 1);
\r
140 value.put("engineId", "engine-001");
\r
141 value.put("engineType", "engineType-001");
\r
142 value.put("creator", "admin");
\r
143 value.put("createTime", new Date());
\r
144 value.put("updator", "admin");
\r
145 value.put("updateTime", new Date());
\r
146 value.put("params", new Properties());
\r
147 value.put("domain", "Domain");
\r
148 value.put("isManual", 0);
\r
149 value.put("vendor", "Vendor");
\r
150 value.put("content", "Contents");
\r
151 value.put("package", "package");
\r
156 private RuleQueryCondition createRuleQueryCondition() {
\r
157 RuleQueryCondition ruleQueryCondition = new RuleQueryCondition();
\r
158 ruleQueryCondition.setRid("rule_" + System.currentTimeMillis());
\r
159 ruleQueryCondition.setName("Rule-001");
\r
160 ruleQueryCondition.setEnabled(0);
\r
161 ruleQueryCondition.setCreator("admin");
\r
162 ruleQueryCondition.setModifier("admin");
\r
163 return ruleQueryCondition;
\r