Search in sources :

Example 1 with NotificationStrategy

use of org.opennms.netmgt.model.notifd.NotificationStrategy in project opennms by OpenNMS.

the class ClassExecutor method execute.

/**
     * {@inheritDoc}
     *
     * This method calls the send method of the specified class in
     */
@Override
public int execute(String className, List<Argument> arguments) {
    LOG.debug("Going for the class instance: {}", className);
    NotificationStrategy ns;
    try {
        ns = (NotificationStrategy) Class.forName(className).newInstance();
        LOG.debug("{} class created: {}", className, ns.getClass());
    } catch (Throwable e) {
        LOG.error("Execption creating notification strategy class: {}", className, e);
        return 1;
    }
    try {
        return ns.send(arguments);
    } catch (Throwable t) {
        LOG.error("Throwable received while sending message", t);
        return 1;
    }
}
Also used : NotificationStrategy(org.opennms.netmgt.model.notifd.NotificationStrategy)

Example 2 with NotificationStrategy

use of org.opennms.netmgt.model.notifd.NotificationStrategy in project opennms by OpenNMS.

the class MicroblogReplyNotificationStrategyTest method postNotice.

@Ignore
@Test
@Override
public void postNotice() {
    NotificationStrategy ns = new MicroblogReplyNotificationStrategy(m_daoConfigResource);
    List<Argument> arguments = configureArgs();
    Assert.assertEquals("NotificationStrategy should return 0 on success", 0, ns.send(arguments));
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) NotificationStrategy(org.opennms.netmgt.model.notifd.NotificationStrategy) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with NotificationStrategy

use of org.opennms.netmgt.model.notifd.NotificationStrategy in project opennms by OpenNMS.

the class MicroblogDMNotificationStrategyTest method postNotice.

@Ignore
@Test
@Override
public void postNotice() {
    NotificationStrategy ns = new MicroblogDMNotificationStrategy(m_daoConfigResource);
    List<Argument> arguments = configureArgs();
    Assert.assertEquals("NotificationStrategy should return 0 on success", 0, ns.send(arguments));
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) NotificationStrategy(org.opennms.netmgt.model.notifd.NotificationStrategy) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with NotificationStrategy

use of org.opennms.netmgt.model.notifd.NotificationStrategy in project opennms by OpenNMS.

the class SlackNotificationStrategyIT method testSendValidMessage.

/*
     * Test method for 'org.opennms.netmgt.notifd.MattermostNotificationStrategy.send(List)'
     */
@Test
@JUnitHttpServer(webapps = { @Webapp(context = "/hooks", path = "src/test/resources/MattermostNotificationStrategyTest") })
public void testSendValidMessage() {
    final int port = JUnitHttpServerExecutionListener.getPort();
    assertTrue(port > 0);
    try {
        final NotificationStrategy ns = new SlackNotificationStrategy();
        final List<Argument> arguments = new ArrayList<Argument>();
        // Set these properties. We will override them with Args on the first run.
        System.setProperty("org.opennms.netmgt.notifd.slack.webhookURL", "http://localhost:" + port + "/hooks/abunchofstuffthatidentifiesawebhook");
        System.setProperty("org.opennms.netmgt.notifd.slack.channel", "integrationtestsXX");
        System.setProperty("org.opennms.netmgt.notifd.slack.iconURL", "http://opennms.org/logo.pngXX");
        System.setProperty("org.opennms.netmgt.notifd.slack.iconEmoji", ":shipitXX:");
        System.setProperty("org.opennms.netmgt.notifd.slack.username", "opennmsXX");
        arguments.add(new Argument("url", null, "http://localhost:" + port + "/hooks/abunchofstuffthatidentifiesawebhook", false));
        arguments.add(new Argument("channel", null, "integrationtests", false));
        arguments.add(new Argument("username", null, "opennms", false));
        arguments.add(new Argument("iconurl", null, "http://opennms.org/logo.png", false));
        arguments.add(new Argument("iconemoji", null, ":shipit:", false));
        arguments.add(new Argument("-subject", null, "Test", false));
        arguments.add(new Argument("-tm", null, "This is only a test", false));
        int statusCode = ns.send(arguments);
        assertEquals(0, statusCode);
        JSONObject inputJson = MattermostNotificationStrategyTestServlet.getInputJson();
        assertNotNull(inputJson);
        assertEquals("opennms", inputJson.get("username"));
        assertEquals("*Test*\nThis is only a test", inputJson.get("text"));
        assertEquals("integrationtests", inputJson.get("channel"));
        assertEquals("http://opennms.org/logo.png", inputJson.get("icon_url"));
        assertEquals(":shipit:", inputJson.get("icon_emoji"));
        assertEquals(5, inputJson.size());
        // Now do it again, without the Args, and verify that the property values come out
        arguments.clear();
        arguments.add(new Argument("-subject", null, "Test again", false));
        arguments.add(new Argument("-tm", null, "This is only a second test", false));
        statusCode = ns.send(arguments);
        assertEquals(0, statusCode);
        inputJson = MattermostNotificationStrategyTestServlet.getInputJson();
        assertNotNull(inputJson);
        assertEquals("opennmsXX", inputJson.get("username"));
        assertEquals("*Test again*\nThis is only a second test", inputJson.get("text"));
        assertEquals("integrationtestsXX", inputJson.get("channel"));
        assertEquals("http://opennms.org/logo.pngXX", inputJson.get("icon_url"));
        assertEquals(":shipitXX:", inputJson.get("icon_emoji"));
        assertEquals(5, inputJson.size());
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Caught Exception: " + e.getMessage());
    }
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) NotificationStrategy(org.opennms.netmgt.model.notifd.NotificationStrategy) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 5 with NotificationStrategy

use of org.opennms.netmgt.model.notifd.NotificationStrategy in project opennms by OpenNMS.

the class SnmpTrapNotificationStrategyTest method testSendWithNamedHost.

/*
     * Test method for 'org.opennms.netmgt.notifd.SnmpTrapNotificationStrategy.send(List)'
     */
public void testSendWithNamedHost() {
    List<Argument> arguments = new ArrayList<Argument>();
    Argument arg = new Argument("trapHost", null, "localhost", false);
    arguments.add(arg);
    NotificationStrategy strategy = new SnmpTrapNotificationStrategy();
    strategy.send(arguments);
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) ArrayList(java.util.ArrayList) NotificationStrategy(org.opennms.netmgt.model.notifd.NotificationStrategy)

Aggregations

NotificationStrategy (org.opennms.netmgt.model.notifd.NotificationStrategy)9 Argument (org.opennms.netmgt.model.notifd.Argument)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 Ignore (org.junit.Ignore)3 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)3 JSONObject (org.json.simple.JSONObject)2