Java Code Examples for org.apache.camel.model.RouteDefinition#adviceWith()

The following examples show how to use org.apache.camel.model.RouteDefinition#adviceWith() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WeaveByToUriTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWeaveByToUri() throws Exception {
    RouteDefinition route = context.getRouteDefinition("quotes");
    route.adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            // replace all to("seda:line") with a mock:line instead
            weaveByToUri("seda:line").replace().to("mock:line");
        }
    });

    context.start();

    getMockEndpoint("mock:line").expectedBodiesReceived("camel rules", "donkey is bad");
    getMockEndpoint("mock:combined").expectedMessageCount(1);
    getMockEndpoint("mock:combined").message(0).body().isInstanceOf(List.class);

    template.sendBody("seda:quotes", "Camel Rules,Donkey is Bad");

    assertMockEndpointsSatisfied();
}
 
Example 2
Source File: WeaveByIdTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWeaveById() throws Exception {
    RouteDefinition route = context.getRouteDefinition("quotes");
    route.adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            // select the route node with the id=transform
            // and then replace it with the following route parts
            weaveById("transform").replace()
                .transform().simple("${body.toUpperCase()}");

            // and add at the end of the route to route to this mock endpoint
            weaveAddLast().to("mock:result");
        }
    });

    context.start();

    // we have replaced the bean transformer call with a simple expression that
    // performs an upper case
    getMockEndpoint("mock:result").expectedBodiesReceived("HELLO CAMEL");

    template.sendBody("seda:quotes", "Hello Camel");

    assertMockEndpointsSatisfied();
}
 
Example 3
Source File: WeaveByTypeSelectFirstTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWeaveByTypeSelectFirst() throws Exception {
    RouteDefinition route = context.getRouteDefinition("quotes");
    route.adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            // find the send to and select the first which gets replaced
            weaveByType(ToDefinition.class).selectFirst().replace().to("mock:line");
        }
    });

    context.start();

    getMockEndpoint("mock:line").expectedBodiesReceived("camel rules", "donkey is bad");
    getMockEndpoint("mock:combined").expectedMessageCount(1);
    getMockEndpoint("mock:combined").message(0).body().isInstanceOf(List.class);

    template.sendBody("seda:quotes", "Camel Rules,Donkey is Bad");

    assertMockEndpointsSatisfied();
}
 
Example 4
Source File: ReplaceFromTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testReplaceFromWithEndpoints() throws Exception {
    RouteDefinition route = context.getRouteDefinition("quotes");
    route.adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            // replace the incoming endpoint with a direct endpoint
            // we can easily call from unit test
            replaceFromWith("direct:hitme");
            // and then mock all seda endpoints os we can use mock endpoints
            // to assert the test is correct
            mockEndpoints("seda:*");
        }
    });

    // must start Camel after we are done using advice-with
    context.start();

    getMockEndpoint("mock:seda:camel").expectedBodiesReceived("Camel rocks");
    getMockEndpoint("mock:seda:other").expectedBodiesReceived("Bad donkey");

    template.sendBody("direct:hitme", "Camel rocks");
    template.sendBody("direct:hitme", "Bad donkey");

    assertMockEndpointsSatisfied();
}
 
Example 5
Source File: AdviceWithMockEndpointsTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMockEndpoints() throws Exception {
    RouteDefinition route = context.getRouteDefinition("quotes");
    route.adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            mockEndpoints();
        }
    });

    // must start Camel after we are done using advice-with
    context.start();

    getMockEndpoint("mock:seda:camel").expectedBodiesReceived("Camel rocks");
    getMockEndpoint("mock:seda:other").expectedBodiesReceived("Bad donkey");

    template.sendBody("seda:quotes", "Camel rocks");
    template.sendBody("seda:quotes", "Bad donkey");

    assertMockEndpointsSatisfied();
}
 
Example 6
Source File: SimulateErrorUsingInterceptorTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimulateErrorUsingInterceptors() throws Exception {
    // first find the route we need to advice. Since we only have one route
    // then just grab the first from the list
    RouteDefinition route = context.getRouteDefinitions().get(0);

    // advice the route by enriching it with the route builder where
    // we add a couple of interceptors to help simulate the error
    route.adviceWith(context, new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            // intercept sending to http and detour to our processor instead
            interceptSendToEndpoint("http://*")
                // skip sending to the real http when the detour ends
                .skipSendToOriginalEndpoint()
                .process(new SimulateHttpErrorProcessor());

            // intercept sending to ftp and detour to the mock instead
            interceptSendToEndpoint("ftp://*")
                // skip sending to the real ftp endpoint
                .skipSendToOriginalEndpoint()
                .to("mock:ftp");
        }
    });

    // our mock should receive the message
    MockEndpoint mock = getMockEndpoint("mock:ftp");
    mock.expectedBodiesReceived("Camel rocks");

    // start the test by creating a file that gets picked up by the route
    template.sendBodyAndHeader(file, "Camel rocks", Exchange.FILE_NAME, "hello.txt");

    // assert our test passes
    assertMockEndpointsSatisfied();
}
 
Example 7
Source File: WeaveByTypeTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testWeaveByType() throws Exception {
    RouteDefinition route = context.getRouteDefinition("quotes");
    route.adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            // find the splitter and insert the route snippet before it
            weaveByType(SplitDefinition.class)
                .before()
                    .filter(body().contains("Donkey"))
                    .transform(simple("${body},Mules cannot do this"));
        }
    });

    context.start();

    getMockEndpoint("mock:line").expectedBodiesReceived("camel rules", "donkey is bad", "mules cannot do this");
    getMockEndpoint("mock:combined").expectedMessageCount(1);
    getMockEndpoint("mock:combined").message(0).body().isInstanceOf(List.class);

    template.sendBody("seda:quotes", "Camel Rules,Donkey is Bad");

    assertMockEndpointsSatisfied();

    resetMocks();

    // try again without the donkeys

    getMockEndpoint("mock:line").expectedBodiesReceived("beer is good", "whiskey is better");
    getMockEndpoint("mock:combined").expectedMessageCount(1);
    getMockEndpoint("mock:combined").message(0).body().isInstanceOf(List.class);

    template.sendBody("seda:quotes", "Beer is good,Whiskey is better");

    assertMockEndpointsSatisfied();
}
 
Example 8
Source File: SimulateErrorUsingInterceptorTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimulateErrorUsingInterceptors() throws Exception {
    // first find the route we need to advice. Since we only have one route
    // then just grab the first from the list
    RouteDefinition route = context.getRouteDefinitions().get(0);

    // advice the route by enriching it with the route builder where
    // we add a couple of interceptors to help simulate the error
    route.adviceWith(context, new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            // intercept sending to http and detour to our processor instead
            interceptSendToEndpoint("http://*")
                // skip sending to the real http when the detour ends
                .skipSendToOriginalEndpoint()
                .process(new SimulateHttpErrorProcessor());

            // intercept sending to ftp and detour to the mock instead
            interceptSendToEndpoint("ftp://*")
                // skip sending to the real ftp endpoint
                .skipSendToOriginalEndpoint()
                .to("mock:ftp");
        }
    });

    // our mock should receive the message
    MockEndpoint mock = getMockEndpoint("mock:ftp");
    mock.expectedBodiesReceived("Camel rocks");

    // start the test by creating a file that gets picked up by the route
    template.sendBodyAndHeader(file, "Camel rocks", Exchange.FILE_NAME, "hello.txt");

    // assert our test passes
    assertMockEndpointsSatisfied();
}