Added statistics tests and handled errors from a KoralQuery processor.

Change-Id: I859655d13c40d75550406b43c56420b571d8488c
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktCoreRestTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktCoreRestTest.java
index b5b30a8..0cf4dcc 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktCoreRestTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktCoreRestTest.java
@@ -1,17 +1,14 @@
 package de.ids_mannheim.korap.web.service.full;
 
-import com.sun.jersey.api.client.ClientResponse;
-import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.query.serialize.CollectionQueryProcessor;
-import de.ids_mannheim.korap.query.serialize.QuerySerializer;
-import de.ids_mannheim.korap.utils.JsonUtils;
-import de.ids_mannheim.korap.web.service.FastJerseyTest;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
 
-import java.util.LinkedHashMap;
-import java.util.Map;
+import com.sun.jersey.api.client.ClientResponse;
+
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.query.serialize.QuerySerializer;
+import de.ids_mannheim.korap.web.service.FastJerseyTest;
 
 /**
  * @author hanl
@@ -75,35 +72,7 @@
         ClientResponse response = resource().path(getAPIVersion()).get(
                 ClientResponse.class);
     }
-
-
-    //    @Test
-    public void testGetStatsThrowsNoException () {
-        CollectionQueryProcessor pr = new CollectionQueryProcessor();
-        pr.process("corpusSigle=WPD & textClass=Sport");
-        Map map = new LinkedHashMap();
-        map.put("collection", pr.getRequestMap());
-        ClientResponse response = resource().path(getAPIVersion())
-                .path("stats")
-                .post(ClientResponse.class, JsonUtils.toJSON(map));
-        assert ClientResponse.Status.OK.getStatusCode() == response.getStatus();
-    }
-
-
-    @Test
-    @Ignore
-    public void testGetStats2ThrowsNoException () {
-        ClientResponse response = resource().path(getAPIVersion())
-                .path("stats")
-                .post(ClientResponse.class, "creationDate in 1787");
-        String ent = response.getEntity(String.class);
-        assert ClientResponse.Status.OK.getStatusCode() == response.getStatus();
-        //        System.out
-        //                .println("___________________________________________________");
-        //        System.out.println("STATS ENTITY " + ent);
-    }
-
-
+   
     //    @Test
     public void testBuildQueryThrowsNoException () {
         ClientResponse response = resource().path(getAPIVersion())
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java
index a5002c9..6a5674d 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java
@@ -5,27 +5,12 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.security.KeyManagementException;
-import java.security.KeyStore;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
-import java.security.UnrecoverableKeyException;
-import java.security.cert.CertificateException;
-import java.util.List;
 import java.util.UUID;
 
-import javax.net.ssl.SSLContext;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
@@ -37,20 +22,13 @@
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.utils.URIBuilder;
-import org.apache.http.config.Registry;
-import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
-import org.apache.http.conn.ssl.SSLContexts;
-import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
 import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.client.HttpClients;
-import org.eclipse.jetty.util.ssl.SslContextFactory;
 import org.junit.Assert;
 import org.junit.Test;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.io.Files;
 import com.sun.jersey.api.client.ClientResponse;
 import com.sun.jersey.core.util.MultivaluedMapImpl;
 
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/StatisticsServiceTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/StatisticsServiceTest.java
new file mode 100644
index 0000000..971817b
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/StatisticsServiceTest.java
@@ -0,0 +1,114 @@
+package de.ids_mannheim.korap.web.service.full;
+
+import java.io.IOException;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.sun.jersey.api.client.ClientResponse;
+
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.web.service.FastJerseyTest;
+
+/** 
+ * @author margaretha
+ * @date 29/06/2017
+ *
+ */
+public class StatisticsServiceTest extends FastJerseyTest {
+
+    private ObjectMapper mapper = new ObjectMapper();
+
+
+    @Override
+    public void initMethod () throws KustvaktException {
+
+    }
+
+    @BeforeClass
+    public static void configure () {
+//        FastJerseyTest.setPackages("de.ids_mannheim.korap.web.service.light",
+        FastJerseyTest.setPackages("de.ids_mannheim.korap.web.service.full",
+                "de.ids_mannheim.korap.web.utils");
+    }
+
+
+    @Test
+    public void testGetStatisticsWithCollectionQuery1 ()
+            throws JsonProcessingException, IOException {
+        String collectionQuery = "corpusSigle=GOE";
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("statistics")
+                .queryParam("collectionQuery", collectionQuery)
+                .get(ClientResponse.class);
+
+        assert ClientResponse.Status.OK.getStatusCode() == response.getStatus();
+
+        String ent = response.getEntity(String.class);
+        JsonNode node = mapper.readTree(ent);
+        assert node.get("documents").asInt() == 11;
+        assert node.get("tokens").asInt() == 665842;
+    }
+
+
+    @Test
+    public void testGetStatisticsWithCollectionQuery2 ()
+            throws JsonProcessingException, IOException {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("statistics")
+                .queryParam("collectionQuery", "creationDate since 1810")
+                .get(ClientResponse.class);
+        String ent = response.getEntity(String.class);
+        JsonNode node = mapper.readTree(ent);
+        assert ClientResponse.Status.OK.getStatusCode() == response.getStatus();
+        assert node.get("documents").asInt() == 7;
+        assert node.get("tokens").asInt() == 279402;
+        // EM: why zero?
+        assert node.get("sentences").asInt() == 0;
+        assert node.get("paragraphs").asInt() == 0;
+    }
+
+
+    @Test
+    public void testGetStatisticsWithWrongCollectionQuery ()
+            throws JsonProcessingException, IOException {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("statistics")
+                .queryParam("collectionQuery", "creationDate geq 1810")
+                .get(ClientResponse.class);
+
+        assert ClientResponse.Status.BAD_REQUEST.getStatusCode() == response
+                .getStatus();
+        String ent = response.getEntity(String.class);
+        JsonNode node = mapper.readTree(ent);
+        assertEquals(node.at("/errors/0/0").asInt(), 302);
+        assertEquals(node.at("/errors/0/1").asText(),
+                "Could not parse query >>> (creationDate geq 1810) <<<.");
+        assertEquals(node.at("/errors/0/2").asText(),
+                "(creationDate geq 1810)");
+    }
+
+
+    @Test
+    public void testGetStatisticsWithWrongCollectionQuery2 ()
+            throws JsonProcessingException, IOException {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("statistics")
+                .queryParam("collectionQuery", "creationDate >= 1810")
+                .get(ClientResponse.class);
+
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                response.getStatus());
+        String ent = response.getEntity(String.class);
+        JsonNode node = mapper.readTree(ent);
+        assertEquals(node.at("/errors/0/0").asInt(), 305);
+        assertEquals(node.at("/errors/0/1").asText(),
+                "Operator >= is not acceptable.");
+        assertEquals(node.at("/errors/0/2").asText(), ">=");
+    }
+
+}