Fix running pipe and updated tests with mockserver

Change-Id: I9e2ad0a9e20db874765e3db957ff356fbe35ff93
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());
     }
 }