Search in sources :

Example 6 with Predicate

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

the class EtcdKeysTest method testKeys.

@Test(expected = EtcdException.class)
public void testKeys() throws Exception {
    final String path = "/camel/" + UUID.randomUUID().toString();
    final String value = UUID.randomUUID().toString();
    final EtcdClient client = getClient();
    final Map<String, Object> headers = new HashMap<>();
    // *******************************************
    // SET
    // *******************************************
    headers.clear();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_SET);
    headers.put(EtcdConstants.ETCD_PATH, path);
    sendBody("direct:keys-set", value, headers);
    MockEndpoint mockSet = getMockEndpoint("mock:result-set");
    mockSet.expectedMinimumMessageCount(1);
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
    mockSet.assertIsSatisfied();
    // *******************************************
    // GET
    // *******************************************
    headers.clear();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_GET);
    headers.put(EtcdConstants.ETCD_PATH, path);
    sendBody("direct:keys-get", value, headers);
    MockEndpoint mockGet = getMockEndpoint("mock:result-get");
    mockGet.expectedMinimumMessageCount(1);
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
    mockGet.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
    mockGet.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            EtcdKeysResponse keysResponse = exchange.getIn().getBody(EtcdKeysResponse.class);
            assertNotNull(keysResponse);
            assertNotNull(keysResponse.node);
            assertNotNull(keysResponse.node.value);
            return keysResponse.node.value.equals(value);
        }
    });
    mockGet.assertIsSatisfied();
    // *******************************************
    // DELETE
    // *******************************************
    headers.clear();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_DELETE);
    headers.put(EtcdConstants.ETCD_PATH, path);
    sendBody("direct:keys-del", "value", headers);
    MockEndpoint mockDel = getMockEndpoint("mock:result-del");
    mockDel.expectedMinimumMessageCount(1);
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
    mockDel.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
    mockDel.assertIsSatisfied();
    // *******************************************
    // VALIDATION
    // *******************************************
    client.get(path).send().get();
    fail("EtcdException should have been thrown");
}
Also used : Exchange(org.apache.camel.Exchange) EtcdClient(mousio.etcd4j.EtcdClient) HashMap(java.util.HashMap) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) Predicate(org.apache.camel.Predicate) Test(org.junit.Test)

Example 7 with Predicate

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

the class EtcdStatsTest method testStatsConsumer.

protected void testStatsConsumer(String mockEnpoint, String expectedPath, final Class<?> expectedType) throws Exception {
    MockEndpoint mock = getMockEndpoint(mockEnpoint);
    mock.expectedMinimumMessageCount(1);
    mock.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.stats.name());
    mock.expectedHeaderReceived(EtcdConstants.ETCD_PATH, expectedPath);
    mock.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            return exchange.getIn().getBody().getClass() == expectedType;
        }
    });
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Predicate(org.apache.camel.Predicate)

Example 8 with Predicate

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

the class AssertionClause method applyAssertionOn.

/**
     * Performs any assertions on the given exchange
     */
protected void applyAssertionOn(MockEndpoint endpoint, int index, Exchange exchange) {
    for (Predicate predicate : predicates) {
        currentIndex = index;
        PredicateAssertHelper.assertMatches(predicate, "Assertion error at index " + index + " on mock " + endpoint.getEndpointUri() + " with predicate: ", exchange);
    }
}
Also used : Predicate(org.apache.camel.Predicate)

Example 9 with Predicate

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

the class RefLanguage method createExpression.

public Expression createExpression(final String expression) {
    final Expression exp = RefLanguage.ref(expression);
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            Expression target = null;
            Object lookup = exp.evaluate(exchange, Object.class);
            // must favor expression over predicate
            if (lookup != null && lookup instanceof Expression) {
                target = (Expression) lookup;
            } else if (lookup != null && lookup instanceof Predicate) {
                target = PredicateToExpressionAdapter.toExpression((Predicate) lookup);
            }
            if (target != null) {
                return target.evaluate(exchange, Object.class);
            } else {
                throw new IllegalArgumentException("Cannot find expression or predicate in registry with ref: " + expression);
            }
        }

        public String toString() {
            return exp.toString();
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter) PredicateToExpressionAdapter(org.apache.camel.util.PredicateToExpressionAdapter) Predicate(org.apache.camel.Predicate)

Example 10 with Predicate

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

the class BinaryExpression method createInExpression.

private Expression createInExpression(final Expression leftExp, final Expression rightExp) {
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            // okay the in operator is a bit more complex as we need to build a list of values
            // from the right hand side expression.
            // each element on the right hand side must be separated by comma (default for create iterator)
            Iterator<Object> it = ObjectHelper.createIterator(rightExp.evaluate(exchange, Object.class));
            List<Object> values = new ArrayList<Object>();
            while (it.hasNext()) {
                values.add(it.next());
            }
            // then reuse value builder to create the in predicate with the list of values
            ValueBuilder vb = new ValueBuilder(leftExp);
            Predicate predicate = vb.in(values.toArray());
            if (operator == BinaryOperatorType.NOT_IN) {
                predicate = PredicateBuilder.not(predicate);
            }
            boolean answer = predicate.matches(exchange);
            return exchange.getContext().getTypeConverter().convertTo(type, answer);
        }

        @Override
        public String toString() {
            return left + " " + token.getText() + " " + right;
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) ValueBuilder(org.apache.camel.builder.ValueBuilder) Expression(org.apache.camel.Expression) ArrayList(java.util.ArrayList) 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