Skip to content

Commit

Permalink
Improved XML schema generation testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Sep 20, 2023
1 parent 4fbe56d commit ebdc62e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions schemagen/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -89,7 +92,7 @@ public abstract class AbstractSchemaGeneratorTestSuite

static {
IMutableConfiguration<SchemaGenerationFeature<?>> features = new DefaultConfiguration<>();
features.disableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
features.enableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
SCHEMA_GENERATION_CONFIG = features;

BiFunction<IModule, Writer, Void> xmlProvider = (module, writer) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SchemaGenerationFeature<?>> 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<Element> xpath = XPathFactory.instance()
.compile("//xs:attribute[not(@type or xs:simpleType)]",
Filters.element(),
null,
Namespace.getNamespace("xs", "http://www.w3.org/2001/XMLSchema"));
List<Element> result = xpath.evaluate(document);

assertTrue(result.isEmpty());
}
}

@Test
Expand Down

0 comments on commit ebdc62e

Please sign in to comment.