Reputation: 1
I need help to create a rule in simple apache camel where I can validate if an integer value is less than or equal, however, I can't make it work, as below:
${in.header.name} == null and ${in.header.id} <= 50
through the documentation I tested the function above and it does not work
Upvotes: 0
Views: 966
Reputation: 2197
Instead of and
you should use &&
you might also want to drop out the in part from the ${in.header.name}
and use ${header.name}
instead.
I also recommend using Predicates with Java-DSL over simple language strings. They're less prone to typos than simple-language strings and one can build fairly complex predicates using PredicateBuilder
.
Example test using simple-lang and predicates:
package org.example;
import java.util.HashMap;
import org.apache.camel.Predicate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Test;
public class ComparisonTest extends CamelTestSupport {
@Test
public void simpleComparisonSixIsGreater() throws Exception {
MockEndpoint isGreaterMockEndpoint = getMockEndpoint("mock:isGreater");
isGreaterMockEndpoint.expectedMessageCount(1);
HashMap<String, Object> headers = createHashmapForNameAndIdHeaders(null, 6);
template.sendBodyAndHeaders("direct:simpleComparison", null, headers);
isGreaterMockEndpoint.assertIsSatisfied();
}
@Test
public void simpleComparisonThreeIsLowerThan() throws Exception {
MockEndpoint isLowerMockEndpoint = getMockEndpoint("mock:isLower");
isLowerMockEndpoint.expectedMessageCount(1);
HashMap<String, Object> headers = createHashmapForNameAndIdHeaders(null, 3);
template.sendBodyAndHeaders("direct:simpleComparison", null, headers);
isLowerMockEndpoint.assertIsSatisfied();
}
@Test
public void predicateComparisonSixIsGreater() throws Exception {
MockEndpoint isGreaterMockEndpoint = getMockEndpoint("mock:isGreater");
isGreaterMockEndpoint.expectedMessageCount(1);
HashMap<String, Object> headers = createHashmapForNameAndIdHeaders(null, 6);
template.sendBodyAndHeaders("direct:predicateComparison", null, headers);
isGreaterMockEndpoint.assertIsSatisfied();
}
@Test
public void predicateComparisonThreeIsLowerThan() throws Exception {
MockEndpoint isLowerMockEndpoint = getMockEndpoint("mock:isLower");
isLowerMockEndpoint.expectedMessageCount(1);
HashMap<String, Object> headers = createHashmapForNameAndIdHeaders(null, 3);
template.sendBodyAndHeaders("direct:predicateComparison", null, headers);
isLowerMockEndpoint.assertIsSatisfied();
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
// Using simple language
from("direct:simpleComparison")
.routeId("simpleComparison")
.choice().when(simple("${header.name} == null && ${header.id} > 5"))
.log("Body value is greater than 5")
.to("mock:isGreater")
.otherwise()
.log("Body value is less than 5")
.to("mock:isLower")
.end()
;
// Using predicate
Predicate nameIsNullAndIdGreaterThanFive = PredicateBuilder.and(
header("name").isEqualTo(null),
header("id").isGreaterThan(5)
);
from("direct:predicateComparison")
.routeId("predicateComparison")
.choice().when(nameIsNullAndIdGreaterThanFive)
.log("Body value is greater than 5")
.to("mock:isGreater")
.otherwise()
.log("Body value is less than 5")
.to("mock:isLower")
.end();
}
};
}
private HashMap<String, Object> createHashmapForNameAndIdHeaders(String name, Integer id) {
HashMap<String, Object> headers = new HashMap<>();
headers.put("name", name);
headers.put("id", id);
return headers;
}
}
Upvotes: 1