You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

AdviceWith

Available as of Camel 2.1

AdviceWith is used for testing Camel routes where you can advice an existing route before its being tested. What adviceWith allows is to changes some factors on the route before the test is being run.

At current time you can advice an existing route by adding Intercept, Exception Clause etc. which then will apply for the route being advice.

For example in the route below we intercept sending a message to the mock:foo endpoint and detour the message.

Error formatting macro: snippet: java.lang.NullPointerException

Using AdviceWithRouteBuilder

Available as of Camel 2.7

The AdviceWithRouteBuilder is a specialized RouteBuilder which has additional methods for advising routes. For example this allows you to manipulate the advised route, such as replacing a node with some other nodes.

The AdviceWithRouteBuilder offers the following extra methods

Method

Description

mockEndpoints

Is used to easily mock all endpoints. See more details and examples at Mock.

mockEndpoints(pattern)

Is used to easily mock endpoints using a pattern. See more details and examples at Mock. See below for pattern matching.

weaveById(pattern)

Is used to select node(s) matching by id's, and weave in the following nodes. See below for pattern matching and examples.

weaveByToString(pattern)

Is used to select nodes(s) matching by their toString representation, and weave in the following nodes. See below for pattern matching and examples.

weaveByType(Class)

Camel 2.8: Is used to select node(s) matching by their class type (the classes from the org.apache.camel.model package), and weave in the following nodes. See below for examples.

The pattern option is used for matching. It uses the same rules as the Intercept, which is applied in the following order:

  • match exact
  • match by wildcard
  • match by regular expression

For example to match exact you can use weaveById("foo") which will match only the id in the route which has the value "foo".
The wildcard is when the pattern ends with a * char, such as: weaveById("foo*") which will match any id's starting with "foo", such as foo, foobar, foobie and so forth.
The regular expression is more advanced and allows you to match multiple ids, such as weaveById("(foo|bar)") which will match both "foo" and "bar".

Using weaveById

The weaveById allows you to manipulate the rote, for example by replacing a node with other nodes. The following methods is available:

Method

Description

remove

Removes the selected node(s).

replace

Replaces the selected node(s) with the following nodes.

before

Before the selected node(s), the following nodes is added.

after

After the selected node(s), the following nodes is added.

For example given the following route:

Route
from("direct:start")
    .to("mock:foo")
    .to("mock:bar").id("bar")
    .to("mock:result");

Then let's go over the four methods to see how you can use them in unit tests:

Error formatting macro: snippet: java.lang.NullPointerException

In this example we replace the .to("mock:bar").id("bar") with the .multicast().to("mock:a").to("mock:b").
That means instead of sending the message to a "mock:bar" endpoint, we do a Multicast to "mock:a" and "mock:b" endpoints instead.

Error formatting macro: snippet: java.lang.NullPointerException

In the example above, we simply just remove the .to("mock:bar").id("bar").

Error formatting macro: snippet: java.lang.NullPointerException

In the example above, we add the following nodes to("mock:a").transform(constant("Bye World")) before the node with the id "bar".
That means the message being send to "mock:bar" would have been transformed to a constant message "Bye World".

Error formatting macro: snippet: java.lang.NullPointerException

In the example above, we add the following nodes to("mock:a").transform(constant("Bye World")) after the node with the id "bar".

Using weaveByToString

The weaveByToString also allows you to manipulate the route, for example by replacing a node with other nodes. As opposed to weaveById, this method uses the toString representation of the node(s) when matching. This allows you to match nodes, which may not have assigned ids, or to match EIP pattern.
You have to be a bit more careful when using this as the toString representation can be verbose and contain characters such as [ ] ( ) -> and so forth. That is why using the regular expression matching is the must useable.

The weaveByToString has the same methods as weaceById.

For example to replace any nodes which has "foo" you can do

Error formatting macro: snippet: java.lang.NullPointerException

Notice that we have to use ".*foo.*" in the pattern to match that "foo" is present anywhere in the string.

Using weaveByType

Available as of Camel 2.8

The weaveByToType also allows you to manipulate the route, for example by replacing a node with other nodes. As opposed to weaveById, and weaveByToString this method uses the class type of the node(s) when matching. This allows you to match EIP pattern by its type.

The weaveByToType has the same methods as weaceById and weaveByToString.

For example to remove a transform from the following route:

Error formatting macro: snippet: java.lang.NullPointerException

You can do:

Error formatting macro: snippet: java.lang.NullPointerException
  • No labels