XpathMessageProcessor extends AbstractMessageProcessor { @Override public void processMessage(final Message message, final TestContext context) { if (message.getPayload() == null || !StringUtils.hasText(message.getPayload(String.class))) { return; } final Document doc = XMLUtils.parseMessagePayload(message.getPayload(String.class)); if (doc == null) { throw new CitrusRuntimeException("Not able to set message elements, because no XML ressource defined"); } for (final Entry<String, Object> entry : xPathExpressions.entrySet()) { final String pathExpression = entry.getKey(); String valueExpression = entry.getValue().toString(); valueExpression = context.replaceDynamicContentInString(valueExpression); final Node node; if (XPathUtils.isXPathExpression(pathExpression)) { node = XPathUtils.evaluateAsNode(doc, pathExpression, context.getNamespaceContextBuilder().buildContext(message, Collections.emptyMap())); } else { node = XMLUtils.findNodeByName(doc, pathExpression); } if (node == null) { throw new UnknownElementException("Could not find element for expression" + pathExpression); } if (node.getNodeType() == Node.ELEMENT_NODE) { node.setTextContent(valueExpression); } else { node.setNodeValue(valueExpression); } if (log.isDebugEnabled()) { log.debug("Element " + pathExpression + " was set to value: " + valueExpression); } } message.setPayload(XMLUtils.serialize(doc)); } XpathMessageProcessor(); private  XpathMessageProcessor(Builder builder); @Override void processMessage(final Message message, final TestContext context); @Override boolean supportsMessageType(final String messageType); Map<String, Object> getXPathExpressions();  }
@Test public void testConstructWithXPath() { final Map<String, Object> xPathExpressions = new HashMap<>(); xPathExpressions.put("/TestMessage/Text", "Hello!"); XpathMessageProcessor processor = new XpathMessageProcessor.Builder() .expressions(xPathExpressions) .build(); processor.processMessage(message, context); Assert.assertTrue(StringUtils.trimAllWhitespace(message.getPayload(String.class)) .endsWith("<TestMessage><Text>Hello!</Text></TestMessage>")); }
@Test public void testConstructWithXPathAndDefaultNamespace() { final Message message = new DefaultMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?><TestMessage xmlns=\"http: "<Text>Hello World!</Text>" + "</TestMessage>"); final Map<String, Object> xPathExpressions = new HashMap<>(); xPathExpressions.put("/:TestMessage/:Text", "Hello!"); XpathMessageProcessor processor = new XpathMessageProcessor.Builder() .expressions(xPathExpressions) .build(); processor.processMessage(message, context); Assert.assertTrue(StringUtils.trimAllWhitespace(message.getPayload(String.class)) .contains("<Text>Hello!</Text>")); }
@Test public void testConstructWithXPathAndNamespace() { final Map<String, Object> xPathExpressions = new HashMap<>(); xPathExpressions.put("/ns0:TestMessage/ns0:Text", "Hello!"); XpathMessageProcessor processor = new XpathMessageProcessor.Builder() .expressions(xPathExpressions) .build(); processor.processMessage(messageNamespace, context); Assert.assertTrue(StringUtils.trimAllWhitespace(messageNamespace.getPayload(String.class)) .contains("<ns0:Text>Hello!</ns0:Text>")); }
@Test public void testConstructWithXPathAndGlobalNamespace() { context.getNamespaceContextBuilder().getNamespaceMappings().put("global", "http: final Map<String, Object> xPathExpressions = new HashMap<>(); xPathExpressions.put("/global:TestMessage/global:Text", "Hello!"); XpathMessageProcessor processor = new XpathMessageProcessor.Builder() .expressions(xPathExpressions) .build(); processor.processMessage(messageNamespace, context); Assert.assertTrue(StringUtils.trimAllWhitespace(messageNamespace.getPayload(String.class)) .contains("<ns0:Text>Hello!</ns0:Text>")); }
@Test public void testConstructWithXPathAndNestedNamespace() { final Message message = new DefaultMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:TestMessage xmlns:ns0=\"http: "<ns1:Text xmlns:ns1=\"http: "</ns0:TestMessage>"); final Map<String, Object> xPathExpressions = new HashMap<>(); xPathExpressions.put("/ns0:TestMessage/ns1:Text", "Hello!"); XpathMessageProcessor processor = new XpathMessageProcessor.Builder() .expressions(xPathExpressions) .build(); processor.processMessage(message, context); Assert.assertTrue(StringUtils.trimAllWhitespace(message.getPayload(String.class)) .contains("<ns1:Textxmlns:ns1=\"http: }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Can not evaluate xpath expression.*") public void testConstructWithInvalidXPath() { final Map<String, Object> xPathExpressions = new HashMap<>(); xPathExpressions.put(".Invalid/Unknown", "Hello!"); XpathMessageProcessor processor = new XpathMessageProcessor.Builder() .expressions(xPathExpressions) .build(); processor.processMessage(message, context); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "No result for XPath expression.*") public void testConstructWithXPathNoResult() { final Map<String, Object> xPathExpressions = new HashMap<>(); xPathExpressions.put("/TestMessage/Unknown", "Hello!"); XpathMessageProcessor processor = new XpathMessageProcessor.Builder() .expressions(xPathExpressions) .build(); processor.processMessage(message, context); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Can not evaluate xpath expression.*") public void testConstructWithXPathAndInvalidGlobalNamespace() { final Map<String, Object> xPathExpressions = new HashMap<>(); xPathExpressions.put("/global:TestMessage/global:Text", "Hello!"); XpathMessageProcessor processor = new XpathMessageProcessor.Builder() .expressions(xPathExpressions) .build(); processor.processMessage(messageNamespace, context); Assert.assertTrue(StringUtils.trimAllWhitespace(messageNamespace.getPayload(String.class)) .contains("<ns0:Text>Hello!</ns0:Text>")); }
@Test public void testAddTextToEmptyElement(){ final Message message = new DefaultMessage( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<TestMessage>" + "<Text></Text>" + "</TestMessage>"); final Map<String, Object> xPathExpression = Collections.singletonMap(" XpathMessageProcessor processor = new XpathMessageProcessor.Builder() .expressions(xPathExpression) .build(); processor.processMessage(message, context); Assert.assertTrue(StringUtils .trimAllWhitespace(message.getPayload(String.class)) .contains("<Text>foobar</Text>")); }