Fixed collection rewrite

Change-Id: Ib9d0eed501b8d27df638f5ec62c7879b255fad3d
diff --git a/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java b/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
index f6e85af..f2742ce 100644
--- a/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
+++ b/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
@@ -81,10 +81,6 @@
     private ArrayList<String> foundries;
     private ArrayList<String> layers;
     
-//    private List<String> publicLicenses;
-//    private List<String> freeLicenses;
-//    private List<String> allLicenses;
-    
     private Pattern publicLicensePattern;
     private Pattern freeLicensePattern;
     private Pattern allLicensePattern;
@@ -167,14 +163,12 @@
         
         ldapConfig = properties.getProperty("ldap.config");
         
-//        freeLicenses = Arrays.asList(license.split("|"));
-//        publicLicenses = Arrays.asList(properties.getProperty("kustvakt.availability.public","").split("|"));
-//        allLicenses = Arrays.asList(properties.getProperty("kustvakt.availability.all","").split("|"));
+        // EM: replace this later with KoralQuery
+        freeLicensePattern = Pattern.compile(properties.getProperty("kustvakt.regex.free",""));
+        publicLicensePattern = Pattern.compile(properties.getProperty("kustvakt.regex.public",""));
+        allLicensePattern = Pattern.compile(properties.getProperty("kustvakt.regex.all",""));
         
-        freeLicensePattern = Pattern.compile(properties.getProperty("kustvakt.availability.free",""));
-        publicLicensePattern = Pattern.compile(properties.getProperty("kustvakt.availability.public",""));
-        allLicensePattern = Pattern.compile(properties.getProperty("kustvakt.availability.all",""));
-        
+        // EM: not use in the future
         policyConfig = properties.getProperty("policies.config");
         setFoundriesAndLayers(policyConfig);
         
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 9d70e36..1de7d2f 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,22 +1,16 @@
 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;
 
@@ -30,62 +24,12 @@
             .getLogger(CollectionRewrite.class);
     public static String AVAILABILITY = "availability";
 
-    public static Pattern notFreeLicense = Pattern.compile("ACA|QAO");
-    public static Pattern notPublicLicense = Pattern.compile("QAO");
-
 
     public CollectionRewrite () {
         super();
     }
 
 
-    private String verifyAvailability (JsonNode node, CorpusAccess access,
-            KustvaktConfiguration config) {
-
-        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())) {
-
-                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 {
@@ -110,24 +54,9 @@
         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);
-            }
+            builder.setBaseQuery(builder.toJSON());
+            rewrittesNode = builder.mergeWith(jsonNode).at("/collection");
+            node.set("collection", rewrittesNode, identifier);
         }
         else {
             rewrittesNode = JsonUtils.readTree(builder.toJSON())
@@ -135,7 +64,7 @@
             node.set("collection", rewrittesNode, identifier);
         }
 
-        jlog.debug("REWRITES: " + node.at("/collection").toString());
+        jlog.info("REWRITES: " + node.at("/collection").toString());
         return node.rawNode();
     }
 }
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 97aace6..03421e1 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
@@ -1171,63 +1171,7 @@
                     e.string());
             throw KustvaktResponseHandler.throwit(e);
         }
-        String results;
-//        // fixme: checks for policy matching
-//        // fixme: currently disabled, due to mishab in foundry/layer spec
-//        // fixme:
-//        if (foundries != null && foundries.size() > 1000) {
-//            Set<String> f_list = new HashSet<>();
-//            Set<String> l_list = new HashSet<>();
-//
-//            for (String spl : new ArrayList<>(foundries)) {
-//                try {
-//                    SecurityManager<?> manager = SecurityManager.init(spl, user,
-//                            Permissions.Permission.READ);
-//                    if (!manager.isAllowed())
-//                        continue;
-//
-//                    String[] sep = StringUtils.splitAnnotations(spl);
-//                    if (spl != null) {
-//                        f_list.add(sep[0]);
-//                        l_list.add(sep[1]);
-//                    };
-//                    results = searchKrill.getMatch(matchid,
-//                            new ArrayList<>(f_list), new ArrayList<>(l_list),
-//                            spans, false, true);
-//                }
-//                catch (NotAuthorizedException e) {
-//                    throw KustvaktResponseHandler.throwit(
-//                            StatusCodes.ACCESS_DENIED, "Permission denied",
-//                            matchid);
-//                }
-//
-//            }
-//            // all foundries shall be returned
-//        }
-//        else if (foundries != null && foundries.contains("*")) {
-//            Set<Layer> resources;
-//            try {
-//                resources = ResourceFinder.search(user, Layer.class);
-//            }
-//            catch (KustvaktException e) {
-//                jlog.error("Exception encountered: {}", e.string());
-//                throw KustvaktResponseHandler.throwit(e);
-//            }
-//            // returns foundries and layers.
-//            // todo: needs testing!
-//            foundries = new HashSet<>();
-//            layers = new HashSet<>();
-//            for (Layer r : resources) {
-//                String[] spl = StringUtils.splitAnnotations(r.getName());
-//                if (spl != null) {
-//                    foundries.add(spl[0]);
-//                    layers.add(spl[1]);
-//                }
-//            }
-//        }
         
-        
-        //EM: I dont need user here just corpusAccess
         CorpusAccess corpusAccess = user.getCorpusAccess();
         Pattern p;
         switch (corpusAccess) {
@@ -1241,6 +1185,8 @@
 			p = config.getFreeLicensePattern();
 			break;
 		}
+        
+        String results;
         try {
             if (!match_only){
             	
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 9bfe2f9..8786030 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
@@ -64,6 +64,8 @@
         assertEquals("CC-BY.*", node.at("/collection/value").asText());
         assertEquals("availability(FREE)",
                 node.at("/collection/rewrites/0/scope").asText());
+        assertEquals("operation:insertion",
+                node.at("/collection/rewrites/0/operation").asText());
     }
 
 
@@ -88,7 +90,51 @@
         assertEquals(-1, node.at("/meta/totalResults").asInt());
     }
 
-
+    @Test
+    public void testSearchQueryFreeExtern () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("search").queryParam("q", "[orth=die]")
+                .queryParam("ql", "poliqarp")
+                .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+        assertNotNull(node);
+        assertNotEquals(0, node.path("matches").size());
+        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(FREE)",
+                node.at("/collection/rewrites/0/scope").asText());
+        assertEquals("operation:insertion",
+                node.at("/collection/rewrites/0/operation").asText());
+    }
+    
+    @Test
+    public void testSearchQueryFreeIntern () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("search").queryParam("q", "[orth=die]")
+                .queryParam("ql", "poliqarp")
+                .header(HttpHeaders.X_FORWARDED_FOR, "172.27.0.32")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+        assertNotNull(node);
+        assertNotEquals(0, node.path("matches").size());
+        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(FREE)",
+                node.at("/collection/rewrites/0/scope").asText());
+        assertEquals("operation:insertion",
+                node.at("/collection/rewrites/0/operation").asText());
+    }
+    
+    
     @Test
     public void testSearchQueryExternAuthorized () {
         ClientResponse response = resource().path(getAPIVersion())
@@ -111,9 +157,10 @@
         assertEquals("operation:or", node.at("/collection/operation").asText());
         assertEquals("availability(PUB)",
                 node.at("/collection/rewrites/0/scope").asText());
+        assertEquals("operation:insertion",
+                node.at("/collection/rewrites/0/operation").asText());
     }
 
-
     @Test
     public void testSearchQueryInternAuthorized () {
         ClientResponse response = resource().path(getAPIVersion())
@@ -129,20 +176,23 @@
         JsonNode node = JsonUtils.readTree(entity);
         assertNotNull(node);
         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("operation:or", node.at("/collection/operation").asText());
-        assertEquals("availability(ALL)",
-                node.at("/collection/rewrites/0/scope").asText());
+        //EM: no rewrite is needed
+//        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("operation:or", node.at("/collection/operation").asText());
+//        assertEquals("availability(ALL)",
+//                node.at("/collection/rewrites/0/scope").asText());
+//        assertEquals("operation:insertion",
+//                node.at("/collection/rewrites/0/operation").asText());
     }
 
-
+ // EM: shouldn't this case gets CorpusAccess.PUB ? 
     @Test
-    public void testSearchQueryWithCollectionQueryAuthorizedNoIP () {
+    public void testSearchQueryWithCollectionQueryAuthorizedWithoutIP () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("search").queryParam("q", "[orth=das]")
                 .queryParam("ql", "poliqarp")
@@ -160,110 +210,40 @@
         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("availability",
+                node.at("/collection/operands/0/key").asText());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/0/value").asText());
         assertEquals("textClass",
                 node.at("/collection/operands/1/operands/0/key").asText());
         assertEquals("corpusSigle",
                 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 () {
+    public void testSearchQueryAuthorizedWithoutIP () {
         ClientResponse response = resource().path(getAPIVersion())
-                .path("search").queryParam("q", "[orth=das]")
+                .path("search").queryParam("q", "[orth=die]")
                 .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));
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
         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());
+        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("ACA.*", node.at("/collection/operands/1/value").asText());
+        assertEquals("operation:or", node.at("/collection/operation").asText());
         assertEquals("availability(PUB)",
                 node.at("/collection/rewrites/0/scope").asText());
     }
-
+    
+    
 
     @Test
     @Ignore
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/SearchWithAvailabilityTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/SearchWithAvailabilityTest.java
new file mode 100644
index 0000000..b2f46b0
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/SearchWithAvailabilityTest.java
@@ -0,0 +1,333 @@
+package de.ids_mannheim.korap.web.service.full;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.eclipse.jetty.http.HttpHeaders;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.sun.jersey.api.client.ClientResponse;
+
+import de.ids_mannheim.korap.config.Attributes;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.security.auth.BasicHttpAuth;
+import de.ids_mannheim.korap.utils.JsonUtils;
+import de.ids_mannheim.korap.web.service.FastJerseyTest;
+
+public class SearchWithAvailabilityTest extends FastJerseyTest {
+
+    @Override
+    public void initMethod () throws KustvaktException {
+        helper().runBootInterfaces();
+    }
+
+
+    @BeforeClass
+    public static void configure () throws Exception {
+        FastJerseyTest.setPackages("de.ids_mannheim.korap.web.service.full",
+                "de.ids_mannheim.korap.web.filter",
+                "de.ids_mannheim.korap.web.utils");
+    }
+
+    private void checkAndFree (String json) {
+        JsonNode node = JsonUtils.readTree(json);
+        assertEquals("availability",
+                node.at("/collection/operands/0/key").asText());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/0/value").asText());
+        assertEquals("operation:insertion",
+                node.at("/collection/rewrites/0/operation").asText());
+        assertEquals("availability(FREE)",
+                node.at("/collection/rewrites/0/scope").asText());
+    }
+
+
+    private void checkAndPublic (String json) {
+        JsonNode node = JsonUtils.readTree(json);
+        assertNotNull(node);
+        assertEquals("operation:and",
+                node.at("/collection/operation").asText());
+        assertEquals("match:eq",
+                node.at("/collection/operands/0/operands/0/match").asText());
+        assertEquals("type:regex",
+                node.at("/collection/operands/0/operands/0/type").asText());
+        assertEquals("availability",
+                node.at("/collection/operands/0/operands/0/key").asText());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/0/operands/0/value").asText());
+        assertEquals("match:eq",
+                node.at("/collection/operands/0/operands/1/match").asText());
+        assertEquals("ACA.*",
+                node.at("/collection/operands/0/operands/1/value").asText());
+        assertEquals("operation:insertion",
+                node.at("/collection/rewrites/0/operation").asText());
+        assertEquals("availability(PUB)",
+                node.at("/collection/rewrites/0/scope").asText());
+    }
+    
+    private void checkAndAll (String json) {
+        JsonNode node = JsonUtils.readTree(json);
+        assertNotNull(node);
+        assertEquals("availability(ALL)",
+                node.at("/collection/rewrites/0/scope").asText());
+        assertEquals("operation:insertion",
+                node.at("/collection/rewrites/0/operation").asText());
+        
+        assertEquals("operation:and",
+                node.at("/collection/operation").asText());
+        
+        node = node.at("/collection/operands/0");
+        assertEquals("operation:or",
+                node.at("/operation").asText());
+        
+        assertEquals("match:eq",
+                node.at("/operands/0/match").asText());
+        assertEquals("match:eq",
+                node.at("/operands/0/match").asText());
+        assertEquals("type:regex",
+                node.at("/operands/0/type").asText());
+        assertEquals("availability",
+                node.at("/operands/0/key").asText());
+        assertEquals("QAO.*",
+                node.at("/operands/0/value").asText());
+        
+        node = node.at("/operands/1");
+        assertEquals("operation:or",
+                node.at("/operation").asText());
+        assertEquals("match:eq",
+                node.at("/operands/0/match").asText());
+        assertEquals("ACA.*",
+                node.at("/operands/0/value").asText());
+        assertEquals("match:eq",
+                node.at("/operands/1/match").asText());
+        assertEquals("CC-BY.*",
+                node.at("/operands/1/value").asText());
+        
+    }
+
+
+    private ClientResponse builtSimpleClientResponse (String collectionQuery) {
+        return resource().path(getAPIVersion()).path("search")
+                .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
+                .queryParam("cq", collectionQuery).get(ClientResponse.class);
+    }
+
+
+    private ClientResponse builtClientResponseWithIP (String collectionQuery,
+            String ip) {
+        return resource().path(getAPIVersion()).path("search")
+                .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
+                .queryParam("cq", collectionQuery)
+                .header(Attributes.AUTHORIZATION,
+                        BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
+                .header(HttpHeaders.X_FORWARDED_FOR, ip)
+                .get(ClientResponse.class);
+    }
+
+
+    @Test
+    public void testAvailabilityFreeAuthorized () {
+        ClientResponse response = builtSimpleClientResponse(
+                "availability = CC-BY-SA");
+
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testAvailabilityRegexFreeAuthorized () {
+        ClientResponse response = builtSimpleClientResponse(
+                "availability = /.*BY.*/");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testAvailabilityFreeUnauthorized () {
+        ClientResponse response = builtSimpleClientResponse(
+                "availability = ACA-NC");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testAvailabilityRegexFreeUnauthorized () {
+        ClientResponse response = builtSimpleClientResponse(
+                "availability = /ACA.*/");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testAvailabilityRegexFreeUnauthorized2 () {
+        ClientResponse response = builtSimpleClientResponse(
+                "availability = /.*NC.*/");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+
+    @Test
+    public void testNegationAvailabilityFreeUnauthorized () {
+        ClientResponse response = builtSimpleClientResponse(
+                "availability != /CC-BY.*/");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testNegationAvailabilityFreeUnauthorized2 () {
+        ClientResponse response = builtSimpleClientResponse(
+                "availability != /.*BY.*/");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testComplexNegationAvailabilityFreeUnauthorized () {
+        ClientResponse response = builtSimpleClientResponse(
+                "textClass=politik & availability != /CC-BY.*/");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testComplexAvailabilityFreeUnauthorized () {
+        ClientResponse response = builtSimpleClientResponse(
+                "textClass=politik & availability=ACA-NC");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testComplexAvailabilityFreeUnauthorized3 () {
+        ClientResponse response = builtSimpleClientResponse(
+                "textClass=politik & availability=/.*NC.*/");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndFree(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testAvailabilityPublicAuthorized () {
+        ClientResponse response = builtClientResponseWithIP(
+                "availability=ACA-NC", "149.27.0.32");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        
+        checkAndPublic(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testAvailabilityPublicUnauthorized () {
+        ClientResponse response = builtClientResponseWithIP(
+                "availability=QAO-NC-LOC:ids", "149.27.0.32");
+
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndPublic(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testAvailabilityRegexPublicAuthorized () {
+        ClientResponse response = builtClientResponseWithIP(
+                "availability= /ACA.*/", "149.27.0.32");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndPublic(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testNegationAvailabilityPublicUnauthorized () {
+        ClientResponse response = builtClientResponseWithIP(
+                "availability != ACA-NC", "149.27.0.32");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndPublic(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testNegationAvailabilityRegexPublicUnauthorized () {
+        ClientResponse response = builtClientResponseWithIP(
+                "availability != /ACA.*/", "149.27.0.32");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndPublic(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testComplexAvailabilityPublicUnauthorized () {
+        ClientResponse response = builtClientResponseWithIP(
+                "textClass=politik & availability=QAO-NC-LOC:ids",
+                "149.27.0.32");
+
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndPublic(response.getEntity(String.class));
+    }
+
+
+    @Test
+    public void testNegationComplexAvailabilityPublicUnauthorized () {
+        ClientResponse response = builtClientResponseWithIP(
+                "textClass=politik & availability!=QAO-NC-LOC:ids",
+                "149.27.0.32");
+
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndPublic(response.getEntity(String.class));
+    }
+
+    @Test
+    public void testAvailabilityRegexAllAuthorized () {
+        ClientResponse response = builtClientResponseWithIP(
+                "availability= /ACA.*/", "10.27.0.32");
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        checkAndAll(response.getEntity(String.class));
+    }
+    
+}
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/UserServiceTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/UserServiceTest.java
index 5502366..a3641b1 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/UserServiceTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/UserServiceTest.java
@@ -155,7 +155,7 @@
 
 	// EM: This test require VPN / IDS Intranet
 	@Test
-	@Ignore
+//	@Ignore
 	public void loginJWT() {
 		String en = BasicHttpAuth.encode(credentials[0], credentials[1]);
 		/* lauffähige Version von Hanl: */