1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 {
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 {
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 {
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 {
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 {
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 {
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 }