Fixed the search service with type and id path parameters.

Change-Id: I685535e1a015827e6f03e60d58b2aa88733fcfde
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/MatchInfoServiceTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/MatchInfoServiceTest.java
index 4e2f9d2..c3f3724 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/MatchInfoServiceTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/MatchInfoServiceTest.java
@@ -68,7 +68,7 @@
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
         String entity = response.getEntity(String.class);
-        System.out.println(entity);
+//        System.out.println(entity);
         JsonNode node = JsonUtils.readTree(entity);
         assertNotNull(node);
         assertEquals("WPD15/B07/51608", node.at("/textSigle").asText());
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/QuerySerializationServiceTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/QuerySerializationServiceTest.java
index 9b77a8b..5fa2cd0 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/QuerySerializationServiceTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/QuerySerializationServiceTest.java
@@ -204,7 +204,7 @@
     public void testQuerySerializationOfVirtualCollection () {
         ClientResponse response = resource()
                 .path(getAPIVersion())
-                .path("collection/WPD15-VC/search")
+                .path("collection/GOE-VC/search")
                 .queryParam("q", "[orth=der]")
                 .queryParam("ql", "poliqarp")
                 .queryParam("context", "base/s:s")
@@ -217,11 +217,11 @@
         assertEquals("koral:doc", node.at("/collection/operands/0/@type")
                 .asText());
         assertEquals("corpusSigle", node.at("/collection/operands/0/key").asText());
-        assertEquals("WPD15", node.at("/collection/operands/0/value").asText());
+        assertEquals("GOE", node.at("/collection/operands/0/value").asText());
         assertEquals("koral:doc", node.at("/collection/operands/1/@type")
                 .asText());
         assertEquals("creationDate", node.at("/collection/operands/1/key").asText());
-        assertEquals("2014-04-01", node.at("/collection/operands/1/value").asText());
+        assertEquals("1810-01-01", node.at("/collection/operands/1/value").asText());
 
     }
     
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceInfoServiceTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceInfoServiceTest.java
index 2dba542..a7689ca 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceInfoServiceTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceInfoServiceTest.java
@@ -43,8 +43,8 @@
                 .path("collection").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(1, node.size());
     }
@@ -63,27 +63,41 @@
         JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
         assertNotNull(node);
         assertTrue(node.isArray());
-        assertEquals(2, node.size());
+        assertEquals(3, node.size());
     }
 
 
     @Test
     public void testGetVirtualCollectionInfoById () {
         ClientResponse response = resource().path(getAPIVersion())
-                .path("collection").path("WPD15-VC").get(ClientResponse.class);
+                .path("collection").path("GOE-VC").get(ClientResponse.class);
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
         String ent = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
         assertNotEquals(0, node.size());
-        assertEquals("Wikipedia Virtual Collection",
+        assertEquals("Goethe Virtual Collection",
                 node.path("name").asText());
-        assertEquals("German Wikipedia 2015",
+        assertEquals("Goethe works from 1810",
                 node.path("description").asText());
     }
-
-
+    
+    @Test
+    public void testGetVirtualCollectionInfoByIdUnauthorized () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("collection").path("WPD15-VC").get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                response.getStatus());
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertNotEquals(0, node.size());
+        assertEquals(101, node.at("/errors/0/0").asInt());
+        assertEquals("[Cannot found public resources with ids: [WPD15-VC]]",
+                node.at("/errors/0/2").asText());
+    }
+    
     @Test
     public void testGetPublicCorporaInfo () {
         ClientResponse response = resource().path(getAPIVersion())
@@ -106,7 +120,7 @@
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
         String ent = response.getEntity(String.class);
-        System.out.println(ent);
+//        System.out.println(ent);
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
         assertTrue(node.isObject());
@@ -163,10 +177,12 @@
         assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
                 response.getStatus());
         String ent = response.getEntity(String.class);
-        JsonNode error = JsonUtils.readTree(ent).get("errors").get(0);
-        assertEquals(101, error.get(0).asInt());
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertNotEquals(0, node.size());
+        assertEquals(101, node.at("/errors/0/0").asInt());
         assertEquals("[Cannot found public resources with ids: [ZUW19]]",
-                error.get(2).asText());
+                node.at("/errors/0/2").asText());
     }
 
 
@@ -179,10 +195,12 @@
         assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
                 response.getStatus());
         String ent = response.getEntity(String.class);
-        JsonNode error = JsonUtils.readTree(ent).get("errors").get(0);
-        assertEquals(101, error.get(0).asInt());
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertNotEquals(0, node.size());
+        assertEquals(101, node.at("/errors/0/0").asInt());
         assertEquals("[Cannot found public resources with ids: [BRZ10]]",
-                error.get(2).asText());
+                node.at("/errors/0/2").asText());
     }
 
 
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 a6f6edd..bad37ca 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
@@ -4,6 +4,9 @@
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.util.Iterator;
+import java.util.Set;
+
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -14,13 +17,15 @@
 import de.ids_mannheim.korap.config.Attributes;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.query.serialize.QuerySerializer;
+import de.ids_mannheim.korap.resources.Corpus;
+import de.ids_mannheim.korap.security.ac.ResourceFinder;
 import de.ids_mannheim.korap.security.auth.BasicHttpAuth;
 import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.web.service.FastJerseyTest;
 
 /**
  * @author hanl, margaretha
- * @lastUpdate 19/04/2017
+ * @lastUpdate 21/04/2017
  *
  */
 public class SearchServiceTest extends FastJerseyTest {
@@ -38,7 +43,6 @@
                 "de.ids_mannheim.korap.web.utils");
     }
 
-
     @Test
     public void testSearchQueryPublicCorpora () {
         ClientResponse response = resource().path(getAPIVersion())
@@ -57,9 +61,8 @@
         assertEquals(6218, node.at("/meta/totalResults").asInt());
     }
 
-
     @Test
-    public void testSearchSimpleAuthorized () {
+    public void testSearchQueryAuthorized () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("search").queryParam("q", "[orth=die]")
                 .queryParam("ql", "poliqarp")
@@ -79,7 +82,7 @@
 
 
     @Test
-    public void testSearchSimpleWithCQAuthorized () {
+    public void testSearchQueryWithCollectionQueryAuthorized () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("search").queryParam("q", "[orth=das]")
                 .queryParam("ql", "poliqarp")
@@ -100,26 +103,8 @@
         assertEquals("koral:token", node.at("/query/@type").asText());
     }
 
-
     @Test
-    public void testSearchSimpleDemo () {
-        ClientResponse response = resource().path(getAPIVersion())
-                .path("search").queryParam("q", "[orth=der]")
-                .queryParam("ql", "poliqarp").get(ClientResponse.class);
-        assertEquals(ClientResponse.Status.OK.getStatusCode(),
-                response.getStatus());
-        String ent = response.getEntity(String.class);
-        JsonNode node = JsonUtils.readTree(ent);
-        assertNotNull(node);
-        assertNotEquals(0, node.path("matches").size());
-        assertEquals("corpusSigle([GOE, WPD13])",
-                node.at("/collection/rewrites/0/scope").asText());
-    }
-
-
-    @Test
-    @Ignore
-    public void testSearchPublicCorpusWithID () {
+    public void testSearchForPublicCorpusWithStringId () {
         ClientResponse response = resource().path(getAPIVersion())
                 .path("corpus").path("GOE").path("search")
                 .queryParam("q", "blau").queryParam("ql", "poliqarp")
@@ -129,14 +114,132 @@
         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());
+        assertNotEquals(0, node.path("matches").size());
+        assertEquals(32, node.at("/meta/totalResults").asInt());
+    }
+    
+    @Test
+    public void testSearchForVirtualCollectionWithStringId () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("collection").path("GOE-VC").path("search")
+                .queryParam("q", "blau").queryParam("ql", "poliqarp")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String ent = response.getEntity(String.class);
+        System.out.println(ent);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
         assertEquals("koral:docGroup", node.at("/collection/@type").asText());
-        assertEquals("operation:or", node.at("/collection/operation").asText());
+        assertEquals("operation:and", node.at("/collection/operation").asText());
         assertNotEquals(0, node.at("/collection/operands").size());
-        assertEquals("corpusSigle([GOE])",
-                node.at("/collection/rewrites/0/scope").asText());
-        assertEquals(6218, node.at("/meta/totalResults").asInt());
+        assertEquals("corpusSigle",
+                node.at("/collection/operands/0/key").asText());
+        assertEquals("GOE",
+                node.at("/collection/operands/0/value").asText());
+        assertEquals("creationDate",
+                node.at("/collection/operands/1/key").asText());
+        assertEquals("1810-01-01",
+                node.at("/collection/operands/1/value").asText());
+        assertEquals(1, node.at("/meta/totalResults").asInt());
     }
 
+    
+    @Test
+    public void testSearchForPublicCorpusWithIntegerId () throws KustvaktException {
+        Set<Corpus> publicCorpora = ResourceFinder.searchPublic(Corpus.class);
+        Iterator<Corpus> i = publicCorpora.iterator();
+        String id = null;
+        while (i.hasNext()){
+            Corpus c = i.next();
+            if (c.getName().equals("Goethe")){
+                id =c.getId().toString();
+            }
+        }
+            
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("corpus").path(id).path("search")
+                .queryParam("q", "blau").queryParam("ql", "poliqarp")
+                .get(ClientResponse.class);
+        
+        String ent = response.getEntity(String.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        
+        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());
+        assertNotEquals(0, node.path("matches").size());
+    }
+    
+    @Test
+    public void testSearchForCorpusWithStringIdUnauthorized () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("corpus").path("WPD15").path("search")
+                .queryParam("q", "blau").queryParam("ql", "poliqarp")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                response.getStatus());
+        String ent = response.getEntity(String.class);
+        JsonNode error = JsonUtils.readTree(ent).get("errors").get(0);
+        assertEquals(101, error.get(0).asInt());
+        assertEquals("[Cannot found public resources with ids: [WPD15]]",
+                error.get(2).asText());
+    }
+    
+    @Test
+    public void testSearchForOwnersCorpusWithStringId () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("corpus").path("WPD15").path("search")
+                .queryParam("q", "[orth=das]")
+                .queryParam("ql", "poliqarp")
+                .header(Attributes.AUTHORIZATION,
+                        BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        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());
+    }
+    
+    @Test
+    public void testSearchForOwnersCorpusWithIntegerId () throws KustvaktException {
+        Set<Corpus> publicCorpora = ResourceFinder.searchPublic(Corpus.class);
+        Iterator<Corpus> i = publicCorpora.iterator();
+        String id = null;
+        while (i.hasNext()){
+            Corpus c = i.next();
+            if (c.getPersistentID().equals("WPD15")){
+                id =c.getId().toString();
+            }
+        }
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("corpus").path("5").path("search")
+                .queryParam("q", "[orth=das]")
+                .queryParam("ql", "poliqarp")
+                .header(Attributes.AUTHORIZATION,
+                        BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        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());
+    }
 
     @Test
     public void testSearchSentenceMeta () {
diff --git a/src/test/resources/policy-test.conf b/src/test/resources/policy-test.conf
index a4c49bd..35b0fd9 100644
--- a/src/test/resources/policy-test.conf
+++ b/src/test/resources/policy-test.conf
@@ -1,12 +1,12 @@
 # type	id	name	description	condition	permissions	collectionQuery
 
-virtualcollection	WPD15-VC	Wikipedia Virtual Collection	German Wikipedia 2015	public	read	corpusSigle=WPD15 & creationDate since 2014-04-01	
+virtualcollection	WPD15-VC	Wikipedia Virtual Collection	German Wikipedia 2015	ids	read	corpusSigle=WPD15 & creationDate since 2014-04-01	
+virtualcollection	GOE-VC	Goethe Virtual Collection	Goethe works from 1810	public	read	corpusSigle=GOE & creationDate since 1810-01-01
 virtualcollection	BRZ10-PC	Braunschweiger Collection	Selected Braunschweiger Zeitung	ids	read	corpusSigle=BRZ10 & foundries ~ Connexor
 corpus	WPD13	Wikipedia 2013	Public German Wikipedia 2013	public	read
 corpus	WPD15	Wikipedia 2015	IDS German Wikipedia 2015	ids	read
 corpus	GOE	Goethe	Goethe corpus	public	read
 corpus	BRZ10	Braunschweiger	Braunschweiger Zeitung 2010	ids	read
-type	id	name	description	condition	permissions
 foundry	base	base	Base foundry	public	read
 foundry	dereko	dereko	DeReKo foundry	public	read
 foundry	corenlp	corenlp	CoreNLP parser	public	read