Added statistics with vc reference tests.

Change-Id: I39d4ed88bd39852f66343b503b7103678ff398be
diff --git a/full/Changes b/full/Changes
index 628696b..e458ca3 100644
--- a/full/Changes
+++ b/full/Changes
@@ -30,6 +30,9 @@
 23/08/2018    
     - Updated RefreshToken implementations with separate DB tables (margaretha)
     - Allows multiple access tokens per refresh token (margaretha)
+27/08/2018
+    - Added statistic with VC reference tests (margaretha)
+    - Fixed OAuth2 SQL files (margaretha) 
 
 # version 0.60.5
 
diff --git a/full/pom.xml b/full/pom.xml
index 557b114..3a7d0aa 100644
--- a/full/pom.xml
+++ b/full/pom.xml
@@ -189,6 +189,12 @@
 			<version>${hibernate.version}</version>
 		</dependency>
 		<dependency>
+		    <groupId>org.hibernate</groupId>
+		    <artifactId>hibernate-c3p0</artifactId>
+		    <version>5.3.5.Final</version>
+		</dependency>
+		
+		<dependency>
 			<groupId>org.javassist</groupId>
 			<artifactId>javassist</artifactId>
 			<version>3.22.0-GA</version>
diff --git a/full/src/main/java/de/ids_mannheim/korap/config/Initializator.java b/full/src/main/java/de/ids_mannheim/korap/config/Initializator.java
index 286195c..57d78b2 100644
--- a/full/src/main/java/de/ids_mannheim/korap/config/Initializator.java
+++ b/full/src/main/java/de/ids_mannheim/korap/config/Initializator.java
@@ -7,6 +7,7 @@
 
 import de.ids_mannheim.korap.oauth2.constant.OAuth2Scope;
 import de.ids_mannheim.korap.oauth2.dao.AccessScopeDao;
+import de.ids_mannheim.korap.util.QueryException;
 
 /**
  * Initializes values in the database from kustvakt configuration and
@@ -24,7 +25,7 @@
 
     public Initializator () {}
 
-    public void init () throws IOException {
+    public void init () throws IOException, QueryException {
         setInitialAccessScope();
         loader.loadVCToCache();
     }
diff --git a/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java b/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
index 0549106..ff303b1 100644
--- a/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
+++ b/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
@@ -13,6 +13,7 @@
 import org.springframework.stereotype.Component;
 
 import de.ids_mannheim.korap.KrillCollection;
+import de.ids_mannheim.korap.util.QueryException;
 import de.ids_mannheim.korap.web.SearchKrill;
 
 @Component
@@ -24,7 +25,7 @@
 
     private static Logger jlog = LogManager.getLogger(NamedVCLoader.class);
 
-    public void loadVCToCache () throws IOException {
+    public void loadVCToCache () throws IOException, QueryException {
 
         String dir = config.getNamedVCPath();
         File d = new File(dir);
diff --git a/full/src/main/resources/db/new-mysql/V1.4__oauth2_tables.sql b/full/src/main/resources/db/new-mysql/V1.4__oauth2_tables.sql
index d5688b8..9469f8a 100644
--- a/full/src/main/resources/db/new-mysql/V1.4__oauth2_tables.sql
+++ b/full/src/main/resources/db/new-mysql/V1.4__oauth2_tables.sql
@@ -54,28 +54,6 @@
 --	UNIQUE INDEX authorization_scope_index(authorization_id, scope_id)
 --);
 
-CREATE TABLE IF NOT EXISTS oauth2_access_token (
-	id INTEGER PRIMARY KEY AUTO_INCREMENT,
-	token VARCHAR(255) NOT NULL,
-	user_id VARCHAR(100) DEFAULT NULL,
-	client_id VARCHAR(100) DEFAULT NULL,
-	created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-	expiry_date TIMESTAMP NULL,
-	is_revoked BOOLEAN DEFAULT 0,
-	user_auth_time TIMESTAMP NULL,
-    refresh_token VARCHAR(255) DEFAULT NULL,
-	FOREIGN KEY (client_id)
-	   REFERENCES oauth2_client(id),
-	FOREIGN KEY (refresh_token)
-	   REFERENCES oauth2_refresh_token(id)
-);
-
-CREATE TABLE oauth2_access_token_scope (
-	token_id INTEGER NOT NULL, 
-	scope_id VARCHAR(100) NOT NULL, 
-	CONSTRAINT primary_key PRIMARY KEY (token_id, scope_id)
-);
-
 CREATE TABLE IF NOT EXISTS oauth2_refresh_token (
 	id INTEGER PRIMARY KEY AUTO_INCREMENT,
 	token VARCHAR(255) NOT NULL,
@@ -93,3 +71,26 @@
 	scope_id VARCHAR(100) NOT NULL, 
 	CONSTRAINT primary_key PRIMARY KEY (token_id, scope_id)
 );
+
+CREATE TABLE IF NOT EXISTS oauth2_access_token (
+	id INTEGER PRIMARY KEY AUTO_INCREMENT,
+	token VARCHAR(255) NOT NULL,
+	user_id VARCHAR(100) DEFAULT NULL,
+	client_id VARCHAR(100) DEFAULT NULL,
+	created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+	expiry_date TIMESTAMP NULL,
+	is_revoked BOOLEAN DEFAULT 0,
+	user_auth_time TIMESTAMP NULL,
+    refresh_token INTEGER DEFAULT NULL,
+	FOREIGN KEY (client_id)
+	   REFERENCES oauth2_client(id),
+	FOREIGN KEY (refresh_token)
+	   REFERENCES oauth2_refresh_token(id)
+);
+
+CREATE TABLE oauth2_access_token_scope (
+	token_id INTEGER NOT NULL, 
+	scope_id VARCHAR(100) NOT NULL, 
+	CONSTRAINT primary_key PRIMARY KEY (token_id, scope_id)
+);
+
diff --git a/full/src/main/resources/db/new-sqlite/V1.4__oauth2_tables.sql b/full/src/main/resources/db/new-sqlite/V1.4__oauth2_tables.sql
index f11cac8..db238ab 100644
--- a/full/src/main/resources/db/new-sqlite/V1.4__oauth2_tables.sql
+++ b/full/src/main/resources/db/new-sqlite/V1.4__oauth2_tables.sql
@@ -59,28 +59,6 @@
 --CREATE UNIQUE INDEX authorization_scope_index on 
 --	oauth2_authorization_scope(authorization_id, scope_id);
 
-CREATE TABLE IF NOT EXISTS oauth2_access_token (
-	id INTEGER PRIMARY KEY AUTOINCREMENT,
-	token VARCHAR(255) NOT NULL,
-	user_id VARCHAR(100) DEFAULT NULL,
-	client_id VARCHAR(100) DEFAULT NULL,
-	created_date TIMESTAMP NOT NULL,
-	expiry_date TIMESTAMP NOT NULL,
-	is_revoked BOOLEAN DEFAULT 0,
-	user_auth_time TIMESTAMP NOT NULL,
-	refresh_token VARCHAR(255) DEFAULT NULL,
-	FOREIGN KEY (client_id)
-	   REFERENCES oauth2_client(id)
-	FOREIGN KEY (refresh_token)
-	   REFERENCES oauth2_refresh_token(id)
-);
-
-CREATE TABLE oauth2_access_token_scope (
-	token_id INTEGER NOT NULL, 
-	scope_id VARCHAR(100) NOT NULL, 
-	primary key (token_id, scope_id)
-);
-
 CREATE TABLE IF NOT EXISTS oauth2_refresh_token (
 	id INTEGER PRIMARY KEY AUTOINCREMENT,
 	token VARCHAR(255) NOT NULL,
@@ -99,3 +77,26 @@
 	scope_id VARCHAR(100) NOT NULL, 
 	CONSTRAINT primary_key PRIMARY KEY (token_id, scope_id)
 );
+
+CREATE TABLE IF NOT EXISTS oauth2_access_token (
+	id INTEGER PRIMARY KEY AUTOINCREMENT,
+	token VARCHAR(255) NOT NULL,
+	user_id VARCHAR(100) DEFAULT NULL,
+	client_id VARCHAR(100) DEFAULT NULL,
+	created_date TIMESTAMP NOT NULL,
+	expiry_date TIMESTAMP NOT NULL,
+	is_revoked BOOLEAN DEFAULT 0,
+	user_auth_time TIMESTAMP NOT NULL,
+	refresh_token INTEGER DEFAULT NULL,
+	FOREIGN KEY (client_id)
+	   REFERENCES oauth2_client(id)
+	FOREIGN KEY (refresh_token)
+	   REFERENCES oauth2_refresh_token(id)
+);
+
+CREATE TABLE oauth2_access_token_scope (
+	token_id INTEGER NOT NULL, 
+	scope_id VARCHAR(100) NOT NULL, 
+	CONSTRAINT primary_key PRIMARY KEY (token_id, scope_id)
+);
+
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 92b6c04..5b01eb7 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
@@ -132,4 +132,5 @@
         assertEquals(25074, node.at("/sentences").asInt());
         assertEquals(772, node.at("/paragraphs").asInt());
     }
+   
 }
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/VCReferenceTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/VCReferenceTest.java
index ae3d38e..976a791 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/VCReferenceTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/VCReferenceTest.java
@@ -1,5 +1,6 @@
 package de.ids_mannheim.korap.web.controller;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
@@ -24,7 +25,7 @@
         testSearchWithVCRefEqual();
         testSearchWithVCRefNotEqual();
     }
-    
+
     private void testSearchWithoutVCRefOr () throws KustvaktException {
         ClientResponse response = resource().path("search")
                 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
@@ -36,7 +37,7 @@
         JsonNode node = JsonUtils.readTree(ent);
         assertTrue(node.at("/matches").size() > 0);
     }
-    
+
     private void testSearchWithoutVCRefAnd () throws KustvaktException {
         ClientResponse response = resource().path("search")
                 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
@@ -58,9 +59,9 @@
         String ent = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         assertTrue(node.at("/matches").size() > 0);
-        
+
     }
-    
+
     public void testSearchWithVCRefNotEqual () throws KustvaktException {
         ClientResponse response = resource().path("search")
                 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
@@ -72,4 +73,22 @@
         assertTrue(node.at("/matches").size() > 0);
     }
 
+    @Test
+    public void testStatisticsWithVCReference () throws KustvaktException {
+        String corpusQuery = "availability = /CC-BY.*/ & referTo named-vc1";
+        ClientResponse response = resource().path("statistics")
+                .queryParam("corpusQuery", corpusQuery)
+                .get(ClientResponse.class);
+
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertEquals(2, node.at("/documents").asInt());
+    }
+
+    @Test
+    public void testStatisticsWithAutoCachingAndVCReference ()
+            throws KustvaktException {
+        testSearchWithVCRefEqual();
+        testStatisticsWithVCReference();
+    }
 }
diff --git a/lite/Changes b/lite/Changes
index 03c57d6..9747ca5 100644
--- a/lite/Changes
+++ b/lite/Changes
@@ -2,6 +2,8 @@
 05/07/2018
     - Added support for unrestricted corpus statistics (ndiewald)
     - Do not pass broken queries to Krill (diewald)
+27/08/2018
+    - Added vc reference test (margaretha)
 
 version 0.60.1
 12/06/2018
diff --git a/lite/pom.xml b/lite/pom.xml
index 103a225..36996fe 100644
--- a/lite/pom.xml
+++ b/lite/pom.xml
@@ -83,9 +83,9 @@
 					<forkCount>2</forkCount>
 					<threadCount>4</threadCount>
 					<argLine>-Xmx512m -XX:MaxPermSize=256m</argLine>
-					<includes>
-						<include>de/ids_mannheim/korap/**/*.java</include>
-					</includes>
+					<excludes>
+						<exclude>**/VCReferenceTest.java</exclude>>
+					</excludes>
 				</configuration>
 			</plugin>
 			<plugin>
diff --git a/lite/src/test/java/de/ids_mannheim/korap/web/service/VCReferenceTest.java b/lite/src/test/java/de/ids_mannheim/korap/web/service/VCReferenceTest.java
new file mode 100644
index 0000000..6de7432
--- /dev/null
+++ b/lite/src/test/java/de/ids_mannheim/korap/web/service/VCReferenceTest.java
@@ -0,0 +1,84 @@
+package de.ids_mannheim.korap.web.service;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.concurrent.ThreadLocalRandom;
+
+import org.junit.Test;
+import org.springframework.web.context.ContextLoaderListener;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
+import com.sun.jersey.test.framework.AppDescriptor;
+import com.sun.jersey.test.framework.JerseyTest;
+import com.sun.jersey.test.framework.WebAppDescriptor;
+import com.sun.jersey.test.framework.spi.container.TestContainerException;
+import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
+import com.sun.jersey.test.framework.spi.container.grizzly.web.GrizzlyWebTestContainerFactory;
+
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.utils.JsonUtils;
+
+public class VCReferenceTest extends JerseyTest{
+
+    public static final String classPackage = "de.ids_mannheim.korap.web.service.light";
+    
+    @Override
+    protected TestContainerFactory getTestContainerFactory ()
+            throws TestContainerException {
+        return new GrizzlyWebTestContainerFactory();
+    }
+
+    @Override
+    protected AppDescriptor configure () {
+        return new WebAppDescriptor.Builder(classPackage)
+                .servletClass(SpringServlet.class)
+                .contextListenerClass(ContextLoaderListener.class)
+                .contextParam("contextConfigLocation",
+                        "classpath:lite-config.xml")
+                .build();
+    }
+
+    @Override
+    protected int getPort (int defaultPort) {
+        int port = ThreadLocalRandom.current().nextInt(5000, 8000 + 1);
+        try {
+            ServerSocket socket = new ServerSocket(port);
+            socket.close();
+        }
+        catch (IOException e) {
+            e.printStackTrace();
+            port = getPort(port);
+        }
+        return port;
+    }
+    
+    @Test
+    public void testSearchWithVCRef () throws KustvaktException {
+        ClientResponse response = resource().path("search")
+                .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
+                .queryParam("cq", "referTo named-vc1")
+                .get(ClientResponse.class);
+
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertTrue(node.at("/matches").size() > 0);
+
+    }
+    
+    @Test
+    public void testStatisticsWithVCReference () throws KustvaktException {
+        String corpusQuery = "referTo named-vc1";
+        ClientResponse response = resource().path("statistics")
+                .queryParam("corpusQuery", corpusQuery)
+                .get(ClientResponse.class);
+
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertEquals(2, node.at("/documents").asInt());
+    }
+}