From 54212e6c6d902dd4a7716039a06152ed86751630 Mon Sep 17 00:00:00 2001
From: Sonia Zorba <sonia.zorba@inaf.it>
Date: Tue, 12 Jan 2021 14:36:31 +0100
Subject: [PATCH] Retrieved groupread and groupwrite properties from array.
 Added test data SQL file

---
 .../oats/vospace/persistence/NodeDAO.java     | 38 +++++++++++--------
 .../vospace/persistence/DataSourceConfig.java |  2 +
 .../oats/vospace/persistence/NodeDAOTest.java | 15 +++++++-
 src/test/resources/test-data.sql              | 13 +++++++
 4 files changed, 52 insertions(+), 16 deletions(-)
 create mode 100644 src/test/resources/test-data.sql

diff --git a/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java b/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java
index 853e5f2..64ab6b6 100644
--- a/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java
+++ b/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java
@@ -1,5 +1,6 @@
 package it.inaf.oats.vospace.persistence;
 
+import java.sql.Array;
 import net.ivoa.xml.vospace.v2.Node;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -93,38 +94,37 @@ public class NodeDAO {
     private Node getNodeFromResultSet(ResultSet rs) throws SQLException {
 
         Node node = getTypedNode(rs.getString("type"));
-        
-        if(node instanceof DataNode)
-        {
+
+        if (node instanceof DataNode) {
             ((DataNode) node).setBusy(rs.getBoolean("busy_state"));
-        }                
-                
+        }
+
         node.setUri(getUri(rs.getString("vos_path")));
 
         List<Property> properties = new ArrayList<>();
-      
+
         addProperty(getPropertyURI("length"), rs.getString("content_length"),
                 properties);
-        
+
         addProperty(getPropertyURI("btime"), rs.getString("created_on"),
                 properties);
-        
+
         addProperty(getPropertyURI("mtime"), rs.getString("last_modified"),
                 properties);
-        
-        addProperty(getPropertyURI("groupread"), rs.getString("group_read"),
+
+        addProperty(getPropertyURI("groupread"), getGroupsString(rs, "group_read"),
                 properties);
-        
-        addProperty(getPropertyURI("groupwrite"), rs.getString("group_write"),
+
+        addProperty(getPropertyURI("groupwrite"), getGroupsString(rs, "group_write"),
                 properties);
-        
+
         addProperty(getPropertyURI("publicread"), rs.getString("is_public"),
                 properties);
-        
+
         addProperty("urn:async_trans", rs.getString("async_trans"),
                 properties);
 
-        node.setProperties(properties);        
+        node.setProperties(properties);
         return node;
     }
 
@@ -132,6 +132,14 @@ public class NodeDAO {
         return "ivo://ivoa.net/vospace/core#".concat(propertyName);
     }
 
+    private String getGroupsString(ResultSet rs, String column) throws SQLException {
+        Array array = rs.getArray(column);
+        if (array == null) {
+            return null;
+        }
+        return String.join(" ", (String[]) array.getArray());
+    }
+
     // If value is null does nothing
     private void addProperty(String uri, String value, List<Property> list) {
         if (value != null) {
diff --git a/src/test/java/it/inaf/oats/vospace/persistence/DataSourceConfig.java b/src/test/java/it/inaf/oats/vospace/persistence/DataSourceConfig.java
index 9ba7203..eadeaec 100644
--- a/src/test/java/it/inaf/oats/vospace/persistence/DataSourceConfig.java
+++ b/src/test/java/it/inaf/oats/vospace/persistence/DataSourceConfig.java
@@ -84,6 +84,8 @@ public class DataSourceConfig {
                 ByteArrayResource scriptResource = replaceDollarQuoting(script.toPath());
                 ScriptUtils.executeSqlScript(conn, scriptResource);
             }
+
+            ScriptUtils.executeSqlScript(conn, new ClassPathResource("test-data.sql"));
         }
     }
 
diff --git a/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java b/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java
index b075723..d1cfcf0 100644
--- a/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java
+++ b/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java
@@ -2,6 +2,8 @@ package it.inaf.oats.vospace.persistence;
 
 import javax.sql.DataSource;
 import net.ivoa.xml.vospace.v2.ContainerNode;
+import net.ivoa.xml.vospace.v2.Node;
+import net.ivoa.xml.vospace.v2.Property;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -28,6 +30,17 @@ public class NodeDAOTest {
     @Test
     public void testListNode() {
         ContainerNode root = (ContainerNode) dao.listNode("/").get();
-        assertEquals(4, root.getNodes().size());
+        assertEquals(2, root.getNodes().size());
+
+        assertEquals("group1 group2", getProperty(root.getNodes().get(0), "ivo://ivoa.net/vospace/core#groupread"));
+    }
+
+    private String getProperty(Node node, String uri) {
+        for (Property property : node.getProperties()) {
+            if (uri.equals(property.getUri())) {
+                return property.getValue();
+            }
+        }
+        return null;
     }
 }
diff --git a/src/test/resources/test-data.sql b/src/test/resources/test-data.sql
new file mode 100644
index 0000000..2a0c10b
--- /dev/null
+++ b/src/test/resources/test-data.sql
@@ -0,0 +1,13 @@
+DELETE FROM node;
+ALTER SEQUENCE node_node_id_seq RESTART WITH 1;
+
+INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id) VALUES (NULL, NULL, '', 'container', '0', '0');
+
+INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, group_read, group_write) VALUES ('', NULL, 'test1', 'container', 'user1', 'user1', '{"group1","group2"}','{"group2"}');      -- /test1
+INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id) VALUES ('2', '', 'f1', 'container', 'user1', 'user1');      -- /test1/f1 (rel: /f1)
+INSERT INTO node (parent_path, parent_relative_path, name, os_name, type, owner_id, creator_id) VALUES ('2.3', '3', 'f2_renamed', 'f2', 'container', 'user1', 'user1');      -- /test1/f1/f2_renamed (rel: /f1/f2)
+INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id) VALUES ('2.3.4', '3.4', 'f3', 'data', 'user1', 'user1');      -- /test1/f1/f2_renamed/f3 (rel: /f1/f2/f3)
+
+INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, is_public) VALUES ('', NULL, 'test2', 'container', 'user2', 'user2', true);      -- /test2
+INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, is_public) VALUES ('5', '', 'f4', 'container', 'user2', 'user2', true);    -- /test2/f4 (rel: /f4)
+INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, is_public) VALUES ('5', '', 'f5', 'container', 'user2', 'user2', true);    -- /test2/f5 (rel: /f5)
-- 
GitLab