Search in sources :

Example 21 with MockMessageProducer

use of com.adaptris.core.stubs.MockMessageProducer in project interlok by adaptris.

the class HttpConsumerTest method testLoopbackWithIncorrectRole.

@Test
public void testLoopbackWithIncorrectRole() throws Exception {
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(getName());
    ConfigurableSecurityHandler csh = new ConfigurableSecurityHandler();
    HashLoginServiceFactory hsl = new HashLoginServiceFactory("InterlokJetty", PROPERTIES.getProperty(JETTY_USER_REALM));
    csh.setLoginService(hsl);
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setMustAuthenticate(true);
    securityConstraint.setRoles("admin");
    securityConstraint.setPaths(Arrays.asList("/"));
    csh.setSecurityConstraints(Arrays.asList(securityConstraint));
    HttpConnection connection = createConnection(csh);
    MockMessageProducer mockProducer = new MockMessageProducer();
    JettyMessageConsumer consumer = JettyHelper.createConsumer(URL_TO_POST_TO);
    Channel adapter = JettyHelper.createChannel(connection, consumer, mockProducer);
    try {
        adapter.requestStart();
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_PAYLOAD);
        msg.addMetadata("content.type", "text/xml");
        httpProducer.setUserName("user");
        httpProducer.setPassword("password");
        httpProducer.setUrl(createProduceDestinationUrl(connection.getPort()));
        start(httpProducer);
        AdaptrisMessage reply = httpProducer.request(msg);
        assertEquals("0 message consumed", 0, mockProducer.getMessages().size());
        assertTrue("Reply Response Code present", reply.containsKey(CoreConstants.HTTP_PRODUCER_RESPONSE_CODE));
        int rc = Integer.valueOf(reply.getMetadataValue(CoreConstants.HTTP_PRODUCER_RESPONSE_CODE)).intValue();
        assertTrue("Reply Response Value 401 || 403", rc == HttpURLConnection.HTTP_UNAUTHORIZED || rc == HttpURLConnection.HTTP_FORBIDDEN);
    } catch (ProduceException e) {
        // This is expected actually,.
        if (e.getCause() instanceof IOException) {
            String s = ((IOException) e.getCause()).getMessage();
            String httpString = "Server returned HTTP response code: ";
            if (s.startsWith(httpString)) {
                int rc = Integer.parseInt(s.substring(httpString.length(), httpString.length() + 3));
                assertTrue(rc == HttpURLConnection.HTTP_UNAUTHORIZED || rc == HttpURLConnection.HTTP_FORBIDDEN);
            } else {
                throw e;
            }
        } else {
            throw e;
        }
    } finally {
        stop(httpProducer);
        adapter.requestClose();
        PortManager.release(connection.getPort());
        Thread.currentThread().setName(threadName);
        assertEquals(0, AdapterResourceAuthenticator.getInstance().currentAuthenticators().size());
    }
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) StaticMockMessageProducer(com.adaptris.core.stubs.StaticMockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) Channel(com.adaptris.core.Channel) IOException(java.io.IOException) Constraint(org.eclipse.jetty.util.security.Constraint) ProduceException(com.adaptris.core.ProduceException) Test(org.junit.Test)

Example 22 with MockMessageProducer

use of com.adaptris.core.stubs.MockMessageProducer in project interlok by adaptris.

the class HttpConsumerTest method testConsume_WithACL_IncorrectPassword.

@Test
public void testConsume_WithACL_IncorrectPassword() throws Exception {
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(getName());
    ConfigurableSecurityHandler csh = new ConfigurableSecurityHandler();
    HashLoginServiceFactory hsl = new HashLoginServiceFactory("InterlokJetty", PROPERTIES.getProperty(JETTY_USER_REALM));
    csh.setLoginService(hsl);
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setMustAuthenticate(true);
    securityConstraint.setRoles("user");
    securityConstraint.setPaths(Arrays.asList("/"));
    csh.setSecurityConstraints(Arrays.asList(securityConstraint));
    HttpConnection connection = createConnection(csh);
    MockMessageProducer mockProducer = new MockMessageProducer();
    JettyMessageConsumer consumer = JettyHelper.createConsumer(URL_TO_POST_TO);
    Channel adapter = JettyHelper.createChannel(connection, consumer, mockProducer);
    try {
        adapter.requestStart();
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_PAYLOAD);
        msg.addMetadata("content.type", "text/xml");
        httpProducer.setUserName("user");
        httpProducer.setPassword("badbadpassword");
        httpProducer.setUrl(createProduceDestinationUrl(connection.getPort()));
        start(httpProducer);
        AdaptrisMessage reply = httpProducer.request(msg);
        assertEquals("0 message consumed", 0, mockProducer.getMessages().size());
        assertTrue("Reply Response Code present", reply.containsKey(CoreConstants.HTTP_PRODUCER_RESPONSE_CODE));
        int rc = Integer.valueOf(reply.getMetadataValue(CoreConstants.HTTP_PRODUCER_RESPONSE_CODE)).intValue();
        assertTrue("Reply Response Value 401 || 403", rc == HttpURLConnection.HTTP_UNAUTHORIZED || rc == HttpURLConnection.HTTP_FORBIDDEN);
    } catch (Exception e) {
        // This is expected actually,.
        if (e.getCause() instanceof IOException) {
            String s = ((IOException) e.getCause()).getMessage();
            String httpString = "Server returned HTTP response code: ";
            if (s.startsWith(httpString)) {
                int rc = Integer.parseInt(s.substring(httpString.length(), httpString.length() + 3));
                assertTrue(rc == HttpURLConnection.HTTP_UNAUTHORIZED || rc == HttpURLConnection.HTTP_FORBIDDEN);
            } else {
                throw e;
            }
        } else {
            throw e;
        }
    } finally {
        stop(httpProducer);
        adapter.requestClose();
        Thread.currentThread().setName(threadName);
        PortManager.release(connection.getPort());
        assertEquals(0, AdapterResourceAuthenticator.getInstance().currentAuthenticators().size());
    }
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) StaticMockMessageProducer(com.adaptris.core.stubs.StaticMockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) Channel(com.adaptris.core.Channel) IOException(java.io.IOException) Constraint(org.eclipse.jetty.util.security.Constraint) ProduceException(com.adaptris.core.ProduceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CoreException(com.adaptris.core.CoreException) Test(org.junit.Test)

Example 23 with MockMessageProducer

use of com.adaptris.core.stubs.MockMessageProducer in project interlok by adaptris.

the class HttpConsumerTest method testBasicConsumeWorkflow_ConsumeDestinationContainsURL.

@Test
public void testBasicConsumeWorkflow_ConsumeDestinationContainsURL() throws Exception {
    HttpConnection connection = createConnection(null);
    MockMessageProducer mockProducer = new MockMessageProducer();
    Channel channel = JettyHelper.createChannel(connection, JettyHelper.createConsumer("http://localhost:8080" + URL_TO_POST_TO), mockProducer);
    try {
        channel.requestStart();
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_PAYLOAD);
        msg.addMetadata(CONTENT_TYPE_METADATA_KEY, "text/xml");
        httpProducer.setUrl(createProduceDestinationUrl(connection.getPort()));
        start(httpProducer);
        AdaptrisMessage reply = httpProducer.request(msg);
        assertEquals("Reply Payloads", XML_PAYLOAD, reply.getContent());
        doAssertions(mockProducer);
    } finally {
        stop(httpProducer);
        channel.requestClose();
        PortManager.release(connection.getPort());
    }
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) StaticMockMessageProducer(com.adaptris.core.stubs.StaticMockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) Channel(com.adaptris.core.Channel) Test(org.junit.Test)

Example 24 with MockMessageProducer

use of com.adaptris.core.stubs.MockMessageProducer in project interlok by adaptris.

the class HttpConsumerTest method testBasicConsumeWorkflow_AcrossRestarts.

@Test
public void testBasicConsumeWorkflow_AcrossRestarts() throws Exception {
    HttpConnection connection = createConnection(null);
    MockMessageProducer mockProducer = new MockMessageProducer();
    Channel channel = JettyHelper.createChannel(connection, JettyHelper.createConsumer(URL_TO_POST_TO), mockProducer);
    try {
        channel.requestStart();
        channel.requestClose();
        channel.requestStart();
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_PAYLOAD);
        msg.addMetadata(CONTENT_TYPE_METADATA_KEY, "text/xml");
        httpProducer.setUrl(createProduceDestinationUrl(connection.getPort()));
        start(httpProducer);
        AdaptrisMessage reply = httpProducer.request(msg);
        assertEquals("Reply Payloads", XML_PAYLOAD, reply.getContent());
        doAssertions(mockProducer);
    } finally {
        stop(httpProducer);
        channel.requestClose();
        PortManager.release(connection.getPort());
    }
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) StaticMockMessageProducer(com.adaptris.core.stubs.StaticMockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) Channel(com.adaptris.core.Channel) Test(org.junit.Test)

Example 25 with MockMessageProducer

use of com.adaptris.core.stubs.MockMessageProducer in project interlok by adaptris.

the class HttpsConsumerTest method testTLS_ConsumeWorkflow.

@Test
public void testTLS_ConsumeWorkflow() throws Exception {
    String oldName = Thread.currentThread().getName();
    Thread.currentThread().setName(getName());
    HttpConnection connection = createConnection(null);
    ((HttpsConnection) connection).getSslProperties().add(new KeyValuePair(SslProperty.ExcludeProtocols.name(), "SSLv3,TLSv1.1,"));
    MockMessageProducer mockProducer = new MockMessageProducer();
    SimpleHttpProducer myHttpProducer = createProducer(new VersionedHttpsProduceConnection("TLSv1.2"));
    Channel channel = JettyHelper.createChannel(connection, JettyHelper.createConsumer(URL_TO_POST_TO), mockProducer);
    try {
        channel.requestStart();
        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_PAYLOAD);
        msg.addMetadata(CONTENT_TYPE_METADATA_KEY, "text/xml");
        myHttpProducer.setUrl(createProduceDestinationUrl(connection.getPort()));
        start(myHttpProducer);
        AdaptrisMessage reply = myHttpProducer.request(msg);
        assertEquals("Reply Payloads", XML_PAYLOAD, reply.getContent());
        doAssertions(mockProducer);
    } finally {
        stop(myHttpProducer);
        channel.requestClose();
        PortManager.release(connection.getPort());
        Thread.currentThread().setName(oldName);
    }
}
Also used : KeyValuePair(com.adaptris.util.KeyValuePair) MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) SimpleHttpProducer(com.adaptris.http.legacy.SimpleHttpProducer) Channel(com.adaptris.core.Channel) VersionedHttpsProduceConnection(com.adaptris.http.legacy.VersionedHttpsProduceConnection) Test(org.junit.Test)

Aggregations

MockMessageProducer (com.adaptris.core.stubs.MockMessageProducer)321 Test (org.junit.Test)306 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)156 MockChannel (com.adaptris.core.stubs.MockChannel)126 Channel (com.adaptris.core.Channel)125 ThrowExceptionService (com.adaptris.core.services.exception.ThrowExceptionService)73 StaticMockMessageProducer (com.adaptris.core.stubs.StaticMockMessageProducer)69 StandaloneProducer (com.adaptris.core.StandaloneProducer)68 PayloadFromTemplateService (com.adaptris.core.services.metadata.PayloadFromTemplateService)65 MockSkipProducerService (com.adaptris.core.stubs.MockSkipProducerService)56 FailFirstMockMessageProducer (com.adaptris.core.stubs.FailFirstMockMessageProducer)52 JettyHelper.createChannel (com.adaptris.core.http.jetty.JettyHelper.createChannel)50 AddMetadataService (com.adaptris.core.services.metadata.AddMetadataService)44 StandardWorkflow (com.adaptris.core.StandardWorkflow)40 TimeInterval (com.adaptris.util.TimeInterval)40 DefaultMessageFactory (com.adaptris.core.DefaultMessageFactory)36 HttpConsumerTest (com.adaptris.core.http.jetty.HttpConsumerTest)35 ServiceList (com.adaptris.core.ServiceList)33 StandaloneRequestor (com.adaptris.core.StandaloneRequestor)33 ConfiguredException (com.adaptris.core.services.exception.ConfiguredException)27