View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.oscal.tools.cli.core;
28  
29  import static org.junit.jupiter.api.Assertions.assertAll;
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertNull;
32  
33  import gov.nist.secauto.metaschema.binding.io.Format;
34  import gov.nist.secauto.metaschema.cli.processor.ExitCode;
35  import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
36  import gov.nist.secauto.oscal.lib.profile.resolver.ProfileResolutionException;
37  
38  import org.junit.jupiter.params.ParameterizedTest;
39  import org.junit.jupiter.params.provider.Arguments;
40  import org.junit.jupiter.params.provider.MethodSource;
41  
42  import java.nio.file.Path;
43  import java.nio.file.Paths;
44  import java.util.ArrayList;
45  import java.util.Arrays;
46  import java.util.List;
47  import java.util.Map;
48  import java.util.stream.Stream;
49  
50  import edu.umd.cs.findbugs.annotations.NonNull;
51  
52  public class CLITest {
53    void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode) {
54      status.generateMessage(true);
55      assertAll(
56          () -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"),
57          () -> assertNull(status.getThrowable(), "expected null Throwable"));
58    }
59  
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(
66          () -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"),
67          () -> assertEquals(thrownClass, thrown.getClass(), "expected Throwable mismatch"));
68    }
69  
70    private static Stream<Arguments> providesValues() {
71      final String[] commands = { "ap", "ar", "catalog", "component-definition", "profile", "poam", "ssp" };
72      final Map<Format, List<Format>> formatEntries = Map.of(
73          Format.XML, Arrays.asList(Format.JSON, Format.YAML),
74          Format.JSON, Arrays.asList(Format.XML, Format.JSON),
75          Format.YAML, Arrays.asList(Format.XML, Format.JSON));
76      List<Arguments> values = new ArrayList<>();
77  
78      values.add(Arguments.of(new String[] { "--version" }, ExitCode.OK, null));
79      // TODO: Test all data formats once usnistgov/oscal-cli#216 fix merged.
80      Path path = Paths.get("src/test/resources/cli/example_profile_invalid" + Format.XML.getDefaultExtension());
81      values.add(
82          Arguments.of(new String[] { "profile", "resolve", "--to=" + Format.XML.name().toLowerCase(), path.toString() },
83              ExitCode.PROCESSING_ERROR, ProfileResolutionException.class));
84  
85      for (String cmd : commands) {
86        values.add(Arguments.of(new String[] { cmd, "validate", "-h" }, ExitCode.OK, null));
87        // TODO: Update when usnistgov/oscal-cli#210 fix merged.
88        values.add(Arguments.of(new String[] { cmd, "convert", "-h" }, ExitCode.INVALID_COMMAND, null));
89  
90        for (Format format : Format.values()) {
91          path = Paths.get("src/test/resources/cli/example_" + cmd + "_invalid" + format.getDefaultExtension());
92          values.add(Arguments.of(new String[] { cmd, "validate", path.toString() }, ExitCode.FAIL, null));
93          path = Paths.get("src/test/resources/cli/example_" + cmd + "_valid" + format.getDefaultExtension());
94          values.add(Arguments.of(new String[] { cmd, "validate", path.toString() }, ExitCode.OK, null));
95          path = Paths.get("src/test/resources/cli/example_profile_valid" + format.getDefaultExtension());
96          List<Format> targetFormats = formatEntries.get(format);
97          for (Format targetFormat : targetFormats) {
98            path = Paths.get("src/test/resources/cli/example_" + cmd + "_valid" + format.getDefaultExtension());
99            String outputPath = path.toString().replace(format.getDefaultExtension(),
100               "_converted" + targetFormat.getDefaultExtension());
101           values.add(Arguments.of(new String[] { cmd, "convert", "--to=" + targetFormat.name().toLowerCase(),
102               path.toString(), outputPath, "--overwrite" }, ExitCode.OK, null));
103           // TODO: Update when usnistgov/oscal#217 fix merged.
104           path = Paths.get("src/test/resources/cli/example_" + cmd + "_invalid" + format.getDefaultExtension());
105           outputPath = path.toString().replace(format.getDefaultExtension(),
106               "_converted" + targetFormat.getDefaultExtension());
107           values.add(Arguments.of(new String[] { cmd, "convert", "--to=" + targetFormat.name().toLowerCase(),
108               path.toString(), outputPath, "--overwrite" }, ExitCode.OK, null));
109         }
110         if (cmd == "profile") {
111           path = Paths.get("src/test/resources/cli/example_profile_valid" + format.getDefaultExtension());
112           values
113               .add(Arguments.of(new String[] { cmd, "resolve", "--to=" + format.name().toLowerCase(), path.toString() },
114                   ExitCode.OK, null));
115         }
116       }
117     }
118 
119     return values.stream();
120   }
121 
122   @ParameterizedTest
123   @MethodSource("providesValues")
124   void testAllSubCommands(@NonNull String[] args, @NonNull ExitCode expectedExitCode,
125       Class<? extends Throwable> expectedThrownClass) {
126     if (expectedThrownClass == null) {
127       evaluateResult(CLI.runCli(args), expectedExitCode);
128     } else {
129       evaluateResult(CLI.runCli(args), expectedExitCode, expectedThrownClass);
130     }
131   }
132 }