Skip to content

Commit

Permalink
Use only paths, not URLs
Browse files Browse the repository at this point in the history
Adjust method to load files PR per @david-waltermire-nist's rec.
  • Loading branch information
aj-stein-nist committed Oct 26, 2023
1 parent 35de995 commit 2952c9c
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 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 @@ -30,25 +30,20 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import gov.nist.secauto.metaschema.binding.io.Format;
import gov.nist.secauto.metaschema.cli.processor.ExitCode;
import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
import gov.nist.secauto.oscal.lib.profile.resolver.ProfileResolutionException;

import org.junit.jupiter.api.Test;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import edu.umd.cs.findbugs.annotations.NonNull;

public class CLITest {
Expand Down Expand Up @@ -81,6 +76,8 @@ void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode,
() -> assertEquals(thrownClass, thrown.getClass(), "expected Throwable mismatch"));
}



@Test
void testValidateSubcommandsHelp() {
for (String cmd : MODEL_COMMANDS) {
Expand All @@ -99,44 +96,41 @@ void testConvertSubcommandsHelp() {
}

@Test
void testValidateSubCommandInvalidFile() throws IOException, URISyntaxException {
void testValidateSubCommandInvalidFile() {
for (String cmd : MODEL_COMMANDS) {
for (Format format : Format.values()) {
URL url = getClass().getResource("/cli/example_" + cmd + "_invalid" + format.getDefaultExtension());
String path = Path.of(url.toURI()).toString();
String[] args = { cmd, "validate", path };
Path path = Paths.get("src/test/resources/cli/example_" + cmd + "_invalid" + format.getDefaultExtension());
String[] args = { cmd, "validate", path.toString() };
ExitStatus result = CLI.runCli(args);
evaluateResult(result, ExitCode.FAIL);
}
}
}

@Test
void testValidateSubCommandValidFile() throws IOException, URISyntaxException {
void testValidateSubCommandValidFile() {
for (String cmd : MODEL_COMMANDS) {
for (Format format : Format.values()) {
URL url = getClass().getResource("/cli/example_" + cmd + "_valid" + format.getDefaultExtension());
String path = Path.of(url.toURI()).toString();
String[] args = { cmd, "validate", path };
Path path = Paths.get("src/test/resources/cli/example_" + cmd + "_valid" + format.getDefaultExtension());
String[] args = { cmd, "validate", path.toString() };
ExitStatus result = CLI.runCli(args);
evaluateResult(result, ExitCode.OK);
}
}
}

@Test
void testConvertSubCommandValidFile() throws IOException, URISyntaxException {
void testConvertSubCommandValidFile() {
for (String cmd : MODEL_COMMANDS) {
for (Entry<Format, List<Format>> entry : FORMAT_ENTRIES.entrySet()) {
Format sourceFormat = entry.getKey();
List<Format> targetFormats = entry.getValue();
for (Format targetFormat : targetFormats) {
URL url = getClass().getResource("/cli/example_" + cmd + "_valid" + sourceFormat.getDefaultExtension());
String path = Path.of(url.toURI()).toString();
Path path = Paths.get("src/test/resources/cli/example_" + cmd + "_valid" + sourceFormat.getDefaultExtension());
String outputPath
= path.replace(sourceFormat.getDefaultExtension(), "_converted" + targetFormat.getDefaultExtension());
= path.toString().replace(sourceFormat.getDefaultExtension(), "_converted" + targetFormat.getDefaultExtension());
String[] args
= { cmd, "convert", "--to=" + targetFormat.name().toLowerCase(), path, outputPath, "--overwrite" };
= { cmd, "convert", "--to=" + targetFormat.name().toLowerCase(), path.toString(), outputPath, "--overwrite" };
ExitStatus result = CLI.runCli(args);
evaluateResult(result, ExitCode.OK);
}
Expand All @@ -145,22 +139,20 @@ void testConvertSubCommandValidFile() throws IOException, URISyntaxException {
}

@Test
void testResolveSubCommandValidFile() throws IOException, URISyntaxException {
void testResolveSubCommandValidFile() {
for (Format format : Format.values()) {
URL url = getClass().getResource("/cli/example_profile_valid" + format.getDefaultExtension());
String path = Path.of(url.toURI()).toString();
String[] args = { "profile", "resolve", "--to=" + format.name().toLowerCase(), path };
Path path = Paths.get("src/test/resources/cli/example_profile_valid" + format.getDefaultExtension());
String[] args = { "profile", "resolve", "--to=" + format.name().toLowerCase(), path.toString() };
ExitStatus result = CLI.runCli(args);
evaluateResult(result, ExitCode.OK);
}
}

@Test
void testResolveSubCommandInvalidFile() throws IOException, URISyntaxException {
void testResolveSubCommandInvalidFile() {
// TODO: Test all data formats once usnistgov/oscal-cli#216 fix merged.
URL url = getClass().getResource("/cli/example_profile_invalid" + Format.XML.getDefaultExtension());
String path = Path.of(url.toURI()).toString();
String[] args = { "profile", "resolve", "--to=" + Format.XML.name().toLowerCase(), path };
Path path = Paths.get("src/test/resources/cli/example_profile_invalid" + Format.XML.getDefaultExtension());
String[] args = { "profile", "resolve", "--to=" + Format.XML.name().toLowerCase(), path.toString() };
ExitStatus result = CLI.runCli(args);
evaluateResult(result, ExitCode.PROCESSING_ERROR, ProfileResolutionException.class);
}
Expand Down

0 comments on commit 2952c9c

Please sign in to comment.