Implemented VC caching & deserialization with string[] and cache option.

Change-Id: I42a7d4211d903587254d6b7fc2d6c242e55a2032
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestVCCaching.java b/src/test/java/de/ids_mannheim/korap/collection/TestVCCaching.java
new file mode 100644
index 0000000..108c1e2
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestVCCaching.java
@@ -0,0 +1,77 @@
+package de.ids_mannheim.korap.collection;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Paths;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.lucene.store.MMapDirectory;
+import org.junit.Test;
+
+import de.ids_mannheim.korap.Krill;
+import de.ids_mannheim.korap.KrillCollection;
+import de.ids_mannheim.korap.KrillIndex;
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Element;
+
+public class TestVCCaching {
+
+    private KrillIndex getSampleIndex () throws IOException {
+        return new KrillIndex(new MMapDirectory(
+                Paths.get(getClass().getResource("/sample-index").getFile())));
+
+    }
+
+    private KrillIndex index;
+
+    public TestVCCaching () throws IOException {
+        index = getSampleIndex();
+    }
+
+    @Test
+    public void testCacheVC () throws IOException {
+        InputStream is = getClass().getClassLoader()
+                .getResourceAsStream("named-vc/named-vc-free.jsonld");
+        String json = IOUtils.toString(is);
+
+        KrillCollection kc = new KrillCollection(json);
+        kc.setIndex(index);
+        kc.storeInCache();
+
+        Element element = KrillCollection.cache.get("cache-goe");
+        CachedVCData cc = (CachedVCData) element.getObjectValue();
+
+        assertTrue(cc.getDocIdMap().size() > 0);
+
+        testSearchCachedVC();
+        testClearCache();
+    }
+
+    private void testSearchCachedVC () throws IOException {
+        InputStream is = getClass().getClassLoader()
+                .getResourceAsStream("collection/query-with-vc-ref.jsonld");
+        String json = IOUtils.toString(is);
+
+        String result = new Krill(json).apply(this.index).toJsonString();
+        assertNotNull(result);
+        assertTrue(!result.isEmpty());
+
+        // test with match:eq
+        json.replaceFirst("match:ne", "match:eq");
+        result = new Krill(json).apply(this.index).toJsonString();
+        assertNotNull(result);
+        assertTrue(!result.isEmpty());
+    }
+    
+    public void testClearCache () {
+        CacheManager cacheManager = CacheManager.getInstance();
+        cacheManager.clearAll();
+        
+        Element element = KrillCollection.cache.get("cache-goe");
+        assertNull(element);
+    }
+}
diff --git a/src/test/resources/collection/query-with-vc-ref.jsonld b/src/test/resources/collection/query-with-vc-ref.jsonld
new file mode 100644
index 0000000..d50cd2a
--- /dev/null
+++ b/src/test/resources/collection/query-with-vc-ref.jsonld
@@ -0,0 +1,15 @@
+{"query":{
+    "@type":"koral:token",
+    "wrap":{
+      "@type":"koral:term",
+      "layer":"orth",
+      "key":"der",
+      "match":"match:eq",
+      "foundry":"opennlp"
+    }
+  },
+  "collection": {
+    "@type": "koral:docGroupRef",
+    "ref": "cache-goe"
+  }
+}
diff --git a/src/test/resources/named-vc/named-vc-free.jsonld b/src/test/resources/named-vc/named-vc-free.jsonld
index 4e8cd83..1f90e4c 100644
--- a/src/test/resources/named-vc/named-vc-free.jsonld
+++ b/src/test/resources/named-vc/named-vc-free.jsonld
@@ -2,10 +2,11 @@
     "name" : "cache-goe",
     "@type": "koral:doc",
     "key": "textSigle",
-    "match": "match:eq",
+    "match": "match:ne",
+    "type" : "type:string[]",
     "value": [
         "GOE/AGF/00000",
         "GOE/AGA/01784"
-    ]
-}}
-
+    ],
+    "cache" : "true"
+}}
\ No newline at end of file