DomXmlMessageValidator extends AbstractMessageValidator<XmlMessageValidationContext> { @Override public void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, XmlMessageValidationContext validationContext) throws ValidationException { log.debug("Start XML message validation ..."); try { if (validationContext.isSchemaValidationEnabled()) { validateXMLSchema(receivedMessage, context, validationContext); validateDTD(validationContext.getDTDResource(), receivedMessage); } validateNamespaces(validationContext.getControlNamespaces(), receivedMessage); validateMessageContent(receivedMessage, controlMessage, validationContext, context); if (controlMessage != null) { Assert.isTrue(controlMessage.getHeaderData().size() <= receivedMessage.getHeaderData().size(), "Failed to validate header data XML fragments - found " + receivedMessage.getHeaderData().size() + " header fragments, expected " + controlMessage.getHeaderData().size()); for (int i = 0; i < controlMessage.getHeaderData().size(); i++) { validateXmlHeaderFragment(receivedMessage.getHeaderData().get(i), controlMessage.getHeaderData().get(i), validationContext, context); } } log.info("XML message validation successful: All values OK"); } catch (ClassCastException | DOMException | LSException e) { throw new CitrusRuntimeException(e); } catch (IllegalArgumentException e) { log.error("Failed to validate:\n" + XMLUtils.prettyPrint(receivedMessage.getPayload(String.class))); throw new ValidationException("Validation failed:", e); } catch (ValidationException ex) { log.error("Failed to validate:\n" + XMLUtils.prettyPrint(receivedMessage.getPayload(String.class))); throw ex; } }  @Override void validateMessage(Message receivedMessage, Message controlMessage,
                                TestContext context, XmlMessageValidationContext validationContext); @Override boolean supportsMessageType(String messageType, Message message); void addSchemaRepository(XsdSchemaRepository schemaRepository);  }
@Test public void testValidateMessagePayloadSuccess() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='A'>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test public void testValidateMessagePayloadWithIgnoresSuccess() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element1 attribute='A'>THIS_IS_IGNORED_BY_XPATH</sub-element1>" + "<sub-element2 attribute='A'>THIS IS IGNORED BY IGNORE-EXPR</sub-element2>" + "<sub-element3 attribute='A'>a text</sub-element3>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element1 attribute='A'>text-value</sub-element1>" + "<sub-element2 attribute='A'>@ignore@</sub-element2>" + "<sub-element3 attribute='A'>a text</sub-element3>" + "</element>" + "</root>"); Set<String> ignoreExpressions = new HashSet<String>(); ignoreExpressions.add(" XmlMessageValidationContext validationContext = new XmlMessageValidationContext.Builder() .ignore(ignoreExpressions) .build(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testValidateMessagePayloadWithValidationMatchersFailsBecauseOfAttribute() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='text-attribute'>text-element</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='@startsWith(FAIL)@'>@startsWith(text)@</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testValidateMessagePayloadWithValidationMatcherOnElementFails() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='text-attribute'>text-element</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='text-attribute'>@startsWith(FAIL)@</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testValidateMessagePayloadWithValidationMatcherOnAttributeFails() { Message message = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='text-attribute'>text-element</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root>" + "<element attributeA='attribute-value' attributeB='attribute-value'>" + "<sub-element attribute='@startsWith(FAIL)@'>text-element</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test public void testNamespaceQualifiedAttributeValue() { Message message = new DefaultMessage("<root xmlns='http: + "<element xsi:type='ns1:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root xmlns='http: + "<element xsi:type='ns1:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test public void testNamespaceQualifiedAttributeValueParentDeclaration() { Message message = new DefaultMessage("<root xmlns='http: + "<element xmlns:ns1='http: + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root xmlns='http: + "<element xsi:type='ns1:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test public void testNamespaceQualifiedAttributeValueParentDeclarationInSource() { Message message = new DefaultMessage("<root xmlns='http: + "<element xmlns:ns1='http: + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root xmlns='http: + "<element xmlns:ns1='http: + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test public void testNamespaceQualifiedAttributeValueDifferentPrefix() { Message message = new DefaultMessage("<root xmlns='http: + "<element xsi:type='ns1:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root xmlns='http: + "<element xsi:type='cit:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='cit2:AType'>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test public void testNamespaceQualifiedLikeAttributeValues() { Message message = new DefaultMessage("<root xmlns='http: + "<element credentials='username:password' attributeB='attribute-value'>" + "<sub-element>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root xmlns='http: + "<element credentials='username:password' attributeB='attribute-value'>" + "<sub-element>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test public void testCommentBeforeRootElement() { Message message = new DefaultMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!-- some comment -->" + "<root>" + "<element>test</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root>" + "<element>test</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test public void testComment() { Message message = new DefaultMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<root>" + "<!-- some comment -->" + "<element>test</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root>" + "<element>test</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testNamespaceQualifiedAttributeValueFails() { Message message = new DefaultMessage("<root xmlns='http: + "<element xsi:type='ns1:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root xmlns='http: + "<element xsi:type='ns1:wrong-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testNamespaceQualifiedAttributeValueUriMismatch() { Message message = new DefaultMessage("<root xmlns='http: + "<element xsi:type='ns1:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root xmlns='http: + "<element xsi:type='cit:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testNamespaceQualifiedAttributeMissingPrefix() { Message message = new DefaultMessage("<root xmlns='http: + "<element xsi:type='attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root xmlns='http: + "<element xsi:type='ns1:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }
@Test(expectedExceptions = {ValidationException.class}) public void testNamespaceQualifiedAttributeValueMissingDeclaration() { Message message = new DefaultMessage("<root xmlns='http: + "<element xsi:type='ns1:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); Message controlMessage = new DefaultMessage("<root xmlns='http: + "<element xsi:type='cit:attribute-value' attributeB='attribute-value'>" + "<sub-element xsi:type='ns2:AType'>text-value</sub-element>" + "</element>" + "</root>"); XmlMessageValidationContext validationContext = new XmlMessageValidationContext(); DomXmlMessageValidator validator = new DomXmlMessageValidator(); validator.validateMessage(message, controlMessage, context, validationContext); }