diff --git a/metafacture-json/build.gradle b/metafacture-json/build.gradle index 14f574988..01a5b23a7 100644 --- a/metafacture-json/build.gradle +++ b/metafacture-json/build.gradle @@ -25,5 +25,6 @@ dependencies { implementation 'com.github.erosb:everit-json-schema:1.14.1' testImplementation 'junit:junit:4.12' testImplementation 'org.mockito:mockito-core:2.5.5' + testImplementation 'com.github.tomakehurst:wiremock-jre8:2.33.2' testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.21' } diff --git a/metafacture-json/src/test/java/org/metafacture/json/JsonValidatorTest.java b/metafacture-json/src/test/java/org/metafacture/json/JsonValidatorTest.java index aae7dffaf..df5f37dba 100644 --- a/metafacture-json/src/test/java/org/metafacture/json/JsonValidatorTest.java +++ b/metafacture-json/src/test/java/org/metafacture/json/JsonValidatorTest.java @@ -15,8 +15,21 @@ */ package org.metafacture.json; +import static org.hamcrest.CoreMatchers.both; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; + import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.metafacture.framework.MetafactureException; import org.metafacture.framework.ObjectReceiver; @@ -25,6 +38,11 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import com.github.tomakehurst.wiremock.client.WireMock; +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; +import com.github.tomakehurst.wiremock.junit.WireMockRule; + + /** * Tests for {@link JsonValidator}. * @@ -46,15 +64,32 @@ public final class JsonValidatorTest { private ObjectReceiver receiver; private InOrder inOrder; + @Rule + public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig() + .jettyAcceptors(Runtime.getRuntime().availableProcessors()).dynamicPort()); + @Before - public void setup() { + public void setup() throws IOException { MockitoAnnotations.initMocks(this); + WireMock.stubFor(WireMock.request("GET", WireMock.urlEqualTo("/schema")) + .willReturn(WireMock.ok().withBody(readToString(getClass().getResource(SCHEMA))))); validator = new JsonValidator(SCHEMA); validator.setSchemaRoot("/schemas/"); validator.setReceiver(receiver); inOrder = Mockito.inOrder(receiver); } + private String readToString(final URL url) throws IOException { + return new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) + .lines().collect(Collectors.joining("\n")); + } + + @Test + public void callWireMockSchema() throws MalformedURLException, IOException { + final String schemaContent = readToString(new URL(wireMockRule.baseUrl() + "/schema")); + assertThat(schemaContent, both(containsString("$schema")).and(containsString("$ref"))); + } + @Test public void testShouldValidate() { validator.process(JSON_VALID);