1
1
/*
2
- * Copyright 2021, 2022 Fabian Steeg, hbz
2
+ * Copyright 2021, 2023 Fabian Steeg, hbz
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 the "License";
5
5
* you may not use this file except in compliance with the License.
18
18
import static org .hamcrest .CoreMatchers .both ;
19
19
import static org .hamcrest .CoreMatchers .containsString ;
20
20
import static org .junit .Assert .assertThat ;
21
+ import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
22
+ import static com .github .tomakehurst .wiremock .client .WireMock .request ;
21
23
22
24
import java .io .BufferedReader ;
23
25
import java .io .IOException ;
24
26
import java .io .InputStreamReader ;
25
27
import java .net .MalformedURLException ;
26
28
import java .net .URL ;
27
29
import java .nio .charset .StandardCharsets ;
30
+ import java .util .Arrays ;
31
+ import java .util .Collection ;
32
+ import java .util .function .Function ;
28
33
import java .util .stream .Collectors ;
29
34
30
35
import org .junit .After ;
31
36
import org .junit .Before ;
32
37
import org .junit .Rule ;
33
38
import org .junit .Test ;
39
+ import org .junit .runner .RunWith ;
40
+ import org .junit .runners .Parameterized ;
34
41
import org .metafacture .framework .MetafactureException ;
35
42
import org .metafacture .framework .ObjectReceiver ;
36
43
import org .mockito .InOrder ;
49
56
* @author Fabian Steeg
50
57
*
51
58
*/
59
+ @ RunWith (Parameterized .class )
52
60
public final class JsonValidatorTest {
53
61
54
- private static final String SCHEMA = "/schemas/schema.json" ;
62
+ private static final String MAIN_SCHEMA = "/schemas/schema.json" ;
63
+ private static final String ID_SCHEMA = "/schemas/id.json" ;
55
64
private static final String JSON_VALID = "{\" id\" :\" http://example.org/\" }" ;
56
65
private static final String JSON_INVALID_MISSING_REQUIRED = "{}" ;
57
66
private static final String JSON_INVALID_URI_FORMAT = "{\" id\" :\" example.org/\" }" ;
@@ -63,30 +72,51 @@ public final class JsonValidatorTest {
63
72
@ Mock
64
73
private ObjectReceiver <String > receiver ;
65
74
private InOrder inOrder ;
75
+ private Function <Object , String > schemaLocationGetter ;
66
76
67
77
@ Rule
68
78
public WireMockRule wireMockRule = new WireMockRule (WireMockConfiguration .wireMockConfig ()
69
79
.jettyAcceptors (Runtime .getRuntime ().availableProcessors ()).dynamicPort ());
70
80
81
+ @ Parameterized .Parameters (name = "{index}" )
82
+ public static Collection <Object []> siteMaps () {
83
+ return Arrays .asList ((Object [][]) (new Function [][] { //
84
+ // Pass the schema to each test as path on classpath, file url, and http url:
85
+ { (Object rule ) -> MAIN_SCHEMA },
86
+ { (Object rule ) -> JsonValidatorTest .class .getResource (MAIN_SCHEMA ).toString () },
87
+ { (Object rule ) -> ((WireMockRule ) rule ).baseUrl () + MAIN_SCHEMA } }));
88
+ }
89
+
90
+ public JsonValidatorTest (Function <Object , String > schemaLocationGetter ) {
91
+ this .schemaLocationGetter = schemaLocationGetter ;
92
+ }
93
+
71
94
@ Before
72
95
public void setup () throws IOException {
73
96
MockitoAnnotations .initMocks (this );
74
- WireMock .stubFor (WireMock .request ("GET" , WireMock .urlEqualTo ("/schema" ))
75
- .willReturn (WireMock .ok ().withBody (readToString (getClass ().getResource (SCHEMA )))));
76
- validator = new JsonValidator (SCHEMA );
77
- validator .setSchemaRoot ("/schemas/" );
97
+ wireMock (MAIN_SCHEMA , ID_SCHEMA );
98
+ String schemaLocation = schemaLocationGetter .apply (wireMockRule );
99
+ validator = new JsonValidator (schemaLocation );
78
100
validator .setReceiver (receiver );
79
101
inOrder = Mockito .inOrder (receiver );
80
102
}
81
103
104
+ private void wireMock (final String ... schemaLocations ) throws IOException {
105
+ for (String schemaLocation : schemaLocations ) {
106
+ stubFor (request ("GET" , WireMock .urlEqualTo (schemaLocation )).willReturn (
107
+ WireMock .ok ().withBody (readToString (getClass ().getResource (schemaLocation )))
108
+ .withHeader ("Content-type" , "application/json" )));
109
+ }
110
+ }
111
+
82
112
private String readToString (final URL url ) throws IOException {
83
113
return new BufferedReader (new InputStreamReader (url .openStream (), StandardCharsets .UTF_8 ))
84
114
.lines ().collect (Collectors .joining ("\n " ));
85
115
}
86
116
87
117
@ Test
88
118
public void callWireMockSchema () throws MalformedURLException , IOException {
89
- final String schemaContent = readToString (new URL (wireMockRule .baseUrl () + "/schema" ));
119
+ final String schemaContent = readToString (new URL (wireMockRule .baseUrl () + MAIN_SCHEMA ));
90
120
assertThat (schemaContent , both (containsString ("$schema" )).and (containsString ("$ref" )));
91
121
}
92
122
0 commit comments