DispatchingMessageSelector implements MessageSelector { @Override public boolean accept(Message<?> message) { return matchingHeaders.entrySet() .stream() .allMatch(entry -> factories.stream() .filter(factory -> factory.supports(entry.getKey())) .findAny() .orElse(new HeaderMatchingMessageSelector.Factory()) .create(entry.getKey(), entry.getValue(), context) .accept(message)); } DispatchingMessageSelector(String selector, BeanFactory beanFactory, TestContext context); @Override boolean accept(Message<?> message); void addMessageSelectorFactory(MessageSelectorFactory<?> factory);  }
@Test public void testHeaderMatchingSelector() { DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("operation = 'foo'", beanFactory, 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 testHeaderMatchingSelectorAndOperation() { DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("foo = 'bar' AND operation = 'foo'", beanFactory, 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 testPayloadMatchingDelegation() { DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("foo = 'bar' AND payload = 'FooTest'", beanFactory, context); Message<String> acceptMessage = MessageBuilder.withPayload("FooTest") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("BarTest") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); messageSelector = new DispatchingMessageSelector("payload = 'FooTest'", beanFactory, context); acceptMessage = MessageBuilder.withPayload("FooTest") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); declineMessage = MessageBuilder.withPayload("BarTest") .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testPayloadAndHeaderMatchingDelegation() { DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("header:payload = 'foo' AND payload = 'foo'", beanFactory, context); Assert.assertTrue(messageSelector.accept(MessageBuilder.withPayload("foo") .setHeader("payload", "foo") .build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload("foo") .setHeader("payload", "bar") .build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload("bar") .setHeader("payload", "foo") .build())); }
@Test public void testRootQNameDelegation() { DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("foo = 'bar' AND root-qname = 'FooTest'", beanFactory, context); Message<String> acceptMessage = MessageBuilder.withPayload("<FooTest><text>foobar</text></FooTest>") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("<BarTest><text>foobar</text></BarTest>") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); messageSelector = new DispatchingMessageSelector("root-qname = 'FooTest'", beanFactory, context); acceptMessage = MessageBuilder.withPayload("<FooTest><text>foobar</text></FooTest>") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); declineMessage = MessageBuilder.withPayload("<BarTest><text>foobar</text></BarTest>") .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testRootQNameDelegationWithNamespace() { DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("root-qname = '{http: Message<String> acceptMessage = MessageBuilder.withPayload("<FooTest xmlns=\"http: .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("<FooTest xmlns=\"http: .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testXPathEvaluationDelegation() { DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("foo = 'bar' AND root-qname = 'FooTest' AND xpath: Message<String> acceptMessage = MessageBuilder.withPayload("<FooTest><text>foobar</text></FooTest>") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("<FooTest><text>barfoo</text></FooTest>") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); messageSelector = new DispatchingMessageSelector("xpath: Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testXPathEvaluationDelegationWithNamespaceBuilder() { NamespaceContextBuilder nsContextBuilder = new NamespaceContextBuilder(); nsContextBuilder.getNamespaceMappings().put("foo", "http: context.setNamespaceContextBuilder(nsContextBuilder); reset(beanFactory); when(beanFactory.getBean(NamespaceContextBuilder.class)).thenReturn(nsContextBuilder); DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("foo = 'bar' AND root-qname = 'FooTest' AND xpath: Message<String> acceptMessage = MessageBuilder.withPayload("<FooTest xmlns=\"http: .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("<FooTest><text>barfoo</text></FooTest>") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); messageSelector = new DispatchingMessageSelector("xpath: Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testJsonPathEvaluationDelegation() { DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("foo = 'bar' AND jsonPath:$.foo.text = 'foobar'", beanFactory, context); Message<String> acceptMessage = MessageBuilder.withPayload("{ \"foo\": { \"text\": \"foobar\"} }") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("{ \"foo\": { \"text\": \"barfoo\"} }") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); messageSelector = new DispatchingMessageSelector("jsonPath:$.foo.text = 'foobar'", beanFactory, context); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }
@Test public void testCustomMessageSelectorDelegation() { ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); Map<String, MessageSelectorFactory> factories = new HashMap<>(); factories.put("customSelectorFactory", new MessageSelectorFactory() { @Override public boolean supports(String key) { return key.startsWith("x:"); } @Override public MessageSelector create(String key, String value, TestContext context) { return message -> message.getHeaders().get("foo").equals(value); } }); when(applicationContext.getBeansOfType(MessageSelectorFactory.class)).thenReturn(factories); DispatchingMessageSelector messageSelector = new DispatchingMessageSelector("x:foo = 'bar'", applicationContext, context); Message<String> acceptMessage = MessageBuilder.withPayload("FooBar") .setHeader("foo", "bar") .setHeader("operation", "foo") .build(); Message<String> declineMessage = MessageBuilder.withPayload("FooBar") .setHeader("foo", "bars") .setHeader("operation", "foo") .build(); Assert.assertTrue(messageSelector.accept(acceptMessage)); Assert.assertFalse(messageSelector.accept(declineMessage)); }