Skip to content

Commit

Permalink
Filter AdviceWithRouteBuilder types from build time route discovery
Browse files Browse the repository at this point in the history
Fixes #6807
  • Loading branch information
jamesnetherton committed Nov 20, 2024
1 parent b84a2a6 commit 0552946
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ class CamelProcessor {

private static final DotName ROUTES_BUILDER_TYPE = DotName.createSimple(
"org.apache.camel.RoutesBuilder");
private static final DotName ROUTE_BUILDER_TYPE = DotName.createSimple(
"org.apache.camel.builder.RouteBuilder");
private static final DotName LAMBDA_ROUTE_BUILDER_TYPE = DotName.createSimple(
"org.apache.camel.builder.LambdaRouteBuilder");
private static final DotName LAMBDA_ENDPOINT_ROUTE_BUILDER_TYPE = DotName.createSimple(
Expand Down Expand Up @@ -386,10 +384,10 @@ public List<CamelRoutesBuilderClassBuildItem> discoverRoutesBuilderClassNames(

final IndexView index = combinedIndex.getIndex();

Set<ClassInfo> allKnownImplementors = new HashSet<>();
allKnownImplementors.addAll(index.getAllKnownImplementors(ROUTES_BUILDER_TYPE));
allKnownImplementors.addAll(index.getAllKnownSubclasses(ROUTE_BUILDER_TYPE));
allKnownImplementors.addAll(index.getAllKnownSubclasses(ADVICE_WITH_ROUTE_BUILDER_TYPE));
Set<ClassInfo> allKnownImplementors = index.getAllKnownImplementors(ROUTES_BUILDER_TYPE)
.stream()
.filter(classInfo -> !classInfo.superName().equals(ADVICE_WITH_ROUTE_BUILDER_TYPE))
.collect(Collectors.toSet());

final Predicate<DotName> pathFilter = new PathFilter.Builder()
.exclude(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.ModelCamelContext;
import org.jboss.logging.Logger;
import org.wildfly.common.Assert;

Expand All @@ -48,18 +47,11 @@ public class MockResource {
@Path("/advice")
@GET
public void advice() throws Exception {

// advice the first route using the inlined AdviceWith route builder
// which has extended capabilities than the regular route builder
AdviceWith.adviceWith(((ModelCamelContext) context).getRouteDefinition("forMocking"), context,
new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
mockEndpoints("direct:mock.*", "log:mock.*");
}
});
AdviceWith.adviceWith("forMocking", context, new RouteAdvisor());

MockEndpoint mockEndpoint1 = context.getEndpoint("mock:direct:mockStart", MockEndpoint.class);
MockEndpoint mockEndpoint1 = context.getEndpoint("mock:direct:mockStartAdvised", MockEndpoint.class);
mockEndpoint1.expectedBodiesReceived("Hello World");
MockEndpoint mockEndpoint2 = context.getEndpoint("mock:direct:mockFoo", MockEndpoint.class);
mockEndpoint2.expectedBodiesReceived("Hello World");
Expand All @@ -68,20 +60,20 @@ public void configure() throws Exception {
MockEndpoint mockEndpoint4 = context.getEndpoint("mock:result", MockEndpoint.class);
mockEndpoint4.expectedBodiesReceived("Bye World");

producerTemplate.sendBody("direct:mockStart", "Hello World");
producerTemplate.sendBody("direct:mockStartAdvised", "Hello World");

mockEndpoint1.assertIsSatisfied();
mockEndpoint2.assertIsSatisfied();
mockEndpoint3.assertIsSatisfied();
mockEndpoint4.assertIsSatisfied();

// test to ensure correct endpoints in registry
Assert.assertNotNull(context.hasEndpoint("direct:mockStart"));
Assert.assertNotNull(context.hasEndpoint("direct:mockStartAdvised"));
Assert.assertNotNull(context.hasEndpoint("direct:mockFoo"));
Assert.assertNotNull(context.hasEndpoint("log:mockFoo"));
Assert.assertNotNull(context.hasEndpoint("mock:result"));
// all the endpoints was mocked
Assert.assertNotNull(context.hasEndpoint("mock:direct:mockStart"));
Assert.assertNotNull(context.hasEndpoint("mock:direct:mockStartAdvised"));
Assert.assertNotNull(context.hasEndpoint("mock:direct:mockFoo"));
Assert.assertNotNull(context.hasEndpoint("mock:log:mockFoo"));
}
Expand All @@ -107,4 +99,11 @@ public String post(String message, @PathParam("route") String route) throws Exce
return producerTemplate.requestBody("direct:" + route, message, String.class);
}

public static class RouteAdvisor extends AdviceWithRouteBuilder {
@Override
public void configure() throws Exception {
replaceFromWith("direct:mockStartAdvised");
mockEndpoints("direct:mock.*", "log:mock.*");
}
}
}

0 comments on commit 0552946

Please sign in to comment.