This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 4ff0019ee4dc66ee4fd1e001c2cfbdccaebe79c7 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Aug 11 14:38:40 2020 +0200 CAMEL-14297: Introduce RouteBuilderConfigurer --- .../org/apache/camel/builder/RouteBuilder.java | 2 +- .../camel/builder/RouteBuilderConfigurer.java | 36 ++++++++++ .../processor/RouteBuilderConfigurerTest.java | 76 ++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java index c30c0e6..8e608f1 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilder.java @@ -101,7 +101,7 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild * to create routes * @throws Exception if an error occurs */ - public static void addRoutes(CamelContext context, ThrowingConsumer<RouteBuilder, Exception> rbc) throws Exception { + public static void addRoutes(CamelContext context, RouteBuilderConfigurer rbc) throws Exception { context.addRoutes(new RouteBuilder(context) { @Override public void configure() throws Exception { diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilderConfigurer.java b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilderConfigurer.java new file mode 100644 index 0000000..05ac752 --- /dev/null +++ b/core/camel-core-engine/src/main/java/org/apache/camel/builder/RouteBuilderConfigurer.java @@ -0,0 +1,36 @@ +/* + * 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.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> + * RouteBuilder.addRoutes(context, rb -> + * rb.from("direct:inbound").bean(ProduceTemplateBean.class))); + * </pre> + * + * @see RouteBuilder#addRoutes(CamelContext, RouteBuilderConfigurer) + */ +@FunctionalInterface +public interface RouteBuilderConfigurer extends ThrowingConsumer<RouteBuilder, Exception> { + +} diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/RouteBuilderConfigurerTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/RouteBuilderConfigurerTest.java new file mode 100644 index 0000000..37cf971 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/processor/RouteBuilderConfigurerTest.java @@ -0,0 +1,76 @@ +/* + * 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.processor; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.builder.RouteBuilderConfigurer; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Test; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class RouteBuilderConfigurerTest extends ContextTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testRouteBuilderConfigurer() throws Exception { + assertEquals(0, context.getRoutesSize()); + + RouteBuilderConfigurer builder = rb -> rb.from("direct:start").to("mock:result"); + context.addRoutes(new RouteBuilder(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 testRouteBuilderConfigurerLambda() throws Exception { + assertEquals(0, context.getRoutesSize()); + + RouteBuilder.addRoutes(context, rb -> rb.from("direct:start").to("mock:result")); + + context.start(); + + assertEquals(1, context.getRoutesSize()); + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Hello World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + +}