Search in sources :

Example 36 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class LatencyStoringServiceMonitorAdaptorIT method testThresholds.

@Test
@JUnitTemporaryDatabase(tempDbClass = MockDatabase.class)
public void testThresholds() throws Exception {
    EventBuilder bldr = new EventBuilder(EventConstants.HIGH_THRESHOLD_EVENT_UEI, "LatencyStoringServiceMonitorAdaptorTest");
    bldr.setNodeid(1);
    bldr.setInterface(addr("127.0.0.1"));
    bldr.setService("ICMP");
    m_eventIpcManager.getEventAnticipator().anticipateEvent(bldr.getEvent());
    bldr = new EventBuilder(EventConstants.HIGH_THRESHOLD_REARM_EVENT_UEI, "LatencyStoringServiceMonitorAdaptorTest");
    bldr.setNodeid(1);
    bldr.setInterface(addr("127.0.0.1"));
    bldr.setService("ICMP");
    m_eventIpcManager.getEventAnticipator().anticipateEvent(bldr.getEvent());
    // This should emulate a trigger and a rearm
    executeThresholdTest(new Double[] { 100.0, 10.0 });
    m_eventIpcManager.getEventAnticipator().verifyAnticipated();
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 37 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class TemporaryDatabaseExecutionListener method beforeTestClass.

@Override
public void beforeTestClass(final TestContext testContext) throws Exception {
    TemporaryDatabasePostgreSQL.failIfUnitTest();
    // Fire up a thread pool for each CPU to create test databases
    ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    final JUnitTemporaryDatabase classJtd = testContext.getTestClass().getAnnotation(JUnitTemporaryDatabase.class);
    final Future<TemporaryDatabase> classDs;
    if (classJtd != null) {
        classDs = pool.submit(new CreateNewDatabaseCallable(classJtd, testContext.getTestClass().getName(), null));
        if (classJtd.reuseDatabase() == false) {
            m_createNewDatabases = true;
        }
    } else {
        classDs = null;
    }
    List<Future<TemporaryDatabase>> futures = new ArrayList<Future<TemporaryDatabase>>();
    for (Method method : getOrderedTestMethods(testContext.getTestClass())) {
        if (method != null) {
            final JUnitTemporaryDatabase methodJtd = method.getAnnotation(JUnitTemporaryDatabase.class);
            boolean methodHasTest = method.getAnnotation(Test.class) != null;
            if (methodHasTest) {
                // If there is a method-specific annotation, use it to create the temporary database
                if (methodJtd != null) {
                    // Create a new database based on the method-specific annotation
                    Future<TemporaryDatabase> submit = pool.submit(new CreateNewDatabaseCallable(methodJtd, testContext.getTestClass().getName(), method.getName()));
                    Assert.notNull(submit, "pool.submit(new CreateNewDatabaseCallable(methodJtd = " + methodJtd + ")");
                    futures.add(submit);
                } else if (classJtd != null) {
                    if (m_createNewDatabases) {
                        // Create a new database based on the test class' annotation
                        Future<TemporaryDatabase> submit = pool.submit(new CreateNewDatabaseCallable(classJtd, testContext.getTestClass().getName(), method.getName()));
                        Assert.notNull(submit, "pool.submit(new CreateNewDatabaseCallable(classJtd = " + classJtd + ")");
                        futures.add(submit);
                    } else {
                        // Reuse the database based on the test class' annotation
                        Assert.notNull(classDs, "classDs");
                        futures.add(classDs);
                    }
                }
            }
        }
    }
    for (Future<TemporaryDatabase> db : futures) {
        m_databases.add(db.get());
    }
}
Also used : Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Method(java.lang.reflect.Method) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 38 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class TemporaryDatabaseExecutionListener method findAnnotation.

private static JUnitTemporaryDatabase findAnnotation(final TestContext testContext) {
    JUnitTemporaryDatabase jtd = null;
    final Method testMethod = testContext.getTestMethod();
    if (testMethod != null) {
        jtd = testMethod.getAnnotation(JUnitTemporaryDatabase.class);
    }
    if (jtd == null) {
        final Class<?> testClass = testContext.getTestClass();
        jtd = testClass.getAnnotation(JUnitTemporaryDatabase.class);
    }
    return jtd;
}
Also used : JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) Method(java.lang.reflect.Method)

Example 39 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class MigratorIT method testMultipleChangelogs.

@Test
@JUnitTemporaryDatabase(createSchema = false)
public void testMultipleChangelogs() throws Exception {
    assertFalse(changelogExists());
    final Migration migration = new Migration();
    migration.setAdminUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setAdminPassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setDatabaseUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setDatabasePassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setChangeLog("changelog.xml");
    final Migrator m = new Migrator();
    m.setDataSource(m_dataSource);
    m.setAdminDataSource(m_dataSource);
    m.setValidateDatabaseVersion(false);
    m.setCreateUser(false);
    m.setCreateDatabase(false);
    // from the classpath
    for (final Resource resource : getTestResources()) {
        URI uri = resource.getURI();
        if (uri.getScheme().equals("jar") && !uri.toString().contains("test-api.schema"))
            continue;
        if (uri.getScheme().equals("file") && !uri.toString().contains("test-api/schema"))
            continue;
        LOG.info("=== found resource: {} ===", resource);
        migration.setAccessor(new ExistingResourceAccessor(resource));
        m.migrate(migration);
    }
    final List<ChangelogEntry> ids = getChangelogEntries();
    assertTrue(ids.size() > 0);
    assertEquals("test-api.schema.a", ids.get(0).getId());
    assertEquals("test-api.schema.b", ids.get(1).getId());
}
Also used : Migration(org.opennms.core.schema.Migration) Resource(org.springframework.core.io.Resource) Migrator(org.opennms.core.schema.Migrator) URI(java.net.URI) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 40 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class MigratorIT method testRealChangelog.

@Test
@JUnitTemporaryDatabase(createSchema = false)
public void testRealChangelog() throws Exception {
    assertFalse(changelogExists());
    final Migration migration = new Migration();
    migration.setAdminUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setAdminPassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setDatabaseUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setDatabasePassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setChangeLog("changelog.xml");
    final Migrator m = new Migrator();
    m.setDataSource(m_dataSource);
    m.setAdminDataSource(m_dataSource);
    m.setValidateDatabaseVersion(false);
    m.setCreateUser(false);
    m.setCreateDatabase(false);
    // from the classpath
    for (final Resource resource : getRealChangelog()) {
        LOG.info("=== found resource: {} ===", resource);
        migration.setAccessor(new ExistingResourceAccessor(resource));
        m.migrate(migration);
    }
    final List<ChangelogEntry> ids = getChangelogEntries();
    assertTrue(ids.size() > 0);
    // Check to make sure some of the changelogs ran
    assertTrue(ids.stream().anyMatch(id -> "17.0.0-remove-legacy-ipinterface-composite-key-fields".equals(id.getId())));
    assertTrue(ids.stream().anyMatch(id -> "17.0.0-remove-legacy-outages-composite-key-fields".equals(id.getId())));
}
Also used : Migrator(org.opennms.core.schema.Migrator) Connection(java.sql.Connection) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) MigrationException(org.opennms.core.schema.MigrationException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataSource(javax.sql.DataSource) URI(java.net.URI) Before(org.junit.Before) Migration(org.opennms.core.schema.Migration) Resource(org.springframework.core.io.Resource) Logger(org.slf4j.Logger) ResourceLoader(org.springframework.core.io.ResourceLoader) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) IOException(java.io.IOException) Test(org.junit.Test) PreparedStatement(java.sql.PreparedStatement) ApplicationContext(org.springframework.context.ApplicationContext) MockLogAppender(org.opennms.core.test.MockLogAppender) List(java.util.List) OpenNMSJUnit4ClassRunner(org.opennms.core.test.OpenNMSJUnit4ClassRunner) Assert.assertFalse(org.junit.Assert.assertFalse) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Assert.assertEquals(org.junit.Assert.assertEquals) Migration(org.opennms.core.schema.Migration) Resource(org.springframework.core.io.Resource) Migrator(org.opennms.core.schema.Migrator) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Aggregations

JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)109 Test (org.junit.Test)105 OnmsNode (org.opennms.netmgt.model.OnmsNode)21 Date (java.util.Date)20 JSONObject (org.json.JSONObject)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)18 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)17 Transactional (org.springframework.transaction.annotation.Transactional)14 FileInputStream (java.io.FileInputStream)10 AlarmCriteria (org.opennms.web.alarm.filter.AlarmCriteria)10 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)9 OnmsOutage (org.opennms.netmgt.model.OnmsOutage)8 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)7 AlarmIdFilter (org.opennms.web.alarm.filter.AlarmIdFilter)7 HashMap (java.util.HashMap)6 OutageCriteria (org.opennms.web.outage.filter.OutageCriteria)6 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 JSONArray (org.json.JSONArray)4 AcknowledgedByFilter (org.opennms.web.alarm.filter.AcknowledgedByFilter)4