Handling requests with unauthorized availability values.

Change-Id: Ifd66aa2158b93c267de5410b97426ccd84afeb7d
diff --git a/pom.xml b/pom.xml
index b5fee52..13e0025 100644
--- a/pom.xml
+++ b/pom.xml
@@ -290,7 +290,7 @@
         <dependency>
             <groupId>de.ids_mannheim.korap</groupId>
             <artifactId>Koral</artifactId>
-            <version>0.25</version>
+            <version>0.26</version>
             <exclusions>
                 <exclusion>
                     <groupId>org.eclipse.jetty</groupId>
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java
index 6fcae2f..9d70e36 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java
@@ -1,63 +1,141 @@
 package de.ids_mannheim.korap.resource.rewrite;
 
+import java.util.ArrayList;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.collect.Lists;
 
 import de.ids_mannheim.korap.config.Attributes;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.query.object.KoralMatchOperator;
 import de.ids_mannheim.korap.resource.rewrite.KoralNode.RewriteIdentifier;
 import de.ids_mannheim.korap.user.User;
+import de.ids_mannheim.korap.user.User.CorpusAccess;
 import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
 
 /**
  * @author margaretha
- * @date 22 May 2017
+ * @date 30 May 2017
  */
 public class CollectionRewrite implements RewriteTask.RewriteQuery {
 
-	private static Logger jlog = LoggerFactory.getLogger(CollectionRewrite.class);
+    private static Logger jlog = LoggerFactory
+            .getLogger(CollectionRewrite.class);
+    public static String AVAILABILITY = "availability";
 
-	public CollectionRewrite() {
-		super();
-	}
+    public static Pattern notFreeLicense = Pattern.compile("ACA|QAO");
+    public static Pattern notPublicLicense = Pattern.compile("QAO");
 
-	@Override
-	public JsonNode rewriteQuery(KoralNode node, KustvaktConfiguration config, User user) throws KustvaktException {
-		JsonNode subnode = node.rawNode();
-		KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
-		if (subnode.at("/collection").isMissingNode()) {
-			if (subnode.has("collection")) {
-				builder.setBaseQuery(JsonUtils.toJSON(subnode));
-			}
 
-			switch (user.getCorpusAccess()) {
-			case PUB:
-				builder = new KoralCollectionQueryBuilder();
-				builder.with("availability = /CC-BY.*/ | availablity = /ACA.*/");
-				break;
+    public CollectionRewrite () {
+        super();
+    }
 
-			case ALL:
-				builder = new KoralCollectionQueryBuilder();
-				builder.with("availability = /QAO.*/ | availablity = /ACA.*/ |  availablity = /CC-BY.*/");
-				break;
 
-			default: // FREE
-				builder = new KoralCollectionQueryBuilder();
-				builder.with("availability	 = /CC-BY.*/");
-				break;
-			}
+    private String verifyAvailability (JsonNode node, CorpusAccess access,
+            KustvaktConfiguration config) {
 
-			JsonNode rewritten = JsonUtils.readTree(builder.toJSON()).at("/collection");
-			RewriteIdentifier identifier = new KoralNode.RewriteIdentifier(Attributes.AVAILABILITY,
-					rewritten.at("/value"));
-			node.set("collection", rewritten, identifier);
-			jlog.debug(node.at("/collection").toString());
-		}
+        if (node.has("operands")) {
+            ArrayList<JsonNode> operands = Lists
+                    .newArrayList(node.at("/operands").elements());
+            for (int i = 0; i < operands.size(); i++) {
+                String path = verifyAvailability(operands.get(i), access,
+                        config);
+                if (!path.isEmpty()) { return "/operands/" + i; }
+            }
+        }
+        else if (node.has("key")
+                && node.at("/key").asText().equals(AVAILABILITY)) {
+            Matcher m;
+            String queryAvailability = node.at("/value").asText();
+            if (node.at("/match").asText()
+                    .equals(KoralMatchOperator.EQUALS.toString())) {
 
-		return node.rawNode();
-	}
+                if (access.equals(CorpusAccess.FREE)) {
+                    m = notFreeLicense.matcher(queryAvailability);
+                    if (m.find()) return "/value";
+                }
+                else if (access.equals(CorpusAccess.PUB)) {
+                    m = notPublicLicense.matcher(queryAvailability);
+                    if (m.find()) return "/value";
+                }
+            }
+            // match:ne
+            else {
+                if (access.equals(CorpusAccess.FREE)) {
+                    m = config.getFreeLicensePattern()
+                            .matcher(queryAvailability);
+                    if (m.find()) return "/value";
+                }
+                else if (access.equals(CorpusAccess.PUB)) {
+                    m = config.getPublicLicensePattern()
+                            .matcher(queryAvailability);
+                    if (m.find()) return "/value";
+                }
+            }
+        }
+
+        return "";
+    }
+
+
+    @Override
+    public JsonNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+            User user) throws KustvaktException {
+        JsonNode jsonNode = node.rawNode();
+        KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
+        switch (user.getCorpusAccess()) {
+            case PUB:
+                builder.with(
+                        "availability = /CC-BY.*/ | availability = /ACA.*/");
+                break;
+            case ALL:
+                builder.with("availability = /QAO.*/ | availability = /ACA.*/ |"
+                        + "  availability = /CC-BY.*/");
+                break;
+            case FREE:
+                builder.with("availability   = /CC-BY.*/");
+                break;
+        }
+
+        RewriteIdentifier identifier = new KoralNode.RewriteIdentifier(
+                Attributes.AVAILABILITY, user.getCorpusAccess());
+        JsonNode rewrittesNode;
+
+        if (jsonNode.has("collection")) {
+            String path = verifyAvailability(jsonNode.at("/collection"),
+                    user.getCorpusAccess(), config);
+            if (!path.isEmpty()) {
+                rewrittesNode = JsonUtils.readTree(builder.toJSON())
+                        .at("/collection");
+                if (path.equals("/value")) {
+                    node.replace("collection", rewrittesNode, identifier);
+                }
+                else {
+                    node.replaceAt("/collection" + path, rewrittesNode,
+                            identifier);
+                }
+            }
+            else {
+                builder.setBaseQuery(builder.toJSON());
+                rewrittesNode = builder.mergeWith(jsonNode).at("/collection");
+                node.set("collection", rewrittesNode, identifier);
+            }
+        }
+        else {
+            rewrittesNode = JsonUtils.readTree(builder.toJSON())
+                    .at("/collection");
+            node.set("collection", rewrittesNode, identifier);
+        }
+
+        jlog.debug("REWRITES: " + node.at("/collection").toString());
+        return node.rawNode();
+    }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
index 6de5552..0614f91 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
@@ -100,7 +100,20 @@
             this.rewrites.add("override", name);
         }
     }
+    
+    public void replaceAt (String path, Object value, RewriteIdentifier ident) {
+        if (this.node.isObject() && !this.node.at(path).isMissingNode()) {
+            ObjectNode n = (ObjectNode) this.node.at(path);
+            n.removeAll();
+            n.putAll((ObjectNode)value);
 
+            String name = path;
+            if (ident != null)
+                name = ident.toString();
+
+            this.rewrites.add("override", name);
+        }
+    }
 
     public void set (String name, Object value, RewriteIdentifier ident) {
         if (this.node.isObject()) {
diff --git a/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java b/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
index 3fc69d1..17995bf 100644
--- a/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
+++ b/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
@@ -205,7 +205,10 @@
 		MultivaluedMap<String, String> headerMap = headers.getRequestHeaders();
 		Location location = Location.EXTERN;
 		CorpusAccess corpusAccess = CorpusAccess.FREE;
-
+		
+		// EM: There is no check for a demo user in intranet?
+		// EM: LDAP user without IP gets CorpusAccess.FREE ?
+		
 		if (headerMap != null && headerMap.containsKey(org.eclipse.jetty.http.HttpHeaders.X_FORWARDED_FOR)) {
 
 			String[] vals = headerMap.getFirst(org.eclipse.jetty.http.HttpHeaders.X_FORWARDED_FOR).split(",");
@@ -217,17 +220,12 @@
 //				if (clientAddress.startsWith("10.0.") || clientAddress.startsWith("172.16.")
 //						|| clientAddress.startsWith("192.168."))
 					location = Location.INTERN;
-				}
-				else{
-					location = Location.EXTERN;
-				}
-				
-				if (location == Location.EXTERN) {
-					corpusAccess = CorpusAccess.PUB;
-				} 
-				else {
 					corpusAccess = CorpusAccess.ALL;
 				}
+				else{
+					corpusAccess = CorpusAccess.PUB;
+				}
+				
 				if (DEBUG_LOG == true) {
 					System.out.printf("Debug: X-Forwarded-For : '%s' (%d values) -> %s\n", vals, vals.length, vals[0]);
 					System.out.printf("Debug: X-Forwarded-For : location = %s corpusAccess = %s\n",
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 11169d8..64a5351 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
@@ -470,6 +470,8 @@
 
 
     // was heißt search by name all? FB
+    // EM: ich glaube es ist ALL corpora gemeint (ohne spezifische name), 
+    // weil es searchbyName() auch gibt.
     @SuppressWarnings("unchecked")
     @GET
     @Path("search")
@@ -503,7 +505,7 @@
         serializer.setQuery(q, ql, v);
         if (cq != null)
             serializer.setCollection(cq);
-        	// combine cq with availability CQ according to corpusAccess 
+
         MetaQueryBuilder meta = createMetaQuery(pageIndex, pageInteger, ctx,
                 pageLength, cutoff);
         serializer.setMeta(meta.raw());
@@ -595,8 +597,9 @@
      * @return
      */
     // todo: remove raw
-    @GET
-    @Path("/{type}/{id}/search")
+//    @GET
+//    @Path("/{type}/{id}/search")
+    @Deprecated //EM
     public Response searchbyName (@Context SecurityContext securityContext,
             @Context Locale locale, @QueryParam("q") String query,
             @QueryParam("ql") String ql, @QueryParam("v") String v,
@@ -630,7 +633,9 @@
                 // add collection query
                 KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
                 builder.setBaseQuery(s.toJSON());
-                query = createQuery(user, type, id, builder);
+                //query = createQuery(user, type, id, builder);
+                builder.with(Attributes.CORPUS_SIGLE, "=", id);
+                builder.toJSON();
 
             }
             else {
@@ -648,7 +653,7 @@
             }
 
             String result = doSearch(eng, query, pageLength, meta);
-            KustvaktLogger.QUERY_LOGGER.trace("The result set: {}", result);
+            jlog.debug("The result set: {}", result);
             return Response.ok(result).build();
         }
         catch (KustvaktException e) {
@@ -659,6 +664,7 @@
     }
 
 
+    @Deprecated
     private String createQuery (User user, String type, String id,
             KoralCollectionQueryBuilder builder) {
         KustvaktResource resource = null;
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/SearchServiceTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/SearchServiceTest.java
index b4f90bb..9bfe2f9 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/SearchServiceTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/SearchServiceTest.java
@@ -10,6 +10,7 @@
 
 import org.eclipse.jetty.http.HttpHeaders;
 import org.junit.BeforeClass;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import com.fasterxml.jackson.databind.JsonNode;
@@ -29,7 +30,7 @@
 
 /**
  * @author hanl, margaretha
- * @lastUpdate 21/04/2017
+ * @lastUpdate 30/05/2017
  *
  */
 public class SearchServiceTest extends FastJerseyTest {
@@ -47,6 +48,7 @@
                 "de.ids_mannheim.korap.web.utils");
     }
 
+
     @Test
     public void testSearchQueryPublicCorpora () {
         ClientResponse response = resource().path(getAPIVersion())
@@ -60,7 +62,7 @@
         assertEquals("koral:doc", node.at("/collection/@type").asText());
         assertEquals("availability", node.at("/collection/key").asText());
         assertEquals("CC-BY.*", node.at("/collection/value").asText());
-        assertEquals("availability(\"CC-BY.*\")",
+        assertEquals("availability(FREE)",
                 node.at("/collection/rewrites/0/scope").asText());
     }
 
@@ -70,8 +72,7 @@
         ClientResponse response = resource().path(getAPIVersion())
                 .path("search").queryParam("q", "[orth=der]")
                 .queryParam("ql", "poliqarp").queryParam("cutoff", "true")
-                .queryParam("count", "5")
-                .queryParam("page", "1")
+                .queryParam("count", "5").queryParam("page", "1")
                 .queryParam("context", "40-t,30-t").get(ClientResponse.class);
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -84,11 +85,12 @@
         assertEquals("token", node.at("/meta/context/left/0").asText());
         assertEquals(40, node.at("/meta/context/left/1").asInt());
         assertEquals(30, node.at("/meta/context/right/1").asInt());
-        assertEquals(-1,node.at("/meta/totalResults").asInt());
+        assertEquals(-1, node.at("/meta/totalResults").asInt());
     }
 
+
     @Test
-    public void testSearchQueryAuthorized () {
+    public void testSearchQueryExternAuthorized () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("search").queryParam("q", "[orth=die]")
                 .queryParam("ql", "poliqarp")
@@ -103,15 +105,17 @@
         assertNotNull(node);
         assertNotEquals(0, node.path("matches").size());
         assertEquals("koral:docGroup", node.at("/collection/@type").asText());
-        assertEquals("CC-BY.*", node.at("/collection/operands/0/value").asText());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/0/value").asText());
         assertEquals("ACA.*", node.at("/collection/operands/1/value").asText());
         assertEquals("operation:or", node.at("/collection/operation").asText());
-        assertEquals("availability()",
+        assertEquals("availability(PUB)",
                 node.at("/collection/rewrites/0/scope").asText());
     }
 
+
     @Test
-    public void testSearchQueryAuthorizedALL () {
+    public void testSearchQueryInternAuthorized () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("search").queryParam("q", "[orth=die]")
                 .queryParam("ql", "poliqarp")
@@ -127,15 +131,18 @@
         assertNotEquals(0, node.path("matches").size());
         assertEquals("koral:docGroup", node.at("/collection/@type").asText());
         assertEquals("QAO.*", node.at("/collection/operands/0/value").asText());
-        assertEquals("ACA.*", node.at("/collection/operands/1/operands/0/value").asText());
-        assertEquals("CC-BY.*", node.at("/collection/operands/1/operands/1/value").asText());
+        assertEquals("ACA.*",
+                node.at("/collection/operands/1/operands/0/value").asText());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/1/operands/1/value").asText());
         assertEquals("operation:or", node.at("/collection/operation").asText());
-        assertEquals("availability()",
+        assertEquals("availability(ALL)",
                 node.at("/collection/rewrites/0/scope").asText());
     }
 
+
     @Test
-    public void testSearchQueryWithCollectionQueryAuthorized () {
+    public void testSearchQueryWithCollectionQueryAuthorizedNoIP () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("search").queryParam("q", "[orth=das]")
                 .queryParam("ql", "poliqarp")
@@ -148,16 +155,118 @@
 
         JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
         assertNotNull(node);
-        assertEquals(2, node.at("/collection/operands").size());
+        assertEquals("operation:insertion",
+                node.at("/collection/rewrites/0/operation").asText());
+        assertEquals("availability(PUB)",
+                node.at("/collection/rewrites/0/scope").asText());
+        // EM: double AND operations
+        assertEquals("availability", node.at("/collection/operands/0/key").asText());
+        assertEquals("CC-BY.*", node.at("/collection/operands/0/value").asText());
         assertEquals("textClass",
-                node.at("/collection/operands/0/key").asText());
+                node.at("/collection/operands/1/operands/0/key").asText());
         assertEquals("corpusSigle",
-                node.at("/collection/operands/1/key").asText());
-        assertEquals("koral:token", node.at("/query/@type").asText());
+                node.at("/collection/operands/1/operands/1/key").asText());
     }
 
 
     @Test
+    public void testSearchQueryWithCollectionQueryUnauthorized () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("search").queryParam("q", "[orth=das]")
+                .queryParam("ql", "poliqarp")
+                .queryParam("cq", "availability != /CC-BY.*/")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
+        assertNotNull(node);
+        assertEquals("availability", node.at("/collection/key").asText());
+        assertEquals("CC-BY.*", node.at("/collection/value").asText());
+        assertEquals("operation:override",
+                node.at("/collection/rewrites/0/operation").asText());
+        assertEquals("availability(FREE)",
+                node.at("/collection/rewrites/0/scope").asText());
+        assertEquals("koral:token", node.at("/query/@type").asText());
+    }
+    
+    @Test
+    public void testSearchQueryWithComplexCollectionQueryUnauthorized () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("search").queryParam("q", "[orth=das]")
+                .queryParam("ql", "poliqarp")
+                .queryParam("cq", "textClass=politik & availability != /CC-BY.*/")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
+        assertNotNull(node);
+        assertEquals("textClass", node.at("/collection/operands/0/key").asText());
+        assertEquals("politik", node.at("/collection/operands/0/value").asText());
+        assertEquals("match:eq", node.at("/collection/operands/1/match").asText());
+        assertEquals("availability", node.at("/collection/operands/1/key").asText());
+        assertEquals("CC-BY.*", node.at("/collection/operands/1/value").asText());
+        assertEquals("operation:override",
+                node.at("/collection/rewrites/0/operation").asText());
+        assertEquals("availability(FREE)",
+                node.at("/collection/rewrites/0/scope").asText());
+    }
+    
+    @Test
+    public void testSearchQueryWithComplexCollectionQueryUnauthorized2 () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("search").queryParam("q", "[orth=das]")
+                .queryParam("ql", "poliqarp")
+                .queryParam("cq", "textClass=politik & availability=ACA-NC")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
+        assertNotNull(node);
+        assertEquals("textClass", node.at("/collection/operands/0/key").asText());
+        assertEquals("politik", node.at("/collection/operands/0/value").asText());
+        assertEquals("match:eq", node.at("/collection/operands/1/match").asText());
+        assertEquals("availability", node.at("/collection/operands/1/key").asText());
+        assertEquals("CC-BY.*", node.at("/collection/operands/1/value").asText());
+        assertEquals("operation:override",
+                node.at("/collection/rewrites/0/operation").asText());
+        assertEquals("availability(FREE)",
+                node.at("/collection/rewrites/0/scope").asText());
+    }
+
+    @Test
+    public void testSearchQueryWithComplexCollectionQueryPublicOverride () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("search").queryParam("q", "[orth=das]")
+                .queryParam("ql", "poliqarp")
+                .queryParam("cq", "textClass=politik & availability=QAO-NC-LOC:ids")
+                .header(Attributes.AUTHORIZATION,
+                        BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
+                .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
+        assertNotNull(node);
+        assertEquals("textClass", node.at("/collection/operands/0/key").asText());
+        assertEquals("politik", node.at("/collection/operands/0/value").asText());
+        assertEquals("match:eq", node.at("/collection/operands/1/operands/0/match").asText());
+        assertEquals("availability", node.at("/collection/operands/1/operands/0/key").asText());
+        assertEquals("CC-BY.*", node.at("/collection/operands/1/operands/0/value").asText());
+        assertEquals("match:eq", node.at("/collection/operands/1/operands/1/match").asText());
+        assertEquals("ACA.*", node.at("/collection/operands/1/operands/1/value").asText());
+        assertEquals("operation:override",
+                node.at("/collection/rewrites/0/operation").asText());
+        assertEquals("availability(PUB)",
+                node.at("/collection/rewrites/0/scope").asText());
+    }
+
+
+    @Test
+    @Ignore
     public void testSearchForPublicCorpusWithStringId () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("corpus").path("GOE").path("search")
@@ -168,15 +277,22 @@
         String ent = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
-        assertEquals("koral:doc", node.at("/collection/@type").asText());
-        assertEquals("corpusSigle", node.at("/collection/key").asText());
-        assertEquals("GOE", node.at("/collection/value").asText());
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:and",
+                node.at("/collection/operation").asText());
+        assertEquals("availability",
+                node.at("/collection/operands/0/key").asText());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/0/value").asText());
+        assertEquals("corpusSigle",
+                node.at("/collection/operands/1/key").asText());
+        assertEquals("GOE", node.at("/collection/operands/1/value").asText());
         assertNotEquals(0, node.path("matches").size());
-        assertEquals(33, node.at("/meta/totalResults").asInt());
     }
 
 
     @Test
+    @Ignore
     public void testSearchForVirtualCollectionWithStringId () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("collection").path("GOE-VC").path("search")
@@ -201,8 +317,9 @@
         assertEquals(1, node.at("/meta/totalResults").asInt());
     }
 
-
+    // EM: non practical use-case
     @Test
+    @Ignore
     public void testSearchForPublicCorpusWithIntegerId ()
             throws KustvaktException {
         Set<Corpus> publicCorpora = ResourceFinder.searchPublic(Corpus.class);
@@ -225,14 +342,22 @@
 
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
-        assertEquals("koral:doc", node.at("/collection/@type").asText());
-        assertEquals("corpusSigle", node.at("/collection/key").asText());
-        assertEquals("GOE", node.at("/collection/value").asText());
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:and",
+                node.at("/collection/operation").asText());
+        assertEquals("availability",
+                node.at("/collection/operands/0/key").asText());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/0/value").asText());
+        assertEquals("corpusSigle",
+                node.at("/collection/operands/1/key").asText());
+        assertEquals("GOE", node.at("/collection/operands/1/value").asText());
         assertNotEquals(0, node.path("matches").size());
     }
 
 
     @Test
+    @Ignore
     public void testSearchForCorpusWithStringIdUnauthorized () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("corpus").path("WPD15").path("search")
@@ -249,9 +374,10 @@
 
 
     @Test
-    public void testSearchForOwnersCorpusWithStringId () {
+    @Ignore
+    public void testSearchForSpecificCorpus () {
         ClientResponse response = resource().path(getAPIVersion())
-                .path("corpus").path("WPD15").path("search")
+                .path("corpus").path("GOE").path("search")
                 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
                 .header(Attributes.AUTHORIZATION,
                         BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
@@ -261,15 +387,21 @@
         String entity = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(entity);
         assertNotNull(node);
-        assertEquals("koral:doc", node.at("/collection/@type").asText());
-        assertEquals("corpusSigle", node.at("/collection/key").asText());
-        assertEquals("WPD15", node.at("/collection/value").asText());
-        // EM: sample index does not include this corpus
-//        assertNotEquals(0, node.path("matches").size());
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:and",
+                node.at("/collection/operation").asText());
+        assertEquals("availability",
+                node.at("/collection/operands/0/key").asText());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/0/value").asText());
+        assertEquals("corpusSigle",
+                node.at("/collection/operands/1/key").asText());
+        assertEquals("GOE", node.at("/collection/operands/1/value").asText());
     }
 
 
     @Test
+    @Ignore
     public void testSearchForOwnersCorpusWithIntegerId ()
             throws KustvaktException {
 
@@ -281,7 +413,7 @@
         String id = null;
         while (i.hasNext()) {
             Corpus c = i.next();
-            if (c.getPersistentID().equals("WPD15")) {
+            if (c.getPersistentID().equals("GOE")) {
                 id = c.getId().toString();
                 //                System.out.println("Corpus "+id);
             }
@@ -297,10 +429,16 @@
         String entity = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(entity);
         assertNotNull(node);
-        assertEquals("koral:doc", node.at("/collection/@type").asText());
-        assertEquals("corpusSigle", node.at("/collection/key").asText());
-        assertEquals("WPD15", node.at("/collection/value").asText());
-//        assertNotEquals(0, node.path("matches").size());
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:and",
+                node.at("/collection/operation").asText());
+        assertEquals("availability",
+                node.at("/collection/operands/0/key").asText());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/0/value").asText());
+        assertEquals("corpusSigle",
+                node.at("/collection/operands/1/key").asText());
+        assertEquals("GOE", node.at("/collection/operands/1/value").asText());
     }