XpathMessageValidator extends AbstractMessageValidator<XpathMessageValidationContext> { @Override public void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, XpathMessageValidationContext validationContext) throws ValidationException { if (CollectionUtils.isEmpty(validationContext.getXpathExpressions())) { return; } if (receivedMessage.getPayload() == null || !StringUtils.hasText(receivedMessage.getPayload(String.class))) { throw new ValidationException("Unable to validate message elements - receive message payload was empty"); } log.debug("Start XPath element validation ..."); Document received = XMLUtils.parseMessagePayload(receivedMessage.getPayload(String.class)); NamespaceContext namespaceContext = namespaceContextBuilder.buildContext( receivedMessage, validationContext.getNamespaces()); for (Map.Entry<String, Object> entry : validationContext.getXpathExpressions().entrySet()) { String xPathExpression = entry.getKey(); Object expectedValue = entry.getValue(); xPathExpression = context.replaceDynamicContentInString(xPathExpression); Object xPathResult; if (XPathUtils.isXPathExpression(xPathExpression)) { XPathExpressionResult resultType = XPathExpressionResult.fromString( xPathExpression, XPathExpressionResult.NODE); xPathExpression = XPathExpressionResult.cutOffPrefix(xPathExpression); if (resultType.equals(XPathExpressionResult.NODE) && XmlValidationUtils.isElementIgnored(XPathUtils.evaluateAsNode(received, xPathExpression, namespaceContext), validationContext.getIgnoreExpressions(), namespaceContext)) { continue; } xPathResult = XPathUtils.evaluate(received, xPathExpression, namespaceContext, resultType); } else { Node node = XMLUtils.findNodeByName(received, xPathExpression); if (node == null) { throw new UnknownElementException( "Element ' " + xPathExpression + "' could not be found in DOM tree"); } if (XmlValidationUtils.isElementIgnored(node, validationContext.getIgnoreExpressions(), namespaceContext)) { continue; } xPathResult = getNodeValue(node); } if (expectedValue instanceof String) { expectedValue = context.replaceDynamicContentInString(String.valueOf(expectedValue)); } ValidationUtils.validateValues(xPathResult, expectedValue, xPathExpression, context); if (log.isDebugEnabled()) { log.debug("Validating element: " + xPathExpression + "='" + expectedValue + "': OK."); } } log.info("XPath element validation successful: All elements OK"); }  @Override void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, XpathMessageValidationContext validationContext); @Override boolean supportsMessageType(String messageType, Message message);  }
@Test public void testValidateMessageElementsWithNodeListResultNoMatch() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "<sub-element attribute='B'>other-value</sub-element>" + "</element>" + "</root>"); HashMap<String, Object> expressions = new HashMap<>(); expressions.put("node-set: expressions.put("boolean: expressions.put("boolean: XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expressions(expressions) .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test public void testValidateMessageElementsWithNodeListCount() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "<sub-element attribute='B'>text-value</sub-element>" + "</element>" + "</root>"); HashMap<String, Object> expressions = new HashMap<>(); expressions.put("number:count( expressions.put("integer:count( expressions.put("number:count( expressions.put("integer:count( XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expressions(expressions) .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test public void testValidateMessageElementsWithXPathSuccessful() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expression(" .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test public void testValidateMessageElementsWithValidationMatcherSuccessful() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, Object> validationExpressions = new HashMap<>(); validationExpressions.put(" validationExpressions.put(" validationExpressions.put(" XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expressions(validationExpressions) .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testValidateMessageElementsWithValidationMatcherNotSuccessful() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, Object> validationExpressions = new HashMap<>(); validationExpressions.put(" validationExpressions.put(" validationExpressions.put(" XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expressions(validationExpressions) .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testValidateMessageElementsWithXPathNotSuccessful() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expression(" .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test public void testValidateMessageElementsWithDotNotationSuccessful() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expression("root.element.sub-element", "text-value") .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test public void testValidateMessageElementsWithDotNotationValidationMatcherSuccessful() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expression("root.element.sub-element", "@contains('ext-val')@") .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testValidateMessageElementsWithDotNotationValidationMatcherNotSuccessful() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expression("root.element.sub-element", "@contains(false-value)@") .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testValidateMessageElementsWithDotNotationNotSuccessful() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expression("root.element.sub-element", "false-value") .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test public void testValidateMessageElementsWithMixedNotationsSuccessful() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Map<String, Object> validationExpressions = new HashMap<>(); validationExpressions.put(" validationExpressions.put("root.element.sub-element", "text-value"); XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expressions(validationExpressions) .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }
@Test public void testValidateMessageElementsWithNodeListResult() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "<sub-element attribute='B'>other-value</sub-element>" + "</element>" + "</root>"); XpathMessageValidationContext validationContext = new XpathMessageValidationContext.Builder() .expression("node-set: .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); validationContext = new XpathMessageValidationContext.Builder() .expression("node-set: .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); validationContext = new XpathMessageValidationContext.Builder() .expression("node-set: .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); validationContext = new XpathMessageValidationContext.Builder() .expression("node-set: .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); validationContext = new XpathMessageValidationContext.Builder() .expression("node-set: .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); validationContext = new XpathMessageValidationContext.Builder() .expression("node-set: .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); validationContext = new XpathMessageValidationContext.Builder() .expression("node-set: .build(); validator.validateMessage(message, new DefaultMessage(), context, validationContext); }