Skip to content

Commit

Permalink
Merge pull request #1 from aj-stein-gsa/feature/issue215-custom-const…
Browse files Browse the repository at this point in the history
…raint-messages-unit-tests

Custom message constraint tests
  • Loading branch information
david-waltermire authored Nov 3, 2024
2 parents 38f49ea + d846ff2 commit 2dd24cb
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.assertj.core.api.Assertions.assertThat;

import gov.nist.secauto.metaschema.cli.processor.ExitCode;
import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
import nl.altindag.log.LogCaptor;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -182,7 +184,6 @@ private static Stream<Arguments> providesValues() {
ExitCode.OK, NO_EXCEPTION_CLASS));
}
};

return values.stream();
}

Expand All @@ -202,13 +203,28 @@ void testAllCommands(@NonNull String[] args, @NonNull ExitCode expectedExitCode,

@Test
void test() {
String[] cliArgs = { "validate-content",
"-m",
"src/test/resources/content/schema-validation-module.xml",
"src/test/resources/content/schema-validation-module-missing-required.xml",
"--as=xml",
"--disable-schema-validation"
};
CLI.runCli(cliArgs);
try (LogCaptor captor = LogCaptor.forRoot()) {
String[] cliArgs = { "validate-content",
"-m",
"src/test/resources/content/215-module.xml",
"src/test/resources/content/215.xml",
"--disable-schema-validation"
};
CLI.runCli(cliArgs);
assertThat(captor.getErrorLogs().toString())
.contains(new String[] {
"expect-default-non-zero: Expect constraint '. > 0' did not match the data",
"expect-custom-non-zero: No default message, custom error message for expect-custom-non-zero constraint.",
"matches-default-regex-letters-only: Value '1' did not match the pattern",
"matches-custom-regex-letters-only: No default message, custom error message for matches-custom-regex-letters-only constraint.",
"cardinality-default-two-minimum: The cardinality '1' is below the required minimum '2' for items matching",
"index-items-default: Index 'index-items-default' has duplicate key for items",
"index-items-custom: No default message, custom error message for index-item-custom.",
"is-unique-default: Unique constraint violation at paths",
"is-unique-custom: No default message, custom error message for is-unique-custom.",
"index-has-key-default: Key reference [2] not found in index 'index-items-default' for item",
"index-has-key-custom: No default message, custom error message for index-has-key-custom."
});
}
}
}
73 changes: 73 additions & 0 deletions metaschema-cli/src/test/resources/content/215-module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<METASCHEMA xmlns="http://csrc.nist.gov/ns/oscal/metaschema/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://csrc.nist.gov/ns/oscal/metaschema/1.0 https://raw.githubusercontent.com/metaschema-framework/metaschema/0441e6d4c9bce5b6c40b4647148019e4f47bed08/schema/xml/metaschema.xsd">
<schema-name>Module for metaschema-framework/metaschema-java#215</schema-name>
<schema-version>0.1.0</schema-version>
<short-name>model215</short-name>
<namespace>https://github.com/metaschema-framework/metaschema-java/issues/215</namespace>
<json-base-uri>https://github.com/metaschema-framework/metaschema-java/issues/215</json-base-uri>
<define-assembly name="root">
<root-name>message-root</root-name>
<model>
<define-field name="expect-default-non-zero" as-type="integer" min-occurs="1"/>
<define-field name="expect-custom-non-zero" as-type="integer" min-occurs="1"/>
<define-field name="matches-default-regex-letters-only" as-type="string" min-occurs="1"/>
<define-field name="matches-custom-regex-letters-only" as-type="string" min-occurs="1"/>
<define-field name="item-default" as-type="string" min-occurs="1" max-occurs="unbounded">
<group-as name="items-default" in-json="ARRAY"/>
<define-flag name="id" as-type="integer"/>
</define-field>
<define-field name="item-custom" as-type="string" min-occurs="1" max-occurs="unbounded">
<group-as name="items-custom" in-json="ARRAY"/>
<define-flag name="id" as-type="integer"/>
</define-field>
<define-field name="item-cardinality" max-occurs="unbounded">
<group-as name="items-cardinality" in-json="ARRAY"/>
<define-flag name="value" as-type="integer"/>
</define-field>
<define-field name="item-unique" max-occurs="unbounded">
<group-as name="items-unique" in-json="ARRAY"/>
<define-flag name="id" as-type="integer" required="yes"/>
<define-flag name="x" as-type="integer" required="yes"/>
<define-flag name="y" as-type="integer" required="yes"/>
</define-field>
</model>
<constraint>
<expect id="expect-default-non-zero" level="ERROR" target="expect-default-non-zero" test=". > 0"/>
<expect id="expect-custom-non-zero" level="ERROR" target="expect-custom-non-zero" test=". > 0">
<message>No default message, custom error message for expect-custom-non-zero constraint.</message>
</expect>
<has-cardinality id="cardinality-default-two-minimum" level="ERROR" target="item-cardinality" min-occurs="2" max-occurs="10"/>
<index id="index-items-default" name="index-items-default" level="ERROR" target="item-default">
<key-field target="@id"/>
</index>
<index id="index-items-custom" name="index-items-custom" level="ERROR" target="item-custom">
<key-field target="@id"/>
<message>No default message, custom error message for index-item-custom.</message>
</index>
<index-has-key id="index-has-key-default" level="ERROR" name="index-items-default" target="item-cardinality">
<key-field target="@value"/>
</index-has-key>
<index-has-key id="index-has-key-custom" level="ERROR" name="index-items-default" target="item-cardinality">
<key-field target="@value"/>
<message>No default message, custom error message for index-has-key-custom.</message>
</index-has-key>
<is-unique id="is-unique-default" level="ERROR" target="item-unique">
<key-field target="@id"/>
<key-field target="@x"/>
<key-field target="@y"/>
</is-unique>
<is-unique id="is-unique-custom" level="ERROR" target="item-unique">
<key-field target="@id"/>
<key-field target="@x"/>
<key-field target="@y"/>
<message>No default message, custom error message for is-unique-custom.</message>
</is-unique>
<matches id="matches-default-regex-letters-only" level="ERROR" target="matches-default-regex-letters-only" regex="^[a-zA-Z]$"/>
<matches id="matches-custom-regex-letters-only" level="ERROR" target="matches-custom-regex-letters-only" regex="^[a-zA-Z]$">
<message>No default message, custom error message for matches-custom-regex-letters-only constraint.</message>
</matches>
</constraint>
</define-assembly>
</METASCHEMA>
15 changes: 15 additions & 0 deletions metaschema-cli/src/test/resources/content/215.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<message-root xmlns="https://github.com/metaschema-framework/metaschema-java/issues/215">
<expect-default-non-zero>0</expect-default-non-zero>
<expect-custom-non-zero>0</expect-custom-non-zero>
<matches-default-regex-letters-only>1</matches-default-regex-letters-only>
<matches-custom-regex-letters-only>1</matches-custom-regex-letters-only>
<item-default id="1">red</item-default>
<item-default id="1">blue</item-default>
<item-custom id="1">green</item-custom>
<item-custom id="1">yellow</item-custom>
<item-cardinality value="2"/>
<item-unique id="1" x="2" y="3"/>
<item-unique id="1" x="2" y="3"/>
<!-- allowed-values and has-cardinality do not support custom message fields at this time. -->
</message-root>

0 comments on commit 2dd24cb

Please sign in to comment.