Search in sources :

Example 36 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class AdviceWithMockEndpointsTest method testAdvisedMockEndpointsWithPattern.

// end::e1[]
// END SNIPPET: e1
// START SNIPPET: e2
// tag::e2[]
public void testAdvisedMockEndpointsWithPattern() throws Exception {
    // advice the first route using the inlined AdviceWith route builder
    // which has extended capabilities than the regular route builder
    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            // mock only log endpoints
            mockEndpoints("log*");
        }
    });
    // now we can refer to log:foo as a mock and set our expectations
    getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
    getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    // additional test to ensure correct endpoints in registry
    assertNotNull(context.hasEndpoint("direct:start"));
    assertNotNull(context.hasEndpoint("direct:foo"));
    assertNotNull(context.hasEndpoint("log:foo"));
    assertNotNull(context.hasEndpoint("mock:result"));
    // only the log:foo endpoint was mocked
    assertNotNull(context.hasEndpoint("mock:log:foo"));
    assertNull(context.hasEndpoint("mock:direct:start"));
    assertNull(context.hasEndpoint("mock:direct:foo"));
}
Also used : AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 37 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class AdviceWithMockMultipleEndpointsWithSkipTest method testAdvisedMockEndpointsWithSkip.

// START SNIPPET: e1
// tag::e1[]
public void testAdvisedMockEndpointsWithSkip() throws Exception {
    // advice the first route using the inlined AdviceWith route builder
    // which has extended capabilities than the regular route builder
    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            // mock sending to direct:foo and direct:bar and skip send to it
            mockEndpointsAndSkip("direct:foo", "direct:bar");
        }
    });
    getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
    getMockEndpoint("mock:direct:foo").expectedMessageCount(1);
    getMockEndpoint("mock:direct:bar").expectedMessageCount(1);
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    // the message was not send to the direct:foo route and thus not sent to the seda endpoint
    SedaEndpoint seda = context.getEndpoint("seda:foo", SedaEndpoint.class);
    assertEquals(0, seda.getCurrentQueueSize());
}
Also used : SedaEndpoint(org.apache.camel.component.seda.SedaEndpoint) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 38 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class AdviceAndInterceptHttp4IssueTest method doTestHttp4Parameter.

private void doTestHttp4Parameter(final String provider) throws Exception {
    messageIntercepted = false;
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to(provider).to("mock:result");
        }
    });
    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("http4:fakeHTTPADDRESS.com:80*").skipSendToOriginalEndpoint().process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    messageIntercepted = true;
                }
            }).to("mock:advised");
        }
    });
    context.start();
    getMockEndpoint("mock:advised").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedMessageCount(1);
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    assertTrue(messageIntercepted);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 39 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class AdviceWithIssueTest method testAdviceWith.

@Test
public void testAdviceWith() throws Exception {
    context.getRouteDefinition("starter").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            // when advicing then use wildcard as URI options cannot be matched
            mockEndpointsAndSkip(advicedPub + "?*");
        }
    });
    context.start();
    MockEndpoint topicEndpointMock = getMockEndpoint("mock:" + advicedPub);
    topicEndpointMock.expectedMessageCount(1);
    template.sendBody("direct:start", Collections.singletonMap("foo", "bar"));
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder) Test(org.junit.Test)

Example 40 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class JMSTransactionIsTransactedRedeliveredTest method testTransactionSuccess.

@Test
public void testTransactionSuccess() throws Exception {
    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(AssertionError.class).to("log:error", "mock:error");
        }
    });
    context.start();
    // there should be no assertion errors
    MockEndpoint error = getMockEndpoint("mock:error");
    error.expectedMessageCount(0);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived("Bye World");
    // success at 3rd attempt
    mock.message(0).header("count").isEqualTo(3);
    template.sendBody("activemq:queue:okay", "Hello World");
    mock.assertIsSatisfied();
    error.assertIsSatisfied();
    // check JMX stats
    // need a little sleep to ensure JMX is updated
    Thread.sleep(500);
    ObjectName name = ObjectName.getInstance("org.apache.camel:context=camel-1,type=routes,name=\"myRoute\"");
    Long total = (Long) getMBeanServer().getAttribute(name, "ExchangesTotal");
    assertEquals(3, total.intValue());
    Long completed = (Long) getMBeanServer().getAttribute(name, "ExchangesCompleted");
    assertEquals(1, completed.intValue());
    Long failed = (Long) getMBeanServer().getAttribute(name, "ExchangesFailed");
    assertEquals(2, failed.intValue());
    // Camel error handler redeliveries (we do not use that in this example)
    Long redeliveries = (Long) getMBeanServer().getAttribute(name, "Redeliveries");
    assertEquals(0, redeliveries.intValue());
    // Camel error handler redeliveries (we do not use that in this example)
    // there should be 2 external redeliveries
    Long externalRedeliveries = (Long) getMBeanServer().getAttribute(name, "ExternalRedeliveries");
    assertEquals(2, externalRedeliveries.intValue());
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

AdviceWithRouteBuilder (org.apache.camel.builder.AdviceWithRouteBuilder)94 RouteDefinition (org.apache.camel.model.RouteDefinition)17 Test (org.junit.Test)17 Exchange (org.apache.camel.Exchange)10 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)10 Processor (org.apache.camel.Processor)9 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)3 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)3 SedaEndpoint (org.apache.camel.component.seda.SedaEndpoint)2 ToDefinition (org.apache.camel.model.ToDefinition)2 ObjectName (javax.management.ObjectName)1 Endpoint (org.apache.camel.Endpoint)1 StatefulService (org.apache.camel.StatefulService)1 AdviceWithTask (org.apache.camel.builder.AdviceWithTask)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 ChoiceDefinition (org.apache.camel.model.ChoiceDefinition)1 LogDefinition (org.apache.camel.model.LogDefinition)1 SplitDefinition (org.apache.camel.model.SplitDefinition)1 InSequence (org.jboss.arquillian.junit.InSequence)1 Before (org.junit.Before)1