From 0e5afa81a19891719c5b3bf716d7e1419b0d93cd Mon Sep 17 00:00:00 2001 From: Tom Jegge Date: Fri, 15 Sep 2023 13:11:09 +0200 Subject: [PATCH] test(#990): Improve schema resolving xml --- .../xml/schema/XmlSchemaValidation.java | 18 ++++---- .../xml/DomXmlMessageValidatorTest.java | 45 +++++++++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/validation/citrus-validation-xml/src/main/java/org/citrusframework/validation/xml/schema/XmlSchemaValidation.java b/validation/citrus-validation-xml/src/main/java/org/citrusframework/validation/xml/schema/XmlSchemaValidation.java index 162bd3c68d..63141dd85f 100644 --- a/validation/citrus-validation-xml/src/main/java/org/citrusframework/validation/xml/schema/XmlSchemaValidation.java +++ b/validation/citrus-validation-xml/src/main/java/org/citrusframework/validation/xml/schema/XmlSchemaValidation.java @@ -36,13 +36,13 @@ public class XmlSchemaValidation implements SchemaValidator { /** Logger */ - private Logger log = LoggerFactory.getLogger(XmlSchemaValidation.class); + private static final Logger logger = LoggerFactory.getLogger(XmlSchemaValidation.class); /** Transformer factory */ private final TransformerFactory transformerFactory = TransformerFactory.newInstance(); /** - * Validate message with a XML schema. + * Validate message with an XML schema. * * @param message * @param context @@ -66,7 +66,7 @@ private void validateSchema(Message message, TestContext context, XmlMessageVali return; } - log.debug("Starting XML schema validation ..."); + logger.debug("Starting XML schema validation ..."); XmlValidator validator = null; XsdSchemaRepository schemaRepository = null; @@ -80,7 +80,7 @@ private void validateSchema(Message message, TestContext context, XmlMessageVali } else if (schemaRepositories.size() > 0) { schemaRepository = schemaRepositories.stream().filter(repository -> repository.canValidate(doc)).findFirst().orElseThrow(() -> new CitrusRuntimeException(String.format("Failed to find proper schema " + "repository for validating element '%s(%s)'", doc.getFirstChild().getLocalName(), doc.getFirstChild().getNamespaceURI()))); } else { - log.warn("Neither schema instance nor schema repository defined - skipping XML schema validation"); + logger.warn("Neither schema instance nor schema repository defined - skipping XML schema validation"); return; } @@ -113,18 +113,18 @@ private void validateSchema(Message message, TestContext context, XmlMessageVali SAXParseException[] results = validator.validate(new DOMSource(doc)); if (results.length == 0) { - log.info("XML schema validation successful: All values OK"); + logger.info("XML schema validation successful: All values OK"); } else { - log.error("XML schema validation failed for message:\n" + XMLUtils.prettyPrint(message.getPayload(String.class))); + logger.error("XML schema validation failed for message:\n" + XMLUtils.prettyPrint(message.getPayload(String.class))); // Report all parsing errors - log.debug("Found " + results.length + " schema validation errors"); + logger.debug("Found " + results.length + " schema validation errors"); StringBuilder errors = new StringBuilder(); for (SAXParseException e : results) { errors.append(e.toString()); errors.append("\n"); } - log.debug(errors.toString()); + logger.debug(errors.toString()); throw new ValidationException("XML schema validation failed:", results[0]); } @@ -153,4 +153,4 @@ protected void validateDTD(Resource dtdResource, Message receivedMessage) { public boolean supportsMessageType(String messageType, Message message) { return "XML".equals(messageType) || (message != null && IsXmlPredicate.getInstance().test(message.getPayload(String.class))); } -} +} \ No newline at end of file diff --git a/validation/citrus-validation-xml/src/test/java/org/citrusframework/validation/xml/DomXmlMessageValidatorTest.java b/validation/citrus-validation-xml/src/test/java/org/citrusframework/validation/xml/DomXmlMessageValidatorTest.java index ee1d558fbb..6043bc11a4 100644 --- a/validation/citrus-validation-xml/src/test/java/org/citrusframework/validation/xml/DomXmlMessageValidatorTest.java +++ b/validation/citrus-validation-xml/src/test/java/org/citrusframework/validation/xml/DomXmlMessageValidatorTest.java @@ -41,6 +41,7 @@ import org.citrusframework.validation.script.ScriptValidationContext; import org.citrusframework.validation.xml.schema.XmlSchemaValidation; import org.citrusframework.xml.XsdSchemaRepository; +import org.citrusframework.xml.schema.XsdSchemaCollection; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.xml.xsd.SimpleXsdSchema; @@ -414,6 +415,50 @@ public void validateNoSchemaRepositoryAtAll() throws SAXException, IOException, validator.validateXMLSchema(message, context, new XmlMessageValidationContext()); } + @Test + public void validateSchemaWithSchemaImport() throws SAXException, IOException, ParserConfigurationException { + Message message = new DefaultMessage("" + + "Cm123456789" + + "FOO" + + ""); + + XsdSchemaRepository schemaRepository = new XsdSchemaRepository(); + schemaRepository.setName("schemaRepository"); + + XsdSchemaCollection schemaCollection = new XsdSchemaCollection(); + schemaCollection.setSchemas(List.of("org/citrusframework/validation/SampleMessage.xsd", "org/citrusframework/validation/SampleTypes.xsd")); + schemaCollection.initialize(); + schemaCollection.afterPropertiesSet(); + + schemaRepository.getSchemas().add(schemaCollection); + + context.getReferenceResolver().bind("schemaRepository", schemaRepository); + + validator.validateXMLSchema(message, context, new XmlMessageValidationContext()); + } + + @Test + public void validateSchemaWithSchemaImportAndWildcard() throws ParserConfigurationException, IOException, SAXException { + Message message = new DefaultMessage("" + + "Cm123456789" + + "FOO" + + ""); + + XsdSchemaRepository schemaRepository = new XsdSchemaRepository(); + schemaRepository.setName("schemaRepository"); + + XsdSchemaCollection schemaCollection = new XsdSchemaCollection(); + schemaCollection.setSchemas(List.of("org/citrusframework/validation/Sample*.xsd")); + schemaCollection.initialize(); + schemaCollection.afterPropertiesSet(); + + schemaRepository.getSchemas().add(schemaCollection); + + context.getReferenceResolver().bind("schemaRepository", schemaRepository); + + validator.validateXMLSchema(message, context, new XmlMessageValidationContext()); + } + @Test(expectedExceptions = {ValidationException.class}) public void validateXMLSchemaError() throws SAXException, IOException, ParserConfigurationException { Message message = new DefaultMessage(""