Fixed cached-VC ref with username.

Change-Id: Ifa02ac84ec83e9cf1f6530848e71b5a31590b715
diff --git a/full/Changes b/full/Changes
index 91f4520..51705e0 100644
--- a/full/Changes
+++ b/full/Changes
@@ -10,6 +10,8 @@
 18/09/2018
     - Handled unique constraints (margaretha)
     - Added a controller for editing user group member roles (margaretha)
+19/09/2018
+    - Fixed cached-VC ref with username (margaretha)
 
 # version 0.61.1
 28/08/2018
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 86d11c5..2154c47 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
@@ -43,11 +43,13 @@
             else {
                 String vcName = koralNode.get("ref");
                 String vcOwner = "system";
+                boolean ownerExist = false;
                 if (vcName.contains("/")) {
                     String[] names = vcName.split("/");
                     if (names.length == 2) {
                         vcOwner = names[0];
                         vcName = names[1];
+                        ownerExist = true;
                     }
                 }
 
@@ -56,6 +58,9 @@
                 if (!vc.isCached()) {
                     rewriteVC(vc, koralNode);
                 }
+                else if (ownerExist) {
+                    removeOwner(vc.getKoralQuery(), vcOwner, koralNode);
+                }
             }
 
         }
@@ -71,6 +76,16 @@
         }
     }
 
+    private void removeOwner (String koralQuery, String vcOwner,
+            KoralNode koralNode) throws KustvaktException {
+        JsonNode jsonNode = koralNode.rawNode();
+        String ref = jsonNode.at("/ref").asText();
+        koralNode.remove("ref", new RewriteIdentifier("ref", ref));
+
+        ref = ref.substring(vcOwner.length()+1, ref.length());
+        koralNode.set("ref", ref, new RewriteIdentifier("ref", ref));
+    }
+
     private void rewriteVC (VirtualCorpus vc, KoralNode koralNode)
             throws KustvaktException {
         String koralQuery = vc.getKoralQuery();
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 04931d8..e91dde2 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
@@ -34,32 +34,56 @@
     private NamedVCLoader vcLoader;
     @Autowired
     private VirtualCorpusDao dao;
-    
+
     @Test
     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());
+
+        testNoRewriteWithUsername();
+
+        KrillCollection.cache.removeAll();
+        VirtualCorpus vc = dao.retrieveVCByName("named-vc1", "system");
+        dao.deleteVirtualCorpus(vc);
+    }
+
+    private void testNoRewriteWithUsername ()
+            throws KustvaktException, IOException, QueryException {
+
         ClientResponse response = resource().path(API_VERSION).path("search")
                 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
-                .queryParam("cq", "referTo named-vc1")
+                .queryParam("cq", "referTo \"system/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);
+
+        node = node.at("/operands/1/rewrites");
+        assertEquals(2, node.size());
+        assertEquals("operation:deletion", node.at("/0/operation").asText());
+        assertEquals("operation:insertion", node.at("/1/operation").asText());
+
     }
-    
+
     @Test
-    public void testRewriteFreeAndSystemVCRef () throws KustvaktException, Exception {
+    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\"")
@@ -71,7 +95,7 @@
 
         assertEquals("koral:docGroup", node.at("/@type").asText());
         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());
@@ -98,7 +122,7 @@
         node = node.at("/collection");
         assertEquals("koral:docGroup", node.at("/@type").asText());
         assertEquals("koral:docGroup", node.at("/operands/0/@type").asText());
-        
+
         node = node.at("/operands/1/rewrites");
         assertEquals(3, node.size());
         assertEquals("operation:deletion", node.at("/0/operation").asText());
@@ -120,7 +144,7 @@
         String ent = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         node = node.at("/collection");
-//        System.out.println(node);
+        // 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/VCReferenceTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/VCReferenceTest.java
index 55f289d..997da1a 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
@@ -80,13 +80,12 @@
     public void testSearchWithVCRefEqual () throws KustvaktException {
         ClientResponse response = resource().path(API_VERSION).path("search")
                 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
-                .queryParam("cq", "referTo named-vc1")
+                .queryParam("cq", "referTo \"system/named-vc1\"")
                 .get(ClientResponse.class);
 
         String ent = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         assertTrue(node.at("/matches").size() > 0);
-
     }
 
     public void testSearchWithVCRefNotEqual () throws KustvaktException {