From 61bedfc26ccb2af48929d3ef3c53101f0d438636 Mon Sep 17 00:00:00 2001 From: Ioannis Sermetziadis Date: Mon, 20 Nov 2017 00:05:28 +0200 Subject: [PATCH 1/4] CAMEL-11532: DSL enhancements to support Java 8 supplier and function arguments for setBody and transform expressions. --- .../camel/builder/ExpressionClause.java | 23 +++++++++ .../camel/model/ProcessorDefinition.java | 37 ++++++++++++++ .../builder/ExpressionClauseSupplierTest.java | 48 +++++++++++++++++++ .../model/ProcessDefinitionSetBodyTest.java | 40 ++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.java create mode 100644 camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java 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 9e3106a9a9fdb..c5930013d1a62 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.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; @@ -114,6 +115,17 @@ public Object evaluate(Exchange exchange) { }); } + /** + * A functional expression of an inbound message + */ + public T inMessage(final Supplier supplier) { + return delegate.expression(new ExpressionAdapter() { + public Object evaluate(Exchange exchange) { + return supplier.get(); + } + }); + } + /** * An expression of an outbound message */ @@ -150,6 +162,17 @@ public Object evaluate(Exchange exchange) { }); } + /** + * A functional expression of an inbound message body + */ + public T body(final Supplier supplier) { + return delegate.expression(new ExpressionAdapter() { + public Object evaluate(Exchange exchange) { + return supplier.get(); + } + }); + } + /** * A functional expression of an inbound message body and headers */ 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 0ac2474afaa48..056676c5560d0 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.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; @@ -3051,6 +3052,42 @@ public Type setBody(Expression expression) { return (Type) this; } + /** + * Message Translator EIP: + * 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 Type setBody(Supplier supplier) { + SetBodyDefinition answer = new SetBodyDefinition(new ExpressionAdapter() { + @Override + public Object evaluate(Exchange exchange) { + return supplier.get(); + } + }); + addOutput(answer); + return (Type) this; + } + + /** + * Message Translator EIP: + * 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 Type setBody(Function function) { + SetBodyDefinition answer = new SetBodyDefinition(new ExpressionAdapter() { + @Override + public Object evaluate(Exchange exchange) { + return function.apply(exchange); + } + }); + addOutput(answer); + return (Type) this; + } + /** * Message Translator EIP: * Adds a processor which sets the body on the OUT message 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 0000000000000..3bd49f69fd1af --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.java @@ -0,0 +1,48 @@ +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!"; + private static final String INMESSAGE_SUPPLIER_MSG = "I am the in-message 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(); + } + + public void testInMessageSupplier() throws Exception { + MockEndpoint functionMock2 = getMockEndpoint("mock:output2"); + functionMock2.expectedMessageCount(1); + functionMock2.expectedBodyReceived().constant(INMESSAGE_SUPPLIER_MSG); + + template.sendBody("direct:supplier2", "who are you?"); + + 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"); + + from("direct:supplier2") + .transform().inMessage(() -> INMESSAGE_SUPPLIER_MSG) + .to("mock:output2"); + } + }; + } + +} 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 0000000000000..7ceecfa94e9cb --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java @@ -0,0 +1,40 @@ +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 final String SUPPLIER_MESSAGE = "Hello from the Supplier!"; + private final String FUNCTION_MESSAGE = "Hello from the 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"); + } + }; + } +} From 1797d841ed1e608386b7c402f06b2bf5c38e110c Mon Sep 17 00:00:00 2001 From: Ioannis Sermetziadis Date: Thu, 23 Nov 2017 23:41:02 +0200 Subject: [PATCH 2/4] CAMEL-11532 Removed inMessage expression clause because it is of no use. Fixed unit tests, style errors & missing license. --- .../camel/builder/ExpressionClause.java | 11 ------- .../builder/ExpressionClauseSupplierTest.java | 32 +++++++++---------- .../model/ProcessDefinitionSetBodyTest.java | 20 ++++++++++-- 3 files changed, 34 insertions(+), 29 deletions(-) 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 c5930013d1a62..16714d4e76491 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 @@ -115,17 +115,6 @@ public Object evaluate(Exchange exchange) { }); } - /** - * A functional expression of an inbound message - */ - public T inMessage(final Supplier supplier) { - return delegate.expression(new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return supplier.get(); - } - }); - } - /** * An expression of an outbound message */ 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 index 3bd49f69fd1af..f104e95208172 100644 --- a/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.java +++ b/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseSupplierTest.java @@ -1,3 +1,19 @@ +/** + * 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; @@ -6,8 +22,6 @@ public class ExpressionClauseSupplierTest extends ContextTestSupport { private static final String BODY_SUPPLIER_MSG = "I am the body supplier!"; - private static final String INMESSAGE_SUPPLIER_MSG = "I am the in-message supplier!"; - public void testBodySupplier() throws Exception { MockEndpoint functionMock1 = getMockEndpoint("mock:output1"); @@ -19,16 +33,6 @@ public void testBodySupplier() throws Exception { assertMockEndpointsSatisfied(); } - public void testInMessageSupplier() throws Exception { - MockEndpoint functionMock2 = getMockEndpoint("mock:output2"); - functionMock2.expectedMessageCount(1); - functionMock2.expectedBodyReceived().constant(INMESSAGE_SUPPLIER_MSG); - - template.sendBody("direct:supplier2", "who are you?"); - - assertMockEndpointsSatisfied(); - } - @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @@ -37,10 +41,6 @@ public void configure() throws Exception { from("direct:supplier1") .transform().body(() -> BODY_SUPPLIER_MSG) .to("mock:output1"); - - from("direct:supplier2") - .transform().inMessage(() -> INMESSAGE_SUPPLIER_MSG) - .to("mock:output2"); } }; } 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 index 7ceecfa94e9cb..b0248083557b4 100644 --- a/camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java +++ b/camel-core/src/test/java/org/apache/camel/model/ProcessDefinitionSetBodyTest.java @@ -1,3 +1,19 @@ +/** + * 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; @@ -6,8 +22,8 @@ public class ProcessDefinitionSetBodyTest extends ContextTestSupport { - private final String SUPPLIER_MESSAGE = "Hello from the Supplier!"; - private final String FUNCTION_MESSAGE = "Hello from the Function!"; + 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 { From 3409ec8bea95ec9394973291fb1c8d9dcc1102d0 Mon Sep 17 00:00:00 2001 From: Ioannis Sermetziadis Date: Fri, 24 Nov 2017 13:23:47 +0200 Subject: [PATCH 3/4] CAMEL-11532 Added missing generics. --- .../java/org/apache/camel/model/ProcessorDefinition.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 056676c5560d0..7123689c9d51c 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 @@ -3059,10 +3059,10 @@ public Type setBody(Expression expression) { * @param supplier the supplier that provides a value to the IN message body * @return the builder */ - public Type setBody(Supplier supplier) { + public Type setBody(Supplier supplier) { SetBodyDefinition answer = new SetBodyDefinition(new ExpressionAdapter() { @Override - public Object evaluate(Exchange exchange) { + public Result evaluate(Exchange exchange) { return supplier.get(); } }); @@ -3077,7 +3077,7 @@ public Object evaluate(Exchange exchange) { * @param function the function that provides a value to the IN message body * @return the builder */ - public Type setBody(Function function) { + public Type setBody(Function function) { SetBodyDefinition answer = new SetBodyDefinition(new ExpressionAdapter() { @Override public Object evaluate(Exchange exchange) { From cc6fd02f8e4d753591feca935e9b58ed97480a1a Mon Sep 17 00:00:00 2001 From: Ioannis Sermetziadis Date: Fri, 24 Nov 2017 13:28:43 +0200 Subject: [PATCH 4/4] CAMEL-11532 Improved missing generics. --- .../main/java/org/apache/camel/model/ProcessorDefinition.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7123689c9d51c..594f68dd91109 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 @@ -3080,7 +3080,7 @@ public Result evaluate(Exchange exchange) { public Type setBody(Function function) { SetBodyDefinition answer = new SetBodyDefinition(new ExpressionAdapter() { @Override - public Object evaluate(Exchange exchange) { + public Result evaluate(Exchange exchange) { return function.apply(exchange); } });