Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: POC for WireMock to MockServer migration #4429

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
<dependencies>
<!-- shared test dependencies -->
<!-- Testing: JUnit -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions core/repository/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-jupiter-no-dependencies</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
*******************************************************************************/
package org.eclipse.rdf4j.repository.util;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.permanentRedirect;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.eclipse.rdf4j.model.util.Statements.statement;
import static org.eclipse.rdf4j.model.util.Values.getValueFactory;
import static org.eclipse.rdf4j.model.util.Values.iri;
Expand All @@ -24,6 +20,8 @@
import static org.hamcrest.Matchers.hasProperty;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import java.net.ProtocolException;
import java.net.URL;
Expand All @@ -45,18 +43,68 @@
import org.eclipse.rdf4j.rio.ParserConfig;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandler;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockserver.client.MockServerClient;
import org.mockserver.junit.jupiter.MockServerExtension;
import org.mockserver.model.MediaType;

/**
* Unit tests for {@link RDFLoader}.
*
* @author Manuel Fiorelli
*/
@WireMockTest
@ExtendWith(MockServerExtension.class)
public class RDFLoaderTest {
@BeforeAll
static void defineMockServerBehavior(MockServerClient client) {
client.when(
request()
.withMethod("GET")
.withPath("/Socrates.ttl")
)
.respond(
response()
.withContentType(MediaType.parse(RDFFormat.TURTLE.getDefaultMIMEType()))
.withBody("<http://example.org/Socrates> a <http://xmlns.com/foaf/0.1/Person> .")

);
client.when(
request()
.withMethod("GET")
.withPath("/Socrates")
)
.respond(
response()
.withStatusCode(301)
.withHeader("Location", "/Socrates.ttl")

);
client.when(
request()
.withMethod("GET")
.withPath("/Socrates1")
)
.respond(
response()
.withStatusCode(301)
.withHeader("Location", "/Socrates2")

);
client.when(
request()
.withMethod("GET")
.withPath("/Socrates2")
)
.respond(
response()
.withStatusCode(301)
.withHeader("Location", "/Socrates.ttl")

);
}

@Test
public void testTurtleJavaResource() throws Exception {
RDFLoader rdfLoader = new RDFLoader(new ParserConfig(), getValueFactory());
Expand All @@ -74,18 +122,12 @@ public void testTurtleJavaResource() throws Exception {
}

@Test
public void testTurtleDocument(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
stubFor(get("/Socrates.ttl")
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", RDFFormat.TURTLE.getDefaultMIMEType())
.withBody("<http://example.org/Socrates> a <http://xmlns.com/foaf/0.1/Person> .")));

public void testTurtleDocument(MockServerClient client) throws Exception {
RDFLoader rdfLoader = new RDFLoader(new ParserConfig(), getValueFactory());

RDFHandler rdfHandler = mock(RDFHandler.class);

rdfLoader.load(new URL("http://localhost:" + wmRuntimeInfo.getHttpPort() + "/Socrates.ttl"), null, null,
rdfLoader.load(new URL("http://localhost:" + client.getPort() + "/Socrates.ttl"), null, null,
rdfHandler);

verify(rdfHandler).startRDF();
Expand All @@ -97,24 +139,12 @@ public void testTurtleDocument(WireMockRuntimeInfo wmRuntimeInfo) throws Excepti
}

@Test
public void testMultipleRedirects(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
stubFor(get("/Socrates")
.willReturn(permanentRedirect("/Socrates2")));
stubFor(get("/Socrates2")
.willReturn(permanentRedirect("/Socrates3")));
stubFor(get("/Socrates3")
.willReturn(permanentRedirect("/Socrates.ttl")));
stubFor(get("/Socrates.ttl")
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", RDFFormat.TURTLE.getDefaultMIMEType())
.withBody("<http://example.org/Socrates> a <http://xmlns.com/foaf/0.1/Person> .")));

public void testMultipleRedirects(MockServerClient client) throws Exception {
RDFLoader rdfLoader = new RDFLoader(new ParserConfig(), getValueFactory());

RDFHandler rdfHandler = mock(RDFHandler.class);

rdfLoader.load(new URL("http://localhost:" + wmRuntimeInfo.getHttpPort() + "/Socrates"), null, null,
rdfLoader.load(new URL("http://localhost:" + client.getPort() + "/Socrates1"), null, null,
rdfHandler);

verify(rdfHandler).startRDF();
Expand All @@ -126,17 +156,7 @@ public void testMultipleRedirects(WireMockRuntimeInfo wmRuntimeInfo) throws Exce
}

@Test
public void testAbortOverMaxRedirects(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
stubFor(get("/Socrates1")
.willReturn(permanentRedirect("/Socrates2")));
stubFor(get("/Socrates2")
.willReturn(permanentRedirect("/Socrates3")));
stubFor(get("/Socrates3")
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", RDFFormat.TURTLE.getDefaultMIMEType())
.withBody("<http://example.org/Socrates> a <http://xmlns.com/foaf/0.1/Person> .")));

public void testAbortOverMaxRedirects(MockServerClient client) throws Exception {
/* nullable */
String oldMaxRedirects = System.getProperty("http.maxRedirects");
try {
Expand All @@ -148,7 +168,7 @@ public void testAbortOverMaxRedirects(WireMockRuntimeInfo wmRuntimeInfo) throws

RDFHandler rdfHandler = mock(RDFHandler.class);
try {
rdfLoader.load(new URL("http://localhost:" + wmRuntimeInfo.getHttpPort() + "/Socrates1"), null, null,
rdfLoader.load(new URL("http://localhost:" + client.getPort() + "/Socrates1"), null, null,
rdfHandler);
} catch (ProtocolException e) {
actualException = e;
Expand All @@ -166,27 +186,16 @@ public void testAbortOverMaxRedirects(WireMockRuntimeInfo wmRuntimeInfo) throws
}

@Test
public void testNonInformationResource(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
public void testNonInformationResource(MockServerClient client) throws Exception {
final SSLSocketFactory toRestoreSocketFactory = disableSSLCertificatCheck();
try {
final HostnameVerifier toRestoreHostnameVerifier = disableHostnameVerifier();
try {
stubFor(get("/Socrates")
.willReturn(
permanentRedirect(
"http://localhost:" + wmRuntimeInfo.getHttpPort() + "/Socrates.ttl")));

stubFor(get("/Socrates.ttl")
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", RDFFormat.TURTLE.getDefaultMIMEType())
.withBody("<http://example.org/Socrates> a <http://xmlns.com/foaf/0.1/Person> .")));

RDFLoader rdfLoader = new RDFLoader(new ParserConfig(), getValueFactory());

RDFHandler rdfHandler = mock(RDFHandler.class);

rdfLoader.load(new URL("http://localhost:" + wmRuntimeInfo.getHttpPort() + "/Socrates"), null, null,
rdfLoader.load(new URL("http://localhost:" + client.getPort() + "/Socrates"), null, null,
rdfHandler);

verify(rdfHandler).startRDF();
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@
<artifactId>wiremock-jre8</artifactId>
<version>2.35.0</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-jupiter-no-dependencies</artifactId>
<version>5.15.0</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down