Search in sources :

Example 51 with JUnitTemporaryDatabase

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

the class TemporaryDatabaseExecutionListener method afterTestMethod.

@Override
public void afterTestMethod(final TestContext testContext) throws Exception {
    Throwable closeThrowable = null;
    // System.err.println(String.format("TemporaryDatabaseExecutionListener.afterTestMethod(%s)", testContext));
    final JUnitTemporaryDatabase jtd = findAnnotation(testContext);
    if (jtd == null) {
        return;
    }
    // Close down the data sources that are referenced by the static DataSourceFactory helper classes
    try {
        DataSourceFactory.close();
        XADataSourceFactory.close();
    } catch (Throwable t) {
        closeThrowable = t;
    }
    try {
        if (m_createNewDatabases && m_database != null) {
            m_database.drop();
        }
    } finally {
        // 
        if (jtd.dirtiesContext()) {
            testContext.markApplicationContextDirty(HierarchyMode.CURRENT_LEVEL);
            testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
        } else {
            if (m_database != m_databases.peek()) {
                testContext.markApplicationContextDirty(HierarchyMode.CURRENT_LEVEL);
                testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
            }
        }
    }
    if (closeThrowable != null) {
        throw new Exception("Caught a Throwable while closing database connections after test. Pickup after yourself! " + closeThrowable, closeThrowable);
    }
}
Also used : JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 52 with JUnitTemporaryDatabase

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

the class ProvisionerIT method testImportWithGeoData.

@Test(timeout = 300000)
@JUnitTemporaryDatabase
public void testImportWithGeoData() throws Exception {
    importFromResource("classpath:/tec_dump.xml", Boolean.TRUE.toString());
    final NodeDao nodeDao = getNodeDao();
    OnmsNode node = nodeDao.findByForeignId("empty", "4243");
    nodeDao.initialize(node.getAssetRecord());
    nodeDao.initialize(node.getAssetRecord().getGeolocation());
    OnmsGeolocation geolocation = new OnmsGeolocation();
    geolocation.setAddress1("220 Chatham Business Dr.");
    geolocation.setCity("Pittsboro");
    geolocation.setState("NC");
    geolocation.setZip("27312");
    geolocation.setLatitude(35.715723);
    geolocation.setLongitude(-79.162261);
    node.getAssetRecord().setGeolocation(geolocation);
    nodeDao.saveOrUpdate(node);
    nodeDao.flush();
    node = nodeDao.findByForeignId("empty", "4243");
    geolocation = node.getAssetRecord().getGeolocation();
    assertNotNull(geolocation.getLatitude());
    assertNotNull(geolocation.getLongitude());
    assertEquals(Float.valueOf(35.715723f).doubleValue(), geolocation.getLatitude().doubleValue(), 0.1d);
    assertEquals(Float.valueOf(-79.162261f).doubleValue(), geolocation.getLongitude().doubleValue(), 0.1d);
    System.err.println("=================================================================BLEARGH");
    importFromResource("classpath:/tec_dump.xml", Boolean.TRUE.toString());
    node = nodeDao.findByForeignId("empty", "4243");
    geolocation = node.getAssetRecord().getGeolocation();
    // Ensure it is reset
    assertNull(geolocation.asAddressString());
    assertNull(geolocation.getLatitude());
    assertNull(geolocation.getLongitude());
}
Also used : MockNodeDao(org.opennms.netmgt.dao.mock.MockNodeDao) NodeDao(org.opennms.netmgt.dao.api.NodeDao) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 53 with JUnitTemporaryDatabase

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

the class ProvisionerIT method testImportWithNodeCategoryEvents.

@Test(timeout = 300000)
// Relies on records created in @Before so we need a fresh database
@JUnitTemporaryDatabase
public void testImportWithNodeCategoryEvents() throws Exception {
    final int nextNodeId = m_nodeDao.getNextNodeId();
    final MockNetwork network = new MockNetwork();
    final MockNode node = network.addNode(nextNodeId, "test");
    network.addInterface("172.16.1.1");
    network.addService("ICMP");
    anticipateCreationEvents(node);
    m_eventAnticipator.anticipateEvent(getNodeCategoryEvent(nextNodeId, "test"));
    m_eventAnticipator.anticipateEvent(new EventBuilder(EventConstants.NODE_UPDATED_EVENT_UEI, "Test").setNodeid(nextNodeId).getEvent());
    // we should not get category update events on a re-import now, that happens during the scan phase
    // m_eventAnticipator.anticipateEvent(getNodeCategoryEvent(nextNodeId, "test"));
    importFromResource("classpath:/requisition_with_node_categories.xml", Boolean.TRUE.toString());
    importFromResource("classpath:/requisition_with_node_categories_changed.xml", Boolean.TRUE.toString());
    m_eventAnticipator.verifyAnticipated();
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) MockNetwork(org.opennms.netmgt.mock.MockNetwork) MockNode(org.opennms.netmgt.mock.MockNode) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 54 with JUnitTemporaryDatabase

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

the class AlarmStatsRestServiceIT method testGetAlarmStatsJson.

/**
 * TODO: Doesn't test firstAutomationTime, lastAutomationTime, reductionKey,
 * reductionKeyMemo, suppressedTime, suppressedUntil, clearKey, or stickyMemo
 * fields.
 */
@Test
@JUnitTemporaryDatabase
public void testGetAlarmStatsJson() throws Exception {
    createAlarm(OnmsSeverity.CLEARED, "admin");
    createAlarm(OnmsSeverity.MAJOR, "admin");
    createAlarm(OnmsSeverity.CRITICAL, "admin");
    createAlarm(OnmsSeverity.CRITICAL, null);
    createAlarm(OnmsSeverity.MINOR, null);
    createAlarm(OnmsSeverity.NORMAL, null);
    // GET all users
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, "/stats/alarms");
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);
    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(IOUtils.toString(new FileInputStream("src/test/resources/v1/stats_alarms.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}
Also used : JSONObject(org.json.JSONObject) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FileInputStream(java.io.FileInputStream) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 55 with JUnitTemporaryDatabase

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

the class AvailabilityRestServiceIT method testGetAvailabilityJson.

@Test
@JUnitTemporaryDatabase
public void testGetAvailabilityJson() throws Exception {
    String url = "/availability";
    // GET all items
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, url);
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);
    // TODO: The comment and last-updated fields are blank in the objects that are
    // fetched. Figure out how to get them to populate so that we can test serialization
    // of those values.
    // 
    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(IOUtils.toString(new FileInputStream("src/test/resources/v1/availability.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
    // GET node item
    jsonRequest = createRequest(m_servletContext, GET, url + "/nodes/" + m_populator.getNode1().getId());
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    json = sendRequest(jsonRequest, 200);
    restObject = new JSONObject(json);
    expectedObject = new JSONObject(IOUtils.toString(new FileInputStream("src/test/resources/v1/availability_node.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}
Also used : JSONObject(org.json.JSONObject) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FileInputStream(java.io.FileInputStream) 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