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.metaschema.schemagen;
28  
29  import gov.nist.secauto.metaschema.core.configuration.DefaultConfiguration;
30  import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration;
31  import gov.nist.secauto.metaschema.core.model.IModule;
32  import gov.nist.secauto.metaschema.core.model.MetaschemaException;
33  import gov.nist.secauto.metaschema.core.model.validation.IContentValidator;
34  import gov.nist.secauto.metaschema.core.model.validation.JsonSchemaContentValidator;
35  import gov.nist.secauto.metaschema.core.model.xml.ModuleLoader;
36  import gov.nist.secauto.metaschema.databind.io.Format;
37  import gov.nist.secauto.metaschema.schemagen.json.JsonSchemaGenerator;
38  
39  import org.junit.jupiter.api.Disabled;
40  import org.junit.jupiter.api.DisplayName;
41  import org.junit.jupiter.api.DynamicNode;
42  import org.junit.jupiter.api.Test;
43  import org.junit.jupiter.api.TestFactory;
44  import org.junit.jupiter.api.parallel.Execution;
45  import org.junit.jupiter.api.parallel.ExecutionMode;
46  
47  import java.io.IOException;
48  import java.io.Writer;
49  import java.net.URL;
50  import java.nio.charset.StandardCharsets;
51  import java.nio.file.Files;
52  import java.nio.file.Path;
53  import java.util.function.BiFunction;
54  import java.util.function.Function;
55  import java.util.function.Supplier;
56  import java.util.stream.Stream;
57  
58  class JsonSuiteTest
59      extends AbstractSchemaGeneratorTestSuite {
60  
61    @Override
62    protected Supplier<IContentValidator> getSchemaValidatorSupplier() {
63      return () -> JSON_SCHEMA_VALIDATOR;
64    }
65  
66    @Override
67    protected Format getRequiredContentFormat() {
68      return Format.JSON;
69    }
70  
71    @Override
72    protected Function<Path, JsonSchemaContentValidator> getContentValidatorSupplier() {
73      return JSON_CONTENT_VALIDATOR_PROVIDER;
74    }
75  
76    @Override
77    protected BiFunction<IModule, Writer, Void> getGeneratorSupplier() {
78      return JSON_SCHEMA_PROVIDER;
79    }
80  
81    @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
82    @Execution(ExecutionMode.SAME_THREAD)
83    @DisplayName("JSON Schema Generation")
84    @TestFactory
85    Stream<DynamicNode> generateTests() {
86      return testFactory();
87    }
88  
89    @Disabled
90    @Test
91    void testDatatypeUuid() throws IOException, MetaschemaException { // NOPMD - testing delegated to doTest
92      doTest(
93          "datatypes/",
94          "datatypes-uuid_metaschema.xml",
95          "test-schema",
96          contentCase(Format.JSON, "datatypes-uuid_test_valid_PASS.json", true));
97    }
98  
99    @Disabled
100   @Test
101   void testChoice() throws IOException, MetaschemaException { // NOPMD - testing delegated to doTest
102     doTest(
103         "choice/",
104         "choice-multiple_metaschema.xml",
105         "test-choice-schema",
106         contentCase(Format.JSON, "choice-multiple_test_multiple_PASS.json", true));
107   }
108 
109   @Disabled
110   @Test
111   void testDatatypeCharStrings() throws IOException, MetaschemaException { // NOPMD - testing delegated to doTest
112     doTest(
113         "datatypes/",
114         "charstrings_metaschema.xml",
115         "datatypes-charstrings-schema",
116         contentCase(Format.JSON, "charstrings_test_okay_PASS.json", true),
117         contentCase(Format.XML, "charstrings_test_okay_PASS.xml", true));
118   }
119 
120   @Disabled
121   @Test
122   void testFlagBasic() throws IOException, MetaschemaException { // NOPMD - testing delegated to doTest
123     doTest(
124         "flag/",
125         "flag-basic_metaschema.xml",
126         "flag-basic-schema",
127         contentCase(Format.JSON, "flag-basic_test_datatype_FAIL.json", false),
128         contentCase(Format.JSON, "flag-basic_test_simple_PASS.json", true));
129   }
130 
131   @Disabled
132   @Test
133   void testOSCALComplete() throws IOException, MetaschemaException { // NOPMD - delegated to doTest
134     ModuleLoader loader = new ModuleLoader();
135     loader.allowEntityResolution();
136 
137     IModule module = loader.load(new URL(
138         "https://raw.githubusercontent.com/usnistgov/OSCAL/develop/src/metaschema/oscal_complete_metaschema.xml"));
139     ISchemaGenerator schemaGenerator = new JsonSchemaGenerator();
140     IMutableConfiguration<SchemaGenerationFeature<?>> features
141         = new DefaultConfiguration<>();
142     features.disableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
143     try (Writer writer = Files.newBufferedWriter(
144         Path.of("target/oscal-complete_schema.json"),
145         StandardCharsets.UTF_8,
146         getWriteOpenOptions())) {
147       assert writer != null;
148       schemaGenerator.generateFromModule(module, writer, features);
149     }
150   }
151 
152   @Disabled
153   @Test
154   void testTestMetaschema() throws IOException, MetaschemaException { // NOPMD - delegated to doTest
155     ModuleLoader loader = new ModuleLoader();
156     loader.allowEntityResolution();
157     IModule module = loader.load(new URL(
158         "https://raw.githubusercontent.com/usnistgov/metaschema/71233f4eb6854e820c7949144e86afa4d7981b22/test-suite/metaschema-xspec/json-schema-gen/json-value-testing-mini-metaschema.xml"));
159     ISchemaGenerator schemaGenerator = new JsonSchemaGenerator();
160     IMutableConfiguration<SchemaGenerationFeature<?>> features = new DefaultConfiguration<>();
161     features.disableFeature(SchemaGenerationFeature.INLINE_DEFINITIONS);
162     try (Writer writer = Files.newBufferedWriter(
163         Path.of("target/json-value-testing-mini_schema.json"),
164         StandardCharsets.UTF_8,
165         getWriteOpenOptions())) {
166       assert writer != null;
167       schemaGenerator.generateFromModule(module, writer, features);
168     }
169   }
170 
171 }