Hello Camel crew,
I will explain in details what I have discovered, how to reproduce it and
potential idea for fixing the issue. I am using Camel version 3.18.4.
In my code, I have Camel unit tests where I extend CamelTestSupport. I am
testing routes from one single RouteBuilder object which has some simple
routes, but it has also routes created with REST DSL.
What I am doing in this test is simply mocking these REST DSL routes in my
test, hence I am using AdviceWith:
AdviceWith.adviceWith( "myRestRouteId", context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith("direct: myRestRouteIdTesting");
}
});
The problem is that Advice will search for a RouteDefinition with given id
"myRestRouteId", but it can't find it and I get following exception:
java.lang.IllegalArgumentException: Cannot advice route as route with id:
myRestRouteId does not exists This exception happens only for REST DSL routes.
I have an explanation what happens and what should be a fix. Let me explain:
Before starting the routes in the Camel context, what happens first is that all
RouteDefinitions will be collected. But REST DSL routes will be collected as
VerbDefinition-s. So at the beginning given routeId will be stored in a
VerbDefinition object. After that in a class RestDefinition there is a method
asRouteDefinition() which calls addRouteDefinition() method.
Then addRouteDefinition() will actually loop through all VerbDefinition-s and
will transform them one by one in RouteDefinition-s. But here it is missed to
set routeId for RouteDefinition.
My proposal for fix would be just to add a single line in mentioned
transformation loop: route.setId(verb.getId());
All of this happens before Camel Context starts the routes. Problem was well
hidden because later during the route starting phase, there is forceAssignIds()
method in RouteDefinitionHelper class, which will take routeId from
VerbDefinition and assign it to proper RouteDefinition. And then at the end as
final result, when all routes are started, you will have your route id for your
REST DSL route.
But semantically looking, route id should be assigned to RouteDefinition
earlier, during lets call it "reading and building RouteDefinitions phase".
Anyway the problem exists when you try to test routes and use AdviceWith.
I think this is a bug and fix is necessary since AdviceWith currently doesn't
work for all routes.
What you think about this? Should I open Jira ticket for this?
Just to mention that I already tried this fix and AdviceWith worked as expected.
Kind Regards,
Nikola Glidzic