2 * Copyright 2017-2021 ZTE Corporation.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.onap.holmes.engine.manager;
\r
19 import org.junit.Rule;
\r
20 import org.junit.Test;
\r
21 import org.junit.rules.ExpectedException;
\r
22 import org.onap.holmes.common.api.entity.AlarmInfo;
\r
23 import org.onap.holmes.common.api.entity.CorrelationRule;
\r
24 import org.onap.holmes.common.api.stat.VesAlarm;
\r
25 import org.onap.holmes.common.config.MicroServiceConfig;
\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.engine.db.AlarmInfoDaoService;
\r
29 import org.onap.holmes.engine.db.CorrelationRuleDaoService;
\r
30 import org.onap.holmes.engine.request.DeployRuleRequest;
\r
31 import org.onap.holmes.engine.wrapper.RuleMgtWrapper;
\r
32 import org.powermock.reflect.Whitebox;
\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
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
43 public class DroolsEngineTest {
\r
46 public ExpectedException thrown = ExpectedException.none();
\r
48 private RuleMgtWrapper ruleMgtWrapperMock;
\r
50 private DroolsEngine droolsEngine;
\r
52 private AlarmInfoDaoService alarmInfoDaoServiceStub;
\r
54 private CorrelationRuleDaoService correlationRuleDaoServiceStub;
\r
56 private ClosedLoopControlNameCache closedLoopControlNameCache;
\r
58 public DroolsEngineTest() throws Exception {
\r
59 System.setProperty(MicroServiceConfig.HOSTNAME, "127.0.0.1:80");
\r
60 droolsEngine = new DroolsEngine();
\r
61 alarmInfoDaoServiceStub = new AlarmInfoDaoServiceStub();
\r
62 droolsEngine.setAlarmInfoDaoService(alarmInfoDaoServiceStub);
\r
63 correlationRuleDaoServiceStub = new CorrelationRuleDaoServiceStub();
\r
64 ruleMgtWrapperMock = new RuleMgtWrapperStub(correlationRuleDaoServiceStub);
\r
65 closedLoopControlNameCache = new ClosedLoopControlNameCache();
\r
66 droolsEngine.setClosedLoopControlNameCache(closedLoopControlNameCache);
\r
67 droolsEngine.setRuleMgtWrapper(ruleMgtWrapperMock);
\r
69 Whitebox.invokeMethod(droolsEngine, "run", null);
\r
70 System.clearProperty(MicroServiceConfig.HOSTNAME);
\r
74 public void deployRule_rule_is_null() throws CorrelationException {
\r
75 thrown.expect(NullPointerException.class);
\r
76 droolsEngine.deployRule(null);
\r
80 public void deployRule_invalid_rule_no_pkg_name() throws CorrelationException {
\r
81 DeployRuleRequest rule = new DeployRuleRequest();
\r
82 rule.setContent("rule123;");
\r
83 thrown.expect(CorrelationException.class);
\r
84 thrown.expectMessage("The package name can not be empty.");
\r
86 droolsEngine.deployRule(rule);
\r
90 public void deployRule_invalid_rule_illegal_contents() throws CorrelationException {
\r
91 DeployRuleRequest rule = new DeployRuleRequest();
\r
92 rule.setContent("package rule123; a random string");
\r
93 thrown.expect(CorrelationException.class);
\r
95 droolsEngine.deployRule(rule);
\r
99 public void deployRule_package_name_repeat() throws CorrelationException {
\r
100 DeployRuleRequest rule = new DeployRuleRequest();
\r
101 rule.setContent("package rule123");
\r
103 thrown.expect(CorrelationException.class);
\r
104 thrown.expectMessage("A rule with the same package name already exists in the system.");
\r
105 droolsEngine.deployRule(rule);
\r
106 droolsEngine.deployRule(rule);
\r
110 public void undeployRule_package_name_is_null() throws CorrelationException {
\r
111 String packageName = null;
\r
112 thrown.expect(CorrelationException.class);
\r
113 thrown.expectMessage("The package name should not be null.");
\r
115 droolsEngine.undeployRule(packageName);
\r
119 public void undeployRule_normal() throws CorrelationException {
\r
120 DeployRuleRequest rule = new DeployRuleRequest();
\r
121 rule.setContent("package rule123");
\r
122 droolsEngine.deployRule(rule);
\r
123 droolsEngine.undeployRule("rule123");
\r
127 public void compileRule_compilation_failure() throws CorrelationException {
\r
128 String content = "invalid contents";
\r
130 thrown.expect(CorrelationException.class);
\r
132 droolsEngine.compileRule(content);
\r
136 public void compileRule_compilation_deployed_rule() throws CorrelationException {
\r
137 String content = "package deployed;";
\r
138 DeployRuleRequest rule = new DeployRuleRequest();
\r
139 rule.setContent(content);
\r
140 rule.setLoopControlName(UUID.randomUUID().toString());
\r
141 thrown.expect(CorrelationException.class);
\r
143 droolsEngine.deployRule(rule);
\r
144 droolsEngine.compileRule(content);
\r
148 public void compileRule_compilation_normal() throws CorrelationException {
\r
149 String content = "package deployed;";
\r
150 DeployRuleRequest rule = new DeployRuleRequest();
\r
151 rule.setContent(content);
\r
152 rule.setLoopControlName(UUID.randomUUID().toString());
\r
154 droolsEngine.compileRule(content);
\r
158 public void putRaisedIntoStream_facthandle_is_not_null() {
\r
159 VesAlarm raiseAlarm = new VesAlarm();
\r
160 raiseAlarm.setSourceId("11111");
\r
161 raiseAlarm.setEventName("alarm");
\r
162 droolsEngine.putRaisedIntoStream(raiseAlarm);
\r
163 droolsEngine.putRaisedIntoStream(raiseAlarm);
\r
167 public void putRaisedIntoStream_factHandle_is_null() {
\r
168 VesAlarm raiseAlarm = new VesAlarm();
\r
169 raiseAlarm.setSourceId("11111");
\r
170 raiseAlarm.setEventName("alarm");
\r
171 droolsEngine.putRaisedIntoStream(raiseAlarm);
\r
175 public void stop() throws Exception {
\r
176 droolsEngine.stop();
\r
180 public void testConvertAlarmInfo2VesAlarm() throws Exception {
\r
181 AlarmInfo alarmInfo = new AlarmInfo();
\r
182 alarmInfo.setEventId("eventId");
\r
183 alarmInfo.setEventName("eventName");
\r
184 alarmInfo.setStartEpochMicroSec(1L);
\r
185 alarmInfo.setLastEpochMicroSec(1L);
\r
186 alarmInfo.setSourceId("sourceId");
\r
187 alarmInfo.setSourceName("sourceName");
\r
188 alarmInfo.setRootFlag(0);
\r
189 alarmInfo.setAlarmIsCleared(1);
\r
191 VesAlarm vesAlarm = Whitebox.invokeMethod(droolsEngine, "convertAlarmInfo2VesAlarm", alarmInfo);
\r
193 assertThat(vesAlarm.getAlarmIsCleared(), is(1));
\r
194 assertThat(vesAlarm.getSourceName(), equalTo("sourceName"));
\r
195 assertThat(vesAlarm.getSourceId(), equalTo("sourceId"));
\r
196 assertThat(vesAlarm.getStartEpochMicrosec(), is(1L));
\r
197 assertThat(vesAlarm.getLastEpochMicrosec(), is(1L));
\r
198 assertThat(vesAlarm.getEventName(), equalTo("eventName"));
\r
199 assertThat(vesAlarm.getEventId(), equalTo("eventId"));
\r
200 assertThat(vesAlarm.getRootFlag(), is(0));
\r
204 public void testConvertVesAlarm2AlarmInfo() throws Exception {
\r
205 VesAlarm vesAlarm = new VesAlarm();
\r
206 vesAlarm.setEventId("eventId");
\r
207 vesAlarm.setEventName("eventName");
\r
208 vesAlarm.setStartEpochMicrosec(1L);
\r
209 vesAlarm.setLastEpochMicrosec(1L);
\r
210 vesAlarm.setSourceId("sourceId");
\r
211 vesAlarm.setSourceName("sourceName");
\r
212 vesAlarm.setRootFlag(0);
\r
213 vesAlarm.setAlarmIsCleared(1);
\r
215 AlarmInfo alarmInfo = Whitebox.invokeMethod(droolsEngine, "convertVesAlarm2AlarmInfo", vesAlarm);
\r
217 assertThat(alarmInfo.getAlarmIsCleared(), is(1));
\r
218 assertThat(alarmInfo.getSourceName(), equalTo("sourceName"));
\r
219 assertThat(alarmInfo.getSourceId(), equalTo("sourceId"));
\r
220 assertThat(alarmInfo.getStartEpochMicroSec(), is(1L));
\r
221 assertThat(alarmInfo.getLastEpochMicroSec(), is(1L));
\r
222 assertThat(alarmInfo.getEventName(), equalTo("eventName"));
\r
223 assertThat(alarmInfo.getEventId(), equalTo("eventId"));
\r
224 assertThat(alarmInfo.getRootFlag(), is(0));
\r
228 public void testQueryPackagesFromEngine() throws CorrelationException {
\r
230 DeployRuleRequest rule = new DeployRuleRequest();
\r
231 rule.setContent("package packageCheck; rule \"test\" when eval(1==1) then System.out.println(1); end");
\r
232 rule.setLoopControlName(UUID.randomUUID().toString());
\r
234 droolsEngine.deployRule(rule);
\r
236 List<String> packages = droolsEngine.queryPackagesFromEngine();
\r
238 assertThat(packages.contains("packageCheck"), is(true));
\r
241 class RuleMgtWrapperStub extends RuleMgtWrapper {
\r
242 private List<CorrelationRule> rules;
\r
244 public RuleMgtWrapperStub(CorrelationRuleDaoService correlationRuleDaoService) {
\r
245 super(correlationRuleDaoService);
\r
246 rules = new ArrayList<>();
\r
247 CorrelationRule rule = new CorrelationRule();
\r
248 rule.setEnabled(1);
\r
249 rule.setContent("package org.onap.holmes;");
\r
250 rule.setPackageName("UT");
\r
251 rule.setClosedControlLoopName(UUID.randomUUID().toString());
\r
252 rule.setEngineInstance("127.0.0.1");
\r
256 public List<CorrelationRule> getRules() {
\r
260 public void setRules(List<CorrelationRule> rules) {
\r
261 this.rules = rules;
\r
265 public List<CorrelationRule> queryRuleByEnable(int enabled) throws CorrelationException {
\r
266 return rules.stream().filter(rule -> rule.getEnabled() == enabled).collect(Collectors.toList());
\r
270 class AlarmInfoDaoServiceStub extends AlarmInfoDaoService {
\r
272 private List<AlarmInfo> alarms;
\r
274 public AlarmInfoDaoServiceStub() {
\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
289 public AlarmInfo saveAlarm(AlarmInfo alarmInfo) {
\r
290 alarms.add(alarmInfo);
\r
295 public List<AlarmInfo> queryAllAlarm() {
\r
300 public void deleteAlarm(AlarmInfo alarmInfo) {
\r
305 class CorrelationRuleDaoServiceStub extends CorrelationRuleDaoService {
\r