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 b951ed7ba898677a9f52a4d8bf5d7d2fb148b8a6 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Jun 4 05:55:10 2019 +0200 CAMEL-13608: Add route filter to model camel context so you can filter out unwanted routes, such as from unit testing. --- .../camel/impl/AbstractModelCamelContext.java | 14 ++++- .../java/org/apache/camel/impl/DefaultModel.java | 36 +++++++++++-- .../main/java/org/apache/camel/model/Model.java | 21 ++++++-- .../apache/camel/model/ModelRouteFilterTest.java | 60 ++++++++++++++++++++++ 4 files changed, 124 insertions(+), 7 deletions(-) diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/AbstractModelCamelContext.java b/core/camel-core/src/main/java/org/apache/camel/impl/AbstractModelCamelContext.java index 10e4826..c103488 100644 --- a/core/camel-core/src/main/java/org/apache/camel/impl/AbstractModelCamelContext.java +++ b/core/camel-core/src/main/java/org/apache/camel/impl/AbstractModelCamelContext.java @@ -21,11 +21,11 @@ import java.util.Collection; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; +import java.util.function.Function; import org.apache.camel.AsyncProcessor; import org.apache.camel.CatalogCamelContext; import org.apache.camel.Processor; -import org.apache.camel.builder.ErrorHandlerBuilderSupport; import org.apache.camel.health.HealthCheckRegistry; import org.apache.camel.impl.engine.AbstractCamelContext; import org.apache.camel.impl.engine.BaseRouteService; @@ -231,6 +231,17 @@ public abstract class AbstractModelCamelContext extends AbstractCamelContext imp model.addServiceCallConfiguration(serviceName, configuration); } + @Override + public void setRouteFilter(Function<RouteDefinition, Boolean> filter) { + model.setRouteFilter(filter); + } + + @Override + public Function<RouteDefinition, Boolean> getRouteFilter() { + return model.getRouteFilter(); + } + + @Override protected ValidatorRegistry<ValidatorKey> createValidatorRegistry() throws Exception { DefaultValidatorRegistry registry = new DefaultValidatorRegistry(this); for (ValidatorDefinition def : getValidators()) { @@ -244,6 +255,7 @@ public abstract class AbstractModelCamelContext extends AbstractCamelContext imp return new ValidatorKey(new DataType(def.getType())); } + @Override protected TransformerRegistry<TransformerKey> createTransformerRegistry() throws Exception { DefaultTransformerRegistry registry = new DefaultTransformerRegistry(this); for (TransformerDefinition def : getTransformers()) { diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultModel.java b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultModel.java index 19454de..970937c 100644 --- a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultModel.java +++ b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultModel.java @@ -25,6 +25,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; import org.apache.camel.CamelContext; import org.apache.camel.ExtendedCamelContext; @@ -60,6 +61,7 @@ public class DefaultModel implements Model { private List<ValidatorDefinition> validators = new ArrayList<>(); private Map<String, ServiceCallConfigurationDefinition> serviceCallConfigurations = new ConcurrentHashMap<>(); private Map<String, HystrixConfigurationDefinition> hystrixConfigurations = new ConcurrentHashMap<>(); + private Function<RouteDefinition, Boolean> routeFilter; public DefaultModel(CamelContext camelContext) { this.camelContext = camelContext; @@ -69,6 +71,7 @@ public class DefaultModel implements Model { return camelContext; } + @Override public void addRouteDefinitions(InputStream is) throws Exception { RoutesDefinition def = ModelHelper.loadRoutesDefinition(camelContext, is); if (def != null) { @@ -76,27 +79,38 @@ public class DefaultModel implements Model { } } + @Override public synchronized void addRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception { if (routeDefinitions == null || routeDefinitions.isEmpty()) { return; } - removeRouteDefinitions(routeDefinitions); - this.routeDefinitions.addAll(routeDefinitions); + List<RouteDefinition> list = new ArrayList<>(); + routeDefinitions.forEach(r -> { + if (routeFilter == null || routeFilter.apply(r)) { + list.add(r); + } + }); + + removeRouteDefinitions(list); + this.routeDefinitions.addAll(list); if (shouldStartRoutes()) { - startRouteDefinitions(routeDefinitions); + startRouteDefinitions(list); } } + @Override public void addRouteDefinition(RouteDefinition routeDefinition) throws Exception { addRouteDefinitions(Collections.singletonList(routeDefinition)); } + @Override public synchronized void removeRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception { for (RouteDefinition routeDefinition : routeDefinitions) { removeRouteDefinition(routeDefinition); } } + @Override public synchronized void removeRouteDefinition(RouteDefinition routeDefinition) throws Exception { RouteDefinition toBeRemoved = routeDefinition; String id = routeDefinition.getId(); @@ -109,10 +123,12 @@ public class DefaultModel implements Model { this.routeDefinitions.remove(toBeRemoved); } + @Override public synchronized List<RouteDefinition> getRouteDefinitions() { return routeDefinitions; } + @Override public synchronized RouteDefinition getRouteDefinition(String id) { for (RouteDefinition route : routeDefinitions) { if (route.idOrCreate(camelContext.adapt(ExtendedCamelContext.class).getNodeIdFactory()).equals(id)) { @@ -122,10 +138,12 @@ public class DefaultModel implements Model { return null; } + @Override public synchronized List<RestDefinition> getRestDefinitions() { return restDefinitions; } + @Override public void addRestDefinitions(InputStream is, boolean addToRoutes) throws Exception { RestsDefinition rests = ModelHelper.loadRestsDefinition(camelContext, is); if (rests != null) { @@ -133,6 +151,7 @@ public class DefaultModel implements Model { } } + @Override public synchronized void addRestDefinitions(Collection<RestDefinition> restDefinitions, boolean addToRoutes) throws Exception { if (restDefinitions == null || restDefinitions.isEmpty()) { return; @@ -267,10 +286,21 @@ public class DefaultModel implements Model { return validators; } + @Override public void startRouteDefinitions() throws Exception { startRouteDefinitions(routeDefinitions); } + @Override + public Function<RouteDefinition, Boolean> getRouteFilter() { + return routeFilter; + } + + @Override + public void setRouteFilter(Function<RouteDefinition, Boolean> routeFilter) { + this.routeFilter = routeFilter; + } + protected void startRouteDefinitions(Collection<RouteDefinition> list) throws Exception { if (list != null) { for (RouteDefinition route : list) { diff --git a/core/camel-core/src/main/java/org/apache/camel/model/Model.java b/core/camel-core/src/main/java/org/apache/camel/model/Model.java index f3b77be..d1292df 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/Model.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/Model.java @@ -20,6 +20,7 @@ import java.io.InputStream; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.function.Function; import org.apache.camel.CamelContext; import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition; @@ -55,7 +56,7 @@ public interface Model { * new routes which has a route id that matches an old route, then the old route is replaced by the new route. * * @param is input stream with the route(s) definition to add - * @throws Exception if the route definitions could not be created for whatever reason + * @throws Exception if the route definitions could not be added for whatever reason */ void addRouteDefinitions(InputStream is) throws Exception; @@ -67,7 +68,7 @@ public interface Model { * new routes which has a route id that matches an old route, then the old route is replaced by the new route. * * @param routeDefinitions the route(s) definition to add - * @throws Exception if the route definitions could not be created for whatever reason + * @throws Exception if the route definitions could not be added for whatever reason */ void addRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception; @@ -79,7 +80,7 @@ public interface Model { * new routes which has a route id that matches an old route, then the old route is replaced by the new route. * * @param routeDefinition the route definition to add - * @throws Exception if the route definition could not be created for whatever reason + * @throws Exception if the route definition could not be added for whatever reason */ void addRouteDefinition(RouteDefinition routeDefinition) throws Exception; @@ -261,4 +262,18 @@ public interface Model { */ void startRouteDefinitions() throws Exception; + /** + * Sets a custom route filter to use for filtering unwanted routes when routes are added. + * + * @param filter the filter + */ + void setRouteFilter(Function<RouteDefinition, Boolean> filter); + + /** + * Gets the current route filter + * + * @return the filter, or <tt>null</tt> if no custom filter has been configured. + */ + Function<RouteDefinition, Boolean> getRouteFilter(); + } diff --git a/core/camel-core/src/test/java/org/apache/camel/model/ModelRouteFilterTest.java b/core/camel-core/src/test/java/org/apache/camel/model/ModelRouteFilterTest.java new file mode 100644 index 0000000..f6a637d --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/model/ModelRouteFilterTest.java @@ -0,0 +1,60 @@ +/* + * 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.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class ModelRouteFilterTest extends ContextTestSupport { + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + // filter to only include foo route + context.getExtension(ModelCamelContext.class).setRouteFilter((r) -> r.getId().equals("foo") ); + return context; + } + + @Test + public void testRouteFilter() throws Exception { + assertEquals(1, context.getRoutes().size()); + assertEquals(1, context.getRouteDefinitions().size()); + assertEquals("foo", context.getRouteDefinitions().get(0).getId()); + + getMockEndpoint("mock:foo").expectedMessageCount(1); + + template.sendBody("direct:foo", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:foo").routeId("foo") + .to("mock:foo"); + + from("direct:bar").routeId("bar") + .to("mock:bar"); + } + }; + } +}