use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.
the class AlarmRepositoryFilterIT method testParmsLikeFilter.
@Test
@Transactional
@JUnitTemporaryDatabase
public void testParmsLikeFilter() {
OnmsDistPoller poller = m_dbPopulator.getDistPollerDao().whoami();
assertNotNull(poller);
final OnmsEvent event1 = new OnmsEvent();
event1.setDistPoller(poller);
event1.setEventCreateTime(new Date(1436881548292L));
event1.setEventTime(new Date(1436881548292L));
event1.setEventCreateTime(new Date(1437061537126L));
event1.setEventDescr("This is the description of a test event.");
event1.setEventDisplay("Y");
event1.setEventHost("127.0.0.1");
event1.setEventLog("Y");
event1.setEventLogMsg("Test Event Log Message");
event1.setEventSeverity(1);
event1.setEventSource("test");
event1.setEventTime(new Date(1437061537105L));
event1.setEventUei("uei.opennms.org/test");
event1.setIpAddr(InetAddressUtils.getInetAddress("192.168.1.1"));
event1.setNode(m_dbPopulator.getNode1());
event1.setServiceType(m_dbPopulator.getServiceTypeDao().findByName("ICMP"));
event1.setEventParameters(Lists.newArrayList(new OnmsEventParameter(event1, "url", "http://localhost:8980/opennms/rtc/post/Network+Interfaces", "string"), new OnmsEventParameter(event1, "user", "rtc", "string"), new OnmsEventParameter(event1, "passwd", "rtc", "string"), new OnmsEventParameter(event1, "catlabel", "Network Interfaces", "string")));
m_dbPopulator.getEventDao().saveOrUpdate(event1);
m_dbPopulator.getEventDao().flush();
final OnmsEvent event2 = new OnmsEvent();
event2.setDistPoller(poller);
event2.setEventCreateTime(new Date(1436881548292L));
event2.setEventTime(new Date(1436881548292L));
event2.setEventCreateTime(new Date(1437061537126L));
event2.setEventDescr("This is the description of a test event.");
event2.setEventDisplay("Y");
event2.setEventHost("127.0.0.1");
event2.setEventLog("Y");
event2.setEventLogMsg("Test Event Log Message");
event2.setEventSeverity(1);
event2.setEventSource("test");
event2.setEventTime(new Date(1437061537105L));
event2.setEventUei("uei.opennms.org/test");
event2.setIpAddr(InetAddressUtils.getInetAddress("192.168.1.1"));
event2.setNode(m_dbPopulator.getNode1());
event2.setServiceType(m_dbPopulator.getServiceTypeDao().findByName("ICMP"));
event2.setEventParameters(Lists.newArrayList(new OnmsEventParameter(event2, "componentType", "serviceElement", "string"), new OnmsEventParameter(event2, "url", "http://localhost:8980/opennms/rtc/post/Network+Interfaces", "string"), new OnmsEventParameter(event2, "user", "rtcbomb", "string"), new OnmsEventParameter(event2, "passwd", "rtc", "string"), new OnmsEventParameter(event2, "catlabel", "Network Interfaces", "string")));
m_dbPopulator.getEventDao().saveOrUpdate(event2);
m_dbPopulator.getEventDao().flush();
OnmsAlarm alarm = new OnmsAlarm();
alarm.setUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp");
alarm.setLastEvent(event1);
alarm.setSeverityId(3);
alarm.setDistPoller(poller);
alarm.setCounter(100);
alarm.setLastEvent(event1);
AlarmDao alarmDao = m_dbPopulator.getAlarmDao();
alarmDao.save(alarm);
alarmDao.flush();
OnmsAlarm alarm2 = new OnmsAlarm();
alarm2.setUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp");
alarm2.setLastEvent(event2);
alarm2.setSeverityId(3);
alarm2.setDistPoller(poller);
alarm2.setCounter(100);
alarm2.setLastEvent(event2);
alarmDao.save(alarm2);
alarmDao.flush();
EventParmLikeFilter eventParmFilter = new EventParmLikeFilter("user=rtc");
assertEquals("user=\"rtc\"", eventParmFilter.getTextDescription());
AlarmCriteria criteria = new AlarmCriteria(eventParmFilter);
OnmsAlarm[] alarms = m_daoAlarmRepo.getMatchingAlarms(AlarmUtil.getOnmsCriteria(criteria));
assertEquals(1, alarms.length);
}
use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.
the class TemporaryDatabaseExecutionListener method createNewDatabase.
private static TemporaryDatabase createNewDatabase(JUnitTemporaryDatabase jtd, String className, String methodName) throws Exception {
boolean useExisting = false;
if (jtd.useExistingDatabase() != null) {
useExisting = !jtd.useExistingDatabase().equals("");
}
final String dbName = useExisting ? jtd.useExistingDatabase() : null;
final TemporaryDatabase retval = ((jtd.tempDbClass()).getConstructor(String.class, Boolean.TYPE).newInstance(dbName, useExisting));
retval.setPopulateSchema(jtd.createSchema() && !useExisting);
if (className != null) {
retval.setClassName(className);
}
if (methodName != null) {
retval.setMethodName(methodName);
}
final StringBuilder b = new StringBuilder();
if (jtd.useExistingDatabase() != null && !"".equals(jtd.useExistingDatabase())) {
b.append("use existing database: " + jtd.useExistingDatabase() + " ");
}
b.append("reuse database: " + jtd.reuseDatabase());
retval.setTestDetails(b.toString());
retval.create();
return retval;
}
use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.
the class TemporaryDatabaseExecutionListener method prepareTestInstance.
@Override
public void prepareTestInstance(final TestContext testContext) throws Exception {
// System.err.println(String.format("TemporaryDatabaseExecutionListener.prepareTestInstance(%s); details: %s", testContext.hashCode(), testContext));
final JUnitTemporaryDatabase jtd = findAnnotation(testContext);
if (jtd == null) {
return;
}
m_database = m_databases.remove();
//
if (jtd.poolConnections()) {
JdbcDataSource ds = new JdbcDataSource();
ds.setDatabaseName(m_database.getTestDatabase());
ds.setUserName(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
ds.setPassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
ds.setUrl(System.getProperty(TemporaryDatabase.URL_PROPERTY, TemporaryDatabase.DEFAULT_URL) + m_database.getTestDatabase());
ds.setClassName(System.getProperty(TemporaryDatabase.DRIVER_PROPERTY, TemporaryDatabase.DEFAULT_DRIVER));
HikariCPConnectionFactory pool = new HikariCPConnectionFactory(ds);
// NMS-8911: Reduce the max connection lifetime so that HikariCP recycles
// connections more aggressively during tests
pool.setMaxLifetime(500);
DataSourceFactory.setInstance(pool);
} else {
DataSourceFactory.setInstance(m_database);
}
XADataSourceFactory.setInstance(m_database);
// System.err.println(String.format("TemporaryDatabaseExecutionListener.prepareTestInstance(%s) prepared db %s; details: %s", testContext.hashCode(), m_database.toString(), testContext));
// System.err.println("Temporary Database Name: " + m_database.getTestDatabase());
}
use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.
the class NotificationManagerIT method canMatchEventParametersWhenAcknowledgingNotices.
@Test
@JUnitTemporaryDatabase
public void canMatchEventParametersWhenAcknowledgingNotices() throws IOException, SQLException {
// Insert some event in the database with a few event parameters
OnmsEvent dbEvent = new OnmsEvent();
dbEvent.setDistPoller(m_distPollerDao.whoami());
dbEvent.setEventUei(EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI);
dbEvent.setEventCreateTime(new Date());
dbEvent.setEventLog("Y");
dbEvent.setEventDisplay("Y");
dbEvent.setEventSeverity(OnmsSeverity.CRITICAL.getId());
dbEvent.setEventSource("test");
dbEvent.setEventTime(new Date());
dbEvent.setNode(node1);
dbEvent.setEventParameters(Arrays.asList(new OnmsEventParameter(dbEvent, "some-parameter", "some-specific-value", "string"), new OnmsEventParameter(dbEvent, "some-other-parameter", "some-other-specific-value", "string")));
m_eventDao.save(dbEvent);
m_eventDao.flush();
// Create some notification referencing the event we just created
Notification notification = new Notification();
Map<String, String> params = new ImmutableMap.Builder<String, String>().put(NotificationManager.PARAM_TEXT_MSG, "some text message").put(NotificationManager.PARAM_NODE, node1.getNodeId()).put(NotificationManager.PARAM_INTERFACE, InetAddressUtils.toIpAddrString(ipInterfaceOnNode1.getIpAddress())).put(NotificationManager.PARAM_SERVICE, "ICMP").put("eventUEI", dbEvent.getEventUei()).put("eventID", Integer.toString(dbEvent.getId())).build();
m_notificationManager.insertNotice(1, params, "q1", notification);
final String[] parmMatchList = new String[] { "parm[some-parameter]", "parm[#2]" };
// Verify that we're able to match the the notice when we have the same parameters set
Event e = new EventBuilder(EventConstants.SERVICE_RESPONSIVE_EVENT_UEI, "test").addParam("some-parameter", "some-specific-value").addParam("some-other-parameter", "some-other-specific-value").getEvent();
Collection<Integer> eventIds = m_notificationManager.acknowledgeNotice(e, EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI, parmMatchList);
assertEquals(1, eventIds.size());
assertEquals(dbEvent.getId(), eventIds.iterator().next());
unacknowledgeAllNotices();
// It should not match when either of the event parameters are different
e = new EventBuilder(EventConstants.SERVICE_RESPONSIVE_EVENT_UEI, "test").addParam("some-parameter", "!some-specific-value").addParam("some-other-parameter", "some-other-specific-value").getEvent();
eventIds = m_notificationManager.acknowledgeNotice(e, EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI, parmMatchList);
assertEquals(0, eventIds.size());
unacknowledgeAllNotices();
e = new EventBuilder(EventConstants.SERVICE_RESPONSIVE_EVENT_UEI, "test").addParam("some-parameter", "some-specific-value").addParam("some-other-parameter", "!some-other-specific-value").getEvent();
eventIds = m_notificationManager.acknowledgeNotice(e, EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI, parmMatchList);
assertEquals(0, eventIds.size());
unacknowledgeAllNotices();
// It should not match when either of the event parameters are missing
e = new EventBuilder(EventConstants.SERVICE_RESPONSIVE_EVENT_UEI, "test").addParam("some-other-parameter", "some-other-specific-value").getEvent();
eventIds = m_notificationManager.acknowledgeNotice(e, EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI, parmMatchList);
assertEquals(0, eventIds.size());
unacknowledgeAllNotices();
e = new EventBuilder(EventConstants.SERVICE_RESPONSIVE_EVENT_UEI, "test").addParam("some-parameter", "some-specific-value").getEvent();
eventIds = m_notificationManager.acknowledgeNotice(e, EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI, parmMatchList);
assertEquals(0, eventIds.size());
unacknowledgeAllNotices();
// Now try matching on other fields without any event parameters
final String[] fieldMatchList = new String[] { "nodeid", "interfaceid", "serviceid" };
e = new EventBuilder(EventConstants.SERVICE_RESPONSIVE_EVENT_UEI, "test").setNodeid(node1.getId()).setInterface(ipInterfaceOnNode1.getIpAddress()).setService("ICMP").getEvent();
eventIds = m_notificationManager.acknowledgeNotice(e, EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI, fieldMatchList);
assertEquals(1, eventIds.size());
unacknowledgeAllNotices();
// Expect no match if we set different values responsive event
e = new EventBuilder(EventConstants.SERVICE_RESPONSIVE_EVENT_UEI, "test").setNodeid(node1.getId() + 1).setInterface(InetAddressUtils.UNPINGABLE_ADDRESS).setService("HTTP").getEvent();
eventIds = m_notificationManager.acknowledgeNotice(e, EventConstants.SERVICE_UNRESPONSIVE_EVENT_UEI, fieldMatchList);
assertEquals(0, eventIds.size());
unacknowledgeAllNotices();
}
use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.
the class VacuumdIT method testRunTrigger.
/**
* Simple test running a trigger.
*/
@Test
@JUnitTemporaryDatabase(tempDbClass = MockDatabase.class)
public final void testRunTrigger() throws InterruptedException {
Trigger trigger = VacuumdConfigFactory.getInstance().getTrigger("selectAll");
String triggerSql = trigger.getStatement().getContent();
MockUtil.println("Running trigger query: " + triggerSql);
int count = m_jdbcTemplate.queryForList(triggerSql).size();
AutomationProcessor ap = new AutomationProcessor(VacuumdConfigFactory.getInstance().getAutomation("cosmicClear"));
assertFalse("Testing the result rows:" + count + " with the trigger operator " + trigger.getOperator() + " against the required rows:" + trigger.getRowCount(), ap.getTrigger().triggerRowCheck(trigger.getRowCount(), trigger.getOperator(), count));
assertEquals(0, count);
}
Aggregations