Search in sources :

Example 26 with MockNode

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);
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) MockNode(org.opennms.netmgt.mock.MockNode) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 27 with MockNode

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());
}
Also used : MockNode(org.opennms.netmgt.mock.MockNode)

Example 28 with MockNode

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());
}
Also used : MockNode(org.opennms.netmgt.mock.MockNode)

Example 29 with MockNode

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);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) MockNode(org.opennms.netmgt.mock.MockNode) CountDownLatch(java.util.concurrent.CountDownLatch) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) Test(org.junit.Test)

Example 30 with MockNode

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);
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) NorthboundAlarm(org.opennms.netmgt.alarmd.api.NorthboundAlarm) Event(org.opennms.netmgt.xml.event.Event) MockNode(org.opennms.netmgt.mock.MockNode) AlarmData(org.opennms.netmgt.xml.event.AlarmData) Test(org.junit.Test)

Aggregations

MockNode (org.opennms.netmgt.mock.MockNode)52 Test (org.junit.Test)42 Event (org.opennms.netmgt.xml.event.Event)21 MockService (org.opennms.netmgt.mock.MockService)16 MockInterface (org.opennms.netmgt.mock.MockInterface)7 Date (java.util.Date)5 ResultSet (java.sql.ResultSet)4 PollAnticipator (org.opennms.netmgt.mock.PollAnticipator)4 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 MockNetwork (org.opennms.netmgt.mock.MockNetwork)3 MockVisitor (org.opennms.netmgt.mock.MockVisitor)3 MockVisitorAdapter (org.opennms.netmgt.mock.MockVisitorAdapter)3 Ignore (org.junit.Ignore)2 Querier (org.opennms.core.utils.Querier)2 RowCallbackHandler (org.springframework.jdbc.core.RowCallbackHandler)2 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Before (org.junit.Before)1