Search in sources :

Example 6 with JUnitTemporaryDatabase

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

the class SnmpAssetProvisioningAdapterFullIT method testUpdateNode.

@Test
// Relies on records created in @Before so we need a fresh database
@JUnitTemporaryDatabase
public void testUpdateNode() throws InterruptedException {
    AdapterOperationChecker verifyOperations = new AdapterOperationChecker(2);
    m_adapter.getOperationQueue().addListener(verifyOperations);
    try {
        OnmsNode node = m_nodeDao.get(NODE_ID);
        assertNotNull(node);
        int firstNodeId = node.getId();
        assertNull(node.getAssetRecord().getComment());
        m_adapter.addNode(firstNodeId);
        m_adapter.updateNode(firstNodeId);
        assertTrue(verifyOperations.enqueueLatch.await(4, TimeUnit.SECONDS));
        assertTrue(verifyOperations.dequeueLatch.await(4, TimeUnit.SECONDS));
        assertTrue(verifyOperations.executeLatch.await(4, TimeUnit.SECONDS));
        assertEquals(0, m_adapter.getOperationQueue().getOperationQueueForNode(firstNodeId).size());
        node = m_nodeDao.get(firstNodeId);
        assertNotNull(node);
        System.out.println("ID: " + node.getAssetRecord().getId());
        System.out.println("Comment: " + node.getAssetRecord().getComment());
        assertNotNull("AssetRecord comment is null", node.getAssetRecord().getComment());
        assertEquals(EXPECTED_COMMENT_FIELD, node.getAssetRecord().getComment());
    } finally {
        m_adapter.getOperationQueue().removeListener(verifyOperations);
    }
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 7 with JUnitTemporaryDatabase

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

the class AlarmRestServiceIT method testAlarmUpdates.

@Test
@Transactional
@JUnitTemporaryDatabase
public void testAlarmUpdates() throws Exception {
    createAlarm(OnmsSeverity.MAJOR);
    OnmsAlarm alarm = getLastAlarm();
    alarm.setAlarmAckTime(null);
    alarm.setAlarmAckUser(null);
    getAlarmDao().saveOrUpdate(alarm);
    final Integer alarmId = alarm.getId();
    sendPut("/alarms", "ack=true&alarmId=" + alarmId, 204);
    String xml = sendRequest(GET, "/alarms/" + alarmId, 200);
    assertTrue(xml.contains("ackUser>admin<"));
    sendPut("/alarms/" + alarmId, "clear=true", 204);
    xml = sendRequest(GET, "/alarms/" + alarmId, 200);
    assertTrue(xml.contains("severity=\"CLEARED\""));
    sendPut("/alarms/" + alarmId, "escalate=true", 204);
    xml = sendRequest(GET, "/alarms/" + alarmId, 200);
    assertTrue(xml.contains("severity=\"NORMAL\""));
    sendPut("/alarms/" + alarmId, "ticketId=12345", 204);
    xml = sendRequest(GET, "/alarms/" + alarmId, 200);
    assertTrue(xml.contains("<troubleTicket>12345</troubleTicket>"));
    sendPut("/alarms/" + alarmId, "ticketState=UPDATE_PENDING", 204);
    xml = sendRequest(GET, "/alarms/" + alarmId, 200);
    sendPut("/alarms/" + alarmId, "ticketState=UPDATE_PENDING", 204);
    alarm = getLastAlarm();
    alarm.setSeverity(OnmsSeverity.MAJOR);
    alarm.setAlarmAckTime(null);
    alarm.setAlarmAckUser(null);
    getAlarmDao().saveOrUpdate(alarm);
    // Log in as a normal REST user and attempt to resolve an alarm as a different user.
    // This should fail with a 403 forbidden.
    setUser("foo", new String[] { "ROLE_REST" });
    sendPut("/alarms/" + alarmId, "ack=true&ackUser=bar", 403);
}
Also used : OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with JUnitTemporaryDatabase

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

the class AlarmRestServiceIT method testAlarmClearAsUser.

@Test
@JUnitTemporaryDatabase
public void testAlarmClearAsUser() throws Exception {
    setUser("ranger", new String[] { "ROLE_USER" });
    final OnmsAlarm alarm = createAlarm(OnmsSeverity.MAJOR);
    sendRequest(PUT, "/alarms/" + alarm.getId(), parseParamData("clear=true"), 204);
}
Also used : OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 9 with JUnitTemporaryDatabase

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

the class AcknowledgmentRestServiceIT method testAcknowlegeAlarm.

@Test
@JUnitTemporaryDatabase
public void testAcknowlegeAlarm() throws Exception {
    final Pattern p = Pattern.compile("^.*<ackTime>(.*?)</ackTime>.*$", Pattern.DOTALL & Pattern.MULTILINE);
    sendData(POST, MediaType.APPLICATION_FORM_URLENCODED, "/acks", "alarmId=1&action=ack", 200);
    // Try to fetch a non-existent ack, get 404 Not Found
    String xml = sendRequest(GET, "/acks/999999", 404);
    xml = sendRequest(GET, "/acks/count", 200);
    // {@link DatabasePopulator} adds one ack so we have 2 total
    assertEquals("2", xml);
    Integer newAckId = null;
    for (OnmsAcknowledgment ack : getXmlObject(JaxbUtils.getContextFor(OnmsAcknowledgmentCollection.class), "/acks", 200, OnmsAcknowledgmentCollection.class).getObjects()) {
        if (AckType.UNSPECIFIED.equals(ack.getAckType())) {
            // Ack from DatabasePopulator
            assertEquals(AckAction.UNSPECIFIED, ack.getAckAction());
            assertEquals("admin", ack.getAckUser());
        } else if (AckType.ALARM.equals(ack.getAckType())) {
            // Ack that we just created
            assertEquals(new Integer(1), ack.getRefId());
            assertEquals(AckAction.ACKNOWLEDGE, ack.getAckAction());
            newAckId = ack.getId();
        } else {
            fail("Unrecognized alarm type: " + ack.getAckType().toString());
        }
    }
    if (newAckId == null) {
        fail("Couldn't determine ID of new ack");
    }
    xml = sendRequest(GET, "/acks/" + newAckId, 200);
    xml = sendRequest(GET, "/alarms/1", new HashMap<String, String>(), 200);
    Matcher m = p.matcher(xml);
    assertTrue(m.matches());
    assertTrue(m.group(1).length() > 0);
    sendData(POST, MediaType.APPLICATION_FORM_URLENCODED, "/acks", "alarmId=1&action=unack", 200);
    xml = sendRequest(GET, "/alarms/1", new HashMap<String, String>(), 200);
    m = p.matcher(xml);
    assertFalse(m.matches());
    // POST with no argument, this will ack by default
    sendData(POST, MediaType.APPLICATION_FORM_URLENCODED, "/acks", "alarmId=1", 200);
    xml = sendRequest(GET, "/alarms/1", new HashMap<String, String>(), 200);
    m = p.matcher(xml);
    assertTrue(m.matches());
}
Also used : Pattern(java.util.regex.Pattern) OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) OnmsAcknowledgmentCollection(org.opennms.netmgt.model.OnmsAcknowledgmentCollection) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 10 with JUnitTemporaryDatabase

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

the class AcknowledgmentRestServiceIT method testAcknowlegeNotification.

@Test
@JUnitTemporaryDatabase
public void testAcknowlegeNotification() throws Exception {
    final Pattern p = Pattern.compile("^.*<answeredBy>(.*?)</answeredBy>.*$", Pattern.DOTALL & Pattern.MULTILINE);
    sendData(POST, MediaType.APPLICATION_FORM_URLENCODED, "/acks", "notifId=1&action=ack", 200);
    String xml = sendRequest(GET, "/notifications/1", new HashMap<String, String>(), 200);
    Matcher m = p.matcher(xml);
    assertTrue(m.matches());
    assertTrue(m.group(1).equals("admin"));
    sendData(POST, MediaType.APPLICATION_FORM_URLENCODED, "/acks", "notifId=1&action=unack", 200);
    xml = sendRequest(GET, "/notifications/1", new HashMap<String, String>(), 200);
    m = p.matcher(xml);
    assertFalse(m.matches());
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) 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