Updated Krill version & added controller tests with OAuth2 access token.

Change-Id: Ib26d297a1446e5a4a6d6f4c5035b4e0a0e5f3fb9
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/AvailabilityTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/AvailabilityTest.java
index 927e3f0..98502ea 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/AvailabilityTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/AvailabilityTest.java
@@ -169,14 +169,14 @@
 
 
 
-    private ClientResponse builtSimpleClientResponse (String collectionQuery) {
+    private ClientResponse searchQuery (String collectionQuery) {
         return resource().path("search").queryParam("q", "[orth=das]")
                 .queryParam("ql", "poliqarp").queryParam("cq", collectionQuery)
                 .get(ClientResponse.class);
     }
 
 
-    private ClientResponse builtClientResponseWithIP (String collectionQuery,
+    private ClientResponse searchQueryWithIP (String collectionQuery,
             String ip) throws UniformInterfaceException, ClientHandlerException,
             KustvaktException {
         return resource().path("search").queryParam("q", "[orth=das]")
@@ -192,7 +192,7 @@
     @Test
     public void testAvailabilityFreeAuthorized () throws KustvaktException {
         ClientResponse response =
-                builtSimpleClientResponse("availability = CC-BY-SA");
+                searchQuery("availability = CC-BY-SA");
 
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -205,7 +205,7 @@
     public void testAvailabilityRegexFreeAuthorized ()
             throws KustvaktException {
         ClientResponse response =
-                builtSimpleClientResponse("availability = /.*BY.*/");
+                searchQuery("availability = /.*BY.*/");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
 
@@ -216,7 +216,7 @@
     @Test
     public void testAvailabilityFreeUnauthorized () throws KustvaktException {
         ClientResponse response =
-                builtSimpleClientResponse("availability = ACA-NC");
+                searchQuery("availability = ACA-NC");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
 
@@ -228,7 +228,7 @@
     public void testAvailabilityRegexFreeUnauthorized ()
             throws KustvaktException {
         ClientResponse response =
-                builtSimpleClientResponse("availability = /ACA.*/");
+                searchQuery("availability = /ACA.*/");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
 
@@ -237,7 +237,7 @@
 
     @Test
     public void testAvailabilityRegexNoRewrite () throws KustvaktException {
-        ClientResponse response = builtSimpleClientResponse(
+        ClientResponse response = searchQuery(
                 "availability = /CC-BY.*/ & availability = /ACA.*/");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -266,7 +266,7 @@
     public void testAvailabilityRegexFreeUnauthorized3 ()
             throws KustvaktException {
         ClientResponse response =
-                builtSimpleClientResponse("availability = /.*NC.*/");
+                searchQuery("availability = /.*NC.*/");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
         //        System.out.println(response.getEntity(String.class));
@@ -279,7 +279,7 @@
     public void testNegationAvailabilityFreeUnauthorized ()
             throws KustvaktException {
         ClientResponse response =
-                builtSimpleClientResponse("availability != /CC-BY.*/");
+                searchQuery("availability != /CC-BY.*/");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
 
@@ -290,7 +290,7 @@
     public void testNegationAvailabilityFreeUnauthorized2 ()
             throws KustvaktException {
         ClientResponse response =
-                builtSimpleClientResponse("availability != /.*BY.*/");
+                searchQuery("availability != /.*BY.*/");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
 
@@ -300,7 +300,7 @@
     @Test
     public void testNegationAvailabilityWithOperationOrUnauthorized ()
             throws KustvaktException {
-        ClientResponse response = builtSimpleClientResponse(
+        ClientResponse response = searchQuery(
                 "availability = /CC-BY.*/ | availability != /CC-BY.*/");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -310,7 +310,7 @@
     @Test
     public void testComplexNegationAvailabilityFreeUnauthorized ()
             throws KustvaktException {
-        ClientResponse response = builtSimpleClientResponse(
+        ClientResponse response = searchQuery(
                 "textClass=politik & availability != /CC-BY.*/");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -322,7 +322,7 @@
     @Test
     public void testComplexAvailabilityFreeUnauthorized ()
             throws KustvaktException {
-        ClientResponse response = builtSimpleClientResponse(
+        ClientResponse response = searchQuery(
                 "textClass=politik & availability=ACA-NC");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -334,7 +334,7 @@
     @Test
     public void testComplexAvailabilityFreeUnauthorized3 ()
             throws KustvaktException {
-        ClientResponse response = builtSimpleClientResponse(
+        ClientResponse response = searchQuery(
                 "textClass=politik & availability=/.*NC.*/");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -346,7 +346,7 @@
     @Test
     public void testAvailabilityPublicAuthorized () throws KustvaktException {
         ClientResponse response =
-                builtClientResponseWithIP("availability=ACA-NC", "149.27.0.32");
+                searchQueryWithIP("availability=ACA-NC", "149.27.0.32");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
 
@@ -356,7 +356,7 @@
 
     @Test
     public void testAvailabilityPublicUnauthorized () throws KustvaktException {
-        ClientResponse response = builtClientResponseWithIP(
+        ClientResponse response = searchQueryWithIP(
                 "availability=QAO-NC-LOC:ids", "149.27.0.32");
 
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
@@ -369,7 +369,7 @@
     @Test
     public void testAvailabilityRegexPublicAuthorized ()
             throws KustvaktException {
-        ClientResponse response = builtClientResponseWithIP(
+        ClientResponse response = searchQueryWithIP(
                 "availability= /ACA.*/", "149.27.0.32");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -381,7 +381,7 @@
     @Test
     public void testNegationAvailabilityPublicUnauthorized ()
             throws KustvaktException {
-        ClientResponse response = builtClientResponseWithIP(
+        ClientResponse response = searchQueryWithIP(
                 "availability != ACA-NC", "149.27.0.32");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -393,7 +393,7 @@
     @Test
     public void testNegationAvailabilityRegexPublicUnauthorized ()
             throws KustvaktException {
-        ClientResponse response = builtClientResponseWithIP(
+        ClientResponse response = searchQueryWithIP(
                 "availability != /ACA.*/", "149.27.0.32");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -405,7 +405,7 @@
     @Test
     public void testComplexAvailabilityPublicUnauthorized ()
             throws KustvaktException {
-        ClientResponse response = builtClientResponseWithIP(
+        ClientResponse response = searchQueryWithIP(
                 "textClass=politik & availability=QAO-NC-LOC:ids",
                 "149.27.0.32");
 
@@ -419,7 +419,7 @@
     @Test
     public void testNegationComplexAvailabilityPublicUnauthorized ()
             throws KustvaktException {
-        ClientResponse response = builtClientResponseWithIP(
+        ClientResponse response = searchQueryWithIP(
                 "textClass=politik & availability!=QAO-NC-LOC:ids",
                 "149.27.0.32");
 
@@ -431,7 +431,7 @@
 
     @Test
     public void testAvailabilityRegexAllAuthorized () throws KustvaktException {
-        ClientResponse response = builtClientResponseWithIP(
+        ClientResponse response = searchQueryWithIP(
                 "availability= /ACA.*/", "10.27.0.32");
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -441,7 +441,7 @@
 
     @Test
     public void testAvailabilityOr () throws KustvaktException {
-        ClientResponse response = builtSimpleClientResponse(
+        ClientResponse response = searchQuery(
                 "availability=/CC-BY.*/ | availability=/ACA.*/");
 
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
@@ -452,7 +452,7 @@
 
     @Test
     public void testRedundancyOrPub () throws KustvaktException {
-        ClientResponse response = builtClientResponseWithIP(
+        ClientResponse response = searchQueryWithIP(
                 "availability=/CC-BY.*/ | availability=/ACA.*/ | availability=/QAO-NC/",
                 "149.27.0.32");
 
@@ -467,7 +467,7 @@
 
     @Test
     public void testAvailabilityOrCorpusSigle () throws KustvaktException {
-        ClientResponse response = builtSimpleClientResponse(
+        ClientResponse response = searchQuery(
                 "availability=/CC-BY.*/ | corpusSigle=GOE");
 
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
@@ -478,7 +478,7 @@
 
     @Test
     public void testOrWithoutAvailability () throws KustvaktException {
-        ClientResponse response = builtSimpleClientResponse(
+        ClientResponse response = searchQuery(
                 "corpusSigle=GOE | textClass=politik");
 
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
@@ -489,7 +489,7 @@
 
     @Test
     public void testWithoutAvailability () throws KustvaktException {
-        ClientResponse response = builtSimpleClientResponse("corpusSigle=GOE");
+        ClientResponse response = searchQuery("corpusSigle=GOE");
 
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2AccessTokenTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2AccessTokenTest.java
new file mode 100644
index 0000000..00b1eb6
--- /dev/null
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2AccessTokenTest.java
@@ -0,0 +1,108 @@
+package de.ids_mannheim.korap.web.controller;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.net.HttpHeaders;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.ClientResponse.Status;
+
+import de.ids_mannheim.korap.config.Attributes;
+import de.ids_mannheim.korap.config.SpringJerseyTest;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
+import de.ids_mannheim.korap.utils.JsonUtils;
+
+public class OAuth2AccessTokenTest extends SpringJerseyTest {
+
+    // test access token for username: dory
+    private static String testAccessToken;
+
+    @BeforeClass
+    public static void init () throws IOException {
+        InputStream is = OAuth2AccessTokenTest.class.getClassLoader()
+                .getResourceAsStream("test-oauth2.token");
+
+        try (BufferedReader reader =
+                new BufferedReader(new InputStreamReader(is));) {
+            testAccessToken = reader.readLine();
+        }
+    }
+
+    @Test
+    public void testListVC () throws KustvaktException {
+        ClientResponse response = resource().path("vc").path("list")
+                .header(Attributes.AUTHORIZATION, "Bearer " + testAccessToken)
+                .get(ClientResponse.class);
+
+        assertEquals(Status.OK.getStatusCode(), response.getStatus());
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+        assertEquals(4, node.size());
+    }
+
+    @Test
+    public void testSearchWithOAuth2Token ()
+            throws KustvaktException, IOException {
+        ClientResponse response = resource().path("search")
+                .queryParam("q", "Wasser").queryParam("ql", "poliqarp")
+                .header(Attributes.AUTHORIZATION, "Bearer " + testAccessToken)
+                .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
+                .get(ClientResponse.class);
+
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertEquals(25, node.at("/matches").size());
+    }
+
+    @Test
+    public void testSearchWithUnknownToken ()
+            throws KustvaktException, IOException {
+        ClientResponse response = resource().path("search")
+                .queryParam("q", "Wasser").queryParam("ql", "poliqarp")
+                .header(Attributes.AUTHORIZATION,
+                        "Bearer ljsa8tKNRSczJhk20öhq92zG8z350")
+                .get(ClientResponse.class);
+
+        assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
+                response.getStatus());
+
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertEquals(StatusCodes.INVALID_ACCESS_TOKEN,
+                node.at("/errors/0/0").asInt());
+        assertEquals("Access token is not found", node.at("/errors/0/1").asText());
+    }
+
+    @Test
+    public void testSearchWithExpiredToken ()
+            throws KustvaktException, IOException {
+        ClientResponse response = resource().path("search")
+                .queryParam("q", "Wasser").queryParam("ql", "poliqarp")
+                .header(Attributes.AUTHORIZATION,
+                        "Bearer fia0123ikBWn931470H8s5gRqx7Moc4p")
+                .get(ClientResponse.class);
+
+        String ent = response.getEntity(String.class);
+        
+        assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
+                response.getStatus());
+
+        JsonNode node = JsonUtils.readTree(ent);
+        assertEquals(StatusCodes.EXPIRED, node.at("/errors/0/0").asInt());
+        assertEquals("Access token is expired", node.at("/errors/0/1").asText());
+    }
+}
diff --git a/full/src/test/resources/test-config.xml b/full/src/test/resources/test-config.xml
index 964487a..75edbc4 100644
--- a/full/src/test/resources/test-config.xml
+++ b/full/src/test/resources/test-config.xml
@@ -252,6 +252,10 @@
 		<constructor-arg type="de.ids_mannheim.korap.interfaces.EncryptionIface"
 			ref="kustvakt_encryption" />
 	</bean>
+	
+	<bean id="oauth2_auth"
+		class="de.ids_mannheim.korap.authentication.OAuth2Authentication" />
+	
 
 	<util:list id="kustvakt_authproviders"
 		value-type="de.ids_mannheim.korap.interfaces.AuthenticationIface">
@@ -260,6 +264,7 @@
 		<ref bean="session_auth" />
 		<!-- <ref bean="api_auth" /> -->
 		<ref bean="openid_auth" />
+		<ref bean="oauth2_auth" />
 	</util:list>
 
 
diff --git a/full/src/test/resources/test-oauth2.token b/full/src/test/resources/test-oauth2.token
new file mode 100644
index 0000000..eb7b4af
--- /dev/null
+++ b/full/src/test/resources/test-oauth2.token
@@ -0,0 +1 @@
+249c64a77f40e2b5504982cc5521b596
\ No newline at end of file