Fixed SpringJerseyTest, handled VCRef with username, enabled
VCReferenceTest in test suite.

Change-Id: I1e500b85c010496cae5ed809c4cfa6d7e665cb6a
diff --git a/core/Changes b/core/Changes
index 1b78700..02f361e 100644
--- a/core/Changes
+++ b/core/Changes
@@ -3,6 +3,8 @@
    - Removed log4j loader and old OAuth2 implementation codes(margaretha)
 13/09/2018
    - Updated KoralNode and enforced order of RewriteTask (margaretha)
+14/09/2018
+   - Added a status code for nonunique results (margaretha)
     
 version 0.61.1
 28/08/2018
diff --git a/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java b/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
index 1194bd9..ec2a148 100644
--- a/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
+++ b/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
@@ -26,6 +26,7 @@
     public static final int HTTPS_REQUIRED = 110;
     public static final int INVALID_ALGORITHM = 111;
     public static final int UNSUPPORTED_API_VERSION = 112;
+    public static final int NON_UNIQUE_RESULT_FOUND = 113;
     
     /**
      * 200 status codes general JSON serialization error
diff --git a/full/Changes b/full/Changes
index 252191b..b81509d 100644
--- a/full/Changes
+++ b/full/Changes
@@ -3,6 +3,10 @@
     - Added various log4j2 configurations (margaretha)
 13/09/2018
     - Implemented VirtualCorpusRewrite (margaretha)
+14/09/2018
+    - Fixed SpringJerseyTest ApplicationContext (margaretha)
+    - Handled VCRef with username in VirtualCorpusRewrite (margaretha)
+    - Enabled VCReferenceTest in maven test suite (margaretha)
 
 # version 0.61.1
 28/08/2018
diff --git a/full/pom.xml b/full/pom.xml
index 9a40701..6d851a0 100644
--- a/full/pom.xml
+++ b/full/pom.xml
@@ -3,7 +3,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>de.ids_mannheim.korap</groupId>
 	<artifactId>Kustvakt-full</artifactId>
-	<version>0.61.1</version>
+	<version>0.61.2</version>
 	<properties>
 		<java.version>1.8</java.version>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
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 4bee9be..fc86f52 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
@@ -3,9 +3,11 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.zip.GZIPInputStream;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -32,10 +34,22 @@
     private VirtualCorpusDao vcDao;
     @Autowired
     private VirtualCorpusService vcService;
-    
+
     private static Logger jlog = LogManager.getLogger(NamedVCLoader.class);
 
-    public void loadVCToCache () throws IOException, QueryException, KustvaktException {
+    public void loadVCToCache (String filename, String filePath)
+            throws IOException, QueryException, KustvaktException {
+
+        InputStream is = NamedVCLoader.class.getResourceAsStream(filePath);
+        String json = IOUtils.toString(is, "utf-8");
+        if (json != null) {
+            cacheVC(json, filename);
+            storeVC(filename, json);
+        }
+    }
+
+    public void loadVCToCache ()
+            throws IOException, QueryException, KustvaktException {
 
         String dir = config.getNamedVCPath();
         File d = new File(dir);
@@ -48,45 +62,45 @@
                 throw new IOException("File " + file + " is not found.");
             }
 
-            long start, end;
-            String json;
             String filename = file.getName();
-
-            if (file.getName().endsWith(".jsonld")) {
-                filename = filename.substring(0, filename.length() - 7);
-                start = System.currentTimeMillis();
-                json = FileUtils.readFileToString(file, "utf-8");
-                end = System.currentTimeMillis();
+            String json = readFile(file, filename);
+            if (json != null) {
+                cacheVC(json, filename);
+                storeVC(filename, json);
             }
-            else if (filename.endsWith(".jsonld.gz")) {
-                filename = filename.substring(0, filename.length() - 10);
-                start = System.currentTimeMillis();
-
-                GZIPInputStream gzipInputStream =
-                        new GZIPInputStream(new FileInputStream(file));
-                ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
-                bos.write(gzipInputStream);
-                json = bos.toString("utf-8");
-                bos.close();
-                end = System.currentTimeMillis();
-            }
-            else {
-                System.err.println("File " + filename
-                        + " is not allowed. Filename must ends with .jsonld or .jsonld.gz");
-                continue;
-            }
-            jlog.debug(
-                    "READ " + filename + " duration: " + (end - start));
-
-            cacheVC(json, filename);
-            storeVC(filename, json);
         }
     }
-    
-    private void cacheVC (String json, String filename) throws IOException, QueryException {
+
+    private String readFile (File file, String filename) throws IOException {
+        String json = null;
+        long start = System.currentTimeMillis();
+        if (filename.endsWith(".jsonld")) {
+            filename = filename.substring(0, filename.length() - 7);
+            json = FileUtils.readFileToString(file, "utf-8");
+        }
+        else if (filename.endsWith(".jsonld.gz")) {
+            filename = filename.substring(0, filename.length() - 10);
+            GZIPInputStream gzipInputStream =
+                    new GZIPInputStream(new FileInputStream(file));
+            ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
+            bos.write(gzipInputStream);
+            json = bos.toString("utf-8");
+            bos.close();
+        }
+        else {
+            System.err.println("File " + filename
+                    + " is not allowed. Filename must ends with .jsonld or .jsonld.gz");
+        }
+        long end = System.currentTimeMillis();
+        jlog.debug("READ " + filename + " duration: " + (end - start));
+        return json;
+    }
+
+    private void cacheVC (String json, String filename)
+            throws IOException, QueryException {
         long start, end;
         start = System.currentTimeMillis();
-        
+
         KrillCollection collection = new KrillCollection(json);
         collection.setIndex(searchKrill.getIndex());
 
@@ -99,16 +113,17 @@
                 + KrillCollection.cache.calculateInMemorySize());
     }
 
-    private void storeVC (String name, String koralQuery) throws KustvaktException {
+    private void storeVC (String name, String koralQuery)
+            throws KustvaktException {
         if (!VirtualCorpusService.wordPattern.matcher(name).matches()) {
             throw new KustvaktException(StatusCodes.INVALID_ARGUMENT,
                     "Virtual corpus name must only contains letters, numbers, "
                             + "underscores, hypens and spaces",
                     name);
         }
-        CorpusAccess requiredAccess = vcService.determineRequiredAccess(koralQuery);
+        CorpusAccess requiredAccess =
+                vcService.determineRequiredAccess(koralQuery);
         vcDao.createVirtualCorpus(name, VirtualCorpusType.SYSTEM,
-                requiredAccess, koralQuery, null,
-                null, null, true, "system");
+                requiredAccess, koralQuery, null, null, null, true, "system");
     }
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/dao/VirtualCorpusDao.java b/full/src/main/java/de/ids_mannheim/korap/dao/VirtualCorpusDao.java
index c8006aa..ab4fbb8 100644
--- a/full/src/main/java/de/ids_mannheim/korap/dao/VirtualCorpusDao.java
+++ b/full/src/main/java/de/ids_mannheim/korap/dao/VirtualCorpusDao.java
@@ -8,6 +8,7 @@
 
 import javax.persistence.EntityManager;
 import javax.persistence.NoResultException;
+import javax.persistence.NonUniqueResultException;
 import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
 import javax.persistence.criteria.CriteriaBuilder;
@@ -211,6 +212,13 @@
                             + vcCode,
                     String.valueOf(vcCode), e);
         }
+        catch (NonUniqueResultException e) {
+            String vcCode = createdBy + "/" + vcName;
+            throw new KustvaktException(StatusCodes.NON_UNIQUE_RESULT_FOUND,
+                    "Non unique result found for query: retrieve virtual corpus by name "
+                            + vcCode,
+                    String.valueOf(vcCode), e);
+        }
         return vc;
     }
 
diff --git a/full/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java b/full/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
index ef7ec7d..86d11c5 100644
--- a/full/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
+++ b/full/src/main/java/de/ids_mannheim/korap/rewrite/VirtualCorpusRewrite.java
@@ -14,6 +14,7 @@
 import de.ids_mannheim.korap.resource.rewrite.RewriteTask;
 import de.ids_mannheim.korap.service.VirtualCorpusService;
 import de.ids_mannheim.korap.user.User;
+import de.ids_mannheim.korap.util.StatusCodes;
 import de.ids_mannheim.korap.utils.JsonUtils;
 
 @Component
@@ -36,24 +37,24 @@
         if (koralNode.has("@type")
                 && koralNode.get("@type").equals("koral:docGroupRef")) {
             if (!koralNode.has("ref")) {
-                // throw error
+                throw new KustvaktException(StatusCodes.MISSING_VC_REFERENCE,
+                        "ref is not found");
             }
             else {
                 String vcName = koralNode.get("ref");
+                String vcOwner = "system";
+                if (vcName.contains("/")) {
+                    String[] names = vcName.split("/");
+                    if (names.length == 2) {
+                        vcOwner = names[0];
+                        vcName = names[1];
+                    }
+                }
 
                 VirtualCorpus vc =
-                        vcService.searchVCByName(username, vcName, "system");
+                        vcService.searchVCByName(username, vcName, vcOwner);
                 if (!vc.isCached()) {
-                    String koralQuery = vc.getKoralQuery();
-                    JsonNode kq =
-                            JsonUtils.readTree(koralQuery).at("/collection");
-                    JsonNode jsonNode = koralNode.rawNode();
-                    // rewrite
-                    koralNode.remove("@type", new RewriteIdentifier("@type",
-                            jsonNode.at("/@type").asText()));
-                    koralNode.remove("ref", new RewriteIdentifier("ref",
-                            jsonNode.at("/ref").asText()));
-                    koralNode.setAll((ObjectNode) kq);
+                    rewriteVC(vc, koralNode);
                 }
             }
 
@@ -69,4 +70,17 @@
 
         }
     }
+
+    private void rewriteVC (VirtualCorpus vc, KoralNode koralNode)
+            throws KustvaktException {
+        String koralQuery = vc.getKoralQuery();
+        JsonNode kq = JsonUtils.readTree(koralQuery).at("/collection");
+        JsonNode jsonNode = koralNode.rawNode();
+        // rewrite
+        koralNode.remove("@type",
+                new RewriteIdentifier("@type", jsonNode.at("/@type").asText()));
+        koralNode.remove("ref",
+                new RewriteIdentifier("ref", jsonNode.at("/ref").asText()));
+        koralNode.setAll((ObjectNode) kq);
+    }
 }
diff --git a/full/src/test/java/de/ids_mannheim/korap/config/NamedVCLoaderTest.java b/full/src/test/java/de/ids_mannheim/korap/config/NamedVCLoaderTest.java
index b5b218a..3fef08f 100644
--- a/full/src/test/java/de/ids_mannheim/korap/config/NamedVCLoaderTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/config/NamedVCLoaderTest.java
@@ -10,26 +10,36 @@
 
 import de.ids_mannheim.korap.KrillCollection;
 import de.ids_mannheim.korap.collection.CachedVCData;
+import de.ids_mannheim.korap.dao.VirtualCorpusDao;
+import de.ids_mannheim.korap.entity.VirtualCorpus;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.util.QueryException;
+import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.Element;
 
-public class NamedVCLoaderTest extends SpringJerseyTest{
+public class NamedVCLoaderTest extends SpringJerseyTest {
 
     @Autowired
     private NamedVCLoader vcLoader;
-    
-    
+    @Autowired
+    private VirtualCorpusDao dao;
+
     @Test
-    public void testNamedVCLoader () throws IOException, QueryException, KustvaktException {
+    public void testNamedVCLoader ()
+            throws IOException, QueryException, KustvaktException {
+        KrillCollection.cache = CacheManager.newInstance().getCache("named_vc");
         Element element = KrillCollection.cache.get("named-vc1");
-        assertTrue(element==null);
-        
-        vcLoader.loadVCToCache();
+        assertTrue(element == null);
+
+        vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld");
 
         element = KrillCollection.cache.get("named-vc1");
         assertNotNull(element);
         CachedVCData cachedData = (CachedVCData) element.getObjectValue();
         assertTrue(cachedData.getDocIdMap().size() > 0);
+        
+        KrillCollection.cache.removeAll();
+        VirtualCorpus vc = dao.retrieveVCByName("named-vc1", "system");
+        dao.deleteVirtualCorpus(vc);
     }
 }
diff --git a/full/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java b/full/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java
index 36902b4..959dce2 100644
--- a/full/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/config/SpringJerseyTest.java
@@ -5,14 +5,11 @@
 import java.util.concurrent.ThreadLocalRandom;
 
 import org.junit.runner.RunWith;
-import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
-import org.springframework.beans.factory.support.DefaultListableBeanFactory;
 import org.springframework.context.support.GenericApplicationContext;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.web.context.support.AbstractRefreshableWebApplicationContext;
+import org.springframework.web.context.support.GenericWebApplicationContext;
 
 import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
 import com.sun.jersey.test.framework.AppDescriptor;
@@ -27,7 +24,7 @@
 public abstract class SpringJerseyTest extends JerseyTest {
 
     public final static String API_VERSION = "v1.0";
-    
+
     @Autowired
     protected GenericApplicationContext applicationContext;
 
@@ -36,7 +33,6 @@
                     "de.ids_mannheim.korap.web.filter",
                     "de.ids_mannheim.korap.web.utils" };
 
-
     @Override
     protected TestContainerFactory getTestContainerFactory ()
             throws TestContainerException {
@@ -46,27 +42,13 @@
     @Override
     public void setUp () throws Exception {
 
-        StaticContextLoaderListener.applicationContext =
-                new AbstractRefreshableWebApplicationContext() {
+        GenericWebApplicationContext genericContext =
+                new GenericWebApplicationContext();
 
-                    ConfigurableListableBeanFactory existingBeanFactory =
-                            applicationContext.getBeanFactory();
+        genericContext.setParent(this.applicationContext);
+        genericContext.setClassLoader(this.applicationContext.getClassLoader());
 
-                    @Override
-                    protected void loadBeanDefinitions (
-                            DefaultListableBeanFactory beanFactory)
-                            throws BeansException, IOException {
-
-                        String[] beanDefinitionNames =
-                                existingBeanFactory.getBeanDefinitionNames();
-                        for (String beanName : beanDefinitionNames) {
-                            beanFactory.registerBeanDefinition(beanName,
-                                    existingBeanFactory
-                                            .getBeanDefinition(beanName));
-                        }
-                    }
-                };
-
+        StaticContextLoaderListener.applicationContext = genericContext;
         super.setUp();
     }
 
@@ -80,7 +62,7 @@
                 // "classpath:test-config.xml")
                 .build();
     }
-    
+
     @Override
     protected int getPort (int defaultPort) {
         int port = ThreadLocalRandom.current().nextInt(5000, 8000 + 1);
diff --git a/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/VirtualCorpusRewriteTest.java b/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/VirtualCorpusRewriteTest.java
index a388ae1..04931d8 100644
--- a/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/VirtualCorpusRewriteTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/resource/rewrite/VirtualCorpusRewriteTest.java
@@ -1,18 +1,28 @@
 package de.ids_mannheim.korap.resource.rewrite;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
 
 import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.google.common.net.HttpHeaders;
 import com.sun.jersey.api.client.ClientResponse;
 
+import de.ids_mannheim.korap.KrillCollection;
 import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
 import de.ids_mannheim.korap.config.Attributes;
+import de.ids_mannheim.korap.config.NamedVCLoader;
 import de.ids_mannheim.korap.config.SpringJerseyTest;
+import de.ids_mannheim.korap.dao.VirtualCorpusDao;
+import de.ids_mannheim.korap.entity.VirtualCorpus;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.util.QueryException;
 import de.ids_mannheim.korap.utils.JsonUtils;
+import net.sf.ehcache.CacheManager;
 
 /**
  * @author margaretha
@@ -20,8 +30,36 @@
  */
 public class VirtualCorpusRewriteTest extends SpringJerseyTest {
 
+    @Autowired
+    private NamedVCLoader vcLoader;
+    @Autowired
+    private VirtualCorpusDao dao;
+    
     @Test
-    public void testSearchFreeWithVCRef () throws KustvaktException {
+    public void testNoRewriteWithCachedVCRef ()
+            throws KustvaktException, IOException, QueryException {
+        KrillCollection.cache = CacheManager.newInstance().getCache("named_vc");
+        vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld");
+
+        ClientResponse response = resource().path(API_VERSION).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);
+        node = node.at("/collection");
+
+        assertEquals("koral:docGroup", node.at("/@type").asText());
+        assertTrue(node.at("/operands/1/rewrites").isMissingNode());
+        
+        KrillCollection.cache.removeAll();
+        VirtualCorpus vc = dao.retrieveVCByName("named-vc1", "system");
+        dao.deleteVirtualCorpus(vc);
+    }
+    
+    @Test
+    public void testRewriteFreeAndSystemVCRef () throws KustvaktException, Exception {
         ClientResponse response = resource().path(API_VERSION).path("search")
                 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
                 .queryParam("cq", "referTo \"system VC\"")
@@ -32,18 +70,24 @@
         node = node.at("/collection");
 
         assertEquals("koral:docGroup", node.at("/@type").asText());
-        assertEquals(3, node.at("/operands/1/rewrites").size());
+        assertEquals("koral:doc", node.at("/operands/0/@type").asText());
+        
+        assertEquals("koral:doc", node.at("/operands/1/@type").asText());
+        assertEquals("GOE", node.at("/operands/1/value").asText());
+        assertEquals("corpusSigle", node.at("/operands/1/key").asText());
+
         node = node.at("/operands/1/rewrites");
+        assertEquals(3, node.size());
         assertEquals("operation:deletion", node.at("/0/operation").asText());
         assertEquals("operation:deletion", node.at("/1/operation").asText());
         assertEquals("operation:insertion", node.at("/2/operation").asText());
     }
 
     @Test
-    public void testSearchPubWithVCRef () throws KustvaktException {
+    public void testRewritePubAndSystemVCRef () throws KustvaktException {
         ClientResponse response = resource().path(API_VERSION).path("search")
                 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
-                .queryParam("cq", "referTo \"system VC\"")
+                .queryParam("cq", "referTo \"system/system VC\"")
                 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
                         .createBasicAuthorizationHeaderValue("user", "pass"))
                 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
@@ -54,9 +98,31 @@
         node = node.at("/collection");
         assertEquals("koral:docGroup", node.at("/@type").asText());
         assertEquals("koral:docGroup", node.at("/operands/0/@type").asText());
-        assertEquals(3, node.at("/operands/1/rewrites").size());
-        assertEquals("koral:doc", node.at("/operands/1/@type").asText());
-        assertEquals("GOE", node.at("/operands/1/value").asText());
-        assertEquals("corpusSigle", node.at("/operands/1/key").asText());
+        
+        node = node.at("/operands/1/rewrites");
+        assertEquals(3, node.size());
+        assertEquals("operation:deletion", node.at("/0/operation").asText());
+        assertEquals("operation:deletion", node.at("/1/operation").asText());
+        assertEquals("operation:insertion", node.at("/2/operation").asText());
+    }
+
+    @Test
+    public void testRewriteWithDoryVCRef ()
+            throws KustvaktException, IOException, QueryException {
+
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .queryParam("q", "Fisch").queryParam("ql", "poliqarp")
+                .queryParam("cq", "referTo \"dory/dory VC\"")
+                .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+                        .createBasicAuthorizationHeaderValue("dory", "pass"))
+                .get(ClientResponse.class);
+
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        node = node.at("/collection");
+//        System.out.println(node);
+        assertEquals("koral:docGroup", node.at("/@type").asText());
+        node = node.at("/operands/1/rewrites");
+        assertEquals(3, node.size());
     }
 }
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java
index c7f06e1..1c59d76 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java
@@ -382,6 +382,8 @@
 
         testAccessTokenAfterUpgradingClient(clientId, accessToken);
         testAccessTokenAfterDegradingSuperClient(clientId, accessToken);
+        
+        testDeregisterConfidentialClient(clientId, clientSecret);
     }
 
     // old access tokens retain their scopes
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 4ce02ff..55f289d 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,38 +1,56 @@
 package de.ids_mannheim.korap.web.controller;
 
 import static org.junit.Assert.assertEquals;
-
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
 
-import org.junit.Ignore;
 import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.sun.jersey.api.client.ClientResponse;
 
+import de.ids_mannheim.korap.KrillCollection;
+import de.ids_mannheim.korap.config.NamedVCLoader;
 import de.ids_mannheim.korap.config.SpringJerseyTest;
+import de.ids_mannheim.korap.dao.VirtualCorpusDao;
+import de.ids_mannheim.korap.entity.VirtualCorpus;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.util.QueryException;
 import de.ids_mannheim.korap.utils.JsonUtils;
+import net.sf.ehcache.CacheManager;
 
 public class VCReferenceTest extends SpringJerseyTest {
 
-    // auto caching is disabled in krill
-    @Ignore
+    @Autowired
+    private NamedVCLoader vcLoader;
+    @Autowired
+    private VirtualCorpusDao dao;
+
     @Test
     public void testVCRef ()
             throws KustvaktException, IOException, QueryException {
         testSearchWithoutVCRefOr();
         testSearchWithoutVCRefAnd();
 
-        // auto caching
+        KrillCollection.cache = CacheManager.newInstance().getCache("named_vc");
+        vcLoader.loadVCToCache("named-vc1", "/vc/named-vc1.jsonld");
         testStatisticsWithVCReference();
+
+        // TODO: test auto-caching (disabled in krill)
+        vcLoader.loadVCToCache("named-vc2", "/vc/named-vc2.jsonld");
         testSearchWithVCRefNotEqual();
+
         // retrieve from cache
         testSearchWithVCRefEqual();
         testSearchWithVCRefNotEqual();
+
+        KrillCollection.cache.removeAll();
+        VirtualCorpus vc = dao.retrieveVCByName("named-vc1", "system");
+        dao.deleteVirtualCorpus(vc);
+        vc = dao.retrieveVCByName("named-vc2", "system");
+        dao.deleteVirtualCorpus(vc);
     }
 
     private void testSearchWithoutVCRefOr () throws KustvaktException {
diff --git a/full/src/test/resources/vc/named-vc1.jsonld b/full/src/test/resources/vc/named-vc1.jsonld
new file mode 100644
index 0000000..ec25031
--- /dev/null
+++ b/full/src/test/resources/vc/named-vc1.jsonld
@@ -0,0 +1,10 @@
+{"collection": {
+    "@type": "koral:doc",
+    "key": "textSigle",
+    "match": "match:eq",
+    "type" : "type:string",
+    "value": [
+        "GOE/AGF/00000",
+        "GOE/AGA/01784"
+    ]
+}}
diff --git a/full/src/test/resources/vc/named-vc2.jsonld b/full/src/test/resources/vc/named-vc2.jsonld
new file mode 100644
index 0000000..a79d6e5
--- /dev/null
+++ b/full/src/test/resources/vc/named-vc2.jsonld
@@ -0,0 +1,10 @@
+{"collection": {
+    "@type": "koral:doc",
+    "key": "textSigle",
+    "match": "match:ne",
+    "type" : "type:string",
+    "value": [
+        "GOE/AGI/04846",
+        "GOE/AGA/01784"
+    ]
+}}