diff options
author | Fei Tang <tang.fei4@zte.com.cn> | 2018-03-28 08:25:04 +0800 |
---|---|---|
committer | Fei Tang <tang.fei4@zte.com.cn> | 2018-03-28 08:25:04 +0800 |
commit | 43f5b3a0ddb7f22b3c29a4f042991155a46e41d6 (patch) | |
tree | 2e62e86ef13f91cff21244161671c5abb1b4c2b0 | |
parent | 39a1d413420242ceef377f319d3c3f5dc8034398 (diff) |
add alarm synchronization related operation
Issue-ID: HOLMES-106
Change-Id: Idb47aa0726c0c8fc179c085381092e01d7a26fb3
Signed-off-by: Fei Tang <tang.fei4@zte.com.cn>
9 files changed, 322 insertions, 29 deletions
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDao.java b/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDao.java new file mode 100644 index 0000000..c7094c3 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDao.java @@ -0,0 +1,62 @@ +/**
+ * Copyright 2017 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.db;
+
+import org.onap.holmes.common.api.entity.AlarmInfo;
+import org.onap.holmes.common.exception.AlarmInfoException;
+import org.onap.holmes.common.utils.AlarmInfoMapper;
+import org.skife.jdbi.v2.sqlobject.*;
+import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
+
+import java.util.List;
+
+@RegisterMapper(AlarmInfoMapper.class)
+public abstract class AlarmInfoDao {
+
+ @GetGeneratedKeys
+ @SqlUpdate("INSERT INTO ALARM_INFO (EVENTID,EVENTNAME,STARTEPOCHMICROSEC,SOURCEID,SOURCENAME,ALARMISCLEARED,ROOTFLAG,LASTEPOCHMICROSEC) VALUES (:eventId,:eventName,:startEpochMicroSec,:sourceId,:sourceName,:alarmIsCleared,:rootFlag,:lastEpochMicroSec)")
+ protected abstract String addAlarm(@BindBean AlarmInfo alarmInfo);
+
+ @SqlQuery("SELECT * FROM ALARM_INFO")
+ protected abstract List<AlarmInfo> queryAlarm();
+
+ @SqlUpdate("DELETE FROM ALARM_INFO WHERE ALARMISCLEARED=:alarmiscleared")
+ protected abstract int deleteAlarmByAlarmIsCleared(@Bind("alarmiscleared") int alarmIsCleared);
+
+ public AlarmInfo saveAlarm(AlarmInfo alarmInfo) throws AlarmInfoException {
+ try {
+ addAlarm(alarmInfo);
+ return alarmInfo;
+ } catch(Exception e) {
+ throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e);
+ }
+ }
+
+ public List<AlarmInfo> queryAllAlarm() throws AlarmInfoException {
+ try {
+ return queryAlarm();
+ } catch(Exception e) {
+ throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e);
+ }
+ }
+
+ public void deleteClearedAlarm(AlarmInfo alarmInfo) {
+ int alarmIsCleared = alarmInfo.getAlarmIsCleared();
+ if(alarmIsCleared == 1) {
+ deleteAlarmByAlarmIsCleared(alarmIsCleared);
+ }
+ }
+}
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPolling.java b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPolling.java index 935d2c8..1446b14 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPolling.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPolling.java @@ -14,24 +14,32 @@ * limitations under the License. */ package org.onap.holmes.engine.dmaap; -import java.util.ArrayList; -import java.util.List; + import lombok.extern.slf4j.Slf4j; +import org.onap.holmes.common.api.entity.AlarmInfo; import org.onap.holmes.common.api.stat.VesAlarm; +import org.onap.holmes.common.exception.AlarmInfoException; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.dsa.dmaappolling.Subscriber; +import org.onap.holmes.engine.db.AlarmInfoDao; import org.onap.holmes.engine.manager.DroolsEngine; +import java.util.ArrayList; +import java.util.List; + @Slf4j public class DMaaPAlarmPolling implements Runnable { private Subscriber subscriber; private DroolsEngine droolsEngine; private volatile boolean isAlive = true; + private AlarmInfoDao alarmInfoDao; - public DMaaPAlarmPolling(Subscriber subscriber, DroolsEngine droolsEngine) { + + public DMaaPAlarmPolling(Subscriber subscriber, DroolsEngine droolsEngine, AlarmInfoDao alarmInfoDao) { this.subscriber = subscriber; this.droolsEngine = droolsEngine; + this.alarmInfoDao = alarmInfoDao; } public void run() { @@ -39,7 +47,14 @@ public class DMaaPAlarmPolling implements Runnable { List<VesAlarm> vesAlarmList = new ArrayList<>(); try { vesAlarmList = subscriber.subscribe(); - vesAlarmList.forEach(vesAlarm -> droolsEngine.putRaisedIntoStream(vesAlarm)); + vesAlarmList.forEach(vesAlarm -> { + try { + alarmInfoDao.saveAlarm(getAlarmInfo(vesAlarm)); + droolsEngine.putRaisedIntoStream(vesAlarm); + } catch(AlarmInfoException e) { + log.error("Failed to save alarm to database", e); + } + }); } catch (CorrelationException e) { log.error("Failed to process alarms. Sleep for 60 seconds to restart.", e); try { @@ -59,6 +74,18 @@ public class DMaaPAlarmPolling implements Runnable { } } } + private AlarmInfo getAlarmInfo(VesAlarm vesAlarm) { + AlarmInfo alarmInfo = new AlarmInfo(); + alarmInfo.setAlarmIsCleared(vesAlarm.getAlarmIsCleared()); + alarmInfo.setSourceName(vesAlarm.getSourceName()); + alarmInfo.setSourceId(vesAlarm.getSourceId()); + alarmInfo.setStartEpochMicroSec(vesAlarm.getStartEpochMicrosec()); + alarmInfo.setLastEpochMicroSec(vesAlarm.getLastEpochMicrosec()); + alarmInfo.setEventId(vesAlarm.getEventId()); + alarmInfo.setEventName(vesAlarm.getEventName()); + alarmInfo.setRootFlag(vesAlarm.getRootFlag()); + return alarmInfo; + } public void stopTask() { isAlive = false; diff --git a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java index c2fa5b8..b02cbe4 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java @@ -23,7 +23,9 @@ import javax.annotation.PreDestroy; import javax.inject.Inject; import lombok.extern.slf4j.Slf4j; import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.utils.DbDaoUtil; import org.onap.holmes.dsa.dmaappolling.Subscriber; +import org.onap.holmes.engine.db.AlarmInfoDao; import org.onap.holmes.engine.manager.DroolsEngine; @Service @@ -32,12 +34,15 @@ public class SubscriberAction { @Inject private DroolsEngine droolsEngine; + @Inject + private DbDaoUtil daoUtil; private HashMap<String, DMaaPAlarmPolling> pollingTasks = new HashMap<>(); public synchronized void addSubscriber(Subscriber subscriber) { String topic = subscriber.getTopic(); if (topic != null && !pollingTasks.containsKey(topic)) { - DMaaPAlarmPolling pollingTask = new DMaaPAlarmPolling(subscriber, droolsEngine); + AlarmInfoDao alarmInfoDao = daoUtil.getJdbiDaoByOnDemand(AlarmInfoDao.class); + DMaaPAlarmPolling pollingTask = new DMaaPAlarmPolling(subscriber, droolsEngine, alarmInfoDao); Thread thread = new Thread(pollingTask); thread.start(); pollingTasks.put(topic, pollingTask); diff --git a/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java b/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java index d790ee7..3740f18 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java @@ -21,11 +21,11 @@ import java.util.List; import java.util.Locale;
import java.util.Set;
import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import org.drools.compiler.kie.builder.impl.InternalKieModule;
import org.jvnet.hk2.annotations.Service;
+
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
@@ -45,8 +45,14 @@ import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.conf.ClockTypeOption;
import org.kie.api.runtime.rule.FactHandle;
+
+import org.onap.holmes.common.api.entity.AlarmInfo;
+
import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.common.dmaap.DmaapService;
+import org.onap.holmes.common.exception.AlarmInfoException;
+import org.onap.holmes.common.utils.DbDaoUtil;
+import org.onap.holmes.engine.db.AlarmInfoDao;
import org.onap.holmes.engine.request.DeployRuleRequest;
import org.onap.holmes.common.api.entity.CorrelationRule;
import org.onap.holmes.common.exception.CorrelationException;
@@ -62,6 +68,7 @@ public class DroolsEngine { @Inject
private RuleMgtWrapper ruleMgtWrapper;
+
private KieBase kieBase;
private KieSession kieSession;
private KieContainer kieContainer;
@@ -71,8 +78,14 @@ public class DroolsEngine { private KieResources resources;
private KieRepository kieRepository;
+ private AlarmInfoDao alarmInfoDao;
+ @Inject
+ private DbDaoUtil daoUtil;
+
+
@PostConstruct
private void init() {
+ alarmInfoDao = daoUtil.getJdbiDaoByOnDemand(AlarmInfoDao.class);
try {
// start engine
start();
@@ -82,13 +95,14 @@ public class DroolsEngine { }
}
- private void start() throws CorrelationException {
+ private void start() throws AlarmInfoException {
log.info("Drools Engine Initialize Beginning...");
initEngineParameter();
- initDeployRule();
+ alarmSynchronization();
+// initDeployRule();
- log.info("Business Rule Engine Initialize Successfully.");
+ log.info("Alarm synchronization Successfully.");
}
public void stop() {
@@ -220,6 +234,7 @@ public class DroolsEngine { }
this.kieSession.insert(raiseAlarm);
this.kieSession.fireAllRules();
+
}
public List<String> queryAllPackage() {
@@ -261,4 +276,22 @@ public class DroolsEngine { kieBase.removeKiePackage(kiePackage.getName());
}
+ public void alarmSynchronization() throws AlarmInfoException {
+ alarmInfoDao.queryAllAlarm().forEach(alarmInfo -> alarmInfoDao.deleteClearedAlarm(alarmInfo));
+ alarmInfoDao.queryAllAlarm().forEach(alarmInfo -> putRaisedIntoStream(convertAlarmInfo2VesAlarm(alarmInfo)));
+ }
+
+ private VesAlarm convertAlarmInfo2VesAlarm(AlarmInfo alarmInfo) {
+ VesAlarm vesAlarm = new VesAlarm();
+ vesAlarm.setEventId(alarmInfo.getEventId());
+ vesAlarm.setEventName(alarmInfo.getEventName());
+ vesAlarm.setStartEpochMicrosec(alarmInfo.getStartEpochMicroSec());
+ vesAlarm.setSourceId(alarmInfo.getSourceId());
+ vesAlarm.setSourceName(alarmInfo.getSourceName());
+ vesAlarm.setRootFlag(alarmInfo.getRootFlag());
+ vesAlarm.setAlarmIsCleared(alarmInfo.getAlarmIsCleared());
+ vesAlarm.setLastEpochMicrosec(alarmInfo.getLastEpochMicroSec());
+ return vesAlarm;
+ }
+
}
diff --git a/engine-d/src/test/java/org/onap/holmes/engine/EngineDActiveAppTest.java b/engine-d/src/test/java/org/onap/holmes/engine/EngineDActiveAppTest.java index 2c6cdb7..4f8aa2b 100644 --- a/engine-d/src/test/java/org/onap/holmes/engine/EngineDActiveAppTest.java +++ b/engine-d/src/test/java/org/onap/holmes/engine/EngineDActiveAppTest.java @@ -15,9 +15,52 @@ */
package org.onap.holmes.engine;
+import org.easymock.EasyMock;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.holmes.common.config.MicroServiceConfig;
+import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
+import org.onap.msb.sdk.discovery.entity.Node;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+@PrepareForTest(MicroServiceConfig.class)
+@RunWith(PowerMockRunner.class)
public class EngineDActiveAppTest {
+
public static void main(String[] args) throws Exception {
String filePath = "C:\\engine-d.yml";
new EngineDActiveApp().run(new String[]{"server", filePath});
}
+
+ @Test
+ public void testCreateMicroServiceInfo() throws Exception {
+ EngineDActiveApp engineDActiveApp = new EngineDActiveApp();
+ PowerMock.mockStatic(MicroServiceConfig.class);
+ String[] serviceAddrInfo = new String[2];
+ serviceAddrInfo[0] = "10.74.216.82";
+ serviceAddrInfo[1] = "80";
+ EasyMock.expect(MicroServiceConfig.getMicroServiceIpAndPort()).andReturn(serviceAddrInfo);
+ EasyMock.expectLastCall();
+ PowerMock.replayAll();
+
+ MicroServiceInfo msinfo = Whitebox.invokeMethod(engineDActiveApp,"createMicroServiceInfo");
+
+ PowerMock.verifyAll();
+
+ assertThat(msinfo.getServiceName(), equalTo("holmes-engine-mgmt"));
+ assertThat(msinfo.getVersion(), equalTo("v1"));
+ assertThat(msinfo.getUrl(), equalTo("/api/holmes-engine-mgmt/v1"));
+ assertThat(msinfo.getProtocol(), equalTo("REST"));
+ assertThat(msinfo.getVisualRange(), equalTo("0|1"));
+ assertThat(msinfo.isEnable_ssl(), is(true));
+ assertThat(msinfo.getNodes().toArray(new Node[0])[0].getIp(), equalTo(serviceAddrInfo[0]));
+ assertThat(msinfo.getNodes().toArray(new Node[0])[0].getPort(), equalTo(serviceAddrInfo[1]));
+ }
}
diff --git a/engine-d/src/test/java/org/onap/holmes/engine/db/AlarmInfoDaoTest.java b/engine-d/src/test/java/org/onap/holmes/engine/db/AlarmInfoDaoTest.java new file mode 100644 index 0000000..9945322 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/engine/db/AlarmInfoDaoTest.java @@ -0,0 +1,54 @@ +/**
+ * Copyright 2017 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.db;
+
+import org.easymock.EasyMock;
+import org.hamcrest.core.IsNot;
+import org.hamcrest.core.IsNull;
+import org.junit.Assert;
+import org.junit.Before;
+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.exception.AlarmInfoException;
+import org.powermock.api.easymock.PowerMock;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class AlarmInfoDaoTest {
+
+
+ private AlarmInfoDao alarmInfoDao;
+
+ @Before
+ public void setUp() {
+ alarmInfoDao = PowerMock.createMock(AlarmInfoDao.class);
+ }
+
+ @Test
+ public void queryAllAlarm() throws Exception {
+
+ EasyMock.expect(alarmInfoDao.queryAllAlarm()).andReturn(new ArrayList<AlarmInfo>());
+ PowerMock.replayAll();
+
+ List<AlarmInfo> alarmInfoList = alarmInfoDao.queryAllAlarm();
+ PowerMock.verifyAll();
+ Assert.assertThat(alarmInfoList, IsNull.<List<AlarmInfo>>notNullValue());
+ }
+}
diff --git a/engine-d/src/test/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPollingTest.java b/engine-d/src/test/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPollingTest.java index f4804f3..896a384 100644 --- a/engine-d/src/test/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPollingTest.java +++ b/engine-d/src/test/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPollingTest.java @@ -15,6 +15,7 @@ */ package org.onap.holmes.engine.dmaap; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.*; @@ -22,25 +23,31 @@ import java.lang.reflect.Field; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.onap.holmes.common.api.entity.AlarmInfo; +import org.onap.holmes.common.api.stat.VesAlarm; import org.onap.holmes.dsa.dmaappolling.Subscriber; +import org.onap.holmes.engine.db.AlarmInfoDao; import org.onap.holmes.engine.manager.DroolsEngine; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; -@PrepareForTest({Subscriber.class, DroolsEngine.class}) +@PrepareForTest({Subscriber.class, DroolsEngine.class,DMaaPAlarmPolling.class}) @RunWith(PowerMockRunner.class) public class DMaaPAlarmPollingTest { private DMaaPAlarmPolling dMaaPAlarmPolling; private Subscriber subscriber; private DroolsEngine droolsEngine; + private AlarmInfoDao alarmInfoDao; @Before public void setUp() { subscriber = PowerMock.createMock(Subscriber.class); droolsEngine = PowerMock.createMock(DroolsEngine.class); - dMaaPAlarmPolling = new DMaaPAlarmPolling(subscriber, droolsEngine); + alarmInfoDao = PowerMock.createMock(AlarmInfoDao.class); + dMaaPAlarmPolling = new DMaaPAlarmPolling(subscriber, droolsEngine,alarmInfoDao); PowerMock.replayAll(); } @@ -52,4 +59,30 @@ public class DMaaPAlarmPollingTest { assertThat(field.get(dMaaPAlarmPolling), equalTo(false)); } + @Test + public void testGetAlarmInfo() throws Exception { + VesAlarm vesAlarm = new VesAlarm(); + vesAlarm.setAlarmIsCleared(1); + vesAlarm.setSourceName("sourceName"); + vesAlarm.setSourceId("sourceId"); + vesAlarm.setStartEpochMicrosec(1L); + vesAlarm.setLastEpochMicrosec(1L); + vesAlarm.setEventName("eventName"); + vesAlarm.setEventId("eventId"); + vesAlarm.setRootFlag(0); + + PowerMock.replayAll(); + AlarmInfo alarmInfo = Whitebox.invokeMethod(dMaaPAlarmPolling,"getAlarmInfo",vesAlarm); + PowerMock.verifyAll(); + + 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)); + } + }
\ No newline at end of file diff --git a/engine-d/src/test/java/org/onap/holmes/engine/dmaap/SubscriberActionTest.java b/engine-d/src/test/java/org/onap/holmes/engine/dmaap/SubscriberActionTest.java index 7f5d56e..247962f 100644 --- a/engine-d/src/test/java/org/onap/holmes/engine/dmaap/SubscriberActionTest.java +++ b/engine-d/src/test/java/org/onap/holmes/engine/dmaap/SubscriberActionTest.java @@ -30,9 +30,9 @@ public class SubscriberActionTest { public void setUp() { subscriberAction = new SubscriberAction(); HashMap<String, DMaaPAlarmPolling> dMaaPAlarmPollingHashMap = new HashMap<>(); - DMaaPAlarmPolling dMaaPAlarmPolling = new DMaaPAlarmPolling(null, null); + DMaaPAlarmPolling dMaaPAlarmPolling = new DMaaPAlarmPolling(null, null,null); dMaaPAlarmPollingHashMap.put("test", dMaaPAlarmPolling); - DMaaPAlarmPolling dMaaPAlarmPolling1 = new DMaaPAlarmPolling(null, null); + DMaaPAlarmPolling dMaaPAlarmPolling1 = new DMaaPAlarmPolling(null, null,null); dMaaPAlarmPollingHashMap.put("testTopic", dMaaPAlarmPolling1); Whitebox.setInternalState(subscriberAction, "pollingTasks", dMaaPAlarmPollingHashMap); PowerMock.replayAll(); diff --git a/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java b/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java index 3325efa..71ef172 100644 --- a/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java +++ b/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java @@ -16,13 +16,7 @@ package org.onap.holmes.engine.manager;
-import static org.easymock.EasyMock.anyInt;
-import static org.easymock.EasyMock.expect;
-
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Locale;
+import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -35,15 +29,26 @@ import org.kie.api.builder.KieRepository; import org.kie.api.io.KieResources;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
-import org.onap.holmes.common.api.stat.VesAlarm;
-import org.onap.holmes.engine.request.DeployRuleRequest;
+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.constant.AlarmConst;
import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.DbDaoUtil;
+import org.onap.holmes.engine.db.AlarmInfoDao;
+import org.onap.holmes.engine.request.DeployRuleRequest;
import org.onap.holmes.engine.wrapper.RuleMgtWrapper;
import org.powermock.api.easymock.PowerMock;
import org.powermock.reflect.Whitebox;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
public class DroolsEngineTest {
@Rule
@@ -60,9 +65,12 @@ public class DroolsEngineTest { private KieResources resources;
private KieRepository kieRepository;
+ private AlarmInfoDao alarmInfoDaoMock;
private DroolsEngine droolsEngine;
+ private DbDaoUtil dbDaoUtilMock;
+
@Before
public void setUp() throws Exception {
droolsEngine = new DroolsEngine();
@@ -76,9 +84,9 @@ public class DroolsEngineTest { kieBase = kieContainer.getKieBase();
kieSession = kieContainer.newKieSession();
- ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class);
+ alarmInfoDaoMock = PowerMock.createMock(AlarmInfoDao.class);
+ dbDaoUtilMock = PowerMock.createMock(DbDaoUtil.class);
- Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper);
Whitebox.setInternalState(droolsEngine, "kieBase", kieBase);
Whitebox.setInternalState(droolsEngine, "kieSession", kieSession);
@@ -89,6 +97,9 @@ public class DroolsEngineTest { Whitebox.setInternalState(droolsEngine, "resources", resources);
Whitebox.setInternalState(droolsEngine, "kieRepository", kieRepository);
+ Whitebox.setInternalState(droolsEngine, "daoUtil", dbDaoUtilMock);
+ Whitebox.setInternalState(droolsEngine, "alarmInfoDao", alarmInfoDaoMock);
+
PowerMock.resetAll();
}
@@ -102,13 +113,12 @@ public class DroolsEngineTest { rule.setPackageName("org.onap.holmes");
rules.add(rule);
- expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules);
+// expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules);
+ List<AlarmInfo> alarmInfoList = new ArrayList<AlarmInfo>();
+ EasyMock.expect(dbDaoUtilMock.getJdbiDaoByOnDemand(AlarmInfoDao.class)).andReturn(alarmInfoDaoMock).anyTimes();
+ EasyMock.expect(alarmInfoDaoMock.queryAllAlarm()).andReturn(alarmInfoList).anyTimes();
PowerMock.replayAll();
- Method method = DroolsEngine.class.getDeclaredMethod("init");
- method.setAccessible(true);
- method.invoke(droolsEngine);
-
PowerMock.verifyAll();
}
@@ -198,4 +208,30 @@ public class DroolsEngineTest { 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);
+
+ PowerMock.replayAll();
+ VesAlarm vesAlarm = Whitebox.invokeMethod(droolsEngine,"convertAlarmInfo2VesAlarm",alarmInfo);
+ PowerMock.verifyAll();
+
+ 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));
+ }
}
|