Skip to content

Commit

Permalink
Repro test, modules, and doc instances for usnistgov#293
Browse files Browse the repository at this point in the history
  • Loading branch information
aj-stein-nist committed Jan 18, 2024
1 parent 47a9687 commit c239395
Showing 1 changed file with 44 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,13 @@

import gov.nist.secauto.metaschema.cli.processor.ExitCode;
import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
import gov.nist.secauto.metaschema.cli.CLI;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -53,49 +47,51 @@
* Unit test for simple CLI.
*/
public class CLITest {
void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode) {
status.generateMessage(true);
assertAll(() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"),
() -> assertNull(status.getThrowable(), "expected null Throwable"));
}
void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode) {
status.generateMessage(true);
assertAll(() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"),
() -> assertNull(status.getThrowable(), "expected null Throwable"));
}

void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode,
@NonNull Class<? extends Throwable> thrownClass) {
status.generateMessage(true);
Throwable thrown = status.getThrowable();
assert thrown != null;
assertAll(() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"),
() -> assertEquals(thrownClass, thrown.getClass(), "expected Throwable mismatch"));
}
void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode,
@NonNull Class<? extends Throwable> thrownClass) {
status.generateMessage(true);
Throwable thrown = status.getThrowable();
assert thrown != null;
assertAll(() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"),
() -> assertEquals(thrownClass, thrown.getClass(), "expected Throwable mismatch"));
}

private static Stream<Arguments> providesValues() {
ExitCode noExpectedExceptionClass = null;
List<Arguments> values = new ArrayList<>();
values.add(Arguments.of(new String[] {}, ExitCode.INVALID_COMMAND, noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "-h" }, ExitCode.OK, noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.INVALID_COMMAND,
noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "validate", "--help" }, ExitCode.OK, noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.INVALID_COMMAND,
noExpectedExceptionClass));
values.add(Arguments.of(
new String[] { "validate",
"../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml" },
ExitCode.OK, noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "generate-schema", "--overwrite", "--as", "JSON",
"../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml",
"target/schema-test.json" }, ExitCode.OK, noExpectedExceptionClass));
return values.stream();
}
private static Stream<Arguments> providesValues() {
ExitCode noExpectedExceptionClass = null;
List<Arguments> values = new ArrayList<>();
values.add(Arguments.of(new String[] {}, ExitCode.INVALID_COMMAND, noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "-h" }, ExitCode.OK, noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "generate-schema" }, ExitCode.OK,
noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.OK,
noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "validate", "--help" }, ExitCode.OK, noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.INVALID_COMMAND,
noExpectedExceptionClass));
values.add(Arguments.of(
new String[] { "validate",
"../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml" },
ExitCode.OK, noExpectedExceptionClass));
values.add(Arguments.of(new String[] { "generate-schema", "--overwrite", "--as", "JSON",
"../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml",
"target/schema-test.json" }, ExitCode.OK, noExpectedExceptionClass));
return values.stream();
}

@ParameterizedTest
@MethodSource("providesValues")
void testAllCommands(@NonNull String[] args, @NonNull ExitCode expectedExitCode,
Class<? extends Throwable> expectedThrownClass) {
if (expectedThrownClass == null) {
evaluateResult(CLI.runCli(args), expectedExitCode);
} else {
evaluateResult(CLI.runCli(args), expectedExitCode, expectedThrownClass);
}
}
@ParameterizedTest
@MethodSource("providesValues")
void testAllCommands(@NonNull String[] args, @NonNull ExitCode expectedExitCode,
Class<? extends Throwable> expectedThrownClass) {
if (expectedThrownClass == null) {
evaluateResult(CLI.runCli(args), expectedExitCode);
} else {
evaluateResult(CLI.runCli(args), expectedExitCode, expectedThrownClass);
}
}
}

0 comments on commit c239395

Please sign in to comment.