Search in sources :

Example 56 with Predicate

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

the class BeanProcessorSpecializedMessageTest method testBeanSpecializedMessage.

public void testBeanSpecializedMessage() throws Exception {
    MockEndpoint foo = getMockEndpoint("mock:foo");
    foo.expectedBodiesReceived("Hello World");
    foo.expectedHeaderReceived("foo", 123);
    foo.message(0).predicate(new Predicate() {

        public boolean matches(Exchange exchange) {
            // this time we should have the specialized message
            return exchange.getIn() instanceof MyMessage;
        }
    });
    MockEndpoint result = getMockEndpoint("mock:result");
    result.message(0).body().isNull();
    result.expectedHeaderReceived("foo", 123);
    result.message(0).predicate(new Predicate() {

        public boolean matches(Exchange exchange) {
            // this time we should have lost the specialized message
            return !(exchange.getIn() instanceof MyMessage);
        }
    });
    template.send("direct:start", new Processor() {

        public void process(Exchange exchange) throws Exception {
            MyMessage my = new MyMessage();
            my.setBody("Hello World");
            my.setHeader("foo", 123);
            exchange.setIn(my);
        }
    });
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Predicate(org.apache.camel.Predicate)

Example 57 with Predicate

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

the class OnExceptionContinueTwoPredicateTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // use a predicate instance
            Predicate predicate = new Predicate() {

                @Override
                public boolean matches(Exchange exchange) {
                    predicateInvoked.incrementAndGet();
                    return true;
                }
            };
            // tell Camel to handle and continue when this exception is thrown
            onException(IllegalArgumentException.class).continued(predicate).process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    processorInvoked.incrementAndGet();
                }
            }).to("mock:me");
            from("direct:start").to("mock:start").throwException(new IllegalArgumentException("Forced")).to("mock:middle").throwException(new IllegalArgumentException("Forced Again")).to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Predicate(org.apache.camel.Predicate)

Example 58 with Predicate

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

the class CamelConverterTest method testToProcessorPredicate.

public void testToProcessorPredicate() throws Exception {
    Predicate pred = PredicateBuilder.isEqualTo(headerExpression("foo"), constant("bar"));
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setHeader("foo", "bar");
    exchange.getIn().setBody("Hello World");
    Processor pro = CamelConverter.toProcessor(pred);
    pro.process(exchange);
    assertEquals(true, exchange.getOut().getBody());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Processor(org.apache.camel.Processor) Predicate(org.apache.camel.Predicate)

Example 59 with Predicate

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

the class SimpleParserRegexpPredicateTest method testSimpleRegexp.

public void testSimpleRegexp() throws Exception {
    exchange.getIn().setBody("12.34.5678");
    SimplePredicateParser parser = new SimplePredicateParser("${body} regex '^\\d{2}\\.\\d{2}\\.\\d{4}$'", true);
    Predicate pre = parser.parsePredicate();
    assertTrue(pre.matches(exchange));
    exchange.getIn().setBody("12.2a.22ab");
    assertFalse(pre.matches(exchange));
}
Also used : Predicate(org.apache.camel.Predicate)

Example 60 with Predicate

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

the class SimpleBackwardsCompatibleTest method testSimpleLogicalAnd.

public void testSimpleLogicalAnd() throws Exception {
    exchange.getIn().setBody("Hello");
    exchange.getIn().setHeader("high", true);
    exchange.getIn().setHeader("foo", 123);
    SimplePredicateParser parser = new SimplePredicateParser("${header.high} == true and ${header.foo} == 123", true);
    Predicate pre = parser.parsePredicate();
    assertTrue("Should match", pre.matches(exchange));
}
Also used : Predicate(org.apache.camel.Predicate)

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