XpathPayloadMessageSelector extends AbstractMessageSelector { @Override public boolean accept(Message message) { Document doc; try { doc = XMLUtils.parseMessagePayload(getPayloadAsString(message)); } catch (LSException e) { log.warn("Ignoring non XML message for XPath message selector (" + e.getClass().getName() + ")"); return false; } try { Map<String, String> namespaces = XMLUtils.lookupNamespaces(doc); namespaces.putAll(context.getNamespaceContextBuilder().getNamespaceMappings()); String value; if (XPathUtils.hasDynamicNamespaces(selectKey)) { namespaces.putAll(XPathUtils.getDynamicNamespaces(selectKey)); value = XPathExpressionFactory.createXPathExpression(XPathUtils.replaceDynamicNamespaces(selectKey, namespaces), namespaces) .evaluateAsString(doc); } else { value = XPathExpressionFactory.createXPathExpression(selectKey, namespaces) .evaluateAsString(doc); } return evaluate(value); } catch (XPathParseException e) { log.warn("Could not evaluate XPath expression for message selector - ignoring message (" + e.getClass().getName() + ")"); return false; } } XpathPayloadMessageSelector(String selectKey, String matchingValue, TestContext context); @Override boolean accept(Message message); static final String SELECTOR_PREFIX; }
@Test public void testXPathEvaluation() { XpathPayloadMessageSelector messageSelector = new XpathPayloadMessageSelector("xpath: Assert.assertTrue(messageSelector.accept(new DefaultMessage("<Foo><text>foobar</text></Foo>"))); Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Foo xmlns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Bar><text>foobar</text></Bar>"))); Assert.assertFalse(messageSelector.accept(new DefaultMessage("This is plain text!"))); messageSelector = new XpathPayloadMessageSelector("xpath: Assert.assertTrue(messageSelector.accept(new DefaultMessage("<ns:Foo xmlns:ns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<ns1:Foo xmlns:ns1=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Bar><text>foobar</text></Bar>"))); messageSelector = new XpathPayloadMessageSelector("xpath: Assert.assertTrue(messageSelector.accept(new DefaultMessage("<Foo xmlns=\"http: Assert.assertTrue(messageSelector.accept(new DefaultMessage("<ns:Foo xmlns:ns=\"http: Assert.assertTrue(messageSelector.accept(new DefaultMessage("<ns1:Foo xmlns:ns1=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Foo xmlns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<ns:Foo xmlns:ns=\"http: messageSelector = new XpathPayloadMessageSelector("xpath: Assert.assertTrue(messageSelector.accept(new DefaultMessage("<ns1:Foo xmlns:ns1=\"http: messageSelector = new XpathPayloadMessageSelector("xpath: Assert.assertTrue(messageSelector.accept(new DefaultMessage("<ns:Foos xmlns:ns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<ns:Foos xmlns:ns=\"http: }
@Test public void testXPathEvaluationValidationMatcher() { XpathPayloadMessageSelector messageSelector = new XpathPayloadMessageSelector("xpath: Assert.assertTrue(messageSelector.accept(new DefaultMessage("<Foo><text>foobar</text></Foo>"))); Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Foo xmlns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage("<Bar><text>foobar</text></Bar>"))); Assert.assertFalse(messageSelector.accept(new DefaultMessage("This is plain text!"))); }
@Test public void testXPathEvaluationWithMessageObjectPayload() { XpathPayloadMessageSelector messageSelector = new XpathPayloadMessageSelector("xpath: Assert.assertTrue(messageSelector.accept(new DefaultMessage(new DefaultMessage("<Foo><text>foobar</text></Foo>")))); Assert.assertFalse(messageSelector.accept(new DefaultMessage(new DefaultMessage("<Foo xmlns=\"http: Assert.assertFalse(messageSelector.accept(new DefaultMessage(new DefaultMessage("<Bar><text>foobar</text></Bar>")))); Assert.assertFalse(messageSelector.accept(new DefaultMessage(new DefaultMessage("This is plain text!")))); }