You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/01/26 13:02:55 UTC

[camel] branch camel-2.20.x updated: CAMEL-12196: fixed mock endpoint when using matches() DSL would always evaluate as true. The right hand side was never taking into account before.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-2.20.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.20.x by this push:
     new 004e1fb  CAMEL-12196: fixed mock endpoint when using matches() DSL would always evaluate as true. The right hand side was never taking into account before.
004e1fb is described below

commit 004e1fb9da7dc5952403c57a45b925f17cc2ae02
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Jan 26 13:51:11 2018 +0100

    CAMEL-12196: fixed mock endpoint when using matches() DSL would always evaluate as true. The right hand side was never taking into account before.
---
 .../org/apache/camel/builder/ValueBuilder.java     | 39 ++++++++++-
 .../apache/camel/builder/xml/XPathMockTest.java    | 11 +++-
 .../camel/component/mock/MockEndpointTest.java     | 60 +++++++++++++++++
 .../camel/jsonpath/JsonPathMockMatchesTest.java    | 75 ++++++++++++++++++++++
 4 files changed, 183 insertions(+), 2 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
index eb87b11..f7f3a82 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
@@ -19,12 +19,14 @@ package org.apache.camel.builder;
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
 import org.apache.camel.builder.xml.Namespaces;
 import org.apache.camel.spi.NamespaceAware;
+import org.apache.camel.support.ExpressionAdapter;
 import org.apache.camel.util.ExpressionToPredicateAdapter;
 
 /**
@@ -67,7 +69,42 @@ public class ValueBuilder implements Expression, Predicate {
     }
 
     public ExpressionClause<Predicate> matches() {
-        return new ExpressionClause<Predicate>(onNewPredicate(ExpressionToPredicateAdapter.toPredicate(expression))); 
+        // chicken-and-egg situation as we need to return an ExpressionClause
+        // which needs a right-hand side that is being built via the fluent
+        // builder that is returned, and therefore we need to use a ref
+        // to the expression (right hand side) that will be used below
+        // in the onNewPredicate where the actual matching is executed
+        final AtomicReference<Expression> ref = new AtomicReference<>();
+
+        final ExpressionClause<Predicate> answer = new ExpressionClause<>(
+            onNewPredicate(new Predicate() {
+                @Override
+                public boolean matches(Exchange exchange) {
+                    Expression left = expression;
+                    Expression right = ref.get();
+                    return PredicateBuilder.isEqualTo(left, right).matches(exchange);
+                }
+
+                @Override
+                public String toString() {
+                    return expression + " == " + ref.get();
+                }
+            }));
+
+        final Expression right = new ExpressionAdapter() {
+            @Override
+            public Object evaluate(Exchange exchange) {
+                if (answer.getExpressionValue() != null) {
+                    return answer.getExpressionValue().evaluate(exchange, Object.class);
+                } else {
+                    return answer.getExpressionType().evaluate(exchange);
+                }
+            }
+        };
+        // okay now we can set the reference to the right-hand-side
+        ref.set(right);
+
+        return answer;
     }
 
     public Predicate isNotEqualTo(Object value) {
diff --git a/camel-core/src/test/java/org/apache/camel/builder/xml/XPathMockTest.java b/camel-core/src/test/java/org/apache/camel/builder/xml/XPathMockTest.java
index da9ddd6..a2b2e03 100644
--- a/camel-core/src/test/java/org/apache/camel/builder/xml/XPathMockTest.java
+++ b/camel-core/src/test/java/org/apache/camel/builder/xml/XPathMockTest.java
@@ -39,13 +39,22 @@ public class XPathMockTest extends ContextTestSupport {
 
     public void testXPathMock2() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.message(0).body().matches().xpath("/foo/text() = 'Hello World'");
+        mock.message(0).predicate().xpath("/foo/text() = 'Hello World'");
 
         template.sendBody("direct:start", "<foo>Hello World</foo>");
 
         assertMockEndpointsSatisfied();
     }
 
+    public void testXPathMock2Fail() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.message(0).predicate().xpath("/foo/text() = 'Bye World'");
+
+        template.sendBody("direct:start", "<foo>Hello World</foo>");
+
+        mock.assertIsNotSatisfied();
+    }
+
     public void testXPathMock3() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.message(0).predicate().xpath("/foo/text() = 'Hello World'");
diff --git a/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java b/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
index 860d2cd7..73a81b6 100644
--- a/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
@@ -680,6 +680,66 @@ public class MockEndpointTest extends ContextTestSupport {
         assertMockEndpointsSatisfied();
     }
 
+    public void testNotExchangePattern() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.message(0).exchangePattern().isEqualTo(ExchangePattern.InOnly);
+        mock.message(1).exchangePattern().isEqualTo(ExchangePattern.InOnly);
+
+        template.sendBody("direct:a", "Hello World");
+        template.requestBody("direct:a", "Bye World");
+
+        mock.assertIsNotSatisfied();
+    }
+
+    public void testBodyPredicate() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.message(0).body().matches().constant("Hello World");
+        mock.message(1).body().matches().constant("Bye World");
+
+        template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
+        template.sendBodyAndHeader("direct:a", "Bye World", "bar", 234);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testNotBodyPredicate() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.message(0).body().matches().constant("Hello World");
+        mock.message(1).body().matches().constant("Hi World");
+
+        template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
+        template.sendBodyAndHeader("direct:a", "Bye World", "bar", 234);
+
+        mock.assertIsNotSatisfied();
+    }
+
+    public void testHeaderPredicate() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.message(0).header("foo").matches().constant(123);
+        mock.message(1).header("bar").matches().constant(234);
+
+        template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
+        template.sendBodyAndHeader("direct:a", "Bye World", "bar", 234);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testNotHeaderPredicate() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.message(0).header("foo").matches().constant(123);
+        mock.message(1).header("bar").matches().constant(666);
+
+        template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
+        template.sendBodyAndHeader("direct:a", "Bye World", "bar", 234);
+
+        mock.assertIsNotSatisfied();
+    }
+
     public void testExpectedExchangePattern() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathMockMatchesTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathMockMatchesTest.java
new file mode 100644
index 0000000..f6f57bf
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathMockMatchesTest.java
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.jsonpath;
+
+import java.io.File;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JsonPathMockMatchesTest extends CamelTestSupport {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("mock:result");
+            }
+        };
+    }
+
+    @Test
+    public void testMockMatches() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(3);
+        // cheap
+        mock.message(0).predicate().jsonpath("$.store.book[?(@.price < 10)]");
+        // average
+        mock.message(2).predicate().jsonpath("$.store.book[?(@.price < 30)]");
+        // expensive
+        mock.message(1).predicate().jsonpath("$.store.book[?(@.price > 30)]");
+
+        template.sendBody("direct:start", new File("src/test/resources/cheap.json"));
+        template.sendBody("direct:start", new File("src/test/resources/expensive.json"));
+        template.sendBody("direct:start", new File("src/test/resources/average.json"));
+
+        mock.assertIsSatisfied();
+    }
+
+    @Test
+    public void testMockNotMatches() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(3);
+        // cheap
+        mock.message(0).predicate().jsonpath("$.store.book[?(@.price < 10)]");
+        // average
+        mock.message(1).predicate().jsonpath("$.store.book[?(@.price < 30)]");
+        // expensive
+        mock.message(2).predicate().jsonpath("$.store.book[?(@.price > 30)]");
+
+        template.sendBody("direct:start", new File("src/test/resources/cheap.json"));
+        template.sendBody("direct:start", new File("src/test/resources/expensive.json"));
+        template.sendBody("direct:start", new File("src/test/resources/average.json"));
+
+        mock.assertIsNotSatisfied();
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.