use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.
the class AlarmdIT method changeFields.
@Test
public void changeFields() throws InterruptedException, SQLException {
assertEmptyAlarmTable();
String reductionKey = "testUpdateField";
MockNode node1 = m_mockNetwork.getNode(1);
//Verify we have the default alarm
sendNodeDownEvent(reductionKey, node1);
int severity = m_jdbcTemplate.queryForObject("select severity from alarms a where a.reductionKey = ?", new Object[] { reductionKey }, Integer.class).intValue();
assertEquals(OnmsSeverity.MAJOR, OnmsSeverity.get(severity));
//Store the original logmsg from the original alarm (we are about to test changing it with subsequent alarm reduction)
String defaultLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet results) throws SQLException, DataAccessException {
results.next();
int row = results.getRow();
boolean isLast = results.isLast();
boolean isFirst = results.isFirst();
if (row != 1 && !isLast && !isFirst) {
throw new SQLException("Row count is not = 1. There should only be one row returned from the query: \n" + results.getStatement());
}
return results.getString(1);
}
});
assertTrue("The logmsg column should not be null", defaultLogMsg != null);
//Duplicate the alarm but change the severity and verify the change
sendNodeDownEventWithUpdateFieldSeverity(reductionKey, node1, OnmsSeverity.CRITICAL);
severity = m_jdbcTemplate.queryForObject("select severity from alarms", Integer.class).intValue();
assertEquals("Severity should now be Critical", OnmsSeverity.CRITICAL, OnmsSeverity.get(severity));
//Duplicate the alarm but don't force the change of severity
sendNodeDownEvent(reductionKey, node1);
severity = m_jdbcTemplate.queryForObject("select severity from alarms", Integer.class).intValue();
assertEquals("Severity should still be Critical", OnmsSeverity.CRITICAL, OnmsSeverity.get(severity));
//Duplicate the alarm and change the logmsg
sendNodeDownEventChangeLogMsg(reductionKey, node1, "new logMsg");
String newLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet results) throws SQLException, DataAccessException {
results.next();
return results.getString(1);
}
});
assertEquals("new logMsg", newLogMsg);
assertTrue(!newLogMsg.equals(defaultLogMsg));
//Duplicate the alarm but force logmsg to not change (lggmsg field is updated by default)
sendNodeDownEventDontChangeLogMsg(reductionKey, node1, "newer logMsg");
newLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet results) throws SQLException, DataAccessException {
results.next();
return results.getString(1);
}
});
assertTrue("The logMsg should not have changed.", !"newer logMsg".equals(newLogMsg));
assertEquals("The logMsg should still be equal to the previous update.", "new logMsg", newLogMsg);
//Duplicate the alarm with the default configuration and verify the logmsg has changed (as is the default behavior
//for this field)
sendNodeDownEvent(reductionKey, node1);
newLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet results) throws SQLException, DataAccessException {
results.next();
return results.getString(1);
}
});
assertTrue("The logMsg should have changed.", !"new logMsg".equals(newLogMsg));
assertEquals("The logMsg should new be the default logMsg.", newLogMsg, defaultLogMsg);
}
use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.
the class VacuumdIT method bringNodeDownCreatingEvent.
private void bringNodeDownCreatingEvent(int nodeid) {
MockNode node = m_network.getNode(nodeid);
m_eventdIpcMgr.sendNow(node.createDownEvent());
}
use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.
the class VacuumdIT method bringNodeUpCreatingEvent.
private void bringNodeUpCreatingEvent(int nodeid) {
MockNode node = m_network.getNode(nodeid);
m_eventdIpcMgr.sendNow(node.createUpEvent());
}
use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.
the class AlarmdIT method testPersistManyAlarmsAtOnce.
@Test
public void testPersistManyAlarmsAtOnce() throws InterruptedException {
int numberOfAlarmsToReduce = 10;
//there should be no alarms in the alarms table
assertEmptyAlarmTable();
final String reductionKey = "countThese";
final MockNode node = m_mockNetwork.getNode(1);
final long millis = System.currentTimeMillis() + 2500;
final CountDownLatch signal = new CountDownLatch(numberOfAlarmsToReduce);
for (int i = 1; i <= numberOfAlarmsToReduce; i++) {
MockUtil.println("Creating Runnable: " + i + " of " + numberOfAlarmsToReduce + " events to reduce.");
class EventRunner implements Runnable {
@Override
public void run() {
try {
while (System.currentTimeMillis() < millis) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
MockUtil.println(e.getMessage());
}
}
sendNodeDownEvent(reductionKey, node);
} catch (Throwable t) {
t.printStackTrace();
} finally {
signal.countDown();
}
}
}
Runnable r = new EventRunner();
Thread p = new Thread(r);
p.start();
}
signal.await();
//this should be the first occurrence of this alarm
//there should be 1 alarm now
int rowCount = m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue();
Integer counterColumn = m_jdbcTemplate.queryForObject("select counter from alarms where reductionKey = ?", new Object[] { reductionKey }, Integer.class).intValue();
MockUtil.println("rowcCount is: " + rowCount + ", expected 1.");
MockUtil.println("counterColumn is: " + counterColumn + ", expected " + numberOfAlarmsToReduce);
assertEquals(1, rowCount);
if (numberOfAlarmsToReduce != counterColumn) {
final List<Integer> reducedEvents = new ArrayList<Integer>();
m_jdbcTemplate.query("select eventid from events where alarmID is not null", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
reducedEvents.add(rs.getInt(1));
}
});
Collections.sort(reducedEvents);
final List<Integer> nonReducedEvents = new ArrayList<Integer>();
m_jdbcTemplate.query("select eventid from events where alarmID is null", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
nonReducedEvents.add(rs.getInt(1));
}
});
Collections.sort(nonReducedEvents);
fail("number of alarms to reduce (" + numberOfAlarmsToReduce + ") were not reduced into a single alarm (instead the counter column reads " + counterColumn + "); " + "events that were reduced: " + StringUtils.collectionToCommaDelimitedString(reducedEvents) + "; events that were not reduced: " + StringUtils.collectionToCommaDelimitedString(nonReducedEvents));
}
Integer alarmId = m_jdbcTemplate.queryForObject("select alarmId from alarms where reductionKey = ?", new Object[] { reductionKey }, Integer.class).intValue();
rowCount = m_jdbcTemplate.queryForObject("select count(*) from events where alarmid = ?", new Object[] { alarmId }, Integer.class).intValue();
MockUtil.println(String.valueOf(rowCount) + " of events with alarmid: " + alarmId);
// assertEquals(numberOfAlarmsToReduce, rowCount);
rowCount = m_jdbcTemplate.queryForObject("select count(*) from events where alarmid is null", Integer.class).intValue();
MockUtil.println(String.valueOf(rowCount) + " of events with null alarmid");
assertEquals(10, rowCount);
}
use of org.opennms.netmgt.mock.MockNode in project opennms by OpenNMS.
the class AlarmdIT method testNorthbounder.
@Test
public void testNorthbounder() throws Exception {
assertTrue(m_northbounder.isInitialized());
assertTrue(m_northbounder.getAlarms().isEmpty());
final EventBuilder bldr = new EventBuilder("testNoLogmsg", "AlarmdTest");
bldr.setAlarmData(new AlarmData());
bldr.setLogMessage("This is a test.");
final Event event = bldr.getEvent();
event.setDbid(17);
MockNode node = m_mockNetwork.getNode(1);
sendNodeDownEvent("%nodeid%", node);
final List<NorthboundAlarm> alarms = m_northbounder.getAlarms();
assertTrue(alarms.size() > 0);
}
Aggregations