@Test public void routeShouldReturnOddFor1EvenFor6() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveAddLast().setBody(simple("${body} is ${headers.NumberParity}.")).to(resultEndpoint); } }); context.start(); resultEndpoint.expectedMessageCount(2); resultEndpoint.expectedBodiesReceived("1 is odd.", "6 is even."); template.sendBody(1); template.sendBody(6); resultEndpoint.assertIsSatisfied(); context.stop(); }
@Test public void testResponseToJSON() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { replaceFromWith("direct:test"); } } ); ResponseMessage message = new ResponseMessage(ResponseCode.SUCCESS, "MESSAGE"); context.start(); Object object = template.requestBody("direct:test", message); Assert.assertTrue(object instanceof String); }
public void testAdviceWith() throws Exception { context.getRouteDefinition("route-a").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { interceptSendToEndpoint("direct:bar") .skipSendToOriginalEndpoint() .throwException(new IllegalArgumentException("Forced")); } }); getMockEndpoint("mock:error").expectedMessageCount(1); getMockEndpoint("mock:bar").expectedMessageCount(0); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
@Test public void testParseUrl() throws Exception { final String route = "IslandoraTriplestoreIndexerParseUrl"; context.getRouteDefinition(route).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { replaceFromWith("direct:start"); weaveAddLast().to(resultEndpoint); } }); context.start(); resultEndpoint.expectedMessageCount(1); final Exchange exchange = template.send(xchange -> xchange.getIn().setBody(IOUtils.toString(loadResourceAsStream("AS2Event.jsonld"), "UTF-8")) ); this.assertPredicate( exchangeProperty("url").isEqualTo("http://localhost:8000/node/1?_format=jsonld"), exchange, true ); assertMockEndpointsSatisfied(); }
@Test public void testMultipleAdvice() throws Exception { context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { interceptSendToEndpoint("mock:resultA").process(new Processor() { @Override public void process(Exchange exchange) throws Exception { throw new Exception("my exception"); } }); } }); context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { } }); context.start(); getMockEndpoint("mock:resultA").expectedMessageCount(0); template.sendBody("direct:startA", "a trigger"); assertMockEndpointsSatisfied(); }
void advice(@Observes CamelContextStartingEvent event, @Uri("mock:messages") MockEndpoint messages, ModelCamelContext context) throws Exception { messages.expectedMessageCount(2); messages.expectedBodiesReceived("Hello", "Bye"); verifier.messages = messages; context.getRouteDefinition("route") .adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() { weaveAddLast().to("mock:messages"); } }); }
public void testAdviceWithOnException() throws Exception { RouteDefinition route = context.getRouteDefinitions().get(0); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveById("b").after().to("mock:result"); } }); context.start(); getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testAdviceRoutePolicyRemoved() throws Exception { RouteDefinition route = context.getRouteDefinitions().get(0); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // remove the route policy so we can test without it getOriginalRoute().setRoutePolicies(null); } }); getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:foo").message(0).header("MyRoutePolicy").isNull(); getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:bar").message(0).header("MyRoutePolicy").isNull(); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testAdviceWithPolicy() throws Exception { RouteDefinition route = context.getRouteDefinitions().get(0); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveById("b").after().to("mock:result"); } }); context.start(); getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testAdviceCBR() throws Exception { RouteDefinition route = context.getRouteDefinitions().get(0); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveById("foo").after().to("mock:foo2"); weaveById("bar").after().to("mock:bar2"); } }); getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:foo2").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:bar").expectedBodiesReceived("Bye World"); getMockEndpoint("mock:bar2").expectedBodiesReceived("Bye World"); getMockEndpoint("mock:baz").expectedBodiesReceived("Hi World"); template.sendBodyAndHeader("direct:start", "Hello World", "foo", "123"); template.sendBodyAndHeader("direct:start", "Bye World", "bar", "123"); template.sendBody("direct:start", "Hi World"); assertMockEndpointsSatisfied(); }
public void testAdviceToStringCBR() throws Exception { RouteDefinition route = context.getRouteDefinitions().get(0); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveByToString("To[mock:foo]").after().to("mock:foo2"); weaveByToString("To[mock:bar]").after().to("mock:bar2"); } }); getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:foo2").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:bar").expectedBodiesReceived("Bye World"); getMockEndpoint("mock:bar2").expectedBodiesReceived("Bye World"); getMockEndpoint("mock:baz").expectedBodiesReceived("Hi World"); template.sendBodyAndHeader("direct:start", "Hello World", "foo", "123"); template.sendBodyAndHeader("direct:start", "Bye World", "bar", "123"); template.sendBody("direct:start", "Hi World"); assertMockEndpointsSatisfied(); }
public void testNoErrorHandler() throws Exception { try { context.getRouteDefinition("route-a").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { errorHandler(loggingErrorHandler()); interceptSendToEndpoint("direct:bar") .skipSendToOriginalEndpoint() .throwException(new IllegalArgumentException("Forced")); } }); fail("Should have thrown an exception"); } catch (IllegalArgumentException e) { assertEquals("You can not advice with error handlers. Remove the error handlers from the route builder.", e.getMessage()); } }
public void testSimpleMultipleAdvice() throws Exception { context.addRoutes(createRouteBuilder()); context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { interceptSendToEndpoint("mock:resultA").process(new Processor() { @Override public void process(Exchange exchange) throws Exception { } }); } }); context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { } }); context.start(); getMockEndpoint("mock:resultA").expectedMessageCount(1); template.sendBody("direct:startA", "a trigger"); assertMockEndpointsSatisfied(); }
public void testMultipleAdviceWithExceptionThrown() throws Exception { context.addRoutes(createRouteBuilder()); context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { interceptSendToEndpoint("mock:resultA").process(new Processor() { @Override public void process(Exchange exchange) throws Exception { throw new Exception("my exception"); } }); } }); context.start(); getMockEndpoint("mock:resultA").expectedMessageCount(0); template.sendBody("direct:startA", "a trigger"); assertMockEndpointsSatisfied(); }
@Test public void testSimpleMultipleAdvice() throws Exception { context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { interceptSendToEndpoint("mock:resultA").process(new Processor() { @Override public void process(Exchange exchange) throws Exception { } }); } }); context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { } }); context.start(); getMockEndpoint("mock:resultA").expectedMessageCount(1); template.sendBody("direct:startA", "a trigger"); assertMockEndpointsSatisfied(); }
public void testAdviceWithOnException() throws Exception { RouteDefinition route = context.getRouteDefinitions().get(0); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { onException(IllegalArgumentException.class) .handled(true) .to("mock:error"); } }); getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:error").expectedBodiesReceived("Kaboom"); template.sendBody("direct:start", "World"); template.sendBody("direct:start", "Kaboom"); assertMockEndpointsSatisfied(); }
public void testAdviceWithInterceptFrom() throws Exception { RouteDefinition route = context.getRouteDefinitions().get(0); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { interceptFrom().to("mock:from"); } }); getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:from").expectedBodiesReceived("World"); getMockEndpoint("mock:from").expectedHeaderReceived(Exchange.INTERCEPTED_ENDPOINT, "direct://start"); template.sendBody("direct:start", "World"); assertMockEndpointsSatisfied(); }
public void testAdviceWithOnCompletion() throws Exception { RouteDefinition route = context.getRouteDefinitions().get(0); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { onCompletion().to("mock:done"); } }); getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:done").expectedBodiesReceived("Hello World"); template.sendBody("direct:start", "World"); assertMockEndpointsSatisfied(); }
public void testAdviceWithA() throws Exception { RouteDefinition route = context.getRouteDefinition("a"); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { interceptSendToEndpoint("mock://a") .skipSendToOriginalEndpoint() .to("mock:detour"); } }); getMockEndpoint("mock:a").expectedMessageCount(0); getMockEndpoint("mock:detour").expectedMessageCount(1); template.sendBody("direct:a", "Hello World"); assertMockEndpointsSatisfied(); }
public void testAdviceWithB() throws Exception { RouteDefinition route = context.getRouteDefinition("b"); route.adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { interceptSendToEndpoint("mock://b") .skipSendToOriginalEndpoint() .to("mock:detour"); } }); getMockEndpoint("mock:b").expectedMessageCount(0); getMockEndpoint("mock:detour").expectedMessageCount(1); template.sendBody("direct:b", "Hello World"); assertMockEndpointsSatisfied(); }
public void testAdvised() throws Exception { assertFalse(context.getRouteStatus("foo").isStarted()); assertFalse(context.getRouteStatus("bar").isStarted()); context.getRouteDefinition("bar").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { replaceFromWith("seda:newBar"); } }); assertFalse(context.getRouteStatus("foo").isStarted()); assertFalse(context.getRouteStatus("bar").isStarted()); context.startRoute("foo"); context.startRoute("bar"); assertTrue(context.getRouteStatus("foo").isStarted()); assertTrue(context.getRouteStatus("bar").isStarted()); getMockEndpoint("mock:newBar").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testReplace() throws Exception { // START SNIPPET: e1 context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // weave nodes in the route which has foo anywhere in their to string representation // and replace them with the following route path weaveByToString(".*foo.*").replace().multicast().to("mock:a").to("mock:b"); } }); // END SNIPPET: e1 getMockEndpoint("mock:foo").expectedMessageCount(0); getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:bar").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
@Test public void testMultipleAdviceWithExceptionThrown() throws Exception { context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { interceptSendToEndpoint("mock:resultA").process(new Processor() { @Override public void process(Exchange exchange) throws Exception { throw new Exception("my exception"); } }); } }); context.start(); getMockEndpoint("mock:resultA").expectedMessageCount(0); template.sendBody("direct:startA", "a trigger"); assertMockEndpointsSatisfied(); }
public void testBefore() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveByToString(".*bar.*").before().to("mock:a").transform(constant("Bye World")); } }); getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:a").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:bar").expectedBodiesReceived("Bye World"); getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testAfter() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveByToString(".*bar.*").after().to("mock:a").transform(constant("Bye World")); } }); getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:a").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testReplace() throws Exception { // START SNIPPET: e1 context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // weave by type in the route // and replace it with the following route path weaveByType(LogDefinition.class).replace().multicast().to("mock:a").to("mock:b"); } }); // END SNIPPET: e1 getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); template.sendBody("direct:start", "World"); assertMockEndpointsSatisfied(); }
public void testRemove() throws Exception { // START SNIPPET: e2 context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // weave the type in the route and remove it weaveByType(TransformDefinition.class).remove(); } }); // END SNIPPET: e2 getMockEndpoint("mock:result").expectedBodiesReceived("World"); template.sendBody("direct:start", "World"); assertMockEndpointsSatisfied(); }
@Test public void testAdviceWithAddLast() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveAddLast().to("mock:last"); } }); context.start(); MockEndpoint mockLast = getMockEndpoint("mock:last"); mockLast.expectedBodiesReceived("bar"); mockLast.setExpectedMessageCount(1); template.sendBody("seda:start", "bar"); assertMockEndpointsSatisfied(); }
public void testAfter() throws Exception { // START SNIPPET: e4 context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // weave the type in the route and remove it // and insert the following route path after the adviced node weaveByType(ToDefinition.class).after().transform(constant("Bye World")); } }); // END SNIPPET: e4 getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); Object out = template.requestBody("direct:start", "World"); assertEquals("Bye World", out); assertMockEndpointsSatisfied(); }
public void testSelectFirst() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // should only match the first weaveById("gold*").selectFirst().replace().multicast().to("mock:a").to("mock:b"); } }); getMockEndpoint("mock:foo").expectedMessageCount(0); getMockEndpoint("mock:bar").expectedMessageCount(1); getMockEndpoint("mock:baz").expectedMessageCount(1); getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testSelectLast() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // should only match the last weaveById("gold*").selectLast().replace().multicast().to("mock:a").to("mock:b"); } }); getMockEndpoint("mock:foo").expectedMessageCount(1); getMockEndpoint("mock:bar").expectedMessageCount(1); getMockEndpoint("mock:baz").expectedMessageCount(0); getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testSelectIndexZero() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // should match the first index (0 based) weaveById("gold*").selectIndex(0).replace().multicast().to("mock:a").to("mock:b"); } }); getMockEndpoint("mock:foo").expectedMessageCount(0); getMockEndpoint("mock:bar").expectedMessageCount(1); getMockEndpoint("mock:baz").expectedMessageCount(1); getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testSelectIndexOne() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // should match the second index (0 based) weaveById("gold*").selectIndex(1).replace().multicast().to("mock:a").to("mock:b"); } }); getMockEndpoint("mock:foo").expectedMessageCount(1); getMockEndpoint("mock:bar").expectedMessageCount(0); getMockEndpoint("mock:baz").expectedMessageCount(1); getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testSelectIndexTwo() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // should match the third index (0 based) weaveById("gold*").selectIndex(2).replace().multicast().to("mock:a").to("mock:b"); } }); getMockEndpoint("mock:foo").expectedMessageCount(1); getMockEndpoint("mock:bar").expectedMessageCount(1); getMockEndpoint("mock:baz").expectedMessageCount(0); getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
@Test public void shouldMockEndpoints() throws Exception { camelContext.getRouteDefinitions().get(0).adviceWith(camelContext, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { replaceFromWith("seda:start"); weaveAddLast().to("mock:result"); } }); camelContext.start(); MockEndpoint mock = camelContext.getEndpoint("mock:result", MockEndpoint.class); // Given String msg = "msg"; mock.expectedBodiesReceived(msg); // When producerTemplate.sendBody("seda:start", msg); // Then mock.assertIsSatisfied(); }
public void testSelectRangeZeroOne() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // should match the first two (0-based) weaveById("gold*").selectRange(0, 1).replace().multicast().to("mock:a").to("mock:b"); } }); getMockEndpoint("mock:foo").expectedMessageCount(0); getMockEndpoint("mock:bar").expectedMessageCount(0); getMockEndpoint("mock:baz").expectedMessageCount(1); getMockEndpoint("mock:a").expectedMessageCount(2); getMockEndpoint("mock:b").expectedMessageCount(2); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testSelectRangeOneTwo() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // should match the 2nd and 3rd (0-based) weaveById("gold*").selectRange(1, 2).replace().multicast().to("mock:a").to("mock:b"); } }); getMockEndpoint("mock:foo").expectedMessageCount(1); getMockEndpoint("mock:bar").expectedMessageCount(0); getMockEndpoint("mock:baz").expectedMessageCount(0); getMockEndpoint("mock:a").expectedMessageCount(2); getMockEndpoint("mock:b").expectedMessageCount(2); getMockEndpoint("mock:result").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }
public void testBeforeWithOnException() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // weave the node in the route which has id = bar // and insert the following route path before the adviced node weaveById("bar").before().to("mock:a").transform(constant("Bye World")); } }); getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:a").expectedBodiesReceived("Hello World"); getMockEndpoint("mock:bar").expectedBodiesReceived("Bye World"); getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); }