Search in sources :

Example 21 with Argument

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

the class JavaMailNotificationStrategy method buildMessage.

/**
 * This method extracts the to, subject, and message text from the
 * parameters passed in the notification.
 *
 * @param arguments
 * @throws JavaMailerException
 */
private JavaMailer buildMessage(List<Argument> arguments) throws JavaMailerException {
    JavaMailer jm = new JavaMailer();
    for (int i = 0; i < arguments.size(); i++) {
        Argument arg = arguments.get(i);
        LOG.debug("Current arg switch: {} of {} is: {}", i, arguments.size(), arg.getSwitch());
        LOG.debug("Current arg  value: {} of {} is: {}", i, arguments.size(), arg.getValue());
        /*
             * Note: The recipient gets set by whichever of the two switches:
             * (PARAM_EMAIL or PARAM_PAGER_EMAIL) are specified last in the
             * notificationCommands.xml file
             * 
             * And the message body will get set to whichever is set last
             * (PARAM_NUM_MSG or PARAM_TEXT_MSG)
             */
        if (NotificationManager.PARAM_EMAIL.equals(arg.getSwitch())) {
            LOG.debug("Found: PARAM_EMAIL");
            jm.setTo(arg.getValue());
        } else if (NotificationManager.PARAM_PAGER_EMAIL.equals(arg.getSwitch())) {
            LOG.debug("Found: PARAM_PAGER_EMAIL");
            jm.setTo(arg.getValue());
        } else if (NotificationManager.PARAM_SUBJECT.equals(arg.getSwitch())) {
            LOG.debug("Found: PARAM_SUBJECT");
            jm.setSubject(arg.getValue());
        } else if (NotificationManager.PARAM_NUM_MSG.equals(arg.getSwitch())) {
            LOG.debug("Found: PARAM_NUM_MSG");
            jm.setMessageText(arg.getValue());
        } else if (NotificationManager.PARAM_TEXT_MSG.equals(arg.getSwitch())) {
            LOG.debug("Found: PARAM_TEXT_MSG");
            jm.setMessageText(arg.getValue());
        }
    }
    return jm;
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) JavaMailer(org.opennms.javamail.JavaMailer)

Example 22 with Argument

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

the class MicroblogNotificationStrategy method buildMessageBody.

/**
 * <p>buildMessageBody</p>
 *
 * @param arguments a {@link java.util.List} object.
 * @return a {@link java.lang.String} object.
 */
protected String buildMessageBody(List<Argument> arguments) {
    String messageBody = null;
    // Support PARAM_TEXT_MSG and PARAM_NUM_MSG but prefer PARAM_TEXT_MSG
    for (Argument arg : arguments) {
        if (NotificationManager.PARAM_TEXT_MSG.equals(arg.getSwitch())) {
            messageBody = arg.getValue();
        } else if (NotificationManager.PARAM_NUM_MSG.equals(arg.getSwitch())) {
            if (messageBody == null)
                messageBody = arg.getValue();
        }
    }
    if (messageBody == null) {
        // FIXME We should have a better Exception to use here for configuration problems
        throw new IllegalArgumentException("No message specified, but is required");
    }
    // Collapse whitespace in final message
    messageBody = messageBody.replaceAll("\\s+", " ");
    LOG.debug("Final message body after collapsing whitespace is: '{}'", messageBody);
    return messageBody;
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument)

Example 23 with Argument

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

the class HttpNotificationStrategyIT method testSend.

/*
     * Test method for 'org.opennms.netmgt.notifd.HttpNotificationStrategy.send(List)'
     */
@Test
@JUnitHttpServer(webapps = { @Webapp(context = "/cgi-bin/noauth", path = "src/test/resources/HttpNotificationStrategyTest") })
public void testSend() {
    final int port = JUnitHttpServerExecutionListener.getPort();
    assertTrue(port > 0);
    try {
        final String message = "text message for unit testing";
        final NotificationStrategy ns = new HttpNotificationStrategy();
        final List<Argument> arguments = new ArrayList<>();
        arguments.add(new Argument("url", null, "http://localhost:" + port + "/cgi-bin/noauth/nmsgw.pl", false));
        arguments.add(new Argument("post-NodeID", null, "1", false));
        arguments.add(new Argument("result-match", null, "(?s).*OK\\s+([0-9]+).*", false));
        arguments.add(new Argument("post-message", null, "-tm", false));
        arguments.add(new Argument("-tm", null, message, false));
        arguments.add(new Argument("sql", null, "UPDATE alarms SET tticketID=${1} WHERE lastEventID = 1", false));
        final int statusCode = ns.send(arguments);
        assertEquals(200, statusCode);
        final Map<String, String> parameters = HttpNotificationStrategyTestServlet.getRequestParameters();
        assertNotNull(parameters);
        assertEquals(2, parameters.size());
        assertEquals("1", parameters.get("NodeID"));
        assertEquals(message, parameters.get("message"));
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Caught Exception: " + e.getMessage());
    }
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) ArrayList(java.util.ArrayList) NotificationStrategy(org.opennms.netmgt.model.notifd.NotificationStrategy) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 24 with Argument

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

the class IrcCatNotificationStrategyTest method testSend.

// @Test
public void testSend() throws UnknownHostException {
    IrcCatNotificationStrategy strategy = new IrcCatNotificationStrategy();
    List<Argument> arguments = new ArrayList<>();
    arguments.add(new Argument(NotificationManager.PARAM_EMAIL, null, "#opennms-test", false));
    arguments.add(new Argument(NotificationManager.PARAM_TEXT_MSG, null, "Test notification from " + getClass() + " from " + InetAddress.getLocalHost(), false));
    strategy.send(arguments);
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument) ArrayList(java.util.ArrayList)

Example 25 with Argument

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

the class MattermostNotificationStrategyIT method testSendValidJustArgs.

/*
     * Test method for 'org.opennms.netmgt.notifd.MattermostNotificationStrategy.send(List)'
     */
@Test
@JUnitHttpServer(webapps = { @Webapp(context = "/hooks", path = "src/test/resources/MattermostNotificationStrategyTest") })
public void testSendValidJustArgs() {
    final int port = JUnitHttpServerExecutionListener.getPort();
    assertTrue(port > 0);
    try {
        final NotificationStrategy ns = new MattermostNotificationStrategy();
        final List<Argument> arguments = new ArrayList<>();
        // Set these properties. We will override them with Args on the first run.
        System.setProperty("org.opennms.netmgt.notifd.mattermost.webhookURL", "http://localhost:" + port + "/hooks/abunchofstuffthatidentifiesawebhook");
        System.setProperty("org.opennms.netmgt.notifd.mattermost.channel", "integrationtestsXX");
        System.setProperty("org.opennms.netmgt.notifd.mattermost.iconURL", "http://opennms.org/logo.pngXX");
        System.setProperty("org.opennms.netmgt.notifd.mattermost.iconEmoji", ":shipitXX:");
        System.setProperty("org.opennms.netmgt.notifd.mattermost.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)

Aggregations

Argument (org.opennms.netmgt.model.notifd.Argument)29 ArrayList (java.util.ArrayList)12 NotificationStrategy (org.opennms.netmgt.model.notifd.NotificationStrategy)8 Test (org.junit.Test)7 Ignore (org.junit.Ignore)3 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)3 JSONObject (org.json.simple.JSONObject)2 NodeDao (org.opennms.netmgt.dao.api.NodeDao)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Date (java.util.Date)1 NameValuePair (org.apache.http.NameValuePair)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 JavaMailer (org.opennms.javamail.JavaMailer)1 AsteriskOriginator (org.opennms.netmgt.asterisk.utils.AsteriskOriginator)1 MockNotification (org.opennms.netmgt.mock.MockNotification)1 NotificationAnticipator (org.opennms.netmgt.mock.NotificationAnticipator)1 OnmsAssetRecord (org.opennms.netmgt.model.OnmsAssetRecord)1