Change the API Path
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / db / CorrelationRuleQueryDaoTest.java
1 /**\r
2  * Copyright 2017 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 static org.hamcrest.MatcherAssert.assertThat;\r
20 import static org.hamcrest.Matchers.is;\r
21 \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
42 \r
43 \r
44 public class CorrelationRuleQueryDaoTest {\r
45 \r
46     @Rule\r
47     public ExpectedException thrown = ExpectedException.none();\r
48 \r
49     @Rule\r
50     public PowerMockRule powerMockRule = new PowerMockRule();\r
51     private DbDaoUtil dbDaoUtil;\r
52 \r
53     private Handle handle;\r
54 \r
55     private Query query;\r
56 \r
57     private CorrelationRuleQueryDao correlationRuleQueryDao;\r
58     private RuleQueryCondition ruleQueryCondition;\r
59 \r
60     @Before\r
61     public void setUp() throws Exception {\r
62         correlationRuleQueryDao = new CorrelationRuleQueryDao();\r
63 \r
64         dbDaoUtil = PowerMock.createMock(DbDaoUtil.class);\r
65         handle = PowerMock.createMock(Handle.class);\r
66         query = PowerMock.createMock(Query.class);\r
67 \r
68         Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil);\r
69 \r
70         ruleQueryCondition = createRuleQueryCondition();\r
71     }\r
72 \r
73 \r
74     @Test\r
75     public void getCorrelationRulesByCondition_db_exception() throws Exception {\r
76 \r
77         thrown.expect(CorrelationException.class);\r
78         thrown.expectMessage("Failed to query the rule.");\r
79 \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
85 \r
86         PowerMock.replayAll();\r
87 \r
88         correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);\r
89 \r
90         PowerMock.verifyAll();\r
91     }\r
92 \r
93     @Test\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
100 \r
101         PowerMock.replayAll();\r
102 \r
103         List<CorrelationRule> result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);\r
104         assertThat(result.size(), is(1));\r
105 \r
106         PowerMock.verifyAll();\r
107     }\r
108 \r
109     @Test\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
113 \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
119 \r
120         PowerMock.replayAll();\r
121 \r
122         correlationRuleQueryDao.getCorrelationRulesByCondition(null);\r
123 \r
124         PowerMock.verifyAll();\r
125     }\r
126 \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
147         list.add(value);\r
148         return list;\r
149     }\r
150 \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
159     }\r
160 \r
161 }\r