From ebdc62e3178b3e0facda4f478cf4e7c5772c247d Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Wed, 20 Sep 2023 11:46:48 -0400 Subject: [PATCH] Improved XML schema generation testing. --- .../model/testing/AbstractTestSuite.java | 1 + schemagen/src/main/java/module-info.java | 1 + .../AbstractSchemaGeneratorTestSuite.java | 5 ++- .../metaschema/schemagen/XmlSuiteTest.java | 41 +++++++++++++++---- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/metaschema-testing/src/main/java/gov/nist/secauto/metaschema/model/testing/AbstractTestSuite.java b/metaschema-testing/src/main/java/gov/nist/secauto/metaschema/model/testing/AbstractTestSuite.java index 8a5610443..b610f4693 100644 --- a/metaschema-testing/src/main/java/gov/nist/secauto/metaschema/model/testing/AbstractTestSuite.java +++ b/metaschema-testing/src/main/java/gov/nist/secauto/metaschema/model/testing/AbstractTestSuite.java @@ -217,6 +217,7 @@ protected void produceSchema(@NonNull IModule module, @NonNull Path schemaPath, getWriteOpenOptions())) { schemaProducer.apply(module, writer); } + LOGGER.atInfo().log("Produced schema '{}' for module '{}'", schemaPath, module.getLocation()); } protected OpenOption[] getWriteOpenOptions() { diff --git a/schemagen/src/main/java/module-info.java b/schemagen/src/main/java/module-info.java index 9aadf8e8d..a88d4ee6d 100644 --- a/schemagen/src/main/java/module-info.java +++ b/schemagen/src/main/java/module-info.java @@ -33,6 +33,7 @@ requires com.ctc.wstx; requires com.github.spotbugs.annotations; requires transitive org.apache.commons.lang3; + requires org.apache.logging.log4j; requires org.jdom2; requires Saxon.HE; diff --git a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/AbstractSchemaGeneratorTestSuite.java b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/AbstractSchemaGeneratorTestSuite.java index 77ae90165..866d8b518 100644 --- a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/AbstractSchemaGeneratorTestSuite.java +++ b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/AbstractSchemaGeneratorTestSuite.java @@ -43,6 +43,8 @@ import gov.nist.secauto.metaschema.schemagen.json.JsonSchemaGenerator; import gov.nist.secauto.metaschema.schemagen.xml.XmlSchemaGenerator; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.junit.platform.commons.JUnitException; import org.xml.sax.SAXException; @@ -67,6 +69,7 @@ public abstract class AbstractSchemaGeneratorTestSuite extends AbstractTestSuite { + private static final Logger LOGGER = LogManager.getLogger(AbstractTestSuite.class); @NonNull protected static final ISchemaGenerator XML_SCHEMA_GENERATOR = new XmlSchemaGenerator(); @NonNull @@ -89,7 +92,7 @@ public abstract class AbstractSchemaGeneratorTestSuite static { IMutableConfiguration> features = new DefaultConfiguration<>(); - features.disableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS); + features.enableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS); SCHEMA_GENERATION_CONFIG = features; BiFunction xmlProvider = (module, writer) -> { diff --git a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java index d4cd80cb8..6740e898a 100644 --- a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java +++ b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java @@ -27,6 +27,7 @@ package gov.nist.secauto.metaschema.schemagen; import static org.junit.jupiter.api.Assertions.assertTrue; + import gov.nist.secauto.metaschema.core.configuration.DefaultConfiguration; import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration; import gov.nist.secauto.metaschema.core.model.IModule; @@ -163,25 +164,49 @@ void testAllowedValues() throws IOException, MetaschemaException { // NOPMD - de contentCase(Format.XML, "allowed-values-basic_test_valid_PASS.xml", true)); } - @Disabled @Test - void testOSCALComplete() throws IOException, MetaschemaException { // NOPMD - delegated to doTest + void testLocalDeclarations() throws IOException, MetaschemaException { // NOPMD - delegated to doTest + doTest( + "local-declarations", + "global-and-local_metaschema.xml", + "global-and-local"); + } + + @Test + void testliboscalJavaIssue181() throws IOException, MetaschemaException, XMLStreamException, JDOMException { ModuleLoader loader = new ModuleLoader(); loader.allowEntityResolution(); IModule module = loader.load(new URL( // "https://raw.githubusercontent.com/usnistgov/OSCAL/develop/src/metaschema/oscal_complete_metaschema.xml")); - "https://raw.githubusercontent.com/usnistgov/OSCAL/develop/src/metaschema/oscal_complete_metaschema.xml")); + "https://raw.githubusercontent.com/usnistgov/OSCAL/v1.1.1/src/metaschema/oscal_catalog_metaschema.xml")); ISchemaGenerator schemaGenerator = new XmlSchemaGenerator(); IMutableConfiguration> features = new DefaultConfiguration<>(); - features.disableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS); - try (Writer writer = Files.newBufferedWriter( - Path.of("target/oscal-complete_schema.xsd"), - StandardCharsets.UTF_8, - getWriteOpenOptions())) { + features.enableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS); + features.disableFeature(SchemaGenerationFeature.INLINE_CHOICE_DEFINITIONS); + + Path schemaPath = Path.of("target/oscal-catalog_schema.xsd"); + try (Writer writer = Files.newBufferedWriter(schemaPath, StandardCharsets.UTF_8, getWriteOpenOptions())) { assert writer != null; schemaGenerator.generateFromModule(module, writer, features); } + + // check for missing attribute types per liboscal-java#181 + XMLInputFactory factory = XMLInputFactory.newFactory(); + try (Reader fileReader = new FileReader(schemaPath.toFile())) { + XMLEventReader reader = factory.createXMLEventReader(fileReader); + StAXEventBuilder builder = new StAXEventBuilder(); + Document document = document = builder.build(reader); + + XPathExpression xpath = XPathFactory.instance() + .compile("//xs:attribute[not(@type or xs:simpleType)]", + Filters.element(), + null, + Namespace.getNamespace("xs", "http://www.w3.org/2001/XMLSchema")); + List result = xpath.evaluate(document); + + assertTrue(result.isEmpty()); + } } @Test