string to json bug hot fix
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
index ef0c1d2..8d00d7a 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
@@ -116,16 +116,16 @@
 
 
 
-    public JsonNode process(JsonNode root, User user) {
+    public String process(JsonNode root, User user) {
         RewriteProcess process = new RewriteProcess(root, user);
         JsonNode pre = process.start(false);
         //return iterate(pre, user, true);
-        return pre;
+        return JsonUtils.toJSON(pre);
     }
 
 
     public String process(String json, User user) {
-        return JsonUtils.toJSON(process(JsonUtils.readTree(json), user));
+        return process(JsonUtils.readTree(json), user);
     }
 
 
diff --git a/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java b/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java
index ebe6910..532e8f3 100644
--- a/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java
+++ b/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java
@@ -1,6 +1,7 @@
 package de.ids_mannheim.korap.resources;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.databind.JsonNode;
 import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.utils.TimeUtils;
 import lombok.AccessLevel;
@@ -99,7 +100,11 @@
     }
 
 
-    public String getData () {
+    public JsonNode getData () {
+        return JsonUtils.valueToTree(this.fields);
+    }
+
+    public String getStringData() {
         return JsonUtils.toJSON(this.fields);
     }
 
@@ -178,8 +183,9 @@
         //        m.put("path", path);
         m.put("description", description);
         m.put("created", TimeUtils.format(new DateTime(created)));
-        m.put("data", getData());
+        m.put("data", this.fields);
         // deprecated
+        // todo:
         //        m.put("managed", managed);
         //        m.put("shared", shared);
         return m;
diff --git a/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java b/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java
index 9458ffc..c6de012 100644
--- a/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java
+++ b/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java
@@ -63,8 +63,8 @@
 
 
     public static <T extends KustvaktResource> T createID (T resource) {
-        if (resource.getData() != null && !resource.getData().isEmpty())
-            resource.setPersistentID(DigestUtils.sha1Hex(resource.getData()));
+        if (resource.getData() != null && !resource.getStringData().isEmpty())
+            resource.setPersistentID(DigestUtils.sha1Hex(resource.getStringData()));
         return resource;
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java b/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java
index a736b1d..4d6b6f1 100644
--- a/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java
+++ b/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java
@@ -23,6 +23,7 @@
     private boolean verbose;
     private JsonNode base;
     private StringBuilder builder;
+    private String mergeOperator;
 
 
     public KoralCollectionQueryBuilder() {
@@ -34,6 +35,7 @@
         this.verbose = verbose;
         this.builder = new StringBuilder();
         this.base = null;
+        this.mergeOperator = null;
     }
 
 
@@ -75,44 +77,53 @@
 
 
     public KoralCollectionQueryBuilder and () {
-        this.builder.append(" & ");
+        if (this.builder.length() != 0)
+            this.builder.append(" & ");
+        if (this.base != null && this.mergeOperator == null)
+            this.mergeOperator = "AND";
         return this;
     }
 
 
     public KoralCollectionQueryBuilder or () {
-        this.builder.append(" | ");
+        if (this.builder.length() != 0)
+            this.builder.append(" | ");
+        if (this.base != null && this.mergeOperator == null)
+            this.mergeOperator = "OR";
         return this;
     }
 
 
     public Object rebaseCollection () {
-        if (builder.length() == 0 && base == null)
+        if (this.builder.length() == 0 && this.base == null)
             return null;
 
         JsonNode request = null;
-        if (builder.length() != 0) {
+        if (this.builder.length() != 0) {
             CollectionQueryProcessor tree = new CollectionQueryProcessor(
                     this.verbose);
             tree.process(this.builder.toString());
             request = JsonUtils.valueToTree(tree.getRequestMap());
         }
 
-        if (base != null) {
+        if (this.base != null) {
             // check that collection non empty
+            JsonNode tmp = this.base.deepCopy();
             if (request != null) {
                 JsonNode tobase = request.at("/collection");
-                request = base.deepCopy();
-                JsonNode result = JsonBuilder.buildDocGroup("and",
+                request = tmp;
+                JsonNode result = JsonBuilder.buildDocGroup(this.mergeOperator != null
+                        ? this.mergeOperator.toLowerCase() : "and",
                         request.at("/collection"), tobase);
                 ((ObjectNode) request).put("collection", result);
-            }
+            } else
+                request = tmp;
         }
         return request;
     }
 
     public JsonNode mergeWith(JsonNode node) {
-        if (base != null) {
+        if (this.base != null) {
             // check that collection non empty
             if (node != null) {
                 JsonNode tobase = node.at("/collection");
@@ -139,6 +150,10 @@
         this.base = JsonUtils.readTree(query);
         return this;
     }
+    public KoralCollectionQueryBuilder setBaseQuery (JsonNode query) {
+        this.base = query;
+        return this;
+    }
 
 
     public String toJSON () {
diff --git a/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java b/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java
index 17f8595..8ff8f3c 100644
--- a/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java
+++ b/src/main/java/de/ids_mannheim/korap/web/service/full/ResourceService.java
@@ -381,8 +381,9 @@
                 resource = this.resourceHandler.findbyStrId(id, user,
                         ResourceFactory.getResourceClass(type));
 
+            // todo: test this
             if (resource instanceof VirtualCollection)
-                cquery.with((String) resource.getData());
+                cquery.mergeWith(resource.getData());
             else if (resource instanceof Corpus)
                 cquery.with(Attributes.CORPUS_SIGLE + resource.getPersistentID());
 
@@ -548,7 +549,7 @@
                             ResourceFactory.getResourceClass(type));
 
                 if (resource instanceof VirtualCollection)
-                    builder.setBaseQuery((String) resource.getData());
+                    builder.setBaseQuery(resource.getData());
                 else if (resource instanceof Corpus)
                     builder.with(Attributes.CORPUS_SIGLE+ resource.getPersistentID());
                 else
@@ -704,9 +705,7 @@
         VirtualCollection tmp = resourceHandler.getCache(cache.getId(),
                 VirtualCollection.class);
         if (tmp == null) {
-            KoralCollectionQueryBuilder cquery = new KoralCollectionQueryBuilder()
-                    .setBaseQuery((String) cache.getData());
-            String query = this.processor.process((String) cache.getData(),
+            String query = this.processor.process(cache.getData(),
                     user);
             String stats = searchKrill.getStatistics(query);
             cache.setStats(JsonUtils.readSimple(stats, Map.class));
@@ -785,10 +784,10 @@
                 .getResourceClass(type))) {
             VirtualCollection cachetmp, collection;
 
-            String base;
+            JsonNode base;
             if (reference != null && !reference.equals("null")) {
                 try {
-                    base = (String) resourceHandler.findbyStrId(reference,
+                    base = resourceHandler.findbyStrId(reference,
                             user, VirtualCollection.class).getData();
                 }
                 catch (KustvaktException e) {
@@ -797,7 +796,7 @@
 
             }
             else if (query != null)
-                base = query;
+                base = JsonUtils.readTree(query);
             else
                 // todo: throw exception response for no resource to save!
                 return null;
@@ -839,13 +838,14 @@
 
     /**
      * store a virtual collection. Retrieve cached entry first and
-     * then store VCollection
+     * then store collection
      * 
      * @param context
      * @param locale
      * @param query
      * @return
      */
+    // todo: testing
     @POST
     @Path("{type}")
     public Response storeResource (@Context SecurityContext context,
@@ -873,28 +873,21 @@
                 .getResourceClass(type))) {
 
             VirtualCollection cachetmp, collection;
-            // todo: ??
-            Object read = JsonUtils.readTree(query);
+
             KoralCollectionQueryBuilder cquery = new KoralCollectionQueryBuilder();
             if (reference != null && !reference.equals("null")) {
                 try {
-                    cquery.with((String) resourceHandler.findbyStrId(
+                    cquery.setBaseQuery(resourceHandler.findbyStrId(
                             reference, user, VirtualCollection.class).getData());
+
                 }
                 catch (KustvaktException e) {
                     throw KustvaktResponseHandler.throwit(e);
                 }
-                // todo: 11.01
-                //                if (!filter)
-                //                    cquery.addMetaExtendQuery(query);
-                //                else
-                //                    cquery.addMetaFilterQuery(query);
-                //            }else {
-                //                if (read != null)
-                //                    cquery.addResource(query);
-                //                else
-                //                    cquery.addMetaFilterQuery(query);
             }
+            if (query != null && !query.isEmpty())
+                cquery.with(query);
+
             cachetmp = ResourceFactory.getCachedCollection(cquery.toJSON());
 
             // see if vc was cached!
diff --git a/src/test/java/CollectionQueryBuilderTest.java b/src/test/java/CollectionQueryBuilderTest.java
index 38a5ac4..407608d 100644
--- a/src/test/java/CollectionQueryBuilderTest.java
+++ b/src/test/java/CollectionQueryBuilderTest.java
@@ -2,6 +2,7 @@
 import de.ids_mannheim.korap.query.serialize.QuerySerializer;
 import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
 import de.ids_mannheim.korap.utils.JsonUtils;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -84,12 +85,19 @@
         c.with("corpusSigle=WPD");
 
         JsonNode node = JsonUtils.readTree(c.toJSON());
-
         assertNotNull(node);
-        assertEquals("koral:doc", node.at("/collection/operands/0/@type")
+        assertEquals("koral:doc", node.at("/collection/operands/1/@type")
                 .asText());
-        assertEquals("WPD", node.at("/collection/operands/0/value").asText());
+        assertEquals("koral:docGroup", node.at("/collection/operands/0/@type")
+                .asText());
         assertEquals(2, node.at("/collection/operands").size());
+        assertEquals(2, node.at("/collection/operands/0/operands").size());
+        assertEquals(2, node.at("/collection/operands/0/operands/0/operands").size());
+
+        assertEquals("operation:and", node.at("/collection/operation").asText());
+        assertEquals("operation:or", node.at("/collection/operands/0/operation").asText());
+        assertEquals("operation:and", node.at("/collection/operands/0/operands/0/operation").asText());
+        assertEquals("WPD", node.at("/collection/operands/1/value").asText());
     }
 
 
@@ -109,13 +117,13 @@
         assertEquals("koral:docGroup", node.at("/collection/operands/1/@type")
                 .asText());
         assertEquals("BRZ13", node
-                .at("/collection/operands/0/operands/0/value").asText());
-        assertEquals("AZPS", node.at("/collection/operands/0/operands/1/value")
+                .at("/collection/operands/1/operands/0/value").asText());
+        assertEquals("AZPS", node.at("/collection/operands/1/operands/1/value")
                 .asText());
         assertEquals("random title",
-                node.at("/collection/operands/1/operands/0/value").asText());
+                node.at("/collection/operands/0/operands/0/value").asText());
         assertEquals("wissenschaft",
-                node.at("/collection/operands/1/operands/1/value").asText());
+                node.at("/collection/operands/0/operands/1/value").asText());
     }
 
 
@@ -137,14 +145,14 @@
         assertEquals("operation:and", res.at("/collection/operation").asText());
         assertEquals("koral:doc", res.at("/collection/operands/0/@type")
                 .asText());
-        assertEquals("freizeit", res.at("/collection/operands/0/value")
+        assertEquals("freizeit", res.at("/collection/operands/1/value")
                 .asText());
-        assertEquals("textClass", res.at("/collection/operands/0/key").asText());
+        assertEquals("textClass", res.at("/collection/operands/1/key").asText());
 
         assertEquals("koral:doc", res.at("/collection/operands/1/@type")
                 .asText());
-        assertEquals("WPD", res.at("/collection/operands/1/value").asText());
-        assertEquals("corpusSigle", res.at("/collection/operands/1/key").asText());
+        assertEquals("WPD", res.at("/collection/operands/0/value").asText());
+        assertEquals("corpusSigle", res.at("/collection/operands/0/key").asText());
 
         // check also that query is still there
         assertEquals("koral:token", res.at("/query/@type").asText());
@@ -166,12 +174,12 @@
         JsonNode base = (JsonNode) c.rebaseCollection();
         assertNotNull(base);
         assertEquals(base.at("/collection/@type").asText(), "koral:docGroup");
-        assertEquals(base.at("/collection/operands/0/@type").asText(),
-                "koral:doc");
-        assertEquals(base.at("/collection/operands/0/value").asText(), "BRZ13");
         assertEquals(base.at("/collection/operands/1/@type").asText(),
+                "koral:doc");
+        assertEquals(base.at("/collection/operands/1/value").asText(), "BRZ13");
+        assertEquals(base.at("/collection/operands/0/@type").asText(),
                 "koral:docGroup");
-        assertEquals(base.at("/collection/operands/1/operands").size(), 2);
+        assertEquals(base.at("/collection/operands/0/operands").size(), 2);
     }
 
     @Test
@@ -201,4 +209,80 @@
 
     }
 
+    @Test
+    public void testAddOROperator() {
+        String coll = "corpusSigle=WPD";
+        String query = "[base=Haus]";
+        QuerySerializer check = new QuerySerializer();
+        check.setQuery(query, "poliqarp");
+        check.setCollection(coll);
+
+        KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
+        test.setBaseQuery(check.toJSON());
+        test.or().with("textClass=wissenschaft | textClass=politik");
+        JsonNode node = (JsonNode) test.rebaseCollection();
+        assertNotNull(node);
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:or", node.at("/collection/operation").asText());
+        assertEquals(2, node.at("/collection/operands/1/operands").size());
+    }
+
+    @Test
+    public void testAddANDOperator() {
+        String coll = "corpusSigle=WPD";
+        String query = "[base=Haus]";
+        QuerySerializer check = new QuerySerializer();
+        check.setQuery(query, "poliqarp");
+        check.setCollection(coll);
+
+        KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
+        test.setBaseQuery(check.toJSON());
+        test.and().with("textClass=wissenschaft | textClass=politik");
+        JsonNode node = (JsonNode) test.rebaseCollection();
+        assertNotNull(node);
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:and", node.at("/collection/operation").asText());
+        assertEquals(2, node.at("/collection/operands/1/operands").size());
+    }
+
+    @Test
+    public void testAddDefaultOperator() {
+        String coll = "corpusSigle=WPD";
+        String query = "[base=Haus]";
+        QuerySerializer check = new QuerySerializer();
+        check.setQuery(query, "poliqarp");
+        check.setCollection(coll);
+
+        KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
+        test.setBaseQuery(check.toJSON());
+        test.with("textClass=wissenschaft | textClass=politik");
+        JsonNode node = (JsonNode) test.rebaseCollection();
+        assertNotNull(node);
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:and", node.at("/collection/operation").asText());
+        assertEquals(2, node.at("/collection/operands/1/operands").size());
+    }
+
+    @Test
+    @Ignore
+    public void testMergeOperator() {
+        String coll = "corpusSigle=WPD";
+        String query = "[base=Haus]";
+        QuerySerializer check = new QuerySerializer();
+        check.setQuery(query, "poliqarp");
+        check.setCollection(coll);
+
+        KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
+        b.setBaseQuery(check.toJSON());
+
+        KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
+        test.with("textClass=wissenschaft | textClass=politik");
+        JsonNode node = (JsonNode) test.rebaseCollection();
+        node = b.mergeWith(node);
+        assertNotNull(node);
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:and", node.at("/collection/operation").asText());
+        assertEquals(2, node.at("/collection/operands").size());
+    }
+
 }
diff --git a/src/test/java/de/ids_mannheim/korap/resource/rewrite/FoundryRewriteTest.java b/src/test/java/de/ids_mannheim/korap/resource/rewrite/FoundryRewriteTest.java
index 92ef92c..90bcc86 100644
--- a/src/test/java/de/ids_mannheim/korap/resource/rewrite/FoundryRewriteTest.java
+++ b/src/test/java/de/ids_mannheim/korap/resource/rewrite/FoundryRewriteTest.java
@@ -65,8 +65,6 @@
         s.setQuery("[base=Haus]", "poliqarp");
         String result = processor.process(s.toJSON(), null);
         JsonNode node = JsonUtils.readTree(result);
-
-        System.out.println("REWRITTEN "+ node);
         assertNotNull(node);
         assertFalse(node.at("/query/wrap/foundry").isMissingNode());
         assertEquals(c.getDefault_lemma(), node.at("/query/wrap/foundry")
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/OAuth2HandlerTest.java b/src/test/java/de/ids_mannheim/korap/web/service/OAuth2HandlerTest.java
index 0a55637..d7e33b9 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/OAuth2HandlerTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/OAuth2HandlerTest.java
@@ -13,6 +13,8 @@
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import static org.junit.Assert.assertNotNull;
+
 /**
  * @author hanl
  * @date 13/05/2015
@@ -65,7 +67,7 @@
                 .getPersistenceClient());
         handler.authorize(codeInfo, helper().getUser());
         codeInfo = handler.getAuthorization(auth_code);
-        Assert.assertNotNull("client is null!", codeInfo);
+        assertNotNull("client is null!", codeInfo);
     }
 
 
@@ -84,7 +86,7 @@
         handler.addToken(codeInfo.getCode(), t, refresh, 7200);
 
         TokenContext ctx = handler.getContext(t);
-        Assert.assertNotNull("context is null", ctx);
+        assertNotNull("context is null", ctx);
 
         AuthCodeInfo c2 = handler.getAuthorization(codeInfo.getCode());
         Assert.assertNull("clearing authorization failed", c2);
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceServiceTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceServiceTest.java
index d0a5b96..6a5ab99 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceServiceTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceServiceTest.java
@@ -189,13 +189,13 @@
     @Test
     public void testGetResources () {
         ClientResponse response = resource().path(getAPIVersion())
-                .path("corpus").get(ClientResponse.class);
+                .path("collection").get(ClientResponse.class);
         assertEquals(response.getStatus(),
                 ClientResponse.Status.OK.getStatusCode());
         String ent = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
-        System.out.println("NODE 1 " + node);
+        assertNotEquals(0, node.size());
     }