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 2021/08/03 12:33:39 UTC
[camel] 09/22: CAMEL-16833: LambdaEndpointRouteBuilder
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch CAMEL-16757b
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 69771aa7e887458b5ad127facd39e5c7df2416ce
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Aug 2 15:14:32 2021 +0200
CAMEL-16833: LambdaEndpointRouteBuilder
---
.../builder/endpoint/EndpointRouteBuilder.java | 6 +-
.../endpoint/LambdaEndpointRouteBuilder.java | 35 +++++++++++
.../endpoint/LambdaEndpointRouteBuilderTest.java | 70 ++++++++++++++++++++++
3 files changed, 108 insertions(+), 3 deletions(-)
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteBuilder.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteBuilder.java
index 649b86e..77ad7fc 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteBuilder.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/EndpointRouteBuilder.java
@@ -41,15 +41,15 @@ public abstract class EndpointRouteBuilder extends RouteBuilder implements Endpo
* Add routes to a context using a lambda expression. It can be used as following:
*
* <pre>
- * RouteBuilder.addRoutes(context, rb ->
- * rb.from(direct("inbound")).bean(MyBean.class)));
+ * EndpointRouteBuilder.addEndpointRoutes(context, rb ->
+ * rb.from(rb.direct("inbound")).bean(MyBean.class)));
* </pre>
*
* @param context the camel context to add routes
* @param rbc a lambda expression receiving the {@code RouteBuilder} to use for creating routes
* @throws Exception if an error occurs
*/
- public static void addEndpointRoutes(CamelContext context, ThrowingConsumer<EndpointRouteBuilder, Exception> rbc)
+ public static void addEndpointRoutes(CamelContext context, LambdaEndpointRouteBuilder rbc)
throws Exception {
context.addRoutes(new EndpointRouteBuilder(context) {
@Override
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/LambdaEndpointRouteBuilder.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/LambdaEndpointRouteBuilder.java
new file mode 100644
index 0000000..d03121c
--- /dev/null
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/LambdaEndpointRouteBuilder.java
@@ -0,0 +1,35 @@
+/*
+ * 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.endpoint;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.util.function.ThrowingConsumer;
+
+/**
+ * Functional interface for adding routes to a context using a lambda expression. It can be used as following:
+ *
+ * <pre>
+ * EndpointRouteBuilder.addEndpointRoutes(context, rb ->
+ * rb.from(rb.direct("inbound")).bean(MyBean.class)));
+ * </pre>
+ *
+ * @see EndpointRouteBuilder#addEndpointRoutes(CamelContext, LambdaEndpointRouteBuilder)
+ */
+@FunctionalInterface
+public interface LambdaEndpointRouteBuilder extends ThrowingConsumer<EndpointRouteBuilder, Exception> {
+
+}
diff --git a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/LambdaEndpointRouteBuilderTest.java b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/LambdaEndpointRouteBuilderTest.java
new file mode 100644
index 0000000..fb105fc
--- /dev/null
+++ b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/LambdaEndpointRouteBuilderTest.java
@@ -0,0 +1,70 @@
+package org.apache.camel.builder.endpoint;
+
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class LambdaEndpointRouteBuilderTest extends BaseEndpointDslTest {
+
+ @Test
+ public void testLambda() throws Exception {
+ assertEquals(0, context.getRoutesSize());
+
+ LambdaEndpointRouteBuilder builder = rb -> rb.from(rb.direct("start")).to(rb.mock("result"));
+ context.addRoutes(new EndpointRouteBuilder(context) {
+ @Override
+ public void configure() throws Exception {
+ builder.accept(this);
+ }
+ });
+ context.start();
+
+ assertEquals(1, context.getRoutesSize());
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testLambdaTwo() throws Exception {
+ assertEquals(0, context.getRoutesSize());
+
+ EndpointRouteBuilder.addEndpointRoutes(context, rb -> rb.from(rb.direct("start")).to(rb.mock("result")));
+
+ context.start();
+
+ assertEquals(1, context.getRoutesSize());
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testLambdaSimple() throws Exception {
+ assertEquals(0, context.getRoutesSize());
+
+ EndpointRouteBuilder.addEndpointRoutes(context,
+ rb -> rb.from(rb.direct("start")).transform(rb.simple("Hello ${body}")).to(rb.mock("result")));
+
+ context.start();
+
+ assertEquals(1, context.getRoutesSize());
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+}