Skip to content

Commit

Permalink
Mock serving the local test schema via HTTP (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Feb 1, 2023
1 parent 43989af commit 2a9cb32
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions metafacture-json/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}.
*
Expand All @@ -46,15 +64,32 @@ public final class JsonValidatorTest {
private ObjectReceiver<String> 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);
Expand Down

0 comments on commit 2a9cb32

Please sign in to comment.