Skip to content

Commit 2a9cb32

Browse files
committed
Mock serving the local test schema via HTTP (#443)
1 parent 43989af commit 2a9cb32

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

metafacture-json/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ dependencies {
2525
implementation 'com.github.erosb:everit-json-schema:1.14.1'
2626
testImplementation 'junit:junit:4.12'
2727
testImplementation 'org.mockito:mockito-core:2.5.5'
28+
testImplementation 'com.github.tomakehurst:wiremock-jre8:2.33.2'
2829
testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.21'
2930
}

metafacture-json/src/test/java/org/metafacture/json/JsonValidatorTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,21 @@
1515
*/
1616
package org.metafacture.json;
1717

18+
import static org.hamcrest.CoreMatchers.both;
19+
import static org.hamcrest.CoreMatchers.containsString;
20+
import static org.junit.Assert.assertThat;
21+
22+
import java.io.BufferedReader;
23+
import java.io.IOException;
24+
import java.io.InputStreamReader;
25+
import java.net.MalformedURLException;
26+
import java.net.URL;
27+
import java.nio.charset.StandardCharsets;
28+
import java.util.stream.Collectors;
29+
1830
import org.junit.After;
1931
import org.junit.Before;
32+
import org.junit.Rule;
2033
import org.junit.Test;
2134
import org.metafacture.framework.MetafactureException;
2235
import org.metafacture.framework.ObjectReceiver;
@@ -25,6 +38,11 @@
2538
import org.mockito.Mockito;
2639
import org.mockito.MockitoAnnotations;
2740

41+
import com.github.tomakehurst.wiremock.client.WireMock;
42+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
43+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
44+
45+
2846
/**
2947
* Tests for {@link JsonValidator}.
3048
*
@@ -46,15 +64,32 @@ public final class JsonValidatorTest {
4664
private ObjectReceiver<String> receiver;
4765
private InOrder inOrder;
4866

67+
@Rule
68+
public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig()
69+
.jettyAcceptors(Runtime.getRuntime().availableProcessors()).dynamicPort());
70+
4971
@Before
50-
public void setup() {
72+
public void setup() throws IOException {
5173
MockitoAnnotations.initMocks(this);
74+
WireMock.stubFor(WireMock.request("GET", WireMock.urlEqualTo("/schema"))
75+
.willReturn(WireMock.ok().withBody(readToString(getClass().getResource(SCHEMA)))));
5276
validator = new JsonValidator(SCHEMA);
5377
validator.setSchemaRoot("/schemas/");
5478
validator.setReceiver(receiver);
5579
inOrder = Mockito.inOrder(receiver);
5680
}
5781

82+
private String readToString(final URL url) throws IOException {
83+
return new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))
84+
.lines().collect(Collectors.joining("\n"));
85+
}
86+
87+
@Test
88+
public void callWireMockSchema() throws MalformedURLException, IOException {
89+
final String schemaContent = readToString(new URL(wireMockRule.baseUrl() + "/schema"));
90+
assertThat(schemaContent, both(containsString("$schema")).and(containsString("$ref")));
91+
}
92+
5893
@Test
5994
public void testShouldValidate() {
6095
validator.process(JSON_VALID);

0 commit comments

Comments
 (0)