0

I have a route that end on a jdbc endpoint:

from(CONNECTOR).routeId(ROUTE_ID).process(createSelectStatement).to(jdbc);

The jdbc Endpoint is created this way:

public static final String DB_NAME = "db";

private void setupJdbcEndpoint() {
    JdbcEndpoint endpoint = getContext().getEndpoint("jdbc:" + DB_NAME, JdbcEndpoint.class);
    endpoint.setOutputClass(OUTClass.class.getName());
    endpoint.setOutputType(JdbcOutputType.SelectList);
    jdbc = endpoint;
}

In my unit test i want "mock and skip" the database:

@Override
public String isMockEndpointsAndSkip() {
    return "jdbc:*";
}

I also tried other patterns: "jdbc:db", "jdbc://db" (this string is shown in log and is the output of toString)

But no matter what pattern used the database is called. Log shows

org.apache.camel.component.jdbc.JdbcProducer: Executing JDBC Statement: SELECT..

And the correct (empty) result is sent to mock endpoint at the end. And the mocked endpint mock:jdbc:db or mock:jdbc://db () never receives anything.

So how to skip this jdbc endpoint?

And how to get a reference to mock endpoints that are created with wildcards like '*'?

EDIT

With this setup i also see in log:

InterceptSendToMockEndpointStrategy: Adviced endpoint [jdbc://db] with mock endpoint [mock:jdbc:db]

So isMockEndpointAndSkip seems to work?! But in my case the jdbc endpoint is not skipped.

2nd edit - tried answer from Vimsha Not using isMockEndpointAndSkip but providing an AdviceWithRouteBuilder didn't help (i think camel implements isMockEndpointAndSkip the same way). I see in log (using the builder Vimsha suggested):

InterceptSendToEndpoint[jdbc:* -> [To[mock://jdbc://db]]], process[Processor@0x6e9a5ed8], To[jdbc://db]]]

Besides that the database is still called the mock endpoint did not see any exchange.

here is the builder in detail:

new AdviceWithRouteBuilder() {

            @Override
            public void configure() throws Exception {
                replaceFromWith(in);
                interceptSendToEndpoint("jdbc:*").skipSendToOriginalEndpoint().to(dbMock);

            }
        };
dermoritz
  • 12,519
  • 25
  • 97
  • 185

2 Answers2

1

How about using an interceptor to skip sending to jdbc endpoint and sending it to a mock queue

RouteDefinition route = context.getRouteDefinitions().get(0);
route.adviceWith(context, new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("jdbc:*")
                .skipSendToOriginalEndpoint()
                .to("mock:jdbc");
        }
});

you can change your jdbc endpoint to this

private void setupJdbcEndpoint() {
    jdbc = "jdbc:" + DB_NAME + "?outputType=SelectList&outputClass=" + OUTClass.class.getName();
}

See this documentation

usha
  • 28,973
  • 5
  • 72
  • 93
  • thanks, i edited my post. At the moment i can't try your advice because of another problem. and i dont like string based configuration at all - trying to keep out string literals. – dermoritz Sep 27 '16 at 07:11
1

Vimsha guided into right direction but for some reason this isn't working interceptSendToEndpoint("jdbc:*").skipSendToOriginalEndpoint()..

But this works in my route i added an id to jdbc endpoint:

...to(jdbc).id("jdbc")

in test i added this AdviceWithRouteBuilder:

new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            replaceFromWith(in);
            // interceptSendToEndpoint("jdbc:*").skipSendToOriginalEndpoint().to(dbMock);
            weaveById("jdbcOut").replace().to(dbMock);
        }
    };

So weaveById and replace do the job.

dermoritz
  • 12,519
  • 25
  • 97
  • 185