HeaderMatchingMessageSelector extends AbstractMessageSelector { @Override public boolean accept(Message<?> message) { MessageHeaders messageHeaders = message.getHeaders(); Map<String, Object> nestedMessageHeaders = new HashMap<>(); if (message.getPayload() instanceof com.consol.citrus.message.Message) { nestedMessageHeaders = ((com.consol.citrus.message.Message) message.getPayload()).getHeaders(); } if (nestedMessageHeaders.containsKey(selectKey)) { return matchHeader(nestedMessageHeaders); } else if (messageHeaders.containsKey(selectKey)) { return matchHeader(messageHeaders); } else { return false; } } HeaderMatchingMessageSelector(String selectKey, String matchingValue, TestContext context); @Override boolean accept(Message<?> message); static final String SELECTOR_PREFIX; }
@Test public void testHeaderMatchingSelector() { HeaderMatchingMessageSelector messageSelector = new HeaderMatchingMessageSelector("operation", "foo", context); Message<String> acceptMessage = MessageBuilder.withPayload("FooTest") .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("FooTest") .setHeader("operation", "foobar") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testHeaderMatchingSelectorValidationMatcher() { HeaderMatchingMessageSelector messageSelector = new HeaderMatchingMessageSelector("operation", "@contains(foo)@", context); Message<String> acceptMessage = MessageBuilder.withPayload("FooTest") .setHeader("operation", "barfoobar") .build(); Message<String> declineMessage = MessageBuilder.withPayload("FooTest") .setHeader("operation", "bar") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testHeaderMatchingSelectorMultipleValues() { HeaderMatchingMessageSelector messageSelector = new HeaderMatchingMessageSelector("foo", "bar", context); Message<String> acceptMessage = MessageBuilder.withPayload("FooTest") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("FooTest") .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testHeaderMatchingSelectorMissingHeader() { HeaderMatchingMessageSelector messageSelector = new HeaderMatchingMessageSelector("operation", "foo", context); Message<String> acceptMessage = MessageBuilder.withPayload("FooTest") .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("FooTest") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testHeaderMatchingSelectorWithMessageObjectPayload() { HeaderMatchingMessageSelector messageSelector = new HeaderMatchingMessageSelector("operation", "foo", context); Message<DefaultMessage> acceptMessage = MessageBuilder.withPayload(new DefaultMessage("FooTest") .setHeader("operation", "foo")) .build(); Message<DefaultMessage> declineMessage = MessageBuilder.withPayload(new DefaultMessage("FooTest") .setHeader("operation", "foobar")) .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); messageSelector = new HeaderMatchingMessageSelector(MessageHeaders.ID, acceptMessage.getHeaders().getId().toString(), context); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }