XpathFunction implements Function { @Override public String execute(List<String> parameterList, TestContext context) { if (CollectionUtils.isEmpty(parameterList)) { throw new InvalidFunctionUsageException("Function parameters must not be empty"); } if (parameterList.size() < 2) { throw new InvalidFunctionUsageException("Missing parameter for function - usage xpath('xmlSource', 'expression')"); } String xmlSource = parameterList.get(0); String xpathExpression = parameterList.get(1); SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); namespaceContext.setBindings(context.getNamespaceContextBuilder().getNamespaceMappings()); return XPathUtils.evaluateAsString(XMLUtils.parseMessagePayload(context.replaceDynamicContentInString(xmlSource)), context.replaceDynamicContentInString(xpathExpression), namespaceContext); }  @Override String execute(List<String> parameterList, TestContext context);  }
@Test public void testExecuteXpath() throws Exception { List<String> parameters = new ArrayList<>(); parameters.add(xmlSource); parameters.add("/person/name"); Assert.assertEquals(function.execute(parameters, context), "Sheldon"); }
@Test public void testExecuteXpathWithNamespaces() throws Exception { List<String> parameters = new ArrayList<>(); parameters.add(xmlSourceNamespace); parameters.add("/p:person/p:name"); context.getNamespaceContextBuilder().getNamespaceMappings().put("p", "http: Assert.assertEquals(function.execute(parameters, context), "Sheldon"); }
@Test(expectedExceptions = CitrusRuntimeException.class) public void testExecuteXpathUnknown() throws Exception { List<String> parameters = new ArrayList<>(); parameters.add(xmlSource); parameters.add("/person/unknown"); function.execute(parameters, context); }