/** * Copyright 2017-2021 ZTE Corporation. *
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.holmes.engine.manager;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onap.holmes.common.api.entity.AlarmInfo;
import org.onap.holmes.common.api.entity.CorrelationRule;
import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.engine.db.AlarmInfoDaoService;
import org.onap.holmes.engine.db.CorrelationRuleDaoService;
import org.onap.holmes.engine.request.DeployRuleRequest;
import org.onap.holmes.engine.wrapper.RuleMgtWrapper;
import org.powermock.reflect.Whitebox;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
public class DroolsEngineTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private RuleMgtWrapper ruleMgtWrapperMock;
private DroolsEngine droolsEngine;
private AlarmInfoDaoService alarmInfoDaoServiceStub;
private CorrelationRuleDaoService correlationRuleDaoServiceStub;
private ClosedLoopControlNameCache closedLoopControlNameCache;
public DroolsEngineTest() throws Exception {
System.setProperty(MicroServiceConfig.HOSTNAME, "127.0.0.1:80");
droolsEngine = new DroolsEngine();
alarmInfoDaoServiceStub = new AlarmInfoDaoServiceStub();
droolsEngine.setAlarmInfoDaoService(alarmInfoDaoServiceStub);
correlationRuleDaoServiceStub = new CorrelationRuleDaoServiceStub();
ruleMgtWrapperMock = new RuleMgtWrapperStub(correlationRuleDaoServiceStub);
closedLoopControlNameCache = new ClosedLoopControlNameCache();
droolsEngine.setClosedLoopControlNameCache(closedLoopControlNameCache);
droolsEngine.setRuleMgtWrapper(ruleMgtWrapperMock);
Whitebox.invokeMethod(droolsEngine, "run", null);
System.clearProperty(MicroServiceConfig.HOSTNAME);
}
@Test
public void deployRule_rule_is_null() throws CorrelationException {
thrown.expect(NullPointerException.class);
droolsEngine.deployRule(null);
}
@Test
public void deployRule_invalid_rule_no_pkg_name() throws CorrelationException {
DeployRuleRequest rule = new DeployRuleRequest();
rule.setContent("rule123;");
thrown.expect(CorrelationException.class);
thrown.expectMessage("The package name can not be empty.");
droolsEngine.deployRule(rule);
}
@Test
public void deployRule_invalid_rule_illegal_contents() throws CorrelationException {
DeployRuleRequest rule = new DeployRuleRequest();
rule.setContent("package rule123; a random string");
thrown.expect(CorrelationException.class);
droolsEngine.deployRule(rule);
}
@Test
public void deployRule_package_name_repeat() throws CorrelationException {
DeployRuleRequest rule = new DeployRuleRequest();
rule.setContent("package rule123");
thrown.expect(CorrelationException.class);
thrown.expectMessage("A rule with the same package name already exists in the system.");
droolsEngine.deployRule(rule);
droolsEngine.deployRule(rule);
}
@Test
public void undeployRule_package_name_is_null() throws CorrelationException {
String packageName = null;
thrown.expect(CorrelationException.class);
thrown.expectMessage("The package name should not be null.");
droolsEngine.undeployRule(packageName);
}
@Test
public void undeployRule_normal() throws CorrelationException {
DeployRuleRequest rule = new DeployRuleRequest();
rule.setContent("package rule123");
droolsEngine.deployRule(rule);
droolsEngine.undeployRule("rule123");
}
@Test
public void compileRule_compilation_failure() throws CorrelationException {
String content = "invalid contents";
thrown.expect(CorrelationException.class);
droolsEngine.compileRule(content);
}
@Test
public void compileRule_compilation_deployed_rule() throws CorrelationException {
String content = "package deployed;";
DeployRuleRequest rule = new DeployRuleRequest();
rule.setContent(content);
rule.setLoopControlName(UUID.randomUUID().toString());
thrown.expect(CorrelationException.class);
droolsEngine.deployRule(rule);
droolsEngine.compileRule(content);
}
@Test
public void compileRule_compilation_normal() throws CorrelationException {
String content = "package deployed;";
DeployRuleRequest rule = new DeployRuleRequest();
rule.setContent(content);
rule.setLoopControlName(UUID.randomUUID().toString());
droolsEngine.compileRule(content);
}
@Test
public void putRaisedIntoStream_facthandle_is_not_null() {
VesAlarm raiseAlarm = new VesAlarm();
raiseAlarm.setSourceId("11111");
raiseAlarm.setEventName("alarm");
droolsEngine.putRaisedIntoStream(raiseAlarm);
droolsEngine.putRaisedIntoStream(raiseAlarm);
}
@Test
public void putRaisedIntoStream_factHandle_is_null() {
VesAlarm raiseAlarm = new VesAlarm();
raiseAlarm.setSourceId("11111");
raiseAlarm.setEventName("alarm");
droolsEngine.putRaisedIntoStream(raiseAlarm);
}
@Test
public void stop() throws Exception {
droolsEngine.stop();
}
@Test
public void testConvertAlarmInfo2VesAlarm() throws Exception {
AlarmInfo alarmInfo = new AlarmInfo();
alarmInfo.setEventId("eventId");
alarmInfo.setEventName("eventName");
alarmInfo.setStartEpochMicroSec(1L);
alarmInfo.setLastEpochMicroSec(1L);
alarmInfo.setSourceId("sourceId");
alarmInfo.setSourceName("sourceName");
alarmInfo.setRootFlag(0);
alarmInfo.setAlarmIsCleared(1);
VesAlarm vesAlarm = Whitebox.invokeMethod(droolsEngine, "convertAlarmInfo2VesAlarm", alarmInfo);
assertThat(vesAlarm.getAlarmIsCleared(), is(1));
assertThat(vesAlarm.getSourceName(), equalTo("sourceName"));
assertThat(vesAlarm.getSourceId(), equalTo("sourceId"));
assertThat(vesAlarm.getStartEpochMicrosec(), is(1L));
assertThat(vesAlarm.getLastEpochMicrosec(), is(1L));
assertThat(vesAlarm.getEventName(), equalTo("eventName"));
assertThat(vesAlarm.getEventId(), equalTo("eventId"));
assertThat(vesAlarm.getRootFlag(), is(0));
}
@Test
public void testConvertVesAlarm2AlarmInfo() throws Exception {
VesAlarm vesAlarm = new VesAlarm();
vesAlarm.setEventId("eventId");
vesAlarm.setEventName("eventName");
vesAlarm.setStartEpochMicrosec(1L);
vesAlarm.setLastEpochMicrosec(1L);
vesAlarm.setSourceId("sourceId");
vesAlarm.setSourceName("sourceName");
vesAlarm.setRootFlag(0);
vesAlarm.setAlarmIsCleared(1);
AlarmInfo alarmInfo = Whitebox.invokeMethod(droolsEngine, "convertVesAlarm2AlarmInfo", vesAlarm);
assertThat(alarmInfo.getAlarmIsCleared(), is(1));
assertThat(alarmInfo.getSourceName(), equalTo("sourceName"));
assertThat(alarmInfo.getSourceId(), equalTo("sourceId"));
assertThat(alarmInfo.getStartEpochMicroSec(), is(1L));
assertThat(alarmInfo.getLastEpochMicroSec(), is(1L));
assertThat(alarmInfo.getEventName(), equalTo("eventName"));
assertThat(alarmInfo.getEventId(), equalTo("eventId"));
assertThat(alarmInfo.getRootFlag(), is(0));
}
@Test
public void testQueryPackagesFromEngine() throws CorrelationException {
DeployRuleRequest rule = new DeployRuleRequest();
rule.setContent("package packageCheck; rule \"test\" when eval(1==1) then System.out.println(1); end");
rule.setLoopControlName(UUID.randomUUID().toString());
droolsEngine.deployRule(rule);
List