### Purging message channels

Message channels define central messaging destinations in Citrus. These are namely in memory message queues holding messages for test cases. These messages may become obsolete during a test run, especially when test cases fail and stop in their message consumption. Purging these message channel destinations is essential in these scenarios in order to not influence upcoming test cases. Each test case should only receive those messages that actually refer to the test model. Therefore it is a good idea to purge all message channel destinations between the test cases. Obsolete messages that get stuck in a message channel destination for some reason are then removed so that upcoming test case are not broken.

Following action definition purges all messages from a list of message channels:

XML DSL

    <testcase name="purgeChannelTest">
      <actions>
          <purge-channel>
              <channel name="someChannelName"/>
              <channel name="anotherChannelName"/>
          </purge-channel>

          <purge-channel>
              <channel ref="someChannel"/>
              <channel ref="anotherChannel"/>
          </purge-channel>
      </actions>
    </testcase>

As you can see the test action supports channel names as well as channel references to Spring bean instances. When using channel references you refer to the Spring bean id or name in your application context.

The Java DSL works quite similar as you can read from next examples:

Java DSL designer

    @Autowired
    @Qualifier("channelResolver")
    private DestinationResolver<MessageChannel> channelResolver;

    @CitrusTest
    public void purgeTest() {
        purgeChannels()
            .channelResolver(channelResolver)
            .channelNames("ch1", "ch2", "ch3")
            .channel("ch4");
    }

Java DSL runner

    @Autowired
    @Qualifier("channelResolver")
    private DestinationResolver<MessageChannel> channelResolver;

    @CitrusTest
    public void purgeTest() {
        purgeChannels(action ->
            action.channelResolver(channelResolver)
                    .channelNames("ch1", "ch2", "ch3")
                    .channel("ch4"));
    }

The channel resolver reference is optional. By default Citrus will automatically use a Spring application context channel resolver so you just have to use the respective Spring bean names that are configured in the Spring application context. However setting a custom channel resolver may be adequate for you in some special cases.

While speaking of Spring application context bean references the next example uses such bean references for channels to purge.

Java DSL designer

    @Autowired
    @Qualifier("channel1")
    private MessageChannel channel1;

    @Autowired
    @Qualifier("channel2")
    private MessageChannel channel2;

    @Autowired
    @Qualifier("channel3")
    private MessageChannel channel3;

    @CitrusTest
    public void purgeTest() {
        purgeChannels()
            .channels(channel1, channel2)
            .channel(channel3);
    }

Java DSL runner

    @Autowired
    @Qualifier("channel1")
    private MessageChannel channel1;

    @Autowired
    @Qualifier("channel2")
    private MessageChannel channel2;

    @Autowired
    @Qualifier("channel3")
    private MessageChannel channel3;

    @CitrusTest
    public void purgeTest() {
        purgeChannels(action ->
            action.channels(channel1, channel2)
                    .channel(channel3));
    }

Message selectors enable you to selectively remove messages from the destination. All messages that pass the message selection logic get deleted the other messages will remain unchanged inside the channel destination. The message selector is a Spring bean that implements a special message selector interface. A possible implementation could be a selector deleting all messages that are older than five seconds:

    import org.springframework.messaging.Message;
    import org.springframework.integration.core.MessageSelector;

    public class TimeBasedMessageSelector implements MessageSelector {

        public boolean accept(Message<?> message) {
            if (System.currentTimeMillis() - message.getHeaders().getTimestamp() > 5000) {
                return false;
            } else {
                return true;
            }
        }

    }

Note The message selector returns false for those messages that should be deleted from the channel!

You simply define the message selector as a new Spring bean in the Citrus application context and reference it in your test action property.

    <bean id="specialMessageSelector"
        class="com.consol.citrus.special.TimeBasedMessageSelector"/>

Now let us have a look at how you reference the selector in your test case:

XML DSL

    <purge-channels message-selector="specialMessageSelector">
      <channel name="someChannelName"/>
      <channel name="anotherChannelName"/>
    </purge-channels>

Java DSL designer

    @Autowired
    @Qualifier("specialMessageSelector")
    private MessageSelector specialMessageSelector;

    @CitrusTest
    public void purgeTest() {
        purgeChannels()
            .channelNames("ch1", "ch2", "ch3")
            .selector(specialMessageSelector);
    }

Java DSL runner

    @Autowired
    @Qualifier("specialMessageSelector")
    private MessageSelector specialMessageSelector;

    @CitrusTest
    public void purgeTest() {
        purgeChannels(action ->
            action.channelNames("ch1", "ch2", "ch3")
                    .selector(specialMessageSelector));
    }

In the examples above we use a message selector implementation that gets injected via Spring IoC container.

Purging channels in each test case every time is quite exhausting because every test case needs to define a purging action at the very beginning of the test. A more straight forward approach would be to introduce some purging action which is automatically executed before each test. Fortunately the Citrus test suite offers a very simple way to do this. It is described intestsuite-before-test.

When using the special action sequence before test cases we are able to purge channel destinations every time a test case executes. See the upcoming example to find out how the action is defined in the Spring configuration application context.

    <citrus:before-test id="purgeBeforeTest">
        <citrus:actions>
            <purge-channel>
                <channel name="fooChannel"/>
                <channel name="barChannel"/>
            </purge-channel>
        </citrus:actions>
    </citrus:before-test>

Just use this before-test bean in the Spring bean application context and the purge channel action is active. Obsolete messages that are waiting on the message channels for consumption are purged before the next test in line is executed.

Tip Purging message channels becomes also very interesting when working with server instances in Citrus. Each server component automatically has an inbound message channel where incoming messages are stored to internally. So if you need to clean up a server that has already stored some incoming messages you can do this easily by purging the internal message channel. The message channel follows a naming convention {serverName}.inbound where {serverName} is the Spring bean name of the Citrus server endpoint component. If you purge this internal channel in a before test nature you are sure that obsolete messages on a server instance get purged before each test is executed.