Migrate from DW to Springboot
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / db / CorrelationRuleQueryServiceTest.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.jdbi.v3.core.Handle;\r
21 import org.jdbi.v3.core.Jdbi;\r
22 import org.jdbi.v3.core.result.ResultIterable;\r
23 import org.jdbi.v3.core.statement.Query;\r
24 import org.junit.Before;\r
25 import org.junit.Rule;\r
26 import org.junit.Test;\r
27 import org.junit.rules.ExpectedException;\r
28 import org.junit.runner.RunWith;\r
29 import org.onap.holmes.common.api.entity.CorrelationRule;\r
30 import org.onap.holmes.common.exception.CorrelationException;\r
31 import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;\r
32 import org.powermock.api.easymock.PowerMock;\r
33 import org.powermock.core.classloader.annotations.PrepareForTest;\r
34 import org.powermock.modules.junit4.PowerMockRunner;\r
35 import org.powermock.reflect.Whitebox;\r
36 \r
37 import java.util.*;\r
38 \r
39 import static org.hamcrest.MatcherAssert.assertThat;\r
40 import static org.hamcrest.Matchers.is;\r
41 \r
42 @RunWith(PowerMockRunner.class)\r
43 @PrepareForTest({Handle.class, Query.class})\r
44 public class CorrelationRuleQueryServiceTest {\r
45 \r
46     @Rule\r
47     public ExpectedException thrown = ExpectedException.none();\r
48     private Handle handle;\r
49     private Query query;\r
50     private ResultIterable resultIterable;\r
51     private CorrelationRuleQueryService correlationRuleQueryDao;\r
52     private RuleQueryCondition ruleQueryCondition;\r
53     private Jdbi jdbi;\r
54 \r
55     @Before\r
56     public void setUp() throws Exception {\r
57         correlationRuleQueryDao = new CorrelationRuleQueryService();\r
58 \r
59         jdbi = PowerMock.createMock(Jdbi.class);\r
60         handle = PowerMock.createMock(Handle.class);\r
61         query = PowerMock.createMock(Query.class);\r
62         resultIterable = PowerMock.createMock(ResultIterable.class);\r
63 \r
64         Whitebox.setInternalState(correlationRuleQueryDao, "jdbi", jdbi);\r
65 \r
66         ruleQueryCondition = createRuleQueryCondition();\r
67     }\r
68 \r
69 \r
70     @Test\r
71     public void getCorrelationRulesByCondition_db_exception() throws Exception {\r
72 \r
73         thrown.expect(CorrelationException.class);\r
74         thrown.expectMessage("Failed to query the rule.");\r
75 \r
76         EasyMock.expect(jdbi.open()).andReturn(handle);\r
77         EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);\r
78         EasyMock.expect(query.mapToMap()).andThrow(new RuntimeException()).anyTimes();\r
79         handle.close();\r
80         EasyMock.expectLastCall();\r
81 \r
82         PowerMock.replayAll();\r
83 \r
84         correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);\r
85 \r
86         PowerMock.verifyAll();\r
87     }\r
88 \r
89     @Test\r
90     public void getCorrelationRulesByCondition_normal() throws Exception {\r
91         EasyMock.expect(jdbi.open()).andReturn(handle);\r
92         EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);\r
93         EasyMock.expect(query.mapToMap()).andReturn(resultIterable);\r
94         EasyMock.expect(resultIterable.list()).andReturn(createQueryResult());\r
95         handle.close();\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(jdbi.open()).andReturn(handle);\r
112         EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);\r
113         EasyMock.expect(query.mapToMap()).andReturn(resultIterable);\r
114         EasyMock.expect(resultIterable.list()).andReturn(createQueryResult());\r
115         handle.close();\r
116         EasyMock.expectLastCall();\r
117 \r
118         PowerMock.replayAll();\r
119 \r
120         correlationRuleQueryDao.getCorrelationRulesByCondition(null);\r
121 \r
122         PowerMock.verifyAll();\r
123     }\r
124 \r
125     private List<Map<String, Object>> createQueryResult() {\r
126         List<Map<String, Object>> list = new ArrayList<>();\r
127         Map<String, Object> value = new HashMap<>();\r
128         value.put("name", "Rule-001");\r
129         value.put("rid", "rule_" + System.currentTimeMillis());\r
130         value.put("description", "desc");\r
131         value.put("enable", 0);\r
132         value.put("templateid", 1L);\r
133         value.put("engineid", "engine-001");\r
134         value.put("enginetype", "engineType-001");\r
135         value.put("creator", "admin");\r
136         value.put("createtime", new Date());\r
137         value.put("updator", "admin");\r
138         value.put("updatetime", new Date());\r
139         value.put("params", new Properties());\r
140         value.put("domain", "Domain");\r
141         value.put("ismanual", 0);\r
142         value.put("vendor", "Vendor");\r
143         value.put("content", "Contents");\r
144         value.put("package", "package");\r
145         list.add(value);\r
146         return list;\r
147     }\r
148 \r
149     private RuleQueryCondition createRuleQueryCondition() {\r
150         RuleQueryCondition ruleQueryCondition = new RuleQueryCondition();\r
151         ruleQueryCondition.setRid("rule_" + System.currentTimeMillis());\r
152         ruleQueryCondition.setName("Rule-001");\r
153         ruleQueryCondition.setEnabled(0);\r
154         ruleQueryCondition.setCreator("admin");\r
155         ruleQueryCondition.setModifier("admin");\r
156         return ruleQueryCondition;\r
157     }\r
158 \r
159 }\r