Updated to Java 11
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / db / CorrelationRuleQueryDaoTest.java
1 /**\r
2  * Copyright 2017-2020 ZTE Corporation.\r
3  *\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
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 package org.onap.holmes.rulemgt.db;\r
18 \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
35 \r
36 import java.util.*;\r
37 \r
38 import static org.hamcrest.MatcherAssert.assertThat;\r
39 import static org.hamcrest.Matchers.is;\r
40 \r
41 @RunWith(PowerMockRunner.class)\r
42 @PrepareForTest({DbDaoUtil.class, Handle.class, Query.class})\r
43 public class CorrelationRuleQueryDaoTest {\r
44 \r
45     @Rule\r
46     public ExpectedException thrown = ExpectedException.none();\r
47 \r
48     private DbDaoUtil dbDaoUtil;\r
49 \r
50     private Handle handle;\r
51 \r
52     private Query query;\r
53 \r
54     private CorrelationRuleQueryDao correlationRuleQueryDao;\r
55     private RuleQueryCondition ruleQueryCondition;\r
56 \r
57     @Before\r
58     public void setUp() throws Exception {\r
59         correlationRuleQueryDao = new CorrelationRuleQueryDao();\r
60 \r
61         dbDaoUtil = PowerMock.createMock(DbDaoUtil.class);\r
62         handle = PowerMock.createMock(Handle.class);\r
63         query = PowerMock.createMock(Query.class);\r
64 \r
65         Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil);\r
66 \r
67         ruleQueryCondition = createRuleQueryCondition();\r
68     }\r
69 \r
70 \r
71     @Test\r
72     public void getCorrelationRulesByCondition_db_exception() throws Exception {\r
73 \r
74         thrown.expect(CorrelationException.class);\r
75         thrown.expectMessage("Failed to query the rule.");\r
76 \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
82 \r
83         PowerMock.replayAll();\r
84 \r
85         correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);\r
86 \r
87         PowerMock.verifyAll();\r
88     }\r
89 \r
90     @Test\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
97 \r
98         PowerMock.replayAll();\r
99 \r
100         List<CorrelationRule> result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);\r
101         assertThat(result.size(), is(1));\r
102 \r
103         PowerMock.verifyAll();\r
104     }\r
105 \r
106     @Test\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
110 \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
116 \r
117         PowerMock.replayAll();\r
118 \r
119         correlationRuleQueryDao.getCorrelationRulesByCondition(null);\r
120 \r
121         PowerMock.verifyAll();\r
122     }\r
123 \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
144         list.add(value);\r
145         return list;\r
146     }\r
147 \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
156     }\r
157 \r
158 }\r