Updated the statistic API with KoralQuery and added tests.

Change-Id: I974ad7cf97bb69fb8fb85c734418a27d0302f4f3
diff --git a/core/Changes b/core/Changes
index 32a842b..272c8d1 100644
--- a/core/Changes
+++ b/core/Changes
@@ -4,6 +4,8 @@
      accessRewriteDisabled (margaretha, resolved #43).
 14/11/2019
    - Added a check for OAuth2 client, VC, and group name length (margaretha)
+22/11/2019
+   - Updated the statistic API with KoralQuery (margaretha)  
 
 # version 0.62.1
 08/07/2019
@@ -18,7 +20,8 @@
 28/08/2019
    - Resolved #49. Added page param check in the search api (margaretha)
 23/09/2019   
-   - Resolved #50. Added statistics API with KoralQuery input (margaretha) 
+   - Resolved #50. Added statistics API with KoralQuery input (margaretha)
+   
 
 # version 0.62
 18/03/2019
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java b/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java
index 7ef29f6..43f3693 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java
@@ -118,24 +118,26 @@
     
     @POST
     @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
-    public Response getStatisticsFromKoralQuery (@Context SecurityContext context,
-            @Context Locale locale, String koralQuery) {
-        if (koralQuery != null && !koralQuery.isEmpty()) {
-            String stats;
-            try {
+    public Response getStatisticsFromKoralQuery (
+            @Context SecurityContext context, @Context Locale locale,
+            String koralQuery) {
+        String stats;
+        try {
+            if (koralQuery != null && !koralQuery.isEmpty()) {
                 stats = searchKrill.getStatistics(koralQuery);
-                if (stats.contains("-1")){
-                    throw kustvaktResponseHandler.throwit(StatusCodes.NO_RESULT_FOUND);
-                }
-                return Response.ok(stats).build();
             }
-            catch (KustvaktException e) {
-                throw kustvaktResponseHandler.throwit(e);
+            else {
+                stats = searchKrill.getStatistics(null);
             }
+
+            if (stats.contains("-1")) {
+                throw kustvaktResponseHandler
+                        .throwit(StatusCodes.NO_RESULT_FOUND);
+            }
+            return Response.ok(stats).build();
         }
-        else {
-            throw kustvaktResponseHandler.throwit(StatusCodes.NO_QUERY, 
-                    "Koral query is missing", "koralQuery");
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
         }
     }
 }
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/StatisticsControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/StatisticsControllerTest.java
index 8befd15..dc5d3c7 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/StatisticsControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/StatisticsControllerTest.java
@@ -239,16 +239,16 @@
             throws IOException, KustvaktException {
         ClientResponse response = resource().path(API_VERSION)
                 .path("statistics").post(ClientResponse.class);
-
-        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
-                response.getStatus());
+        
         String ent = response.getEntity(String.class);
-
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        
         JsonNode node = JsonUtils.readTree(ent);
-        assertEquals(StatusCodes.NO_QUERY, node.at("/errors/0/0").asInt());
-        assertEquals("Koral query is missing",
-                node.at("/errors/0/1").asText());
-        assertEquals("koralQuery", node.at("/errors/0/2").asText());
+        assertEquals(11, node.at("/documents").asInt());
+        assertEquals(665842, node.at("/tokens").asInt());
+        assertEquals(25074, node.at("/sentences").asInt());
+        assertEquals(772, node.at("/paragraphs").asInt());
     }
     
 }
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());
+    }
 }