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.onap.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.onap.holmes.common.api.entity.CorrelationRule;
\r
34 import org.onap.holmes.common.exception.CorrelationException;
\r
35 import org.onap.holmes.common.utils.DbDaoUtil;
\r
36 import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;
\r
37 import org.powermock.api.easymock.PowerMock;
\r
38 import org.powermock.modules.junit4.rule.PowerMockRule;
\r
39 import org.powermock.reflect.Whitebox;
\r
40 import org.skife.jdbi.v2.Handle;
\r
41 import org.skife.jdbi.v2.Query;
\r
44 public class CorrelationRuleQueryDaoTest {
\r
47 public ExpectedException thrown = ExpectedException.none();
\r
50 public PowerMockRule powerMockRule = new PowerMockRule();
\r
51 private DbDaoUtil dbDaoUtil;
\r
53 private Handle handle;
\r
55 private Query query;
\r
57 private CorrelationRuleQueryDao correlationRuleQueryDao;
\r
58 private RuleQueryCondition ruleQueryCondition;
\r
61 public void setUp() throws Exception {
\r
62 correlationRuleQueryDao = new CorrelationRuleQueryDao();
\r
64 dbDaoUtil = PowerMock.createMock(DbDaoUtil.class);
\r
65 handle = PowerMock.createMock(Handle.class);
\r
66 query = PowerMock.createMock(Query.class);
\r
68 Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil);
\r
70 ruleQueryCondition = createRuleQueryCondition();
\r
75 public void getCorrelationRulesByCondition_db_exception() throws Exception {
\r
77 thrown.expect(CorrelationException.class);
\r
78 thrown.expectMessage("Failed to query the rule.");
\r
80 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
81 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
82 EasyMock.expect(query.list()).andThrow(new RuntimeException()).anyTimes();
\r
83 dbDaoUtil.close(handle);
\r
84 EasyMock.expectLastCall();
\r
86 PowerMock.replayAll();
\r
88 correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
\r
90 PowerMock.verifyAll();
\r
94 public void getCorrelationRulesByCondition_normal() throws Exception {
\r
95 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
96 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
97 EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();
\r
98 dbDaoUtil.close(handle);
\r
99 EasyMock.expectLastCall();
\r
101 PowerMock.replayAll();
\r
103 List<CorrelationRule> result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
\r
104 assertThat(result.size(), is(1));
\r
106 PowerMock.verifyAll();
\r
110 public void getCorrelationRulesByCondition_get_where_sql_exception() throws Exception {
\r
111 thrown.expect(CorrelationException.class);
\r
112 thrown.expectMessage("An error occurred while building the query SQL.");
\r
114 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
115 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
116 EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();
\r
117 dbDaoUtil.close(handle);
\r
118 EasyMock.expectLastCall();
\r
120 PowerMock.replayAll();
\r
122 correlationRuleQueryDao.getCorrelationRulesByCondition(null);
\r
124 PowerMock.verifyAll();
\r
127 private List<Map<String, Object>> createQueryResult() {
\r
128 List<Map<String, Object>> list = new ArrayList<>();
\r
129 Map<String, Object> value = new HashMap<>();
\r
130 value.put("name", "Rule-001");
\r
131 value.put("rid", "rule_" + System.currentTimeMillis());
\r
132 value.put("description", "desc");
\r
133 value.put("enable", 0);
\r
134 value.put("templateID", 1L);
\r
135 value.put("engineId", "engine-001");
\r
136 value.put("engineType", "engineType-001");
\r
137 value.put("creator", "admin");
\r
138 value.put("createTime", new Date());
\r
139 value.put("updator", "admin");
\r
140 value.put("updateTime", new Date());
\r
141 value.put("params", new Properties());
\r
142 value.put("domain", "Domain");
\r
143 value.put("isManual", 0);
\r
144 value.put("vendor", "Vendor");
\r
145 value.put("content", "Contents");
\r
146 value.put("package", "package");
\r
151 private RuleQueryCondition createRuleQueryCondition() {
\r
152 RuleQueryCondition ruleQueryCondition = new RuleQueryCondition();
\r
153 ruleQueryCondition.setRid("rule_" + System.currentTimeMillis());
\r
154 ruleQueryCondition.setName("Rule-001");
\r
155 ruleQueryCondition.setEnabled(0);
\r
156 ruleQueryCondition.setCreator("admin");
\r
157 ruleQueryCondition.setModifier("admin");
\r
158 return ruleQueryCondition;
\r