11/*
2- * Copyright 2021, 2022 Fabian Steeg, hbz
2+ * Copyright 2021, 2023 Fabian Steeg, hbz
33 *
44 * Licensed under the Apache License, Version 2.0 the "License";
55 * you may not use this file except in compliance with the License.
1818import static org .hamcrest .CoreMatchers .both ;
1919import static org .hamcrest .CoreMatchers .containsString ;
2020import 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 ;
2123
2224import java .io .BufferedReader ;
2325import java .io .IOException ;
2426import java .io .InputStreamReader ;
2527import java .net .MalformedURLException ;
2628import java .net .URL ;
2729import java .nio .charset .StandardCharsets ;
30+ import java .util .Arrays ;
31+ import java .util .Collection ;
32+ import java .util .function .Function ;
2833import java .util .stream .Collectors ;
2934
3035import org .junit .After ;
3136import org .junit .Before ;
3237import org .junit .Rule ;
3338import org .junit .Test ;
39+ import org .junit .runner .RunWith ;
40+ import org .junit .runners .Parameterized ;
3441import org .metafacture .framework .MetafactureException ;
3542import org .metafacture .framework .ObjectReceiver ;
3643import org .mockito .InOrder ;
4956 * @author Fabian Steeg
5057 *
5158 */
59+ @ RunWith (Parameterized .class )
5260public final class JsonValidatorTest {
5361
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" ;
5564 private static final String JSON_VALID = "{\" id\" :\" http://example.org/\" }" ;
5665 private static final String JSON_INVALID_MISSING_REQUIRED = "{}" ;
5766 private static final String JSON_INVALID_URI_FORMAT = "{\" id\" :\" example.org/\" }" ;
@@ -63,30 +72,51 @@ public final class JsonValidatorTest {
6372 @ Mock
6473 private ObjectReceiver <String > receiver ;
6574 private InOrder inOrder ;
75+ private Function <Object , String > schemaLocationGetter ;
6676
6777 @ Rule
6878 public WireMockRule wireMockRule = new WireMockRule (WireMockConfiguration .wireMockConfig ()
6979 .jettyAcceptors (Runtime .getRuntime ().availableProcessors ()).dynamicPort ());
7080
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+
7194 @ Before
7295 public void setup () throws IOException {
7396 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 );
78100 validator .setReceiver (receiver );
79101 inOrder = Mockito .inOrder (receiver );
80102 }
81103
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+
82112 private String readToString (final URL url ) throws IOException {
83113 return new BufferedReader (new InputStreamReader (url .openStream (), StandardCharsets .UTF_8 ))
84114 .lines ().collect (Collectors .joining ("\n " ));
85115 }
86116
87117 @ Test
88118 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 ));
90120 assertThat (schemaContent , both (containsString ("$schema" )).and (containsString ("$ref" )));
91121 }
92122
0 commit comments