Skip to content

Commit

Permalink
Parameterize CLI tests as prework for #241.
Browse files Browse the repository at this point in the history
  • Loading branch information
aj-stein-nist authored and david-waltermire committed Dec 18, 2023
1 parent 627e981 commit d9109df
Showing 1 changed file with 54 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,56 +32,70 @@

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;

/**
* 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<? 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"));
}

@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<? 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"));
}

@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<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();
}

@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<? extends Throwable> expectedThrownClass) {
if (expectedThrownClass == null) {
evaluateResult(CLI.runCli(args), expectedExitCode);
} else {
evaluateResult(CLI.runCli(args), expectedExitCode, expectedThrownClass);
}
}
}

0 comments on commit d9109df

Please sign in to comment.