Updated the statistic API with KoralQuery and added tests.

Change-Id: I974ad7cf97bb69fb8fb85c734418a27d0302f4f3
diff --git a/lite/Changes b/lite/Changes
index 39ac301..0402601 100644
--- a/lite/Changes
+++ b/lite/Changes
@@ -1,10 +1,12 @@
 # version 0.62.2
 13/11/2019
    - Added tests for issue #43 (margaretha)
+22/11/2019
+   - Added tests for the statistic API with KoralQuery (margaretha)   
 
 # version 0.62.1
 08/07/2019
-   - Added tests for public metadata response in search api (margaretha, issue #43)
+   - Added tests for public metadata response in search API (margaretha, issue #43)
    - Disabled some tests of unused/disabled web-services (margaretha)
 
 version 0.62
diff --git a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteStatisticControllerTest.java b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteStatisticControllerTest.java
index 8f41b8a..268fdb1 100644
--- a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteStatisticControllerTest.java
+++ b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteStatisticControllerTest.java
@@ -3,6 +3,11 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
+import java.io.IOException;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+
 import org.junit.Test;
 
 import com.fasterxml.jackson.databind.JsonNode;
@@ -101,4 +106,78 @@
         assertEquals(772, node.at("/paragraphs").asInt());
     }
     
+    @Test
+    public void testGetStatisticsWithKoralQuery ()
+            throws IOException, KustvaktException {
+        ClientResponse response = resource().path(API_VERSION)
+                .path("statistics")
+                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
+                .post(ClientResponse.class,"{ \"collection\" : {\"@type\": "
+                        + "\"koral:doc\", \"key\": \"availability\", \"match\": "
+                        + "\"match:eq\", \"type\": \"type:regex\", \"value\": "
+                        + "\"CC-BY.*\"} }");
+
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                     response.getStatus());
+        String ent = response.getEntity(String.class);
+        
+        JsonNode node = JsonUtils.readTree(ent);
+        assertEquals(2, node.at("/documents").asInt());
+        assertEquals(72770, node.at("/tokens").asInt());
+        assertEquals(2985, node.at("/sentences").asInt());
+        assertEquals(128, node.at("/paragraphs").asInt());
+    }
+    
+    @Test
+    public void testGetStatisticsWithEmptyCollection ()
+            throws IOException, KustvaktException {
+        ClientResponse response = resource().path(API_VERSION)
+                .path("statistics")
+                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
+                .post(ClientResponse.class,"{}");
+
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                     response.getStatus());
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertEquals(node.at("/errors/0/0").asInt(),
+                de.ids_mannheim.korap.util.StatusCodes.MISSING_COLLECTION);
+        assertEquals(node.at("/errors/0/1").asText(),
+                "Collection is not found");
+    }
+    
+    @Test
+    public void testGetStatisticsWithIncorrectJson ()
+            throws IOException, KustvaktException {
+        ClientResponse response = resource().path(API_VERSION)
+                .path("statistics")
+                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
+                .post(ClientResponse.class,"{ \"collection\" : }");
+
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                     response.getStatus());
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertEquals(node.at("/errors/0/0").asInt(),
+                de.ids_mannheim.korap.util.StatusCodes.UNABLE_TO_PARSE_JSON);
+        assertEquals(node.at("/errors/0/1").asText(),
+                "Unable to parse JSON");
+    }
+    
+    @Test
+    public void testGetStatisticsWithoutKoralQuery ()
+            throws IOException, KustvaktException {
+        ClientResponse response = resource().path(API_VERSION)
+                .path("statistics").post(ClientResponse.class);
+        
+        String ent = response.getEntity(String.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        
+        JsonNode node = JsonUtils.readTree(ent);
+        assertEquals(11, node.at("/documents").asInt());
+        assertEquals(665842, node.at("/tokens").asInt());
+        assertEquals(25074, node.at("/sentences").asInt());
+        assertEquals(772, node.at("/paragraphs").asInt());
+    }
 }