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();
+    }
 }