|
26 | 26 |
|
27 | 27 | package gov.nist.secauto.metaschema.cli;
|
28 | 28 |
|
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
29 | 30 | import static org.junit.jupiter.api.Assertions.assertAll;
|
30 | 31 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
31 | 32 | import static org.junit.jupiter.api.Assertions.assertNull;
|
32 | 33 |
|
| 34 | +import gov.nist.secauto.metaschema.cli.commands.QueryCommand; |
33 | 35 | import gov.nist.secauto.metaschema.cli.processor.ExitCode;
|
34 | 36 | import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
|
35 |
| -import gov.nist.secauto.metaschema.cli.CLI; |
36 | 37 |
|
37 | 38 | import org.junit.jupiter.api.Test;
|
38 | 39 | import org.junit.jupiter.params.ParameterizedTest;
|
39 | 40 | import org.junit.jupiter.params.provider.Arguments;
|
40 | 41 | import org.junit.jupiter.params.provider.MethodSource;
|
41 | 42 |
|
42 |
| -import java.nio.file.Path; |
43 |
| -import java.nio.file.Paths; |
44 | 43 | import java.util.ArrayList;
|
45 |
| -import java.util.Arrays; |
46 | 44 | import java.util.List;
|
47 |
| -import java.util.Map; |
48 | 45 | import java.util.stream.Stream;
|
49 | 46 |
|
50 | 47 | import edu.umd.cs.findbugs.annotations.NonNull;
|
| 48 | +import nl.altindag.log.LogCaptor; |
51 | 49 |
|
52 | 50 | /**
|
53 | 51 | * Unit test for simple CLI.
|
54 | 52 | */
|
55 | 53 | public class CLITest {
|
56 |
| - void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode) { |
57 |
| - status.generateMessage(true); |
58 |
| - assertAll(() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"), |
59 |
| - () -> assertNull(status.getThrowable(), "expected null Throwable")); |
60 |
| - } |
| 54 | + void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode) { |
| 55 | + status.generateMessage(true); |
| 56 | + assertAll(() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"), |
| 57 | + () -> assertNull(status.getThrowable(), "expected null Throwable")); |
| 58 | + } |
61 | 59 |
|
62 |
| - void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode, |
63 |
| - @NonNull Class<? extends Throwable> thrownClass) { |
64 |
| - status.generateMessage(true); |
65 |
| - Throwable thrown = status.getThrowable(); |
66 |
| - assert thrown != null; |
67 |
| - assertAll(() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"), |
68 |
| - () -> assertEquals(thrownClass, thrown.getClass(), "expected Throwable mismatch")); |
69 |
| - } |
| 60 | + void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode, |
| 61 | + @NonNull Class<? extends Throwable> thrownClass) { |
| 62 | + status.generateMessage(true); |
| 63 | + Throwable thrown = status.getThrowable(); |
| 64 | + assert thrown != null; |
| 65 | + assertAll(() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"), |
| 66 | + () -> assertEquals(thrownClass, thrown.getClass(), "expected Throwable mismatch")); |
| 67 | + } |
70 | 68 |
|
71 |
| - private static Stream<Arguments> providesValues() { |
72 |
| - ExitCode noExpectedExceptionClass = null; |
73 |
| - List<Arguments> values = new ArrayList<>(); |
74 |
| - values.add(Arguments.of(new String[] {}, ExitCode.INVALID_COMMAND, noExpectedExceptionClass)); |
75 |
| - values.add(Arguments.of(new String[] { "-h" }, ExitCode.OK, noExpectedExceptionClass)); |
76 |
| - values.add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.INVALID_COMMAND, |
77 |
| - noExpectedExceptionClass)); |
78 |
| - values.add(Arguments.of(new String[] { "validate", "--help" }, ExitCode.OK, noExpectedExceptionClass)); |
79 |
| - values.add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.INVALID_COMMAND, |
80 |
| - noExpectedExceptionClass)); |
81 |
| - values.add(Arguments.of( |
82 |
| - new String[] { "validate", |
83 |
| - "../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml" }, |
84 |
| - ExitCode.OK, noExpectedExceptionClass)); |
85 |
| - values.add(Arguments.of(new String[] { "generate-schema", "--overwrite", "--as", "JSON", |
86 |
| - "../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml", |
87 |
| - "target/schema-test.json" }, ExitCode.OK, noExpectedExceptionClass)); |
88 |
| - values.add(Arguments.of(new String[] { "query", "-m", "module.xml", "-i", "content-instance.xml", "\"2 + 2\""}, ExitCode.INVALID_COMMAND, noExpectedExceptionClass)); |
89 |
| - return values.stream(); |
90 |
| - } |
| 69 | + private static Stream<Arguments> providesValues() { |
| 70 | + ExitCode noExpectedExceptionClass = null; |
| 71 | + List<Arguments> values = new ArrayList<>(); |
| 72 | + values.add(Arguments.of(new String[] {}, ExitCode.INVALID_COMMAND, noExpectedExceptionClass)); |
| 73 | + values.add(Arguments.of(new String[] { "-h" }, ExitCode.OK, noExpectedExceptionClass)); |
| 74 | + values.add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.INVALID_COMMAND, |
| 75 | + noExpectedExceptionClass)); |
| 76 | + values.add(Arguments.of(new String[] { "validate", "--help" }, ExitCode.OK, noExpectedExceptionClass)); |
| 77 | + values.add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.INVALID_COMMAND, |
| 78 | + noExpectedExceptionClass)); |
| 79 | + values.add(Arguments.of( |
| 80 | + new String[] { "validate", |
| 81 | + "../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml" }, |
| 82 | + ExitCode.OK, noExpectedExceptionClass)); |
| 83 | + values.add(Arguments.of(new String[] { "generate-schema", "--overwrite", "--as", "JSON", |
| 84 | + "../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml", |
| 85 | + "target/schema-test.json" }, ExitCode.OK, noExpectedExceptionClass)); |
| 86 | + return values.stream(); |
| 87 | + } |
91 | 88 |
|
92 |
| - @ParameterizedTest |
93 |
| - @MethodSource("providesValues") |
94 |
| - void testAllCommands(@NonNull String[] args, @NonNull ExitCode expectedExitCode, |
95 |
| - Class<? extends Throwable> expectedThrownClass) { |
96 |
| - if (expectedThrownClass == null) { |
97 |
| - evaluateResult(CLI.runCli(args), expectedExitCode); |
98 |
| - } else { |
99 |
| - evaluateResult(CLI.runCli(args), expectedExitCode, expectedThrownClass); |
100 |
| - } |
101 |
| - } |
| 89 | + @ParameterizedTest |
| 90 | + @MethodSource("providesValues") |
| 91 | + void testAllCommands(@NonNull String[] args, @NonNull ExitCode expectedExitCode, |
| 92 | + Class<? extends Throwable> expectedThrownClass) { |
| 93 | + if (expectedThrownClass == null) { |
| 94 | + evaluateResult(CLI.runCli(args), expectedExitCode); |
| 95 | + } else { |
| 96 | + evaluateResult(CLI.runCli(args), expectedExitCode, expectedThrownClass); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void testQueryCommand() { |
| 102 | + LogCaptor logCaptor = LogCaptor.forClass(QueryCommand.class); |
| 103 | + String[] args |
| 104 | + = new String[] { "query", "-m", "../databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml", |
| 105 | + "-i", |
| 106 | + "../databind/src/test/resources/metaschema/fields_with_flags/example.json", "3 + 4 + 5" }; |
| 107 | + CLI.runCli(args); |
| 108 | + assertThat(logCaptor.getInfoLogs()).containsExactly("[12]"); |
| 109 | + }; |
102 | 110 | }
|
0 commit comments