diff --git a/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java b/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java index 4fdb744a5..7666866a4 100644 --- a/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java +++ b/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java @@ -32,8 +32,20 @@ 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; @@ -41,47 +53,49 @@ * 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, - @NonNull Class 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")); - } - - @Test - void testHelp() { - String[] args = {}; - evaluateResult(CLI.runCli(args), ExitCode.INVALID_COMMAND); - } + 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")); + } - @Test - void testHelpArg() { - String[] args = { "-h" }; - evaluateResult(CLI.runCli(args), ExitCode.OK); - } + void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode, + @NonNull Class 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")); + } - @Test - void testMetaschemaValidate() { - String[] args = { "validate", - "../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml" }; - evaluateResult(CLI.runCli(args), ExitCode.OK); - } + private static Stream providesValues() { + ExitCode noExpectedExceptionClass = null; + List 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(); + } - @Test - void testMetaschemaGenerateSchema() { - String[] args = { "generate-schema", "--overwrite", "--as", "JSON", - "../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml", - "target/schema-test.json" }; - evaluateResult(CLI.runCli(args), ExitCode.OK); - } + @ParameterizedTest + @MethodSource("providesValues") + void testAllCommands(@NonNull String[] args, @NonNull ExitCode expectedExitCode, + Class expectedThrownClass) { + if (expectedThrownClass == null) { + evaluateResult(CLI.runCli(args), expectedExitCode); + } else { + evaluateResult(CLI.runCli(args), expectedExitCode, expectedThrownClass); + } + } }