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"));
}
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());
}
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);
}
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();
}
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());
}
Aggregations