Skip to content

Commit 474ea36

Browse files
committed
Add QueryCommand back to command handler
1 parent cd19acc commit 474ea36

File tree

3 files changed

+72
-48
lines changed

3 files changed

+72
-48
lines changed

metaschema-cli/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@
4545
<groupId>com.github.spotbugs</groupId>
4646
<artifactId>spotbugs-annotations</artifactId>
4747
</dependency>
48+
49+
<dependency>
50+
<groupId>io.github.hakky54</groupId>
51+
<artifactId>logcaptor</artifactId>
52+
<version>2.9.2</version>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.assertj</groupId>
58+
<artifactId>assertj-core</artifactId>
59+
<version>3.24.2</version>
60+
<scope>test</scope>
61+
</dependency>
4862
</dependencies>
4963

5064
<build>

metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/CLI.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package gov.nist.secauto.metaschema.cli;
2828

2929
import gov.nist.secauto.metaschema.cli.commands.GenerateSchemaCommand;
30+
import gov.nist.secauto.metaschema.cli.commands.QueryCommand;
3031
import gov.nist.secauto.metaschema.cli.commands.ValidateContentUsingModuleCommand;
3132
import gov.nist.secauto.metaschema.cli.commands.ValidateModuleCommand;
3233
import gov.nist.secauto.metaschema.cli.processor.CLIProcessor;
@@ -59,6 +60,7 @@ public static ExitStatus runCli(String... args) {
5960
processor.addCommandHandler(new ValidateModuleCommand());
6061
processor.addCommandHandler(new GenerateSchemaCommand());
6162
processor.addCommandHandler(new ValidateContentUsingModuleCommand());
63+
processor.addCommandHandler(new QueryCommand());
6264

6365
CommandService.getInstance().getCommands().stream().forEach(command -> {
6466
assert command != null;

metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java

+56-48
Original file line numberDiff line numberDiff line change
@@ -26,77 +26,85 @@
2626

2727
package gov.nist.secauto.metaschema.cli;
2828

29+
import static org.assertj.core.api.Assertions.assertThat;
2930
import static org.junit.jupiter.api.Assertions.assertAll;
3031
import static org.junit.jupiter.api.Assertions.assertEquals;
3132
import static org.junit.jupiter.api.Assertions.assertNull;
3233

34+
import gov.nist.secauto.metaschema.cli.commands.QueryCommand;
3335
import gov.nist.secauto.metaschema.cli.processor.ExitCode;
3436
import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
35-
import gov.nist.secauto.metaschema.cli.CLI;
3637

3738
import org.junit.jupiter.api.Test;
3839
import org.junit.jupiter.params.ParameterizedTest;
3940
import org.junit.jupiter.params.provider.Arguments;
4041
import org.junit.jupiter.params.provider.MethodSource;
4142

42-
import java.nio.file.Path;
43-
import java.nio.file.Paths;
4443
import java.util.ArrayList;
45-
import java.util.Arrays;
4644
import java.util.List;
47-
import java.util.Map;
4845
import java.util.stream.Stream;
4946

5047
import edu.umd.cs.findbugs.annotations.NonNull;
48+
import nl.altindag.log.LogCaptor;
5149

5250
/**
5351
* Unit test for simple CLI.
5452
*/
5553
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+
}
6159

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+
}
7068

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+
}
9188

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+
};
102110
}

0 commit comments

Comments
 (0)