Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.

Paste

Pasted as Java by My Name ( 13 years ago )
package abc;

import junit.framework.Assert;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class TestOnInternet extends CamelTestSupport {

    private String direct = "direct:abc";
    private String oldUri = "http4:stackoverflow.com:80";
    private String oldUriBridge = "http4:stackoverflow.com:80?bridgeEndpoint=true";
    private String newUri = "http4:www.website.com:80";
    private String mock = "mock:mock1";

    private String oldUriBody = "stackoverflow";
    private String newUriBody = "www.website.com";

    /**
     * This test success
     * @throws Exception
     */
    @Test
    public void testValidOldUri() throws Exception {
        doTestValidWebsite(oldUri);
    }

    /**
     * This test success
     * @throws Exception
     */
    @Test
    public void testValidNewUri() throws Exception {
        doTestValidWebsite(newUri);
    }

    /**
     * This test success
     * @throws Exception
     */
    @Test
    public void testSimpleTest() throws Exception {
        doTestWithSetHeaderHttpUri(oldUri, false);
    }

    /**
     * This test will give the following exception :
     * Caused by: java.lang.IllegalArgumentException: Invalid uri: http4:www.website.com:80.
     * If you are forwarding/bridging http endpoints,
     * then enable the bridgeEndpoint option on the endpoint: Endpoint[http4://stackoverflow.com:80]
     * @throws Exception
     */
    @Test
    public void testOverride() throws Exception {
        doTestWithSetHeaderHttpUri(oldUri, true);
    }

    /**
     * This test will fails on the Asserts, the body contains the body of the oldUri (oldUriBody instead of newUriBody)
     * @throws Exception
     */
    @Test
    public void testOverrideWithBridgeParameter() throws Exception {
        doTestWithSetHeaderHttpUri(oldUriBridge, true);
    }

    public void doTestWithSetHeaderHttpUri(final String oldURI, final boolean override) throws Exception {
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                if (override) {
                    from(direct).setHeader(Exchange.HTTP_URI, constant(newUri)).to(oldURI).to(mock);
                } else {
                    from(direct).to(oldURI).to(mock);
                }
            }
        });
        context.start();

        getMockEndpoint(mock).expectedMessageCount(1);

        context.createProducerTemplate().sendBody(direct, "body");

        assertMockEndpointsSatisfied();

        String body = getMessage((InputStream) getMockEndpoint(mock).getExchanges().get(0).getIn().getBody());

        if (override) {
            // Should contain newUriBody
            Assert.assertFalse(body.contains(oldUriBody));
            Assert.assertTrue(body.contains(newUriBody));
        } else {
            // Should contain oldUriBody
            Assert.assertTrue(body.contains(oldUriBody));
            Assert.assertFalse(body.contains(newUriBody));
        }
    }

    public void doTestValidWebsite(final String uri) throws Exception {
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from(direct).to(uri).to(mock);
            }
        });
        context.start();

        getMockEndpoint(mock).expectedMessageCount(1);

        context.createProducerTemplate().sendBody(direct, "body");

        assertMockEndpointsSatisfied();

        String body = getMessage((InputStream) getMockEndpoint(mock).getExchanges().get(0).getIn().getBody());

        if (uri.equals(newUri)) {
            // Should contain newUriBody
            Assert.assertFalse(body.contains(oldUriBody));
            Assert.assertTrue(body.contains(newUriBody));
        } else {
            // Should contain oldUriBody
            Assert.assertTrue(body.contains(oldUriBody));
            Assert.assertFalse(body.contains(newUriBody));
        }
    }

    private String getMessage(InputStream is) throws IOException {
        StringBuilder var = new StringBuilder();
        int read = is.read();

        while (read != -1) {
            var.append((char) read);
            read = is.read();
        }

        return var.toString();
    }

    @Override
    public boolean isUseAdviceWith() {
        return true;
    }
}

 

Revise this Paste

Your Name: Code Language: