Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle validation runtime errors #232

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,18 @@ protected void validateAssembly(
* the Metapath dynamic execution context to use for Metapath
* evaluation
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateHasCardinality( // NOPMD false positive
@NonNull List<? extends ICardinalityConstraint> constraints,
@NonNull IAssemblyNodeItem item,
@NonNull DynamicContext dynamicContext) {
for (ICardinalityConstraint constraint : constraints) {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
assert constraint != null;

try {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateHasCardinality(constraint, item, targets, dynamicContext);
} catch (MetapathException ex) {
} catch (RuntimeException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
Expand Down Expand Up @@ -292,15 +295,18 @@ private void validateHasCardinality(
* the Metapath dynamic execution context to use for Metapath
* evaluation
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateIndex(
@NonNull List<? extends IIndexConstraint> constraints,
@NonNull IAssemblyNodeItem item,
@NonNull DynamicContext dynamicContext) {
for (IIndexConstraint constraint : constraints) {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
assert constraint != null;

try {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateIndex(constraint, item, targets, dynamicContext);
} catch (MetapathException ex) {
} catch (RuntimeException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
Expand Down Expand Up @@ -367,7 +373,7 @@ private void handlePass(
private void handleError(
@NonNull IConstraint constraint,
@NonNull INodeItem node,
@NonNull MetapathException ex,
@NonNull Throwable ex,
@NonNull DynamicContext dynamicContext) {
getConstraintValidationHandler()
.handleError(constraint, node, toErrorMessage(constraint, node, ex), ex, dynamicContext);
Expand All @@ -377,7 +383,7 @@ private void handleError(
private static String toErrorMessage(
@NonNull IConstraint constraint,
@NonNull INodeItem item,
@NonNull MetapathException ex) {
@NonNull Throwable ex) {
StringBuilder builder = new StringBuilder(128);
builder.append("A ")
.append(constraint.getClass().getName())
Expand Down Expand Up @@ -413,15 +419,18 @@ private static String toErrorMessage(
* the Metapath dynamic execution context to use for Metapath
* evaluation
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateUnique(
@NonNull List<? extends IUniqueConstraint> constraints,
@NonNull IAssemblyNodeItem item,
@NonNull DynamicContext dynamicContext) {
for (IUniqueConstraint constraint : constraints) {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
assert constraint != null;

try {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateUnique(constraint, item, targets, dynamicContext);
} catch (MetapathException ex) {
} catch (RuntimeException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
Expand Down Expand Up @@ -482,16 +491,19 @@ private void validateUnique(
* the Metapath dynamic execution context to use for Metapath
* evaluation
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateMatches( // NOPMD false positive
@NonNull List<? extends IMatchesConstraint> constraints,
@NonNull IDefinitionNodeItem<?, ?> item,
@NonNull DynamicContext dynamicContext) {

for (IMatchesConstraint constraint : constraints) {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
assert constraint != null;

try {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateMatches(constraint, item, targets, dynamicContext);
} catch (MetapathException ex) {
} catch (RuntimeException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
Expand Down Expand Up @@ -567,14 +579,21 @@ private void validateMatchesItem(
* the Metapath dynamic execution context to use for Metapath
* evaluation
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateIndexHasKey( // NOPMD false positive
@NonNull List<? extends IIndexHasKeyConstraint> constraints,
@NonNull IDefinitionNodeItem<?, ?> item,
@NonNull DynamicContext dynamicContext) {

for (IIndexHasKeyConstraint constraint : constraints) {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateIndexHasKey(constraint, item, targets);
assert constraint != null;

try {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateIndexHasKey(constraint, item, targets);
} catch (RuntimeException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
}

Expand Down Expand Up @@ -619,13 +638,20 @@ private void validateIndexHasKey(
* the Metapath dynamic execution context to use for Metapath
* evaluation
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateExpect(
@NonNull List<? extends IExpectConstraint> constraints,
@NonNull IDefinitionNodeItem<?, ?> item,
@NonNull DynamicContext dynamicContext) {
for (IExpectConstraint constraint : constraints) {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateExpect(constraint, item, targets, dynamicContext);
assert constraint != null;

try {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateExpect(constraint, item, targets, dynamicContext);
} catch (RuntimeException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
}

Expand Down Expand Up @@ -686,13 +712,19 @@ private void validateExpect(
* the Metapath dynamic execution context to use for Metapath
* evaluation
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateAllowedValues(
@NonNull List<? extends IAllowedValuesConstraint> constraints,
@NonNull IDefinitionNodeItem<?, ?> item,
@NonNull DynamicContext dynamicContext) {
for (IAllowedValuesConstraint constraint : constraints) {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateAllowedValues(constraint, item, targets, dynamicContext);
assert constraint != null;
try {
ISequence<? extends IDefinitionNodeItem<?, ?>> targets = constraint.matchTargets(item, dynamicContext);
validateAllowedValues(constraint, item, targets, dynamicContext);
} catch (RuntimeException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
}

Expand All @@ -712,6 +744,7 @@ private void validateAllowedValues(
* the Metapath dynamic execution context to use for Metapath
* evaluation
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateAllowedValues(
@NonNull IAllowedValuesConstraint constraint,
@NonNull IDefinitionNodeItem<?, ?> node,
Expand All @@ -722,7 +755,7 @@ private void validateAllowedValues(
if (item.hasValue()) {
try {
updateValueStatus(item, constraint, node);
} catch (MetapathException ex) {
} catch (RuntimeException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
Expand Down Expand Up @@ -774,6 +807,7 @@ protected void handleAllowedValues(
}
}

@SuppressWarnings("PMD.AvoidCatchingGenericException")
@Override
public void finalizeValidation(DynamicContext dynamicContext) {
// key references
Expand All @@ -790,8 +824,11 @@ public void finalizeValidation(DynamicContext dynamicContext) {
List<INodeItem> targets = keyRef.getTargets();
for (INodeItem item : targets) {
assert item != null;

validateKeyRef(constraint, node, item, indexName, index, dynamicContext);
try {
validateKeyRef(constraint, node, item, indexName, index, dynamicContext);
} catch (RuntimeException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.MetapathException;
import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem;
import gov.nist.secauto.metaschema.core.model.IAttributable;
import gov.nist.secauto.metaschema.core.model.IDescribable;
Expand Down Expand Up @@ -110,6 +111,8 @@ enum Level {
* @param dynamicContext
* the Metapath evaluation context to use
* @return the matching nodes as a sequence
* @throws MetapathException
* if an error occurred during evaluation
* @see #getTarget()
*/
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

package gov.nist.secauto.metaschema.cli;

import static org.assertj.core.api.Assertions.assertThat;
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 All @@ -24,6 +23,7 @@
import java.util.stream.Stream;

import edu.umd.cs.findbugs.annotations.NonNull;
import nl.altindag.log.LogCaptor;

/**
* Unit test for simple CLI.
Expand Down Expand Up @@ -212,19 +212,17 @@ void test() {
};
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."
});
.contains("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.");
}
}
}
Loading