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