Skip to content

Commit

Permalink
Added unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Oct 18, 2024
1 parent acb6cef commit 04b0886
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public static MetapathExpression compile(@NonNull String path) {
*/
@NonNull
public static MetapathExpression compile(@NonNull String path, @NonNull StaticContext context) {
@NonNull MetapathExpression retval;
@NonNull
MetapathExpression retval;
if (".".equals(path)) {
retval = CONTEXT_NODE;
} else {
Expand Down Expand Up @@ -355,8 +356,7 @@ protected <T> T toResultType(@NonNull ISequence<?> sequence, @NonNull ResultType
throw new InvalidTypeMetapathException(null, String.format("unsupported result type '%s'", resultType.name()));
}

@SuppressWarnings("unchecked") T retval = (T) result;
return retval;
return (T) result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public class DefaultBoundLoader
private final IMutableConfiguration<DeserializationFeature<?>> configuration;

/**
* Construct a new OSCAL loader instance, using the provided
* {@link IBindingContext}.
* Construct a new loader instance, using the provided {@link IBindingContext}.
*
* @param bindingContext
* the Module binding context to use to load Java types
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

package gov.nist.secauto.metaschema.databind.io;

import static org.junit.jupiter.api.Assertions.assertEquals;

import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
import gov.nist.secauto.metaschema.core.model.IModule;
import gov.nist.secauto.metaschema.core.model.MetaschemaException;
import gov.nist.secauto.metaschema.core.model.xml.ModuleLoader;
import gov.nist.secauto.metaschema.databind.IBindingContext;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

class DefaultBoundLoaderTest {

@Test
void testIssue187() throws IOException, MetaschemaException {

IModule module = new ModuleLoader().load(Paths.get("src/test/resources/content/issue187-metaschema.xml"));

IBindingContext bindingContext = IBindingContext.instance();

bindingContext.registerModule(module, Files.createTempDirectory(Paths.get("target"), "modules-"));

IBoundLoader loader = bindingContext.newBoundLoader();

IDocumentNodeItem docItem = loader.loadAsNodeItem(Paths.get("src/test/resources/content/issue187-instance.xml"));

MetapathExpression metapath = MetapathExpression.compile("//a//b", docItem.getStaticContext());

ISequence<?> result = metapath.evaluate(docItem);

assertEquals(8, result.size());
}
}
19 changes: 19 additions & 0 deletions databind/src/test/resources/content/issue187-instance.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns="http://csrc.nist.gov/ns/test/metaschema/issue187-test">
<a value="a1">
<b>
<b value="value1"/>
</b>
<b>
<b value="value2"/>
</b>
</a>
<a value="a2">
<b>
<b value="value1"/>
</b>
<b>
<b value="value2"/>
</b>
</a>
</a>
22 changes: 22 additions & 0 deletions databind/src/test/resources/content/issue187-metaschema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../../../../../core/metaschema/schema/xml/metaschema.xsd" type="application/xml" schematypens="http://www.w3.org/2001/XMLSchema"?>
<METASCHEMA xmlns="http://csrc.nist.gov/ns/oscal/metaschema/1.0">
<schema-name>Test Metaschema with an external entity</schema-name>
<schema-version>1.0.0</schema-version>
<short-name>issue187-test</short-name>
<namespace>http://csrc.nist.gov/ns/test/metaschema/issue187-test</namespace>
<json-base-uri>http://csrc.nist.gov/ns/test/metaschema/issue187-test</json-base-uri>
<define-assembly name="a">
<root-name>a</root-name>
<define-flag name="value" as-type="string"/>
<model>
<assembly ref="a" max-occurs="unbounded">
<group-as name="as" in-json="ARRAY" />
</assembly>
<assembly ref="a" max-occurs="unbounded">
<use-name>b</use-name>
<group-as name="bs" in-json="ARRAY" />
</assembly>
</model>
</define-assembly>
</METASCHEMA>

0 comments on commit 04b0886

Please sign in to comment.