Fixed some resource service testcases.

Change-Id: I935e8e8c9aaf166983d33d0f0fd07b953be0bc12
diff --git a/src/main/java/de/ids_mannheim/korap/resources/VirtualCollection.java b/src/main/java/de/ids_mannheim/korap/resources/VirtualCollection.java
index cf87bff..bda1eea 100644
--- a/src/main/java/de/ids_mannheim/korap/resources/VirtualCollection.java
+++ b/src/main/java/de/ids_mannheim/korap/resources/VirtualCollection.java
@@ -38,8 +38,9 @@
         if (resource == null | !(resource instanceof VirtualCollection))
             return;
         VirtualCollection other = (VirtualCollection) resource;
-        this.setFields(this.getFields() == null ? other.getFields() : this
-                .getFields());
+        if (this.getFields() == null || this.getFields().isEmpty()){
+        	setFields(other.getFields());
+        }
     }
 
 
diff --git a/src/main/java/de/ids_mannheim/korap/web/service/CollectionLoader.java b/src/main/java/de/ids_mannheim/korap/web/service/CollectionLoader.java
index 359643a..9dfe0a6 100644
--- a/src/main/java/de/ids_mannheim/korap/web/service/CollectionLoader.java
+++ b/src/main/java/de/ids_mannheim/korap/web/service/CollectionLoader.java
@@ -29,7 +29,7 @@
 
         KustvaktConfiguration config = beans.getConfiguration();
         
-        KoralCollectionQueryBuilder bui = new KoralCollectionQueryBuilder();
+//        KoralCollectionQueryBuilder bui = new KoralCollectionQueryBuilder();
 //        bui.with("creationDate since 1775 & corpusSigle=GOE");
 //
 //        VirtualCollection c1 = new VirtualCollection();
diff --git a/src/main/java/de/ids_mannheim/korap/web/service/PolicyLoader.java b/src/main/java/de/ids_mannheim/korap/web/service/PolicyLoader.java
index c01fc66..928b490 100644
--- a/src/main/java/de/ids_mannheim/korap/web/service/PolicyLoader.java
+++ b/src/main/java/de/ids_mannheim/korap/web/service/PolicyLoader.java
@@ -4,6 +4,8 @@
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.resources.Corpus;
+import de.ids_mannheim.korap.resources.Foundry;
+import de.ids_mannheim.korap.resources.KustvaktResource;
 import de.ids_mannheim.korap.resources.Permissions;
 import de.ids_mannheim.korap.security.ac.PolicyBuilder;
 import de.ids_mannheim.korap.security.ac.ResourceFinder;
@@ -25,25 +27,26 @@
                 .toUser(KustvaktConfiguration.KUSTVAKT_USER);
         KustvaktConfiguration config =beans.getConfiguration();
         PolicyBuilder builder = new PolicyBuilder(user);
-//        builder.addCondition("public");
-//        builder.setResources(new Corpus("GOE"));
-//        builder.setPermissions(Permissions.Permission.READ);
-//        builder.create();
-
         builder = new PolicyBuilder(user);
         builder.addCondition("public");
         builder.setResources(new Corpus(config.getDefaultVirtualCollectionId()));
         builder.setPermissions(Permissions.Permission.READ);
         builder.create();
 
-//        KustvaktResource tt = new Foundry("tt");
-//        tt.setName("TreeTagger");
-//        tt.setDescription("todo ...");
-//        builder = new PolicyBuilder(user);
-//        builder.addCondition("public");
-//        builder.setResources(tt);
-//        builder.setPermissions(Permissions.Permission.READ);
-//        builder.create();
+        // FIX ME: the following policies are used in test cases and should not be hard coded here.
+        builder.addCondition("public");
+        builder.setResources(new Corpus("GOE"));
+        builder.setPermissions(Permissions.Permission.READ);
+        builder.create();
+        
+        KustvaktResource tt = new Foundry("tt");
+        tt.setName("TreeTagger");
+        tt.setDescription("todo ...");
+        builder = new PolicyBuilder(user);
+        builder.addCondition("public");
+        builder.setResources(tt);
+        builder.setPermissions(Permissions.Permission.READ);
+        builder.create();
     }
 
 
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 afc4851..feee22e 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
@@ -842,16 +842,18 @@
 				cquery.with(query);
 
 			cachetmp = ResourceFactory.getCachedCollection(cquery.toJSON());
-
+			
 			// see if vc was cached!
 			VirtualCollection tmp = resourceHandler.getCache(cachetmp.getId(), VirtualCollection.class);
-
+			
 			// if not cached, fill with stats values
 			if (tmp == null) {
 				String stats = searchKrill.getStatistics(cquery.toJSON());
 				cachetmp.setStats(JsonUtils.readSimple(stats, Map.class));
+				if (query != null && !query.isEmpty())
+					cachetmp.setFields(cquery.toJSON());
 			}
-
+			
 			if (!cache && !User.UserFactory.isDemo(ctx.getUsername())) {
 				collection = ResourceFactory.getPermanentCollection(cachetmp, name, description);
 				vals = collection.toMap();
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 9d0bd88..d036b57 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
@@ -31,12 +31,14 @@
  * @date 14/01/2016
  */
 public class ResourceServiceTest extends FastJerseyTest {
-
+	
     @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");
+        
+        //containerURI = "https://localhost/";
     }
 
 
@@ -262,67 +264,8 @@
         assertNotEquals(0, node.size());
     }
 
-
     @Test
-    public void testCorpusGet () {
-        ClientResponse response = resource().path(getAPIVersion())
-                .path("corpus").path("WPD").get(ClientResponse.class);
-        String ent = response.getEntity(String.class);
-        assertEquals(ClientResponse.Status.OK.getStatusCode(),
-                response.getStatus());
-
-        JsonNode node = JsonUtils.readTree(ent);
-        assertNotNull(node);
-        assertTrue(node.isObject());
-        assertEquals("WPD", node.path("id").asText());
-    }
-
-
-    @Test
-    public void testCorpusGet2 () {
-        ClientResponse response = resource().path(getAPIVersion())
-                .path("corpus").path("GOE").get(ClientResponse.class);
-        assertEquals(ClientResponse.Status.OK.getStatusCode(),
-                response.getStatus());
-        String ent = response.getEntity(String.class);
-        JsonNode node = JsonUtils.readTree(ent);
-        assertNotNull(node);
-        assertTrue(node.isObject());
-        assertEquals("GOE", node.path("id").asText());
-    }
-
-
-    @Test
-    @Ignore
-    public void testCorpusGetUnauthorized () {
-        ClientResponse response = resource().path(getAPIVersion())
-                .path("corpus").path("BRZ20").get(ClientResponse.class);
-        assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
-                response.getStatus());
-        String ent = response.getEntity(String.class);
-        JsonNode node = JsonUtils.readTree(ent);
-        assertNotNull(node);
-        assertTrue(node.isObject());
-        assertNotNull(node);
-    }
-
-
-    @Test
-    public void testFoundryGet () {
-        ClientResponse response = resource().path(getAPIVersion())
-                .path("foundry").path("tt").get(ClientResponse.class);
-        String ent = response.getEntity(String.class);
-        assertEquals(ClientResponse.Status.OK.getStatusCode(),
-                response.getStatus());
-
-        JsonNode node = JsonUtils.readTree(ent);
-        assertNotNull(node);
-        assertNotEquals(0, node.size());
-    }
-
-
-    @Test
-    public void testResourceStoreAndDelete () throws KustvaktException {
+    public void testResourceStore() throws KustvaktException {
     	// resource store service
         ClientResponse response = resource()
                 .path(getAPIVersion())
@@ -344,7 +287,9 @@
         assertTrue(node.isObject());
         assertEquals("Goethe", node.path("name").asText());
         assertEquals("Goethe corpus", node.path("description").asText());
-        
+
+        String id = node.path("id").asText();
+        		
         // check if the resource is in the db
         ResourceDao<?> dao = new ResourceDao<>(helper().getContext()
                 .getPersistenceClient());
@@ -352,15 +297,16 @@
                 .getDatabase());
 
         assertNotEquals(0, dao.size());
-        KustvaktResource res = dao.findbyId(node.path("id").asText(),
+        KustvaktResource res = dao.findbyId(id,
                 User.UserFactory.getDemoUser());
         assertNotNull(res);
         Assert.assertEquals("Goethe",res.getName().toString());
     
         // delete resource service
-        response = resource()
+    	response = resource()
                 .path(getAPIVersion())
-                .path("virtualcollection/"+node.path("id").asText())
+                .path("virtualcollection")
+                .path(id)
                 .header(Attributes.AUTHORIZATION,
                         BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
                 .delete(ClientResponse.class);
@@ -374,16 +320,104 @@
         assertEquals("sqlite", helper().getContext().getPersistenceClient()
                 .getDatabase());
 
-        res = dao.findbyId(node.path("id").asText(),
+        res = dao.findbyId(id,
                 User.UserFactory.getDemoUser());
         assertEquals(null,res);
     }
 
 
     @Test
+    public void testCorpusGet () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("corpus").path("WPD15").get(ClientResponse.class);
+        String ent = response.getEntity(String.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertTrue(node.isObject());
+        assertEquals("WPD15", node.path("id").asText());
+    }
+
+    
+
+//    @Test
+//    public void testCreatePolicyForResource() {
+//    	ClientResponse response = resource()
+//                .path(getAPIVersion())
+//                .path("admin")
+//                .path("createPolicies")
+//                .path(UUID.randomUUID().toString())
+//                .queryParam("type", "virtualcollection")
+//                .queryParam("name", "Goethe")
+//                .queryParam("description", "Goethe corpus")
+//                .queryParam("group", "public")
+//                .queryParam("perm", Permission.READ.name())
+//                .queryParam("loc", "")
+//                .queryParam("expire", "")
+//                .header(Attributes.AUTHORIZATION,
+//                        BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
+//                .post(ClientResponse.class);
+//        
+//        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+//                response.getStatus());
+//        
+//        String ent = response.getEntity(String.class);
+//        JsonNode node = JsonUtils.readTree(ent);
+//        assertNotNull(node);
+//        assertTrue(node.isObject());
+//        assertEquals("GOE", node.path("id").asText());
+//	}
+    
+    @Test
+    public void testCorpusGet2 () {
+    	ClientResponse response = resource().path(getAPIVersion())
+                .path("corpus").path("GOE").get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertTrue(node.isObject());
+        assertEquals("GOE", node.path("id").asText());
+    }
+    
+    
+    @Test
+    @Ignore
+    public void testCorpusGetUnauthorized () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("corpus").path("BRZ20").get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
+                response.getStatus());
+        String ent = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertTrue(node.isObject());
+        assertNotNull(node);
+    }
+
+    @Test
+    public void testFoundryGet () {
+        ClientResponse response = resource().path(getAPIVersion())
+                .path("foundry").path("tt").get(ClientResponse.class);
+        String ent = response.getEntity(String.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertNotEquals(0, node.size());
+    }
+
+
+    
+
+    @Test
     public void testSerializationQueryWithCorpusThroughFilteredPublic () {
         ClientResponse response = resource().path(getAPIVersion())
-                .path("corpus/WPD/search").queryParam("q", "[orth=der]")
+                .path("corpus/WPD15/search").queryParam("q", "[orth=der]")
                 .queryParam("ql", "poliqarp").queryParam("context", "base/s:s")
                 .method("TRACE", ClientResponse.class);
         String ent = response.getEntity(String.class);
@@ -393,7 +427,7 @@
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
         assertEquals("corpusSigle", node.at("/collection/key").asText());
-        assertEquals("WPD", node.at("/collection/value").asText());
+        assertEquals("WPD15", node.at("/collection/value").asText());
     }
 
 
@@ -401,7 +435,7 @@
     public void testSerializationQueryWithCorpus () {
         ClientResponse response = resource()
                 .path(getAPIVersion())
-                .path("corpus/WPD/search")
+                .path("corpus/WPD15/search")
                 .queryParam("q", "[orth=der]")
                 .queryParam("ql", "poliqarp")
                 .header(Attributes.AUTHORIZATION,
@@ -412,15 +446,35 @@
         String ent = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
-        System.out.println("NODE "+node.asText());
         assertEquals("koral:doc", node.at("/collection/@type").asText());
         assertEquals("corpusSigle", node.at("/collection/key").asText());
     }
 
-
     @Test
     public void testSerializationQueryWithCollection () {
-        ClientResponse response = resource()
+    	// Add Virtual Collection
+    	ClientResponse response = resource()
+                .path(getAPIVersion())
+                .path("virtualcollection")
+                .queryParam("filter", "false")
+                .queryParam("name", "Weimarer Werke")
+                .queryParam("description", "Goethe-Werke in Weimar (seit 1775)")
+                .header(Attributes.AUTHORIZATION,
+                        BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
+                .post(ClientResponse.class, "creationDate since 1775 & corpusSigle=GOE");
+        
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        
+        String ent = response.getEntity(String.class);
+        
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertTrue(node.isObject());
+        assertEquals("Weimarer Werke", node.path("name").asText());
+
+        // Get virtual collections
+        response = resource()
                 .path(getAPIVersion())
                 .path("collection")
                 .header(Attributes.AUTHORIZATION,
@@ -428,8 +482,8 @@
                 .get(ClientResponse.class);
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
-        String ent = response.getEntity(String.class);
-        JsonNode node = JsonUtils.readTree(ent);
+        ent = response.getEntity(String.class);
+        node = JsonUtils.readTree(ent);
         assertNotNull(node);
 
         Iterator it = node.elements();