Optimized Rule Deployment Logic
[holmes/engine-management.git] / engine-d / src / test / java / org / onap / holmes / engine / manager / DroolsEngineTest.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  * <p>\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  * <p>\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  * <p>\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.engine.manager;\r
18 \r
19 import org.junit.Before;\r
20 import org.junit.BeforeClass;\r
21 import org.junit.Rule;\r
22 import org.junit.Test;\r
23 import org.junit.rules.ExpectedException;\r
24 import org.onap.holmes.common.api.entity.AlarmInfo;\r
25 import org.onap.holmes.common.api.entity.CorrelationRule;\r
26 import org.onap.holmes.common.api.stat.VesAlarm;\r
27 import org.onap.holmes.common.exception.CorrelationException;\r
28 import org.onap.holmes.common.utils.DbDaoUtil;\r
29 import org.onap.holmes.engine.db.AlarmInfoDao;\r
30 import org.onap.holmes.engine.request.DeployRuleRequest;\r
31 import org.onap.holmes.engine.wrapper.RuleMgtWrapper;\r
32 import org.powermock.api.easymock.PowerMock;\r
33 import org.powermock.reflect.Whitebox;\r
34 \r
35 import java.lang.reflect.InvocationTargetException;\r
36 import java.lang.reflect.Method;\r
37 import java.util.*;\r
38 import java.util.stream.Collectors;\r
39 \r
40 import static org.easymock.EasyMock.anyObject;\r
41 import static org.hamcrest.CoreMatchers.is;\r
42 import static org.hamcrest.core.IsEqual.equalTo;\r
43 import static org.junit.Assert.assertThat;\r
44 \r
45 public class DroolsEngineTest {\r
46 \r
47     @Rule\r
48     public ExpectedException thrown = ExpectedException.none();\r
49 \r
50     private RuleMgtWrapper ruleMgtWrapper;\r
51 \r
52     private AlarmInfoDao alarmInfoDaoMock;\r
53 \r
54     private DroolsEngine droolsEngine;\r
55 \r
56     private DbDaoUtil dbDaoUtilStub;\r
57 \r
58     public DroolsEngineTest() throws Exception {\r
59         droolsEngine = new DroolsEngine();\r
60         ruleMgtWrapper = new RuleMgtWrapperStub();\r
61         dbDaoUtilStub = new DbDaoUtilStub();\r
62         Whitebox.setInternalState(droolsEngine, "daoUtil", dbDaoUtilStub);\r
63         Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper);\r
64         Whitebox.invokeMethod(droolsEngine, "init");\r
65     }\r
66 \r
67     @Before\r
68     public void setUp() throws Exception {\r
69         PowerMock.resetAll();\r
70     }\r
71 \r
72     @Test\r
73     public void deployRule_rule_is_null() throws CorrelationException {\r
74         thrown.expect(NullPointerException.class);\r
75         droolsEngine.deployRule(null);\r
76     }\r
77 \r
78     @Test\r
79     public void deployRule_invalid_rule_no_pkg_name() throws CorrelationException {\r
80         DeployRuleRequest rule = new DeployRuleRequest();\r
81         rule.setContent("rule123;");\r
82         thrown.expect(CorrelationException.class);\r
83         thrown.expectMessage("The package name can not be empty.");\r
84 \r
85         droolsEngine.deployRule(rule);\r
86     }\r
87 \r
88     @Test\r
89     public void deployRule_invalid_rule_illegal_contents() throws CorrelationException {\r
90         DeployRuleRequest rule = new DeployRuleRequest();\r
91         rule.setContent("package rule123; a random string");\r
92         thrown.expect(CorrelationException.class);\r
93 \r
94         droolsEngine.deployRule(rule);\r
95     }\r
96 \r
97     @Test\r
98     public void deployRule_package_name_repeat() throws CorrelationException {\r
99         DeployRuleRequest rule = new DeployRuleRequest();\r
100         rule.setContent("package rule123");\r
101 \r
102         thrown.expect(CorrelationException.class);\r
103         thrown.expectMessage("A rule with the same package name already exists in the system.");\r
104         droolsEngine.deployRule(rule);\r
105         droolsEngine.deployRule(rule);\r
106     }\r
107 \r
108     @Test\r
109     public void undeployRule_package_name_is_null() throws CorrelationException {\r
110         String packageName = null;\r
111         thrown.expect(CorrelationException.class);\r
112         thrown.expectMessage("The package name should not be null.");\r
113 \r
114         droolsEngine.undeployRule(packageName);\r
115     }\r
116 \r
117     @Test\r
118     public void undeployRule_normal() throws CorrelationException {\r
119         DeployRuleRequest rule = new DeployRuleRequest();\r
120         rule.setContent("package rule123");\r
121         droolsEngine.deployRule(rule);\r
122         droolsEngine.undeployRule("rule123");\r
123     }\r
124 \r
125     @Test\r
126     public void compileRule_compilation_failure() throws CorrelationException {\r
127         String content = "invalid contents";\r
128 \r
129         thrown.expect(CorrelationException.class);\r
130 \r
131         droolsEngine.compileRule(content);\r
132     }\r
133 \r
134     @Test\r
135     public void compileRule_compilation_deployed_rule() throws CorrelationException {\r
136         String content = "package deployed;";\r
137         DeployRuleRequest rule = new DeployRuleRequest();\r
138         rule.setContent(content);\r
139         rule.setLoopControlName(UUID.randomUUID().toString());\r
140         thrown.expect(CorrelationException.class);\r
141 \r
142         droolsEngine.deployRule(rule);\r
143         droolsEngine.compileRule(content);\r
144     }\r
145 \r
146     @Test\r
147     public void compileRule_compilation_normal() throws CorrelationException {\r
148         String content = "package deployed;";\r
149         DeployRuleRequest rule = new DeployRuleRequest();\r
150         rule.setContent(content);\r
151         rule.setLoopControlName(UUID.randomUUID().toString());\r
152 \r
153         droolsEngine.compileRule(content);\r
154     }\r
155 \r
156     @Test\r
157     public void putRaisedIntoStream_facthandle_is_not_null() {\r
158         VesAlarm raiseAlarm = new VesAlarm();\r
159         raiseAlarm.setSourceId("11111");\r
160         raiseAlarm.setEventName("alarm");\r
161         droolsEngine.putRaisedIntoStream(raiseAlarm);\r
162         droolsEngine.putRaisedIntoStream(raiseAlarm);\r
163     }\r
164 \r
165     @Test\r
166     public void putRaisedIntoStream_factHandle_is_null() {\r
167         VesAlarm raiseAlarm = new VesAlarm();\r
168         raiseAlarm.setSourceId("11111");\r
169         raiseAlarm.setEventName("alarm");\r
170         droolsEngine.putRaisedIntoStream(raiseAlarm);\r
171     }\r
172 \r
173     @Test\r
174     public void stop() throws Exception {\r
175         droolsEngine.stop();\r
176     }\r
177 \r
178     @Test\r
179     public void testConvertAlarmInfo2VesAlarm() throws Exception {\r
180         AlarmInfo alarmInfo = new AlarmInfo();\r
181         alarmInfo.setEventId("eventId");\r
182         alarmInfo.setEventName("eventName");\r
183         alarmInfo.setStartEpochMicroSec(1L);\r
184         alarmInfo.setLastEpochMicroSec(1L);\r
185         alarmInfo.setSourceId("sourceId");\r
186         alarmInfo.setSourceName("sourceName");\r
187         alarmInfo.setRootFlag(0);\r
188         alarmInfo.setAlarmIsCleared(1);\r
189 \r
190         VesAlarm vesAlarm = Whitebox.invokeMethod(droolsEngine, "convertAlarmInfo2VesAlarm", alarmInfo);\r
191 \r
192         assertThat(vesAlarm.getAlarmIsCleared(), is(1));\r
193         assertThat(vesAlarm.getSourceName(), equalTo("sourceName"));\r
194         assertThat(vesAlarm.getSourceId(), equalTo("sourceId"));\r
195         assertThat(vesAlarm.getStartEpochMicrosec(), is(1L));\r
196         assertThat(vesAlarm.getLastEpochMicrosec(), is(1L));\r
197         assertThat(vesAlarm.getEventName(), equalTo("eventName"));\r
198         assertThat(vesAlarm.getEventId(), equalTo("eventId"));\r
199         assertThat(vesAlarm.getRootFlag(), is(0));\r
200     }\r
201 \r
202     @Test\r
203     public void testConvertVesAlarm2AlarmInfo() throws Exception {\r
204         VesAlarm vesAlarm = new VesAlarm();\r
205         vesAlarm.setEventId("eventId");\r
206         vesAlarm.setEventName("eventName");\r
207         vesAlarm.setStartEpochMicrosec(1L);\r
208         vesAlarm.setLastEpochMicrosec(1L);\r
209         vesAlarm.setSourceId("sourceId");\r
210         vesAlarm.setSourceName("sourceName");\r
211         vesAlarm.setRootFlag(0);\r
212         vesAlarm.setAlarmIsCleared(1);\r
213 \r
214         AlarmInfo alarmInfo = Whitebox.invokeMethod(droolsEngine, "convertVesAlarm2AlarmInfo", vesAlarm);\r
215 \r
216         assertThat(alarmInfo.getAlarmIsCleared(), is(1));\r
217         assertThat(alarmInfo.getSourceName(), equalTo("sourceName"));\r
218         assertThat(alarmInfo.getSourceId(), equalTo("sourceId"));\r
219         assertThat(alarmInfo.getStartEpochMicroSec(), is(1L));\r
220         assertThat(alarmInfo.getLastEpochMicroSec(), is(1L));\r
221         assertThat(alarmInfo.getEventName(), equalTo("eventName"));\r
222         assertThat(alarmInfo.getEventId(), equalTo("eventId"));\r
223         assertThat(alarmInfo.getRootFlag(), is(0));\r
224     }\r
225 \r
226     @Test\r
227     public void testQueryPackagesFromEngine() throws CorrelationException {\r
228 \r
229         DeployRuleRequest rule = new DeployRuleRequest();\r
230         rule.setContent("package packageCheck; rule \"test\" when eval(1==1) then System.out.println(1); end");\r
231         rule.setLoopControlName(UUID.randomUUID().toString());\r
232 \r
233         droolsEngine.deployRule(rule);\r
234 \r
235         List<String> packages = droolsEngine.queryPackagesFromEngine();\r
236 \r
237         assertThat(packages.contains("packageCheck"), is(true));\r
238     }\r
239 }\r
240 \r
241 class RuleMgtWrapperStub extends RuleMgtWrapper {\r
242     private List<CorrelationRule> rules;\r
243 \r
244     public RuleMgtWrapperStub() {\r
245         rules = new ArrayList<>();\r
246         CorrelationRule rule = new CorrelationRule();\r
247         rule.setEnabled(1);\r
248         rule.setContent("package org.onap.holmes;");\r
249         rule.setPackageName("UT");\r
250         rule.setClosedControlLoopName(UUID.randomUUID().toString());\r
251         rules.add(rule);\r
252     }\r
253 \r
254     public List<CorrelationRule> getRules() {\r
255         return rules;\r
256     }\r
257 \r
258     public void setRules(List<CorrelationRule> rules) {\r
259         this.rules = rules;\r
260     }\r
261 \r
262     @Override\r
263     public List<CorrelationRule> queryRuleByEnable(int enabled) throws CorrelationException {\r
264         return rules.stream().filter(rule -> rule.getEnabled() == enabled).collect(Collectors.toList());\r
265     }\r
266 }\r
267 \r
268 class AlarmInfoDaoStub extends AlarmInfoDao {\r
269 \r
270     private List<AlarmInfo> alarms;\r
271 \r
272     public AlarmInfoDaoStub() {\r
273         alarms = new ArrayList<>();\r
274         AlarmInfo info = new AlarmInfo();\r
275         info.setEventId("eventId");\r
276         info.setEventName("eventName");\r
277         info.setStartEpochMicroSec(1L);\r
278         info.setLastEpochMicroSec(1L);\r
279         info.setSourceId("sourceId");\r
280         info.setSourceName("sourceName");\r
281         info.setRootFlag(0);\r
282         info.setAlarmIsCleared(1);\r
283         alarms.add(info);\r
284     }\r
285 \r
286     @Override\r
287     protected String addAlarm(AlarmInfo alarmInfo) {\r
288         alarms.add(alarmInfo);\r
289         return null;\r
290     }\r
291 \r
292     @Override\r
293     protected List<AlarmInfo> queryAlarm() {\r
294         return alarms;\r
295     }\r
296 \r
297     @Override\r
298     protected int deleteAlarmByAlarmIsCleared(int alarmIsCleared) {\r
299         return 1;\r
300     }\r
301 }\r
302 \r
303 class DbDaoUtilStub extends DbDaoUtil {\r
304     private AlarmInfoDao dao = new AlarmInfoDaoStub();\r
305     @Override\r
306     public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {\r
307 \r
308         return (T)dao;\r
309 \r
310     }\r
311 }\r