Camel Components

DataSet Test

Since Camel 1.3

Only producer is supported

Testing of distributed and asynchronous processing is notoriously difficult. The Mock, DataSet, and DataSet Test endpoints work great with the Camel Testing Framework to simplify your unit and integration testing using Enterprise Integration Patterns and Camel’s large range of Components together with the powerful Bean Integration.

The dataset-test component extends the Mock component to support pulling messages from another endpoint on startup to set the expected message bodies on the underlying Mock endpoint.

That is, you use the dataset test endpoint in a route and messages arriving at it will be implicitly compared to some expected messages extracted from some other location.

So you can use, for example, an expected set of message bodies as files. This will then set up a properly configured Mock endpoint, which is only valid if the received messages match the number of expected messages and their message payloads are equal.

URI format

dataset-test:expectedMessagesEndpointUri

Where expectedMessagesEndpointUri refers to some other Component URI that the expected message bodies are pulled from before starting the test.

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The DataSet Test component supports the following options which are listed below.

Name Description Default Type

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

log (producer)

Whether the endpoints of this component log each received message at INFO level, as one line formatted by exchangeFormatter. The log option of an endpoint overrides it.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

exchangeFormatter (advanced)

Autowired The ExchangeFormatter that formats each received message for the log line of the log option. By default, a DefaultExchangeFormatter that shows the exchange id, the body type and the body (up to 10000 characters).

ExchangeFormatter

Endpoint Options

The DataSet Test endpoint is configured using URI syntax:

dataset-test:name

With the following path and query parameters:

Path Parameters

Name Description Default Type

name (producer)

Required The URI of the endpoint to poll the expected message bodies from when this endpoint starts, for example file:src/test/data?noop=true. The URI parameters that this endpoint does not use are passed on to that endpoint.

String

Query Parameters

Name Description Default Type

anyOrder (producer)

Whether the messages can arrive in any order. By default, they must arrive in the order in which the expected bodies were polled.

false

boolean

assertPeriod (producer)

The time in milliseconds that assertIsSatisfied() waits after the expectations are met, before it checks them all again. The first check passes as soon as the expected number of messages arrived, so a period is needed to detect messages that arrive afterwards, for example to assert that exactly 5 messages arrive, or with expectedCount 0 that none arrive. 0 checks only once.

long

delimiter (producer)

The regular expression that splits the polled bodies when split is enabled. The default splits at each line break (a newline or carriage return), and empty parts are skipped. Only a body that is a String or a file (read as text) is split by it; a collection or array body is split into its elements.

String

expectedCount (producer)

The number of messages that this endpoint must receive. assertIsSatisfied() waits up to resultWaitTime for them, and then checks that exactly this number arrived; a message that arrives after the check is not seen, unless assertPeriod is set. With 0, the check runs at once, so set sleepForEmptyTest or assertPeriod to give messages time to arrive. -1 sets no expectation on the number of messages. Expectations that set the messages, such as expectedBodiesReceived(), replace this value, and a dataset endpoint replaces it with the size of its DataSet.

-1

int

split (producer)

Whether to split each polled body into several expected bodies by the delimiter option (by default, one expected body per line), for example to load a file in which each line is an expected message.

false

boolean

timeout (producer)

The time in milliseconds to wait for the next message when polling the expected bodies on startup: the polling stops when no message arrives within this time.

2000

long

copyOnExchange (producer (advanced))

Whether to keep a copy of each received Exchange, instead of the Exchange itself. The copy has its own message, headers and exchange properties (the body object is shared), so changes that the route makes after this endpoint do not affect the received Exchanges and the expectations. Set to false to save memory. A dataset endpoint turns it off unless set.

true

boolean

failFast (producer (advanced))

Whether to check the expectations on each message as it arrives (the expectations that check each message, such as on bodies and headers), so that assertIsSatisfied() fails at the first failed expectation without waiting for the remaining messages. When false, assertIsSatisfied() waits for all the expected messages (up to resultWaitTime) and then checks the expectations.

true

boolean

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

log (producer (advanced))

Whether to log each received message at INFO level, as one line formatted by the exchangeFormatter of the component.

false

boolean

reportGroup (producer (advanced))

The size of the groups for throughput logging: after each group of this many received messages, the endpoint logs at INFO level, with the logger org.apache.camel.component.mock:name, the number of messages received so far and the message rate. Must be a positive number. Only the mock component applies it; the components that extend it, such as dataset, ignore it.

int

resultMinimumWaitTime (producer (advanced))

The minimum time in milliseconds that assertIsSatisfied() must wait for the expected number of messages: the assertion fails when they arrive sooner. It is checked only when the messages had not all arrived yet when assertIsSatisfied() was called. 0 turns it off.

long

resultWaitTime (producer (advanced))

The maximum time in milliseconds that assertIsSatisfied() waits for the expected number of messages to arrive, before it checks the expectations. 0 waits 10 seconds. MockEndpoint.assertIsSatisfied(context, timeout, unit) sets this option on each mock endpoint.

long

retainFirst (producer (advanced))

The number of the first received Exchanges that this endpoint keeps, to save memory in tests with many messages. The endpoint still counts every message, so expectedCount works, but the expectations on the content of the messages (such as bodies and headers) look up the kept Exchanges by position, so they can only check the kept ones. Together with retainLast, the endpoint keeps the first and the last Exchanges; alone, it keeps none after the first ones. 0 keeps none. -1 sets no limit, and the endpoint keeps every Exchange when retainLast is -1 as well.

-1

int

retainLast (producer (advanced))

The number of the last received Exchanges that this endpoint keeps, to save memory in tests with many messages. The endpoint still counts every message, so expectedCount works, but the expectations on the content of the messages (such as bodies and headers) look up the kept Exchanges by position, so they can only check the kept ones. Together with retainFirst, the endpoint keeps the first and the last Exchanges; alone, it keeps only the last ones. 0 keeps none. -1 sets no limit, and the endpoint keeps every Exchange when retainFirst is -1 as well.

-1

int

sleepForEmptyTest (producer (advanced))

The time in milliseconds that assertIsSatisfied() waits, when expectedCount is 0, before it checks that no message arrived. 0 checks at once.

long

browseLimit (advanced)

Has no effect on this endpoint: it does not limit the Exchanges that it keeps by this option (use retainFirst and retainLast). The value is only reported as the browse limit, for example in JMX.

100

int

Message Body

The message body at each side of the DataSet Test component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Producer input

any

Each message is compared with the next body read at startup from the endpoint in the name. A mismatch fails the assertions of the endpoint, not the message.

Producer output

unchanged

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Example

For example, you could write a test case as follows:

  • Java

  • XML

  • YAML

from("seda:someEndpoint")
    .to("dataset-test:file://data/expectedOutput?noop=true");
<route>
  <from uri="seda:someEndpoint"/>
  <to uri="dataset-test:file://data/expectedOutput?noop=true"/>
</route>
- route:
    from:
      uri: seda:someEndpoint
      steps:
        - to:
            uri: dataset-test:file://data/expectedOutput
            parameters:
              noop: true

If your test then invokes the MockEndpoint.assertIsSatisfied(camelContext) method, your test case will perform the necessary assertions.

To see how you can set other expectations on the test endpoint, see the Mock component.