Fix running pipe and updated tests with mockserver

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