1
1
package io .cucumber .jsonformatter ;
2
2
3
+ import com .networknt .schema .InputFormat ;
4
+ import com .networknt .schema .JsonSchema ;
5
+ import com .networknt .schema .JsonSchemaFactory ;
6
+ import com .networknt .schema .ValidationMessage ;
3
7
import io .cucumber .messages .NdjsonToMessageIterable ;
4
8
import io .cucumber .messages .types .Envelope ;
5
9
import org .json .JSONException ;
15
19
import java .nio .file .Files ;
16
20
import java .nio .file .Path ;
17
21
import java .nio .file .Paths ;
22
+ import java .util .ArrayList ;
18
23
import java .util .Comparator ;
19
24
import java .util .List ;
20
25
import java .util .Objects ;
26
+ import java .util .Set ;
21
27
import java .util .stream .Collectors ;
22
28
import java .util .stream .Stream ;
23
29
30
+ import static com .networknt .schema .SpecVersion .VersionFlag .*;
24
31
import static io .cucumber .jsonformatter .Jackson .OBJECT_MAPPER ;
25
32
import static io .cucumber .jsonformatter .Jackson .PRETTY_PRINTER ;
26
33
import static java .nio .charset .StandardCharsets .UTF_8 ;
34
+ import static org .assertj .core .api .Assertions .assertThat ;
27
35
28
36
29
37
class MessagesToJsonWriterAcceptanceTest {
30
38
private static final NdjsonToMessageIterable .Deserializer deserializer = (json ) -> OBJECT_MAPPER .readValue (json , Envelope .class );
31
39
private static final MessagesToJsonWriter .Serializer serializer = OBJECT_MAPPER .writer (PRETTY_PRINTER )::writeValue ;
32
40
33
- static List <TestCase > compatibilityKit () throws IOException {
34
- return TestCase .fromDirectory ("../testdata/compatibility-kit" );
35
- }
36
-
37
- static List <TestCase > dialectsCucumberJvm726Java () throws IOException {
38
- return TestCase .fromDirectory ("../testdata/cucumber-jvm/7.26.0-java/testdata" );
39
- }
40
-
41
-
42
- static List <TestCase > dialectsCucumberJvm726Java8 () throws IOException {
43
- return TestCase .fromDirectory ("../testdata/cucumber-jvm/7.26.0-java8/testdata" );
41
+ static List <TestCase > all () throws IOException {
42
+ List <TestCase > cases = new ArrayList <>();
43
+ cases .addAll (TestCase .fromDirectory ("../testdata/compatibility-kit" ));
44
+ cases .addAll (TestCase .fromDirectory ("../testdata/cucumber-jvm/7.26.0-java/testdata" ));
45
+ cases .addAll (TestCase .fromDirectory ("../testdata/cucumber-jvm/7.26.0-java8/testdata" ));
46
+ return cases ;
44
47
}
45
48
46
49
private static <T extends OutputStream > T writeJsonReport (TestCase testCase , T out ) throws IOException {
@@ -62,19 +65,28 @@ private static void assertJsonEquals(String expected, String actual) throws JSON
62
65
}
63
66
64
67
@ ParameterizedTest
65
- @ MethodSource ("compatibilityKit" )
66
- @ MethodSource ("dialectsCucumberJvm726Java" )
67
- @ MethodSource ("dialectsCucumberJvm726Java8" )
68
+ @ MethodSource ("all" )
68
69
void testCompatibilityKit (TestCase testCase ) throws IOException , JSONException {
69
70
ByteArrayOutputStream actual = writeJsonReport (testCase , new ByteArrayOutputStream ());
70
71
byte [] expected = Files .readAllBytes (testCase .expected );
71
72
assertJsonEquals (new String (expected , UTF_8 ), new String (actual .toByteArray (), UTF_8 ));
72
73
}
73
74
74
75
@ ParameterizedTest
75
- @ MethodSource ("compatibilityKit " )
76
+ @ MethodSource ("all " )
76
77
void validateAgainstJsonSchema (TestCase testCase ) throws IOException {
77
- // TODO:
78
+ JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory .getInstance (V202012 );
79
+
80
+ InputStream resourceAsStream = MessagesToJsonWriterAcceptanceTest .class .getResourceAsStream ("/cucumber-jvm.json" );
81
+ JsonSchema schema = jsonSchemaFactory .getSchema (resourceAsStream );
82
+ ByteArrayOutputStream actual = writeJsonReport (testCase , new ByteArrayOutputStream ());
83
+
84
+ Set <ValidationMessage > assertions = schema .validate (
85
+ new String (actual .toByteArray (), UTF_8 ),
86
+ InputFormat .JSON ,
87
+ // By default, since Draft 2019-09 the format keyword only generates annotations and not assertions
88
+ executionContext -> executionContext .getExecutionConfig ().setFormatAssertionsEnabled (true ));
89
+ assertThat (assertions ).isEmpty ();
78
90
}
79
91
80
92
@ ParameterizedTest
0 commit comments