Search in sources :

Example 11 with JUnitHttpServer

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

the class JUnitHttpServerTest method testWebapp.

@Test
@JUnitHttpServer(port = 9162, webapps = { @Webapp(context = "/testContext", path = "src/test/resources/test-webapp") })
public void testWebapp() throws Exception {
    HttpUriRequest method = new HttpGet("http://localhost:9162/testContext/index.html");
    final CloseableHttpResponse response = m_clientWrapper.execute(method);
    String responseString = EntityUtils.toString(response.getEntity());
    LOG.debug("got response:\n{}", responseString);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertTrue(responseString.contains("This is a webapp."));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 12 with JUnitHttpServer

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

the class NCSNorthbounderIT method testTestServlet.

@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/fmpm", path = "src/test/resources/test-webapp") })
public void testTestServlet() throws Exception {
    TestServlet.reset();
    CloseableHttpClient client = HttpClientBuilder.create().build();
    try {
        HttpEntity entity = new StringEntity(xml);
        HttpPost method = new HttpPost("http://localhost:10342/fmpm/restful/NotificationMessageRelay");
        method.setEntity(entity);
        HttpResponse response = client.execute(method);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(xml, TestServlet.getPosted());
    } finally {
        IOUtils.closeQuietly(client);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test) XmlTest(org.opennms.core.test.xml.XmlTest) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 13 with JUnitHttpServer

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

the class JUnitHttpServerExecutionListener method beforeTestMethod.

/**
 * {@inheritDoc}
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
    super.beforeTestMethod(testContext);
    final JUnitHttpServer config = findTestAnnotation(JUnitHttpServer.class, testContext);
    if (config == null)
        return;
    m_junitServer = new JUnitServer(config);
    m_junitServer.start();
}
Also used : JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 14 with JUnitHttpServer

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

the class JUnitServer method initializeServerWithConfig.

protected void initializeServerWithConfig(final JUnitHttpServer config) {
    Server server = null;
    if (config.https()) {
        server = new Server();
        // SSL context configuration
        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setKeyStorePath(config.keystore());
        sslContextFactory.setKeyStorePassword(config.keystorePassword());
        sslContextFactory.setKeyManagerPassword(config.keyPassword());
        sslContextFactory.setTrustStorePath(config.keystore());
        sslContextFactory.setTrustStorePassword(config.keystorePassword());
        // HTTP Configuration
        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setSecureScheme("https");
        http_config.setSecurePort(config.port());
        http_config.setOutputBufferSize(32768);
        http_config.setRequestHeaderSize(8192);
        http_config.setResponseHeaderSize(8192);
        http_config.setSendServerVersion(true);
        http_config.setSendDateHeader(false);
        // SSL HTTP Configuration
        HttpConfiguration https_config = new HttpConfiguration(http_config);
        https_config.addCustomizer(new SecureRequestCustomizer());
        // SSL Connector
        ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config));
        sslConnector.setPort(config.port());
        server.addConnector(sslConnector);
    } else {
        server = new Server(config.port());
    }
    m_server = server;
    final ContextHandler context1 = new ContextHandler();
    context1.setContextPath("/");
    context1.setWelcomeFiles(new String[] { "index.html" });
    context1.setResourceBase(config.resource());
    context1.setClassLoader(Thread.currentThread().getContextClassLoader());
    context1.setVirtualHosts(config.vhosts());
    final ContextHandler context = context1;
    Handler topLevelHandler = null;
    final HandlerList handlers = new HandlerList();
    if (config.basicAuth()) {
        // check for basic auth if we're configured to do so
        LOG.debug("configuring basic auth");
        final HashLoginService loginService = new HashLoginService("MyRealm", config.basicAuthFile());
        loginService.setHotReload(true);
        m_server.addBean(loginService);
        final ConstraintSecurityHandler security = new ConstraintSecurityHandler();
        final Set<String> knownRoles = new HashSet<>();
        knownRoles.add("user");
        knownRoles.add("admin");
        knownRoles.add("moderator");
        final Constraint constraint = new Constraint();
        constraint.setName("auth");
        constraint.setAuthenticate(true);
        constraint.setRoles(knownRoles.toArray(new String[0]));
        final ConstraintMapping mapping = new ConstraintMapping();
        mapping.setPathSpec("/*");
        mapping.setConstraint(constraint);
        security.setConstraintMappings(Collections.singletonList(mapping), knownRoles);
        security.setAuthenticator(new BasicAuthenticator());
        security.setLoginService(loginService);
        security.setRealmName("MyRealm");
        security.setHandler(context);
        topLevelHandler = security;
    } else {
        topLevelHandler = context;
    }
    final Webapp[] webapps = config.webapps();
    if (webapps != null) {
        for (final Webapp webapp : webapps) {
            final WebAppContext wac = new WebAppContext();
            String path = null;
            if (!"".equals(webapp.pathSystemProperty()) && System.getProperty(webapp.pathSystemProperty()) != null) {
                path = System.getProperty(webapp.pathSystemProperty());
            } else {
                path = webapp.path();
            }
            if (path == null || "".equals(path)) {
                throw new IllegalArgumentException("path or pathSystemProperty of @Webapp points to a null or blank value");
            }
            wac.setWar(path);
            wac.setContextPath(webapp.context());
            handlers.addHandler(wac);
        }
    }
    final ResourceHandler rh = new ResourceHandler();
    rh.setWelcomeFiles(new String[] { "index.html" });
    rh.setResourceBase(config.resource());
    handlers.addHandler(rh);
    // fall through to default
    handlers.addHandler(new DefaultHandler());
    context.setHandler(handlers);
    m_server.setHandler(topLevelHandler);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) Constraint(org.eclipse.jetty.util.security.Constraint) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HashLoginService(org.eclipse.jetty.security.HashLoginService) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) HashSet(java.util.HashSet) Webapp(org.opennms.core.test.http.annotations.Webapp)

Example 15 with JUnitHttpServer

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

the class PageSequenceMonitorIT method testRequireIPv6.

@Test
@JUnitHttpServer(port = 10342, webapps = @Webapp(context = "/opennms", path = "src/test/resources/loginTestWar"))
public void testRequireIPv6() throws Exception {
    assumeTrue(!Boolean.getBoolean("skipIpv6Tests"));
    m_params.put("page-sequence", "" + "<?xml version=\"1.0\"?>" + "<page-sequence>\n" + "  <page host=\"localhost\" virtual-host=\"localhost\" path=\"/opennms/\" port=\"10342\" requireIPv6=\"true\"/>\n" + "</page-sequence>\n");
    PollStatus status = m_monitor.poll(getHttpService("localhost"), m_params);
    assertTrue("Expected available but was " + status + ": reason = " + status.getReason(), status.isAvailable());
    assertTrue("Expected a DS called 'response-time' but did not find one", status.getProperties().containsKey(PollStatus.PROPERTY_RESPONSE_TIME));
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) 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