Skip to content

Commit

Permalink
Added unit tests for metaschema-java bug related to metaschema-framew…
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Nov 9, 2024
1 parent 55c9e0d commit e06d969
Show file tree
Hide file tree
Showing 2 changed files with 882 additions and 22 deletions.
85 changes: 63 additions & 22 deletions src/test/java/gov/nist/secauto/oscal/tools/cli/core/CLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import gov.nist.secauto.metaschema.cli.processor.ExitCode;
Expand Down Expand Up @@ -35,6 +36,8 @@
import edu.umd.cs.findbugs.annotations.NonNull;

class CLITest {
private static final Throwable NO_THROWABLE_RESULT = null;

void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode) {
assertAll(
() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"),
Expand Down Expand Up @@ -90,7 +93,7 @@ private static Stream<Arguments> providesValues() throws IOException {
Paths.get("src/test/resources/cli/example_" + cmd + "_invalid" + sourceExtension).toString()
},
ExitCode.FAIL,
null));
NO_THROWABLE_RESULT));
values.add(
Arguments.of(
new String[] {
Expand All @@ -101,7 +104,7 @@ private static Stream<Arguments> providesValues() throws IOException {
Paths.get("src/test/resources/cli/example_" + cmd + "_valid" + sourceExtension).toString()
},
ExitCode.OK,
null));
NO_THROWABLE_RESULT));

// test general commands
values.add(
Expand All @@ -113,7 +116,7 @@ private static Stream<Arguments> providesValues() throws IOException {
Paths.get("src/test/resources/cli/example_" + cmd + "_invalid" + sourceExtension).toString()
},
ExitCode.FAIL,
null));
NO_THROWABLE_RESULT));
values.add(
Arguments.of(
new String[] {
Expand All @@ -123,7 +126,7 @@ private static Stream<Arguments> providesValues() throws IOException {
Paths.get("src/test/resources/cli/example_" + cmd + "_valid" + sourceExtension).toString()
},
ExitCode.OK,
null));
NO_THROWABLE_RESULT));

for (Format targetFormat : formatEntries.get(format)) {
Path path = Paths.get("src/test/resources/cli/example_" + cmd + "_valid" + sourceExtension);
Expand All @@ -139,7 +142,7 @@ private static Stream<Arguments> providesValues() throws IOException {
"--overwrite"
},
ExitCode.OK,
null));
NO_THROWABLE_RESULT));
// test general command
values.add(
Arguments.of(
Expand All @@ -151,7 +154,7 @@ private static Stream<Arguments> providesValues() throws IOException {
"--overwrite"
},
ExitCode.OK,
null));
NO_THROWABLE_RESULT));

// test command path-specific command
path = Paths.get("src/test/resources/cli/example_" + cmd + "_invalid" + sourceExtension);
Expand All @@ -166,7 +169,7 @@ private static Stream<Arguments> providesValues() throws IOException {
"--overwrite"
},
ExitCode.OK,
null));
NO_THROWABLE_RESULT));
// test general command
values.add(
Arguments.of(
Expand All @@ -178,7 +181,7 @@ private static Stream<Arguments> providesValues() throws IOException {
"--overwrite"
},
ExitCode.OK,
null));
NO_THROWABLE_RESULT));
}
if ("profile".equals(cmd)) {
// test command path-specific command
Expand All @@ -191,7 +194,7 @@ private static Stream<Arguments> providesValues() throws IOException {
Paths.get("src/test/resources/cli/example_profile_valid" + sourceExtension).toString()
},
ExitCode.OK,
null));
NO_THROWABLE_RESULT));
values.add(
Arguments.of(
new String[] {
Expand Down Expand Up @@ -243,20 +246,58 @@ void testAllSubCommands(@NonNull String[] commandArgs, @NonNull ExitCode expecte
evaluateResult(CLI.runCli(args), expectedExitCode, expectedThrownClass);
}
}

@Test
void testSystemSecurityPlanQuietlyFailing() {
Throwable NO_THROWABLE_RESULT = null;
String[] args= new String[] {
"convert",
"--to=yaml",
"src/test/resources/cli/quietly_failing_ssp.xml",
"target/oscal-cli-convert/quietly_failing_ssp_converted.json",
"--show-stack-trace"
};
ExitStatus status = CLI.runCli(args);
Throwable thrown = status.getThrowable();
assertAll(() -> assertEquals(ExitCode.OK, status.getExitCode()),
()-> assertEquals(NO_THROWABLE_RESULT, thrown));
String[] args = {
"convert",
"--to=yaml",
"src/test/resources/cli/quietly_failing_ssp.xml",
"target/oscal-cli-convert/quietly_failing_ssp_converted.json",
"--show-stack-trace",
"--overwrite"
};

ExitStatus status = CLI.runCli(args);
Throwable thrown = status.getThrowable();
assertAll(
() -> assertEquals(ExitCode.OK, status.getExitCode()),
() -> assertEquals(NO_THROWABLE_RESULT, thrown),
() -> assertNotEquals(Files.size(Paths.get("target/oscal-cli-convert/quietly_failing_ssp_converted.json")), 0));
}

@Test
void testSystemSecurityPlanQuietlyFailing2() {
String[] args = {
"validate",
"src/test/resources/AwesomeCloudSSP1extrainvalid.xml",
"--show-stack-trace"
};

ExitStatus status = CLI.runCli(args);
Throwable thrown = status.getThrowable();
assertAll(
() -> assertEquals(ExitCode.IO_ERROR, status.getExitCode()),
() -> assertEquals(IOException.class, thrown == null ? null : thrown.getClass()),
() -> assertNotEquals(Files.size(Paths.get("target/oscal-cli-convert/quietly_failing_ssp_converted.json")), 0));
}

@Test
void testSystemSecurityPlanQuietlyFailing3() {
String[] args = {
"convert",
"--to=json",
"src/test/resources/AwesomeCloudSSP1extrainvalid.xml",
"target/oscal-cli-convert/quietly_failing_ssp_converted2.json",
"--show-stack-trace",
"--overwrite"
};

ExitStatus status = CLI.runCli(args);
Throwable thrown = status.getThrowable();
assertAll(
() -> assertEquals(ExitCode.IO_ERROR, status.getExitCode()),
() -> assertEquals(IOException.class, thrown == null ? null : thrown.getClass()),
() -> assertNotEquals(Files.size(Paths.get("target/oscal-cli-convert/quietly_failing_ssp_converted.json")), 0));
}
}
Loading

0 comments on commit e06d969

Please sign in to comment.