Fixed testing expired access token.

Change-Id: Iddd45837c25779001272fa27c8df9ea344c0df9b
diff --git a/full/Changes b/full/Changes
index ce46822..b961e0d 100644
--- a/full/Changes
+++ b/full/Changes
@@ -9,7 +9,8 @@
 - Make sure totalResults and other numeric results are numbers (Kupietz)
 - Introduce CQP query language in Koral 0.41
 - Changed DB connection (fixed #670)
-- Resolved duplicate libraries.
+- Resolved duplicate libraries
+- Fixed testing expiry access token
 
 # version 0.70.1
 
diff --git a/full/src/main/java/de/ids_mannheim/korap/oauth2/dao/AccessTokenDao.java b/full/src/main/java/de/ids_mannheim/korap/oauth2/dao/AccessTokenDao.java
index 6d8dd43..ac32e87 100644
--- a/full/src/main/java/de/ids_mannheim/korap/oauth2/dao/AccessTokenDao.java
+++ b/full/src/main/java/de/ids_mannheim/korap/oauth2/dao/AccessTokenDao.java
@@ -91,6 +91,11 @@
         entityManager.persist(accessToken);
     }
 
+    public void storeAccessToken (AccessToken accessToken) 
+                    throws KustvaktException {
+        ParameterChecker.checkObjectValue(accessToken, "access token");
+        entityManager.persist(accessToken);
+    }
     public AccessToken updateAccessToken (AccessToken accessToken)
             throws KustvaktException {
         ParameterChecker.checkObjectValue(accessToken, "access_token");
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2AdminControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2AdminControllerTest.java
index 75e29e6..c8d15b9 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2AdminControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2AdminControllerTest.java
@@ -88,7 +88,9 @@
     }
 
     @Test
-    public void testCleanExpiredTokensUsingAdminToken () {
+    public void testCleanExpiredTokensUsingAdminToken () throws KustvaktException {
+        createExpiredAccessToken();
+        
         int refreshTokensBefore =
                 refreshDao.retrieveInvalidRefreshTokens().size();
         assertTrue(refreshTokensBefore > 0);
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2PluginTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2PluginTest.java
index 5e33841..bcce74f 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2PluginTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2PluginTest.java
@@ -15,7 +15,6 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.ws.rs.ProcessingException;
 import javax.ws.rs.client.Entity;
@@ -48,9 +47,6 @@
     @Autowired
     private InstalledPluginDao pluginDao;
     
-    private final AtomicBoolean testFailed = new AtomicBoolean(false);
-
-
     @Test
     public void testRegisterPlugin () throws
             ProcessingException, KustvaktException {
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2TestBase.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2TestBase.java
index f2559ae..351491b 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2TestBase.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2TestBase.java
@@ -6,6 +6,10 @@
 
 import java.io.IOException;
 import java.net.URI;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.util.HashSet;
+import java.util.Set;
 
 import javax.ws.rs.ProcessingException;
 import javax.ws.rs.client.Client;
@@ -33,11 +37,18 @@
 import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
 import de.ids_mannheim.korap.config.Attributes;
 import de.ids_mannheim.korap.config.SpringJerseyTest;
+import de.ids_mannheim.korap.constant.OAuth2Scope;
+import de.ids_mannheim.korap.encryption.RandomCodeGenerator;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.exceptions.StatusCodes;
 import de.ids_mannheim.korap.oauth2.constant.OAuth2ClientType;
 import de.ids_mannheim.korap.oauth2.constant.OAuth2Error;
+import de.ids_mannheim.korap.oauth2.dao.AccessTokenDao;
+import de.ids_mannheim.korap.oauth2.dao.OAuth2ClientDao;
 import de.ids_mannheim.korap.oauth2.dao.RefreshTokenDao;
+import de.ids_mannheim.korap.oauth2.entity.AccessScope;
+import de.ids_mannheim.korap.oauth2.entity.AccessToken;
+import de.ids_mannheim.korap.oauth2.entity.OAuth2Client;
 import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.utils.TimeUtils;
 import de.ids_mannheim.korap.web.input.OAuth2ClientJson;
@@ -52,6 +63,12 @@
 public abstract class OAuth2TestBase extends SpringJerseyTest {
 
     @Autowired
+    private AccessTokenDao tokenDao;
+    @Autowired
+    private OAuth2ClientDao clientDao;
+    @Autowired
+    private RandomCodeGenerator codeGenerator;
+    @Autowired
     protected RefreshTokenDao refreshTokenDao;
 
     protected String publicClientId = "8bIDtZnH6NvRkW2Fq";
@@ -461,4 +478,27 @@
         assertEquals("application/json;charset=utf-8", contentType);
         assertEquals(Status.BAD_REQUEST.getStatusCode(), status);
     }
+    
+    protected String createExpiredAccessToken () throws KustvaktException {
+        String authToken = codeGenerator.createRandomCode();
+        
+        // create new access token
+        OAuth2Client client = clientDao.retrieveClientById(publicClientId);
+
+        ZonedDateTime now =
+                ZonedDateTime.now(ZoneId.of(Attributes.DEFAULT_TIME_ZONE));
+        Set<AccessScope> scopes = new HashSet<>();
+        scopes.add(new AccessScope(OAuth2Scope.EDIT_VC));
+        
+        AccessToken accessToken = new AccessToken();
+        accessToken.setCreatedDate(now.minusSeconds(5));
+        accessToken.setExpiryDate(now.minusSeconds(3));
+        accessToken.setToken(authToken);
+        accessToken.setScopes(scopes);
+        accessToken.setUserId("marlin");
+        accessToken.setClient(client);
+        accessToken.setUserAuthenticationTime(now.minusSeconds(5));
+        tokenDao.storeAccessToken(accessToken);
+        return authToken;
+    }
 }
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java
index 4106a7a..47e80ec 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java
@@ -159,8 +159,9 @@
         String json = "{\"type\": \"PRIVATE\","
                 + "\"corpusQuery\": \"corpusSigle=GOE\"}";
 
-        String authToken = "fia0123ikBWn931470H8s5gRqx7Moc4p";
-
+        //String authToken = "fia0123ikBWn931470H8s5gRqx7Moc4p";
+        String authToken = createExpiredAccessToken();
+        
         Response response = target().path(API_VERSION).path("vc")
                 .path("~marlin").path("new_vc")
                 .request()