Fix running pipe and updated tests with mockserver
Change-Id: I9e2ad0a9e20db874765e3db957ff356fbe35ff93
diff --git a/core/Changes b/core/Changes
index 611af1f..cab5c5b 100644
--- a/core/Changes
+++ b/core/Changes
@@ -1,5 +1,9 @@
# version 0.63
-
+14/01/2021
+ - Updated Flyway (margaretha)
+21/01/2021
+ - Fixed running pipe and updated tests with mockserver (margaretha)
+
# version 0.62.4
24/01/2020
- Removed salt from config and updated config files.
diff --git a/core/pom.xml b/core/pom.xml
index 59a689b..a14de06 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.ids_mannheim.korap</groupId>
<artifactId>Kustvakt-core</artifactId>
@@ -63,19 +63,18 @@
<!-- build tests jar, so extensions can use fastjerseytest class to build
rest tests -->
- <!-- <plugin>
+ <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
- <version>3.0.2</version>
+ <version>3.2.0</version>
<executions>
<execution>
- <phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
- </plugin> -->
+ </plugin>
<!-- Generate source jar -->
<plugin>
@@ -252,6 +251,10 @@
<version>[0.37,)</version>
<exclusions>
<exclusion>
+ <groupId>org.sonatype.sisu</groupId>
+ <artifactId>sisu-guava</artifactId>
+ </exclusion>
+ <exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</exclusion>
@@ -494,7 +497,6 @@
<version>0.1.0</version>
</dependency>
-
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
diff --git a/core/src/main/java/de/ids_mannheim/korap/server/KustvaktBaseServer.java b/core/src/main/java/de/ids_mannheim/korap/server/KustvaktBaseServer.java
index 2ec9040..647009d 100644
--- a/core/src/main/java/de/ids_mannheim/korap/server/KustvaktBaseServer.java
+++ b/core/src/main/java/de/ids_mannheim/korap/server/KustvaktBaseServer.java
@@ -6,7 +6,9 @@
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
import java.security.NoSuchAlgorithmException;
+import java.util.Scanner;
import javax.servlet.ServletContextListener;
@@ -19,7 +21,6 @@
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
-import com.google.common.io.Files;
import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
import de.ids_mannheim.korap.config.KustvaktConfiguration;
@@ -90,8 +91,9 @@
writer.close();
}
else {
- adminToken = Files.readFirstLine(f, Charset.forName("utf-8"))
- .substring(6);
+ Scanner scanner = new Scanner(f);
+ adminToken = scanner.nextLine().substring(6);
+ scanner.close();
}
Server server = new Server();
diff --git a/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java b/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
index 28c526d..43114cb 100644
--- a/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
+++ b/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
@@ -1,5 +1,10 @@
package de.ids_mannheim.korap.service;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@@ -8,7 +13,6 @@
import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;
import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;
@@ -21,9 +25,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import de.ids_mannheim.de.init.VCLoader;
@@ -207,22 +208,36 @@
for (int i=0; i<pipeArray.length; i++){
String pipeURL = pipeArray[i];
try {
- Client client = Client.create();
- WebResource resource = client.resource(pipeURL);
- ClientResponse response =
- resource.type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, query);
- if (response.getStatus() == HttpStatus.SC_OK) {
- String entity = response.getEntity(String.class);
- if (entity != null && !entity.isEmpty()) {
- query = entity;
+ URL url = new URL(pipeURL);
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setRequestMethod("POST");
+ connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
+ connection.setRequestProperty("Accept", "application/json");
+ connection.setDoOutput(true);
+ OutputStream os = connection.getOutputStream();
+ byte[] input = query.getBytes("utf-8");
+ os.write(input, 0, input.length);
+
+ String entity = null;
+ if (connection.getResponseCode() == HttpStatus.SC_OK) {
+ BufferedReader br =
+ new BufferedReader(new InputStreamReader(
+ connection.getInputStream(), "utf-8"));
+ StringBuilder response = new StringBuilder();
+ String responseLine = null;
+ while ((responseLine = br.readLine()) != null) {
+ response.append(responseLine.trim());
}
+ entity = response.toString();
+ }
+
+ if (entity != null && !entity.isEmpty()) {
+ query = entity;
}
else {
query = handlePipeError(query, pipeURL,
- response.getStatus() + " "
- + response.getStatusInfo().toString());
+ connection.getResponseCode() + " "
+ + connection.getResponseMessage());
}
}
catch (Exception e) {
diff --git a/core/src/main/java/de/ids_mannheim/korap/test/TestController.java b/core/src/main/java/de/ids_mannheim/korap/test/TestController.java
deleted file mode 100644
index 03b8f96..0000000
--- a/core/src/main/java/de/ids_mannheim/korap/test/TestController.java
+++ /dev/null
@@ -1,93 +0,0 @@
-package de.ids_mannheim.korap.test;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Controller;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-
-import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.utils.JsonUtils;
-import de.ids_mannheim.korap.web.KustvaktResponseHandler;
-
-/**
- * Controllers used only for testing
- *
- * @author margaretha
- *
- */
-@Controller
-@Path("/{version}/test")
-@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
-public class TestController {
-
- @Autowired
- private KustvaktResponseHandler kustvaktResponseHandler;
-
- public static ObjectMapper mapper = new ObjectMapper();
-
- @POST
- @Path("glemm")
- @Consumes(MediaType.APPLICATION_JSON)
- public Response dummyGlemm (String jsonld,
- @QueryParam("param") String param) throws IOException {
- InputStream is;
- is = getClass().getClassLoader()
- .getResourceAsStream("test-pipes.jsonld");
-
- ObjectNode newJson = (ObjectNode) mapper.readTree(is);
-
- try {
- JsonNode node = JsonUtils.readTree(jsonld);
- if (node.has("warnings")) {
- node = node.get("warnings");
- newJson.set("warnings", node);
- }
- if (param != null && !param.isEmpty()) {
- ArrayNode keys = (ArrayNode) newJson.at("/query/wrap/key");
- keys.add("die");
- newJson.set("key", keys);
- }
- }
- catch (KustvaktException e) {
- throw kustvaktResponseHandler.throwit(e);
- }
- return Response.ok(newJson.toString()).build();
- }
-
- @POST
- @Path("invalid-json-pipe")
- @Consumes(MediaType.APPLICATION_JSON)
- public Response dummyPipe1 (String jsonld) {
- String incorrectJson = "{blah:}";
- return Response.ok(incorrectJson).build();
- }
-
- @POST
- @Path("plain-response-pipe")
- @Consumes(MediaType.APPLICATION_JSON)
- public Response dummyPipe2 (String jsonld) {
- String incorrectJson = "brumbrum";
- return Response.ok(incorrectJson).build();
- }
-
- @POST
- @Path("urlencoded-pipe")
- @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
- public Response dummyPipe3 (String jsonld) {
- return Response.ok().build();
- }
-}
diff --git a/core/src/main/resources/test-pipes.jsonld b/core/src/test/resources/pipe-output/test-pipes.jsonld
similarity index 100%
rename from core/src/main/resources/test-pipes.jsonld
rename to core/src/test/resources/pipe-output/test-pipes.jsonld
diff --git a/core/src/test/resources/pipe-output/with-param.jsonld b/core/src/test/resources/pipe-output/with-param.jsonld
new file mode 100644
index 0000000..08c7fda
--- /dev/null
+++ b/core/src/test/resources/pipe-output/with-param.jsonld
@@ -0,0 +1,29 @@
+{
+ "meta": {
+ "snippets": true,
+ "timeout": 10000
+ },
+ "query": {
+ "@type": "koral:token",
+ "wrap": {
+ "@type": "koral:term",
+ "match": "match:eq",
+ "key": [
+ "der",
+ "das",
+ "die"
+ ],
+ "layer": "orth",
+ "rewrites": [
+ {
+ "@type": "koral:rewrite",
+ "src": "Glemm",
+ "operation": "operation:override",
+ "scope": "key"
+ }
+ ]
+ }
+ },
+ "@context": "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld"
+}
+
diff --git a/full/Changes b/full/Changes
index b604b95..40f20af 100644
--- a/full/Changes
+++ b/full/Changes
@@ -9,6 +9,10 @@
- Fix hibernate dialect for SQLite. (margaretha)
04/12/2020
- Fix pipe warning. (margaretha)
+14/01/2021
+ - Updated Flyway (margaretha)
+21/01/2021
+ - Fixed running pipe and updated tests with mockserver (margaretha)
# version 0.62.4
24/01/2020
diff --git a/full/pom.xml b/full/pom.xml
index d083b8c..407a212 100644
--- a/full/pom.xml
+++ b/full/pom.xml
@@ -205,7 +205,15 @@
<dependency>
<groupId>de.ids_mannheim.korap</groupId>
<artifactId>Kustvakt-core</artifactId>
- <version>[0.62.4,)</version>
+ <version>[0.63.,)</version>
+ </dependency>
+ <dependency>
+ <groupId>de.ids_mannheim.korap</groupId>
+ <artifactId>Kustvakt-core</artifactId>
+ <version>[0.63.,)</version>
+ <classifier>tests</classifier>
+ <type>test-jar</type>
+ <scope>test</scope>
</dependency>
<!-- LDAP -->
<dependency>
@@ -324,5 +332,13 @@
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
+
+ <!-- Mockserver -->
+ <dependency>
+ <groupId>org.mock-server</groupId>
+ <artifactId>mockserver-netty</artifactId>
+ <version>5.11.2</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchPipeTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchPipeTest.java
index 7018a1a..99a6d86 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchPipeTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchPipeTest.java
@@ -1,11 +1,28 @@
package de.ids_mannheim.korap.web.controller;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockserver.integration.ClientAndServer.startClientAndServer;
+import static org.mockserver.model.HttpRequest.request;
+import static org.mockserver.model.HttpResponse.response;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URISyntaxException;
+import java.net.URL;
import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import org.apache.commons.io.IOUtils;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
+import org.mockserver.client.MockServerClient;
+import org.mockserver.integration.ClientAndServer;
+import org.mockserver.model.Header;
import com.fasterxml.jackson.databind.JsonNode;
import com.sun.jersey.api.client.ClientResponse;
@@ -17,10 +34,82 @@
public class SearchPipeTest extends SpringJerseyTest {
+ private ClientAndServer mockServer;
+ private MockServerClient mockClient;
+
+ private String pipeJson, pipeWithParamJson;
+ private String glemmUri = "http://localhost:1080/glemm";
+
+ public SearchPipeTest () throws URISyntaxException, IOException {
+ pipeJson = IOUtils.toString(
+ ClassLoader.getSystemResourceAsStream(
+ "pipe-output/test-pipes.jsonld"),
+ StandardCharsets.UTF_8);
+
+ pipeWithParamJson = IOUtils.toString(
+ ClassLoader.getSystemResourceAsStream(
+ "pipe-output/with-param.jsonld"),
+ StandardCharsets.UTF_8);
+ }
+
+ @Before
+ public void startMockServer () {
+ mockServer = startClientAndServer(1080);
+ mockClient = new MockServerClient("localhost", mockServer.getPort());
+ }
+
+ @After
+ public void stopMockServer () {
+ mockServer.stop();
+ }
+
@Test
- public void testSearchWithPipes () throws IOException, KustvaktException {
- String glemmUri =
- resource().getURI().toString() + API_VERSION + "/test/glemm";
+ public void testMockServer () throws IOException {
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/test")
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody("{test}").withStatusCode(200));
+
+ URL url = new URL("http://localhost:1080/test");
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setRequestMethod("POST");
+ connection.setRequestProperty("Content-Type",
+ "application/json; charset=UTF-8");
+ connection.setRequestProperty("Accept", "application/json");
+ connection.setDoOutput(true);
+
+ String json = "{\"name\" : \"dory\"}";
+ try (OutputStream os = connection.getOutputStream()) {
+ byte[] input = json.getBytes("utf-8");
+ os.write(input, 0, input.length);
+ }
+
+ assertEquals(200, connection.getResponseCode());
+
+ BufferedReader br = new BufferedReader(
+ new InputStreamReader(connection.getInputStream(), "utf-8"));
+ assertEquals("{test}", br.readLine());
+
+ }
+
+ @Test
+ public void testSearchWithPipes ()
+ throws IOException, KustvaktException, URISyntaxException {
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/glemm")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody(pipeJson).withStatusCode(200));
+
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
.queryParam("pipes", glemmUri).get(ClientResponse.class);
@@ -49,13 +138,24 @@
assertEquals("operation:injection", node.at("/1/operation").asText());
assertEquals("foundry", node.at("/1/scope").asText());
}
-
+
@Test
- public void testSearchWithUrlEncodedPipes () throws IOException, KustvaktException {
- String glemmUri =
- resource().getURI().toString() + API_VERSION + "/test/glemm";
- glemmUri = URLEncoder.encode(glemmUri,"utf-8");
-
+ public void testSearchWithUrlEncodedPipes ()
+ throws IOException, KustvaktException {
+
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/glemm")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody(pipeJson).withStatusCode(200));
+
+ glemmUri = URLEncoder.encode(glemmUri, "utf-8");
+
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
.queryParam("pipes", glemmUri).get(ClientResponse.class);
@@ -67,8 +167,18 @@
@Test
public void testSearchWithMultiplePipes () throws KustvaktException {
- String glemmUri =
- resource().getURI().toString() + API_VERSION + "/test/glemm";
+
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/glemm")
+ .withQueryStringParameter("param").withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody(pipeWithParamJson).withStatusCode(200));
+
String glemmUri2 = glemmUri + "?param=blah";
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
@@ -98,6 +208,7 @@
JsonNode node = JsonUtils.readTree(entity);
assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt());
+ assertEquals("404 Not Found", node.at("/warnings/0/3").asText());
}
@Test
@@ -113,15 +224,20 @@
JsonNode node = JsonUtils.readTree(entity);
assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt());
+ assertEquals("glemm", node.at("/warnings/0/3").asText());
}
@Test
- public void testSearchWithUrlEncodedPipe () throws KustvaktException {
- String pipe = resource().getURI().toString() + API_VERSION
- + "/test/urlencoded-pipe";
+ public void testSearchUnsupportedMediaType () throws KustvaktException {
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/non-json-pipe"))
+ .respond(response().withStatusCode(415));
+
+ String pipeUri = "http://localhost:1080/non-json-pipe";
+
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
- .queryParam("pipes", pipe).get(ClientResponse.class);
+ .queryParam("pipes", pipeUri).get(ClientResponse.class);
String entity = response.getEntity(String.class);
assertEquals(ClientResponse.Status.OK.getStatusCode(),
@@ -155,18 +271,26 @@
assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/1/0").asInt());
assertEquals("http://glemm", node.at("/warnings/1/2").asText());
- assertEquals("java.net.UnknownHostException: glemm",
- node.at("/warnings/1/3").asText());
+ assertEquals("glemm", node.at("/warnings/1/3").asText());
}
@Test
public void testSearchWithInvalidJsonResponse () throws KustvaktException {
- String pipe = resource().getURI().toString() + API_VERSION
- + "/test/invalid-json-pipe";
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/invalid-response")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response().withBody("{blah:}").withStatusCode(200)
+ .withHeaders(new Header("Content-Type",
+ "application/json; charset=utf-8")));
+
+ String pipeUri = "http://localhost:1080/invalid-response";
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
- .queryParam("pipes", pipe).get(ClientResponse.class);
+ .queryParam("pipes", pipeUri).get(ClientResponse.class);
String entity = response.getEntity(String.class);
assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
@@ -179,11 +303,18 @@
@Test
public void testSearchWithPlainTextResponse () throws KustvaktException {
- String pipe = resource().getURI().toString() + API_VERSION
- + "/test/plain-response-pipe";
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/plain-text")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response().withBody("blah").withStatusCode(200));
+
+ String pipeUri = "http://localhost:1080/plain-text";
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
- .queryParam("pipes", pipe).get(ClientResponse.class);
+ .queryParam("pipes", pipeUri).get(ClientResponse.class);
String entity = response.getEntity(String.class);
assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
@@ -197,8 +328,18 @@
@Test
public void testSearchWithMultipleAndUnknownPipes ()
throws KustvaktException {
- String glemmUri =
- resource().getURI().toString() + API_VERSION + "/test/glemm";
+
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/glemm")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody(pipeJson).withStatusCode(200));
+
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
.queryParam("pipes", "http://unknown" + "," + glemmUri)
@@ -209,7 +350,19 @@
String entity = response.getEntity(String.class);
JsonNode node = JsonUtils.readTree(entity);
- assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt());
assertEquals(2, node.at("/query/wrap/key").size());
+ assertTrue(node.at("/warnings").isMissingNode());
+
+ response = resource().path(API_VERSION).path("search")
+ .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
+ .queryParam("pipes", glemmUri + ",http://unknown")
+ .get(ClientResponse.class);
+
+ assertEquals(ClientResponse.Status.OK.getStatusCode(),
+ response.getStatus());
+
+ entity = response.getEntity(String.class);
+ node = JsonUtils.readTree(entity);
+ assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt());
}
}
diff --git a/lite/Changes b/lite/Changes
index c9ccc2e..be5532b 100644
--- a/lite/Changes
+++ b/lite/Changes
@@ -1,6 +1,11 @@
# version 0.63
04/12/2020
- Fix hibernate dialect for SQLite. (margaretha)
+14/01/2021
+ - Updated Flyway (margaretha)
+21/01/2021
+ - Fixed running pipe and updated tests with mockserver (margaretha)
+
# version 0.62.4
24/01/2020
diff --git a/lite/pom.xml b/lite/pom.xml
index b40e638..46484fd 100644
--- a/lite/pom.xml
+++ b/lite/pom.xml
@@ -146,11 +146,26 @@
<dependencies>
+ <!-- Mockserver -->
+ <dependency>
+ <groupId>org.mock-server</groupId>
+ <artifactId>mockserver-netty</artifactId>
+ <version>5.11.2</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>de.ids_mannheim.korap</groupId>
<artifactId>Kustvakt-core</artifactId>
- <version>[0.62.4,)</version>
+ <version>[0.63.,)</version>
</dependency>
+ <dependency>
+ <groupId>de.ids_mannheim.korap</groupId>
+ <artifactId>Kustvakt-core</artifactId>
+ <version>[0.63.,)</version>
+ <classifier>tests</classifier>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
<!-- Jersey test framework -->
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
@@ -178,6 +193,12 @@
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/backport-util-concurrent/backport-util-concurrent -->
<dependency>
diff --git a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteSearchPipeTest.java b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteSearchPipeTest.java
index 3f0c0bb..38e5f21 100644
--- a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteSearchPipeTest.java
+++ b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteSearchPipeTest.java
@@ -1,10 +1,28 @@
package de.ids_mannheim.korap.web.service;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockserver.integration.ClientAndServer.startClientAndServer;
+import static org.mockserver.model.HttpRequest.request;
+import static org.mockserver.model.HttpResponse.response;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import org.apache.commons.io.IOUtils;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
+import org.mockserver.client.MockServerClient;
+import org.mockserver.integration.ClientAndServer;
+import org.mockserver.model.Header;
import com.fasterxml.jackson.databind.JsonNode;
import com.sun.jersey.api.client.ClientResponse;
@@ -16,10 +34,82 @@
public class LiteSearchPipeTest extends LiteJerseyTest {
+ private ClientAndServer mockServer;
+ private MockServerClient mockClient;
+
+ private String pipeJson, pipeWithParamJson;
+ private String glemmUri = "http://localhost:1080/glemm";
+
+ public LiteSearchPipeTest () throws IOException{
+ pipeJson = IOUtils.toString(
+ ClassLoader.getSystemResourceAsStream(
+ "pipe-output/test-pipes.jsonld"),
+ StandardCharsets.UTF_8);
+
+ pipeWithParamJson = IOUtils.toString(
+ ClassLoader.getSystemResourceAsStream(
+ "pipe-output/with-param.jsonld"),
+ StandardCharsets.UTF_8);
+ }
+
+ @Before
+ public void startMockServer () {
+ mockServer = startClientAndServer(1080);
+ mockClient = new MockServerClient("localhost", mockServer.getPort());
+ }
+
+ @After
+ public void stopMockServer () {
+ mockServer.stop();
+ }
+
@Test
- public void testSearchWithPipes () throws IOException, KustvaktException {
- String glemmUri =
- resource().getURI().toString() + API_VERSION + "/test/glemm";
+ public void testMockServer () throws IOException {
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/test")
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody("{test}").withStatusCode(200));
+
+ URL url = new URL("http://localhost:1080/test");
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setRequestMethod("POST");
+ connection.setRequestProperty("Content-Type",
+ "application/json; charset=UTF-8");
+ connection.setRequestProperty("Accept", "application/json");
+ connection.setDoOutput(true);
+
+ String json = "{\"name\" : \"dory\"}";
+ try (OutputStream os = connection.getOutputStream()) {
+ byte[] input = json.getBytes("utf-8");
+ os.write(input, 0, input.length);
+ }
+
+ assertEquals(200, connection.getResponseCode());
+
+ BufferedReader br = new BufferedReader(
+ new InputStreamReader(connection.getInputStream(), "utf-8"));
+ assertEquals("{test}", br.readLine());
+
+ }
+
+ @Test
+ public void testSearchWithPipes ()
+ throws IOException, KustvaktException, URISyntaxException {
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/glemm")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody(pipeJson).withStatusCode(200));
+
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
.queryParam("pipes", glemmUri).get(ClientResponse.class);
@@ -28,9 +118,9 @@
response.getStatus());
String entity = response.getEntity(String.class);
-
JsonNode node = JsonUtils.readTree(entity);
assertEquals(2, node.at("/query/wrap/key").size());
+
node = node.at("/query/wrap/rewrites");
assertEquals(2, node.size());
assertEquals("Glemm", node.at("/0/src").asText());
@@ -43,9 +133,45 @@
}
@Test
+ public void testSearchWithUrlEncodedPipes ()
+ throws IOException, KustvaktException {
+
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/glemm")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody(pipeJson).withStatusCode(200));
+
+ glemmUri = URLEncoder.encode(glemmUri, "utf-8");
+
+ ClientResponse response = resource().path(API_VERSION).path("search")
+ .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
+ .queryParam("pipes", glemmUri).get(ClientResponse.class);
+
+ String entity = response.getEntity(String.class);
+ JsonNode node = JsonUtils.readTree(entity);
+ assertEquals(2, node.at("/query/wrap/key").size());
+ }
+
+ @Test
public void testSearchWithMultiplePipes () throws KustvaktException {
- String glemmUri =
- resource().getURI().toString() + API_VERSION + "/test/glemm";
+
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/glemm")
+ .withQueryStringParameter("param").withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody(pipeWithParamJson).withStatusCode(200));
+
String glemmUri2 = glemmUri + "?param=blah";
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
@@ -75,6 +201,7 @@
JsonNode node = JsonUtils.readTree(entity);
assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt());
+ assertEquals("404 Not Found", node.at("/warnings/0/3").asText());
}
@Test
@@ -90,15 +217,20 @@
JsonNode node = JsonUtils.readTree(entity);
assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt());
+ assertEquals("glemm", node.at("/warnings/0/3").asText());
}
@Test
- public void testSearchWithUrlEncodedPipe () throws KustvaktException {
- String pipe = resource().getURI().toString() + API_VERSION
- + "/test/urlencoded-pipe";
+ public void testSearchUnsupportedMediaType () throws KustvaktException {
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/non-json-pipe"))
+ .respond(response().withStatusCode(415));
+
+ String pipeUri = "http://localhost:1080/non-json-pipe";
+
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
- .queryParam("pipes", pipe).get(ClientResponse.class);
+ .queryParam("pipes", pipeUri).get(ClientResponse.class);
String entity = response.getEntity(String.class);
assertEquals(ClientResponse.Status.OK.getStatusCode(),
@@ -132,18 +264,26 @@
assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/1/0").asInt());
assertEquals("http://glemm", node.at("/warnings/1/2").asText());
- assertEquals("java.net.UnknownHostException: glemm",
- node.at("/warnings/1/3").asText());
+ assertEquals("glemm", node.at("/warnings/1/3").asText());
}
@Test
public void testSearchWithInvalidJsonResponse () throws KustvaktException {
- String pipe = resource().getURI().toString() + API_VERSION
- + "/test/invalid-json-pipe";
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/invalid-response")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response().withBody("{blah:}").withStatusCode(200)
+ .withHeaders(new Header("Content-Type",
+ "application/json; charset=utf-8")));
+
+ String pipeUri = "http://localhost:1080/invalid-response";
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
- .queryParam("pipes", pipe).get(ClientResponse.class);
+ .queryParam("pipes", pipeUri).get(ClientResponse.class);
String entity = response.getEntity(String.class);
assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
@@ -156,11 +296,18 @@
@Test
public void testSearchWithPlainTextResponse () throws KustvaktException {
- String pipe = resource().getURI().toString() + API_VERSION
- + "/test/plain-response-pipe";
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/plain-text")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response().withBody("blah").withStatusCode(200));
+
+ String pipeUri = "http://localhost:1080/plain-text";
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
- .queryParam("pipes", pipe).get(ClientResponse.class);
+ .queryParam("pipes", pipeUri).get(ClientResponse.class);
String entity = response.getEntity(String.class);
assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
@@ -174,8 +321,18 @@
@Test
public void testSearchWithMultipleAndUnknownPipes ()
throws KustvaktException {
- String glemmUri =
- resource().getURI().toString() + API_VERSION + "/test/glemm";
+
+ mockClient.reset()
+ .when(request().withMethod("POST").withPath("/glemm")
+ .withHeaders(
+ new Header("Content-Type",
+ "application/json; charset=utf-8"),
+ new Header("Accept", "application/json")))
+ .respond(response()
+ .withHeader(new Header("Content-Type",
+ "application/json; charset=utf-8"))
+ .withBody(pipeJson).withStatusCode(200));
+
ClientResponse response = resource().path(API_VERSION).path("search")
.queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
.queryParam("pipes", "http://unknown" + "," + glemmUri)
@@ -186,7 +343,19 @@
String entity = response.getEntity(String.class);
JsonNode node = JsonUtils.readTree(entity);
- assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt());
assertEquals(2, node.at("/query/wrap/key").size());
+ assertTrue(node.at("/warnings").isMissingNode());
+
+ response = resource().path(API_VERSION).path("search")
+ .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
+ .queryParam("pipes", glemmUri + ",http://unknown")
+ .get(ClientResponse.class);
+
+ assertEquals(ClientResponse.Status.OK.getStatusCode(),
+ response.getStatus());
+
+ entity = response.getEntity(String.class);
+ node = JsonUtils.readTree(entity);
+ assertEquals(StatusCodes.PIPE_FAILED, node.at("/warnings/0/0").asInt());
}
}
diff --git a/lite/src/test/java/de/ids_mannheim/korap/web/service/TestDummyServices.java b/lite/src/test/java/de/ids_mannheim/korap/web/service/TestDummyServices.java
deleted file mode 100644
index 8e4e0d2..0000000
--- a/lite/src/test/java/de/ids_mannheim/korap/web/service/TestDummyServices.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package de.ids_mannheim.korap.web.service;
-
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Test;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.sun.jersey.api.client.ClientResponse;
-
-import de.ids_mannheim.korap.config.LiteJerseyTest;
-import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.utils.JsonUtils;
-
-public class TestDummyServices extends LiteJerseyTest {
-
- @Test
- public void testGlemmService () throws KustvaktException {
- ClientResponse response = resource().path(API_VERSION).path("test")
- .path("glemm").post(ClientResponse.class);
-
- assertEquals(ClientResponse.Status.OK.getStatusCode(),
- response.getStatus());
-
- String entity = response.getEntity(String.class);
-
- JsonNode node = JsonUtils.readTree(entity);
- assertEquals(2, node.at("/query/wrap/key").size());
- node = node.at("/query/wrap/rewrites");
- assertEquals("Glemm", node.at("/0/src").asText());
- assertEquals("operation:override", node.at("/0/operation").asText());
- assertEquals("key", node.at("/0/scope").asText());
- }
-}