16
16
17
17
package com .networknt .schema ;
18
18
19
- import com .fasterxml .jackson .core .type .TypeReference ;
20
19
import com .fasterxml .jackson .databind .ObjectMapper ;
21
- import com .fasterxml .jackson .databind .exc .MismatchedInputException ;
22
20
import com .networknt .schema .SpecVersion .VersionFlag ;
21
+ import com .networknt .schema .suite .TestCase ;
22
+ import com .networknt .schema .suite .TestSource ;
23
+ import com .networknt .schema .suite .TestSpec ;
23
24
import com .networknt .schema .uri .URITranslator ;
25
+
24
26
import org .junit .jupiter .api .AssertionFailureBuilder ;
25
27
import org .junit .jupiter .api .DynamicNode ;
26
28
import org .opentest4j .AssertionFailedError ;
27
29
28
- import java .io .FileInputStream ;
29
30
import java .io .IOException ;
30
- import java .io .InputStream ;
31
31
import java .io .UncheckedIOException ;
32
32
import java .net .URI ;
33
33
import java .nio .file .Files ;
45
45
import java .util .stream .StreamSupport ;
46
46
47
47
import static com .networknt .schema .SpecVersionDetector .detectOptionalVersion ;
48
+ import static org .junit .jupiter .api .Assumptions .abort ;
48
49
import static org .junit .jupiter .api .DynamicContainer .dynamicContainer ;
49
50
import static org .junit .jupiter .api .DynamicTest .dynamicTest ;
50
51
51
52
public abstract class AbstractJsonSchemaTestSuite extends HTTPServiceSupport {
52
- protected static final TypeReference <List <TestCase >> testCaseType = new TypeReference <List <TestCase >>() { /* intentionally empty */ };
53
53
protected static final Map <String , VersionFlag > supportedVersions = new HashMap <>();
54
54
static {
55
55
supportedVersions .put ("draft2019-09" , VersionFlag .V201909 );
@@ -72,24 +72,27 @@ protected boolean enabled(@SuppressWarnings("unused") Path path) {
72
72
return true ;
73
73
}
74
74
75
- protected boolean enabled ( TestCase testCase ) {
76
- return ! testCase . isDisabled ();
75
+ protected Optional < String > reason ( @ SuppressWarnings ( "unused" ) Path path ) {
76
+ return Optional . empty ();
77
77
}
78
78
79
- protected boolean enabled (TestSpec testSpec ) {
80
- return !testSpec .isDisabled ();
79
+ private Stream <DynamicNode > buildContainers (VersionFlag defaultVersion , Path path ) {
80
+ boolean disabled = !enabled (path );
81
+ String reason = reason (path ).orElse ("Unknown" );
82
+ return TestSource .loadFrom (path , disabled , reason )
83
+ .map (testSource -> buildContainer (defaultVersion , testSource ))
84
+ .orElse (Stream .empty ());
81
85
}
82
86
83
- private Stream <DynamicNode > buildContainers (VersionFlag defaultVersion , Path path ) {
84
- return loadTestCases (path )
85
- .map (testCase -> buildContainer (defaultVersion , testCase ));
87
+ private Stream <DynamicNode > buildContainer (VersionFlag defaultVersion , TestSource testSource ) {
88
+ return testSource .getTestCases ().stream ().map (testCase -> buildContainer (defaultVersion , testCase ));
86
89
}
87
90
88
91
private DynamicNode buildContainer (VersionFlag defaultVersion , TestCase testCase ) {
89
92
try {
90
93
JsonSchemaFactory validatorFactory = buildValidatorFactory (defaultVersion , testCase );
91
94
92
- return dynamicContainer (testCase .getDisplayName (), testCase .getTests ().stream ().filter ( this :: enabled ). map (testSpec -> {
95
+ return dynamicContainer (testCase .getDisplayName (), testCase .getTests ().stream ().map (testSpec -> {
93
96
return buildTest (validatorFactory , testSpec );
94
97
}));
95
98
} catch (JsonSchemaException e ) {
@@ -102,6 +105,8 @@ private DynamicNode buildContainer(VersionFlag defaultVersion, TestCase testCase
102
105
}
103
106
104
107
private JsonSchemaFactory buildValidatorFactory (VersionFlag defaultVersion , TestCase testCase ) {
108
+ if (testCase .isDisabled ()) return null ;
109
+
105
110
VersionFlag specVersion = detectVersion (testCase , defaultVersion );
106
111
JsonSchemaFactory base = JsonSchemaFactory .getInstance (specVersion );
107
112
return JsonSchemaFactory
@@ -115,6 +120,10 @@ private JsonSchemaFactory buildValidatorFactory(VersionFlag defaultVersion, Test
115
120
}
116
121
117
122
private DynamicNode buildTest (JsonSchemaFactory validatorFactory , TestSpec testSpec ) {
123
+ if (testSpec .isDisabled ()) {
124
+ return dynamicTest (testSpec .getDescription (), () -> abortAndReset (testSpec .getReason ()));
125
+ }
126
+
118
127
// Configure the schemaValidator to set typeLoose's value based on the test file,
119
128
// if test file do not contains typeLoose flag, use default value: false.
120
129
@ SuppressWarnings ("deprecation" ) boolean typeLoose = testSpec .isTypeLoose ();
@@ -161,6 +170,14 @@ private static Optional<VersionFlag> detectVersionFromPath(Path path) {
161
170
.findAny ();
162
171
}
163
172
173
+ private void abortAndReset (String reason ) {
174
+ try {
175
+ abort (reason );
176
+ } finally {
177
+ cleanup ();
178
+ }
179
+ }
180
+
164
181
private void executeAndReset (JsonSchema schema , TestSpec testSpec ) {
165
182
try {
166
183
executeTest (schema , testSpec );
@@ -170,6 +187,7 @@ private void executeAndReset(JsonSchema schema, TestSpec testSpec) {
170
187
}
171
188
172
189
private static void executeTest (JsonSchema schema , TestSpec testSpec ) {
190
+
173
191
Set <ValidationMessage > errors = schema .validate (testSpec .getData ());
174
192
175
193
if (testSpec .isValid ()) {
@@ -237,28 +255,13 @@ private static void executeTest(JsonSchema schema, TestSpec testSpec) {
237
255
private List <Path > findTestCases (String basePath ) {
238
256
try (Stream <Path > paths = Files .walk (Paths .get (basePath ))) {
239
257
return paths
240
- .filter (this ::enabled )
241
258
.filter (path -> path .toString ().endsWith (".json" ))
242
259
.collect (Collectors .toList ());
243
260
} catch (IOException e ) {
244
261
throw new UncheckedIOException (e );
245
262
}
246
263
}
247
264
248
- private Stream <TestCase > loadTestCases (Path testCaseFile ) {
249
- try (InputStream in = new FileInputStream (testCaseFile .toFile ())) {
250
- return this .mapper .readValue (in , testCaseType )
251
- .stream ()
252
- .peek (testCase -> testCase .setSpecification (testCaseFile ))
253
- .filter (this ::enabled );
254
- } catch (MismatchedInputException e ) {
255
- System .err .append ("Not a valid test case: " ).println (testCaseFile );
256
- return Stream .empty ();
257
- } catch (IOException e ) {
258
- throw new UncheckedIOException (e );
259
- }
260
- }
261
-
262
265
private static Iterable <? extends DynamicNode > unsupportedMetaSchema (TestCase testCase ) {
263
266
return Collections .singleton (
264
267
dynamicTest ("Detected an unsupported schema" , () -> {
0 commit comments