Search in sources :

Example 16 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class XPathTest method assertPredicate.

protected void assertPredicate(String xpath, String xml, boolean expected) {
    Predicate predicate = XPathBuilder.xpath(xpath);
    assertPredicate(predicate, createExchange(xml), expected);
}
Also used : Predicate(org.apache.camel.Predicate)

Example 17 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class RouteScopedOnExceptionWithInterceptSendToEndpointIssueWithPredicateTest method testIssue.

public void testIssue() throws Exception {
    final Predicate fail = PredicateBuilder.or(header(Exchange.REDELIVERY_COUNTER).isNull(), header(Exchange.REDELIVERY_COUNTER).isLessThan(5));
    RouteDefinition route = context.getRouteDefinitions().get(0);
    route.adviceWith(context, new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("seda:*").skipSendToOriginalEndpoint().process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    invoked.incrementAndGet();
                    if (fail.matches(exchange)) {
                        throw new ConnectException("Forced");
                    }
                }
            }).to("mock:ok");
        }
    });
    getMockEndpoint("mock:global").expectedMessageCount(0);
    getMockEndpoint("mock:ok").expectedMessageCount(1);
    getMockEndpoint("mock:exhausted").expectedMessageCount(0);
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    // 5 retry + 1 ok
    assertEquals(6, invoked.get());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteDefinition(org.apache.camel.model.RouteDefinition) RouteBuilder(org.apache.camel.builder.RouteBuilder) ConnectException(java.net.ConnectException) Predicate(org.apache.camel.Predicate) ConnectException(java.net.ConnectException)

Example 18 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class AggregateShouldSkipFilteredExchangesTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            Predicate goodWord = body().contains("World");
            from("direct:start").filter(goodWord).to("mock:filtered").aggregate(header("id"), new MyAggregationStrategy()).completionTimeout(1000).to("mock:result").end().end();
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) Predicate(org.apache.camel.Predicate)

Example 19 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class AggregateProcessorTest method testAggregateProcessorCompletionPredicateEager.

public void testAggregateProcessorCompletionPredicateEager() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+END");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "predicate");
    Processor done = new SendProcessor(context.getEndpoint("mock:result"));
    Expression corr = header("id");
    AggregationStrategy as = new BodyInAggregatingStrategy();
    Predicate complete = body().isEqualTo("END");
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setCompletionPredicate(complete);
    ap.setEagerCheckCompletion(true);
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("END");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    assertMockEndpointsSatisfied();
    ap.stop();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) SendProcessor(org.apache.camel.processor.SendProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Expression(org.apache.camel.Expression) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) SendProcessor(org.apache.camel.processor.SendProcessor) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) Predicate(org.apache.camel.Predicate) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor)

Example 20 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class AggregateProcessorTest method testAggregateProcessorCompletionPredicate.

public void testAggregateProcessorCompletionPredicate() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("A+B+END");
    mock.expectedPropertyReceived(Exchange.AGGREGATED_COMPLETED_BY, "predicate");
    Processor done = new SendProcessor(context.getEndpoint("mock:result"));
    Expression corr = header("id");
    AggregationStrategy as = new BodyInAggregatingStrategy();
    Predicate complete = body().contains("END");
    AggregateProcessor ap = new AggregateProcessor(context, done, corr, as, executorService, true);
    ap.setCompletionPredicate(complete);
    ap.setEagerCheckCompletion(false);
    ap.start();
    Exchange e1 = new DefaultExchange(context);
    e1.getIn().setBody("A");
    e1.getIn().setHeader("id", 123);
    Exchange e2 = new DefaultExchange(context);
    e2.getIn().setBody("B");
    e2.getIn().setHeader("id", 123);
    Exchange e3 = new DefaultExchange(context);
    e3.getIn().setBody("END");
    e3.getIn().setHeader("id", 123);
    Exchange e4 = new DefaultExchange(context);
    e4.getIn().setBody("D");
    e4.getIn().setHeader("id", 123);
    ap.process(e1);
    ap.process(e2);
    ap.process(e3);
    ap.process(e4);
    assertMockEndpointsSatisfied();
    ap.stop();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) SendProcessor(org.apache.camel.processor.SendProcessor) Processor(org.apache.camel.Processor) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Expression(org.apache.camel.Expression) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy) SendProcessor(org.apache.camel.processor.SendProcessor) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy) Predicate(org.apache.camel.Predicate) AggregateProcessor(org.apache.camel.processor.aggregate.AggregateProcessor)

Aggregations

Predicate (org.apache.camel.Predicate)124 Exchange (org.apache.camel.Exchange)70 Test (org.junit.Test)40 HashMap (java.util.HashMap)25 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)23 Expression (org.apache.camel.Expression)22 Processor (org.apache.camel.Processor)19 DefaultExchange (org.apache.camel.impl.DefaultExchange)10 RouteBuilder (org.apache.camel.builder.RouteBuilder)9 File (java.io.File)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Map (java.util.Map)7 Ignore (org.junit.Ignore)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)6 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)6 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)6 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)5