You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2017/11/24 13:38:40 UTC

[camel] branch camel-2.20.x updated: CAMEL-11532 Java DSL enhancement to accept supplier and function arguments (#2098)

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

lburgazzoli 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 1a26e25  CAMEL-11532 Java DSL enhancement to accept supplier and function arguments (#2098)
1a26e25 is described below

commit 1a26e25343803e32aa88503831e843e6766ee8d9
Author: Ioannis Sermetziadis <i....@gmail.com>
AuthorDate: Fri Nov 24 15:38:35 2017 +0200

    CAMEL-11532 Java DSL enhancement to accept supplier and function arguments (#2098)
    
    * CAMEL-11532: DSL enhancements to support Java 8 supplier and function
    arguments for setBody and transform expressions.
    
    * CAMEL-11532 Removed inMessage expression clause because it is of no use. Fixed unit tests, style errors & missing license.
    
    * CAMEL-11532 Added missing generics.
    
    * CAMEL-11532 Improved missing generics.
---
 .../org/apache/camel/builder/ExpressionClause.java | 12 +++++
 .../apache/camel/model/ProcessorDefinition.java    | 37 ++++++++++++++
 .../builder/ExpressionClauseSupplierTest.java      | 48 +++++++++++++++++++
 .../camel/model/ProcessDefinitionSetBodyTest.java  | 56 ++++++++++++++++++++++
 4 files changed, 153 insertions(+)

diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
index 9e3106a..16714d4 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
@@ -20,6 +20,7 @@ import java.util.Map;
 import java.util.function.BiFunction;
 import java.util.function.Function;
 
+import java.util.function.Supplier;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Message;
@@ -151,6 +152,17 @@ public class ExpressionClause<T> extends ExpressionDefinition {
     }
 
     /**
+     * A functional expression of an inbound message body
+     */
+    public T body(final Supplier<Object> supplier) {
+        return delegate.expression(new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                return supplier.get();
+            }
+        });
+    }
+
+    /**
      * A functional expression of an inbound message body and headers
      */
     public T body(final BiFunction<Object, Map<String, Object>, Object> function) {
diff --git a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
index 0ac2474..594f68d 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
@@ -28,6 +28,7 @@ import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
 import java.util.function.Supplier;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
@@ -3053,6 +3054,42 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
 
     /**
      * <a href="http://camel.apache.org/message-translator.html">Message Translator EIP:</a>
+     * Adds a processor which sets the body on the IN message
+     *
+     * @param supplier   the supplier that provides a value to the IN message body
+     * @return the builder
+     */
+    public <Result> Type setBody(Supplier<Result> supplier) {
+        SetBodyDefinition answer = new SetBodyDefinition(new ExpressionAdapter() {
+            @Override
+            public Result evaluate(Exchange exchange) {
+                return supplier.get();
+            }
+        });
+        addOutput(answer);
+        return (Type) this;
+    }
+
+    /**
+     * <a href="http://camel.apache.org/message-translator.html">Message Translator EIP:</a>
+     * Adds a processor which sets the body on the IN message
+     *
+     * @param function   the function that provides a value to the IN message body
+     * @return the builder
+     */
+    public <Result> Type setBody(Function<Exchange, Result> function) {
+        SetBodyDefinition answer = new SetBodyDefinition(new ExpressionAdapter() {
+            @Override
+            public Result evaluate(Exchange exchange) {
+                return function.apply(exchange);
+            }
+        });
+        addOutput(answer);
+        return (Type) this;
+    }
+
+    /**
+     * <a href="http://camel.apache.org/message-translator.html">Message Translator EIP:</a>
      * Adds a processor which sets the body on the OUT message
      *
      * @param expression   the expression used to set the body
diff --git a/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.java b/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.java
new file mode 100644
index 0000000..f104e95
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.java
@@ -0,0 +1,48 @@
+/**
+ * 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.builder;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+
+public class ExpressionClauseSupplierTest extends ContextTestSupport {
+
+    private static final String BODY_SUPPLIER_MSG = "I am the body supplier!";
+
+    public void testBodySupplier() throws Exception {
+        MockEndpoint functionMock1 = getMockEndpoint("mock:output1");
+        functionMock1.expectedMessageCount(1);
+        functionMock1.expectedBodyReceived().constant(BODY_SUPPLIER_MSG);
+
+        template.sendBody("direct:supplier1", "are you there?");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:supplier1")
+                    .transform().body(() -> BODY_SUPPLIER_MSG)
+                    .to("mock:output1");
+            }
+        };
+    }
+
+}
diff --git a/camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java b/camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java
new file mode 100644
index 0000000..b024808
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java
@@ -0,0 +1,56 @@
+/**
+ * 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.model;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+public class ProcessDefinitionSetBodyTest extends ContextTestSupport {
+
+    private static final String SUPPLIER_MESSAGE = "Hello from a Supplier!";
+    private static final String FUNCTION_MESSAGE = "Hello from a Function!";
+
+    public void testProcessDefinitionSetBody() throws InterruptedException {
+
+        MockEndpoint functionMock1 = getMockEndpoint("mock:supplierOutput");
+        functionMock1.expectedMessageCount(1);
+        functionMock1.expectedBodyReceived().constant(SUPPLIER_MESSAGE);
+        MockEndpoint functionMock2 = getMockEndpoint("mock:functionOutput");
+        functionMock2.expectedMessageCount(1);
+        functionMock2.expectedBodyReceived().constant(FUNCTION_MESSAGE);
+
+        template.sendBody("direct:start", "are you there?");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .setBody(() -> SUPPLIER_MESSAGE)
+                    .to("mock:supplierOutput")
+                    .setBody(exchange -> FUNCTION_MESSAGE)
+                    .to("mock:functionOutput")
+                    .to("mock:output");
+            }
+        };
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <co...@camel.apache.org>'].