Search in sources :

Example 41 with JUnitHttpServer

use of org.opennms.core.test.http.annotations.JUnitHttpServer 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)

Example 42 with JUnitHttpServer

use of org.opennms.core.test.http.annotations.JUnitHttpServer 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<>();
        // 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 43 with JUnitHttpServer

use of org.opennms.core.test.http.annotations.JUnitHttpServer in project opennms by OpenNMS.

the class HttpUrlConnectionIT method testServlet.

/**
 * Test the Servlet with a simple POST Request based on XML Data.
 *
 * @throws Exception the exception
 */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testServlet() throws Exception {
    String xml = "<person><firstName>Alejandro</firstName></person>";
    final HttpClientWrapper clientWrapper = HttpClientWrapper.create();
    try {
        StringEntity entity = new StringEntity(xml, ContentType.APPLICATION_XML);
        HttpPost method = new HttpPost("http://localhost:10342/junit/test/sample");
        method.setEntity(entity);
        CloseableHttpResponse response = clientWrapper.execute(method);
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        Assert.assertEquals("OK!", EntityUtils.toString(response.getEntity()));
    } finally {
        IOUtils.closeQuietly(clientWrapper);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) HttpClientWrapper(org.opennms.core.web.HttpClientWrapper) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 44 with JUnitHttpServer

use of org.opennms.core.test.http.annotations.JUnitHttpServer in project opennms by OpenNMS.

the class HttpDataCollectionIT method testJsonHttpCollection.

/**
 * Test HTTP Data Collection with JSON
 *
 * @throws Exception the exception
 */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testJsonHttpCollection() throws Exception {
    File configFile = new File("src/test/resources/solaris-zones-datacollection-config.xml");
    XmlDataCollectionConfig config = JaxbUtils.unmarshal(XmlDataCollectionConfig.class, configFile);
    XmlDataCollection collection = config.getDataCollectionByName("Solaris");
    RrdRepository repository = createRrdRepository(collection.getXmlRrd());
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "Solaris");
    DefaultJsonCollectionHandler collector = new DefaultJsonCollectionHandler();
    collector.setRrdRepository(repository);
    collector.setServiceName("HTTP");
    CollectionSet collectionSet = XmlCollectorTestUtils.doCollect(m_nodeDao, collector, m_collectionAgent, collection, parameters);
    Assert.assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    System.err.println(CollectionSetUtils.flatten(collectionSet));
    ServiceParameters serviceParams = new ServiceParameters(new HashMap<String, Object>());
    CollectionSetVisitor persister = m_persisterFactory.createGroupPersister(serviceParams, repository, false, false);
    collectionSet.visit(persister);
    RrdDb jrb = new RrdDb(new File(getSnmpRoot(), "1/solarisZoneStats/global/solaris-zone-stats.jrb"));
    Assert.assertNotNull(jrb);
    Assert.assertEquals(6, jrb.getDsCount());
    Datasource ds = jrb.getDatasource("nproc");
    Assert.assertNotNull(ds);
    Assert.assertEquals(new Double(245.0), Double.valueOf(ds.getLastValue()));
}
Also used : Datasource(org.jrobin.core.Datasource) DefaultJsonCollectionHandler(org.opennms.protocols.json.collector.DefaultJsonCollectionHandler) XmlDataCollectionConfig(org.opennms.protocols.xml.config.XmlDataCollectionConfig) HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) RrdDb(org.jrobin.core.RrdDb) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) File(java.io.File) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 45 with JUnitHttpServer

use of org.opennms.core.test.http.annotations.JUnitHttpServer in project opennms by OpenNMS.

the class HttpDataCollectionIT method testHttpCollection.

/**
 * Test HTTP Data Collection with XPath
 *
 * @throws Exception the exception
 */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testHttpCollection() throws Exception {
    File configFile = new File("src/test/resources/http-datacollection-config.xml");
    XmlDataCollectionConfig config = JaxbUtils.unmarshal(XmlDataCollectionConfig.class, configFile);
    XmlDataCollection collection = config.getDataCollectionByName("Http-Count");
    RrdRepository repository = createRrdRepository(collection.getXmlRrd());
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "Http-Count");
    DefaultXmlCollectionHandler collector = new DefaultXmlCollectionHandler();
    collector.setRrdRepository(repository);
    collector.setServiceName("HTTP");
    CollectionSet collectionSet = XmlCollectorTestUtils.doCollect(m_nodeDao, collector, m_collectionAgent, collection, parameters);
    Assert.assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    ServiceParameters serviceParams = new ServiceParameters(new HashMap<String, Object>());
    CollectionSetVisitor persister = m_persisterFactory.createGroupPersister(serviceParams, repository, false, false);
    collectionSet.visit(persister);
    RrdDb jrb = new RrdDb(new File(getSnmpRoot(), "1/count-stats.jrb"));
    Assert.assertNotNull(jrb);
    Assert.assertEquals(1, jrb.getDsCount());
    Datasource ds = jrb.getDatasource("count");
    Assert.assertNotNull(ds);
    Assert.assertEquals(new Double(5), Double.valueOf(ds.getLastValue()));
}
Also used : Datasource(org.jrobin.core.Datasource) XmlDataCollectionConfig(org.opennms.protocols.xml.config.XmlDataCollectionConfig) HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) RrdDb(org.jrobin.core.RrdDb) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) File(java.io.File) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Aggregations

JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)48 Test (org.junit.Test)46 PollStatus (org.opennms.netmgt.poller.PollStatus)18 MonitoredService (org.opennms.netmgt.poller.MonitoredService)9 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)8 File (java.io.File)7 HashMap (java.util.HashMap)7 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)6 Calendar (java.util.Calendar)5 GregorianCalendar (java.util.GregorianCalendar)5 HttpGet (org.apache.http.client.methods.HttpGet)5 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)5 Datasource (org.jrobin.core.Datasource)4 RrdDb (org.jrobin.core.RrdDb)4 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)4 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)4 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)4 XmlDataCollection (org.opennms.protocols.xml.config.XmlDataCollection)4 XmlDataCollectionConfig (org.opennms.protocols.xml.config.XmlDataCollectionConfig)4