Add tests for paging through JSON exports

Change-Id: I8a94556c8864bab797834be4da1c7b17a78af2e5
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
index 9e85cae..6517c01 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
@@ -47,9 +47,7 @@
 
 /**
  * TODO:
- * - Paging export works only for rtf, JSON has to be integrated
  * - Delete the temp file of the export at the end
- * - Get variable cutoff from URL
  * - Right now, the web service returns one page (cutoff=1) or
  *   all pages.
  * - Handle timeout results (with minimum total results).
@@ -57,7 +55,6 @@
  * - Add mime type to exporters
  * - Add format to exporters
  * - Add file suffix to exporters
- * - Add "..." to snippets in RTF exporter
  * - Test Snippet-Export with multiple classes.
  * - Test Snippet-Export with cutted matches.
  */
diff --git a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java
index 6733a9a..bb7abe6 100644
--- a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java
+++ b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java
@@ -39,6 +39,11 @@
 import org.glassfish.jersey.test.JerseyTest;
 import org.junit.Test;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
 import de.ids_mannheim.korap.plkexport.IdsExportService;
 import de.ids_mannheim.korap.plkexport.ExWSConf;
 
@@ -46,16 +51,12 @@
 
 import static de.ids_mannheim.korap.plkexport.IdsExportService.getClientIP;
 
-/*
- * TODO Find a way to test efficiently the starting of the PluginServer with host and port of the config-file
- * + serving static files
- */
-
 public class IdsExportServiceTest extends JerseyTest {
 
     private static ClientAndServer mockServer;
 	private static MockServerClient mockClient;
-    
+    private ObjectMapper mapper = new ObjectMapper();
+            
     @BeforeClass
     public static void startServer() {
         // Define logging rules for Mock-Server
@@ -385,6 +386,62 @@
         assertTrue("Unicode handling", str.contains("Hintergr\\u252\\'fcnde"));
     }
 
+
+    @Test
+    public void testExportWsJsonPaging () throws IOException {
+
+        mockClient.reset().when(
+            request()
+            .withMethod("GET")
+            .withPath("/api/v1.0/search")
+            .withQueryStringParameter("q", "Plagegeist")
+            .withQueryStringParameter("count", "5")
+            .withQueryStringParameter("offset", "5")
+            )
+            .respond(
+                response()
+                .withHeader("Content-Type: application/json; charset=utf-8")
+                .withBody(getFixture("response_plagegeist_2.json"))
+                .withStatusCode(200)
+                );
+
+        mockClient.when(
+            request()
+            .withMethod("GET")
+            .withPath("/api/v1.0/search")
+            .withQueryStringParameter("q", "Plagegeist")
+            )
+            .respond(
+                response()
+                .withHeader("Content-Type: application/json; charset=utf-8")
+                .withBody(getFixture("response_plagegeist_1.json"))
+                .withStatusCode(200)
+                );
+        
+        MultivaluedHashMap<String, String> frmap = new MultivaluedHashMap<String, String>();
+        frmap.add("format", "json");
+        frmap.add("q", "Plagegeist");
+        frmap.add("ql", "poliqarp");
+        String filenamer = "dateiPagingJson";
+        frmap.putSingle("fname", filenamer);
+
+        Response responsejson = target("/export").request()
+            .post(Entity.form(frmap));
+        assertEquals("Request RTF: Http Response should be 200: ",
+                Status.OK.getStatusCode(), responsejson.getStatus());
+
+        String str = responsejson.readEntity(String.class);
+        JsonParser parser = mapper.getFactory().createParser(str);
+        JsonNode obj = mapper.readTree(parser);
+
+        assertEquals(obj.at("/query/@type").asText(),"koral:token");
+        assertEquals(obj.at("/meta/totalResults").asInt(),9);
+        assertEquals(obj.at("/matches/0/matchID").asText(),"match-WUD17/G59/34284-p4238-4239");
+        assertEquals(obj.at("/matches/1/matchID").asText(),"match-WUD17/C53/60524-p736-737");
+        assertEquals(obj.at("/matches/8/matchID").asText(),"match-WUD17/K35/39955-p16258-16259");
+        assertTrue(obj.at("/matches/0/snippet").asText().contains("<span class=\"context-right\">&quot;"));
+        assertTrue(obj.at("/matches/0/snippet").asText().contains("wie wär's"));
+    }    
     
 
     @Test