2 * Copyright 2017-2020 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 org.easymock.EasyMock;
\r
20 import org.junit.Before;
\r
21 import org.junit.Rule;
\r
22 import org.junit.Test;
\r
23 import org.junit.rules.ExpectedException;
\r
24 import org.junit.runner.RunWith;
\r
25 import org.onap.holmes.common.api.entity.CorrelationRule;
\r
26 import org.onap.holmes.common.exception.CorrelationException;
\r
27 import org.onap.holmes.common.utils.DbDaoUtil;
\r
28 import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;
\r
29 import org.powermock.api.easymock.PowerMock;
\r
30 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
31 import org.powermock.modules.junit4.PowerMockRunner;
\r
32 import org.powermock.reflect.Whitebox;
\r
33 import org.skife.jdbi.v2.Handle;
\r
34 import org.skife.jdbi.v2.Query;
\r
38 import static org.hamcrest.MatcherAssert.assertThat;
\r
39 import static org.hamcrest.Matchers.is;
\r
41 @RunWith(PowerMockRunner.class)
\r
42 @PrepareForTest({DbDaoUtil.class, Handle.class, Query.class})
\r
43 public class CorrelationRuleQueryDaoTest {
\r
46 public ExpectedException thrown = ExpectedException.none();
\r
48 private DbDaoUtil dbDaoUtil;
\r
50 private Handle handle;
\r
52 private Query query;
\r
54 private CorrelationRuleQueryDao correlationRuleQueryDao;
\r
55 private RuleQueryCondition ruleQueryCondition;
\r
58 public void setUp() throws Exception {
\r
59 correlationRuleQueryDao = new CorrelationRuleQueryDao();
\r
61 dbDaoUtil = PowerMock.createMock(DbDaoUtil.class);
\r
62 handle = PowerMock.createMock(Handle.class);
\r
63 query = PowerMock.createMock(Query.class);
\r
65 Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil);
\r
67 ruleQueryCondition = createRuleQueryCondition();
\r
72 public void getCorrelationRulesByCondition_db_exception() throws Exception {
\r
74 thrown.expect(CorrelationException.class);
\r
75 thrown.expectMessage("Failed to query the rule.");
\r
77 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
78 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
79 EasyMock.expect(query.list()).andThrow(new RuntimeException()).anyTimes();
\r
80 dbDaoUtil.close(handle);
\r
81 EasyMock.expectLastCall();
\r
83 PowerMock.replayAll();
\r
85 correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
\r
87 PowerMock.verifyAll();
\r
91 public void getCorrelationRulesByCondition_normal() throws Exception {
\r
92 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
93 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
94 EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();
\r
95 dbDaoUtil.close(handle);
\r
96 EasyMock.expectLastCall();
\r
98 PowerMock.replayAll();
\r
100 List<CorrelationRule> result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);
\r
101 assertThat(result.size(), is(1));
\r
103 PowerMock.verifyAll();
\r
107 public void getCorrelationRulesByCondition_get_where_sql_exception() throws Exception {
\r
108 thrown.expect(CorrelationException.class);
\r
109 thrown.expectMessage("An error occurred while building the query SQL.");
\r
111 EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);
\r
112 EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);
\r
113 EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();
\r
114 dbDaoUtil.close(handle);
\r
115 EasyMock.expectLastCall();
\r
117 PowerMock.replayAll();
\r
119 correlationRuleQueryDao.getCorrelationRulesByCondition(null);
\r
121 PowerMock.verifyAll();
\r
124 private List<Map<String, Object>> createQueryResult() {
\r
125 List<Map<String, Object>> list = new ArrayList<>();
\r
126 Map<String, Object> value = new HashMap<>();
\r
127 value.put("name", "Rule-001");
\r
128 value.put("rid", "rule_" + System.currentTimeMillis());
\r
129 value.put("description", "desc");
\r
130 value.put("enable", 0);
\r
131 value.put("templateID", 1L);
\r
132 value.put("engineId", "engine-001");
\r
133 value.put("engineType", "engineType-001");
\r
134 value.put("creator", "admin");
\r
135 value.put("createTime", new Date());
\r
136 value.put("updator", "admin");
\r
137 value.put("updateTime", new Date());
\r
138 value.put("params", new Properties());
\r
139 value.put("domain", "Domain");
\r
140 value.put("isManual", 0);
\r
141 value.put("vendor", "Vendor");
\r
142 value.put("content", "Contents");
\r
143 value.put("package", "package");
\r
148 private RuleQueryCondition createRuleQueryCondition() {
\r
149 RuleQueryCondition ruleQueryCondition = new RuleQueryCondition();
\r
150 ruleQueryCondition.setRid("rule_" + System.currentTimeMillis());
\r
151 ruleQueryCondition.setName("Rule-001");
\r
152 ruleQueryCondition.setEnabled(0);
\r
153 ruleQueryCondition.setCreator("admin");
\r
154 ruleQueryCondition.setModifier("admin");
\r
155 return ruleQueryCondition;
\r