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