2 * Copyright 2017 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.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
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 ruleMgtWrapper;
\r
50 private AlarmInfoDao alarmInfoDaoMock;
\r
52 private DroolsEngine droolsEngine;
\r
54 private DbDaoUtil dbDaoUtilStub;
\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
66 public void setUp() throws Exception {
\r
67 PowerMock.resetAll();
\r
71 public void deployRule_rule_is_null() throws CorrelationException {
\r
72 thrown.expect(NullPointerException.class);
\r
73 droolsEngine.deployRule(null);
\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
83 droolsEngine.deployRule(rule);
\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
92 droolsEngine.deployRule(rule);
\r
96 public void deployRule_package_name_repeat() throws CorrelationException {
\r
97 DeployRuleRequest rule = new DeployRuleRequest();
\r
98 rule.setContent("package rule123");
\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
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
112 droolsEngine.undeployRule(packageName);
\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
124 public void compileRule_compilation_failure() throws CorrelationException {
\r
125 String content = "invalid contents";
\r
127 thrown.expect(CorrelationException.class);
\r
129 droolsEngine.compileRule(content);
\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
140 droolsEngine.deployRule(rule);
\r
141 droolsEngine.compileRule(content);
\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
151 droolsEngine.compileRule(content);
\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
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
172 public void stop() throws Exception {
\r
173 droolsEngine.stop();
\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
188 VesAlarm vesAlarm = Whitebox.invokeMethod(droolsEngine, "convertAlarmInfo2VesAlarm", alarmInfo);
\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
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
212 AlarmInfo alarmInfo = Whitebox.invokeMethod(droolsEngine, "convertVesAlarm2AlarmInfo", vesAlarm);
\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
225 public void testQueryPackagesFromEngine() throws CorrelationException {
\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
231 droolsEngine.deployRule(rule);
\r
233 List<String> packages = droolsEngine.queryPackagesFromEngine();
\r
235 assertThat(packages.contains("packageCheck"), is(true));
\r
239 class RuleMgtWrapperStub extends RuleMgtWrapper {
\r
240 private List<CorrelationRule> rules;
\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
252 public List<CorrelationRule> getRules() {
\r
256 public void setRules(List<CorrelationRule> rules) {
\r
257 this.rules = rules;
\r
261 public List<CorrelationRule> queryRuleByEnable(int enabled) throws CorrelationException {
\r
262 return rules.stream().filter(rule -> rule.getEnabled() == enabled).collect(Collectors.toList());
\r
266 class AlarmInfoDaoStub extends AlarmInfoDao {
\r
268 private List<AlarmInfo> alarms;
\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
285 protected String addAlarm(AlarmInfo alarmInfo) {
\r
286 alarms.add(alarmInfo);
\r
291 protected List<AlarmInfo> queryAlarm() {
\r
296 protected int deleteAlarmByAlarmIsCleared(String alarmName, String sourceName, String sourceId) {
\r
301 class DbDaoUtilStub extends DbDaoUtil {
\r
302 private AlarmInfoDao dao = new AlarmInfoDaoStub();
\r
305 public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {
\r