HeaderMatchingMessageSelector extends AbstractMessageSelector { @Override public boolean accept(Message message) { Map<String, Object> messageHeaders = message.getHeaders(); Map<String, Object> nestedMessageHeaders = new HashMap<>(); if (message.getPayload() instanceof Message) { nestedMessageHeaders = ((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 acceptMessage = new DefaultMessage("FooTest") .setHeader("operation", "foo"); Message declineMessage = new DefaultMessage("FooTest") .setHeader("operation", "foobar"); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testHeaderMatchingSelectorValidationMatcher() { HeaderMatchingMessageSelector messageSelector = new HeaderMatchingMessageSelector("operation", "@contains(foo)@", context); Message acceptMessage = new DefaultMessage("FooTest") .setHeader("operation", "barfoobar"); Message declineMessage = new DefaultMessage("FooTest") .setHeader("operation", "bar"); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testHeaderMatchingSelectorMultipleValues() { HeaderMatchingMessageSelector messageSelector = new HeaderMatchingMessageSelector("foo", "bar", context); Message acceptMessage = new DefaultMessage("FooTest") .setHeader("foo", "bar") .setHeader("operation", "foo"); Message declineMessage = new DefaultMessage("FooTest") .setHeader("operation", "foo"); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testHeaderMatchingSelectorMissingHeader() { HeaderMatchingMessageSelector messageSelector = new HeaderMatchingMessageSelector("operation", "foo", context); Message acceptMessage = new DefaultMessage("FooTest") .setHeader("operation", "foo"); Message declineMessage = new DefaultMessage("FooTest"); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testHeaderMatchingSelectorWithMessageObjectPayload() { HeaderMatchingMessageSelector messageSelector = new HeaderMatchingMessageSelector("operation", "foo", context); Message acceptMessage = new DefaultMessage(new DefaultMessage("FooTest") .setHeader("operation", "foo")); Message declineMessage = new DefaultMessage(new DefaultMessage("FooTest") .setHeader("operation", "foobar")); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); messageSelector = new HeaderMatchingMessageSelector(MessageHeaders.ID, acceptMessage.getId().toString(), context); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }