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