From 5b8aed9e1024ef9ae5544675d025091f78c423a5 Mon Sep 17 00:00:00 2001 From: Nicola Fulvio Calabria <nicola.calabria@inaf.it> Date: Sat, 30 Jan 2021 01:34:41 +0100 Subject: [PATCH] Task #3550 Implmenetation and tests for deleteNode method in NodeDAO --- .../oats/vospace/persistence/NodeDAO.java | 63 ++++++++++++++----- .../oats/vospace/persistence/NodeDAOTest.java | 17 +++-- src/test/resources/test-data.sql | 16 ++--- 3 files changed, 66 insertions(+), 30 deletions(-) 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 6c2c6cd..ade9e69 100644 --- a/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java +++ b/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java @@ -134,7 +134,17 @@ public class NodeDAO { } select += "*{1}'"; return select; - } + } + + private String getAllLevelsChildrenSelector(String path) { + String select = "(SELECT path FROM node WHERE node_id = (SELECT node_id FROM node_vos_path WHERE vos_path = ?))::varchar || '"; + + if (!"/".equals(path)) { + select += "."; + } + select += "*{1,}'"; + return select; + } private Node getNodeFromResultSet(ResultSet rs) throws SQLException { @@ -181,35 +191,54 @@ public class NodeDAO { return node; } - // just a stub - public void deleteNode(String path) - { + public void deleteNode(String path) { int nodesWithPath = countNodesWithPath(path); - if(nodesWithPath == 0) - { + if (nodesWithPath == 0) { throw new IllegalStateException("Node at path " - + path + " not found"); + + path + " not found"); } - - if(nodesWithPath > 1) - { + + if (nodesWithPath > 1) { throw new IllegalStateException("Multiple nodes at path " + path); } - // Complete stub... + String insertSql = "INSERT INTO deleted_node " + + "(node_id, parent_path, parent_relative_path, " + + "name, os_name, tstamp_wrapper_dir, type, location_id, format, " + + "async_trans, busy_state, owner_id, creator_id, group_read, " + + "group_write, is_public, delta, content_type, content_encoding, " + + "content_length, content_md5, created_on, last_modified, " + + "accept_views, provide_views, protocols)\n"; - } - + String deleteSql = "DELETE \n" + + "FROM node n\n" + + "USING node_vos_path os\n" + + "WHERE n.node_id = os.node_id AND\n" + + "(n.path ~ (" + getAllLevelsChildrenSelector(path) + ")::lquery\n" + + "OR os.vos_path = ?) RETURNING\n" + + "n.node_id, parent_path, parent_relative_path, " + + "name, os_name, tstamp_wrapper_dir, type, location_id, format, " + + "async_trans, busy_state, owner_id, creator_id, group_read, " + + "group_write, is_public, delta, content_type, content_encoding, " + + "content_length, content_md5, created_on, last_modified, " + + "accept_views, provide_views, protocols\n"; + + String withSql = "WITH del AS ("+deleteSql+")"; + + String sql = withSql+insertSql+"SELECT * FROM del\n"; + + jdbcTemplate.update(sql, path, path); + } + // utility method for deleteNode - public int countNodesWithPath(String path) - { + public int countNodesWithPath(String path) { String sql = "SELECT COUNT(*) from " + "node_vos_path p " + "where p.vos_path = ?"; - + Object[] args = {path}; int[] types = {Types.VARCHAR}; - + return jdbcTemplate.queryForObject(sql, args, types, Integer.class); } 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 0eb14df..bb41fc7 100644 --- a/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java +++ b/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java @@ -67,16 +67,23 @@ public class NodeDAOTest { assertEquals(1, dao.countNodesWithPath("/test2"), "Test db has been changed"); - assertEquals(1, dao.countNodesWithPath("/test2/f4")); - assertEquals(1, dao.countNodesWithPath("/test2/f5")); + assertEquals(1, dao.countNodesWithPath("/test2/f4"), "Test db has been changed"); + assertEquals(1, dao.countNodesWithPath("/test2/f5"), "Test db has been changed"); - assertEquals(0, dao.countNodesWithPath("/pippooo")); + assertEquals(0, dao.countNodesWithPath("/pippooo"), "Test db has been changed"); } - // Test stub - //@Test + @Test public void testDeleteNode() { + assertEquals(1, dao.countNodesWithPath("/test1/f1/f2_renamed/f3"), "Test db has been changed"); + dao.deleteNode("/test1"); + + assertEquals(0, dao.countNodesWithPath("/test1")); + assertEquals(0, dao.countNodesWithPath("/test1/f1")); + assertEquals(0, dao.countNodesWithPath("/test1/f1/f2_renamed")); + assertEquals(0, dao.countNodesWithPath("/test1/f1/f2_renamed/f3")); + } private String getProperty(Node node, String uri) { diff --git a/src/test/resources/test-data.sql b/src/test/resources/test-data.sql index 3f032aa..37ca244 100644 --- a/src/test/resources/test-data.sql +++ b/src/test/resources/test-data.sql @@ -1,16 +1,16 @@ 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, location_id) VALUES (NULL, NULL, '', 'container', '0', '0', 1); -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, group_read, group_write, location_id) VALUES ('', NULL, 'test1', 'container', 'user1', 'user1', '{"group1","group2"}','{"group2"}', 1); -- /test1 +INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, location_id) VALUES ('2', '', 'f1', 'container', 'user1', 'user1', 1); -- /test1/f1 (rel: /f1) +INSERT INTO node (parent_path, parent_relative_path, name, os_name, type, owner_id, creator_id, location_id) VALUES ('2.3', '3', 'f2_renamed', 'f2', 'container', 'user1', 'user1', 1); -- /test1/f1/f2_renamed (rel: /f1/f2) +INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, location_id) VALUES ('2.3.4', '3.4', 'f3', 'data', 'user1', 'user1', 1); -- /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 ('6', '', '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 ('6', '', 'f5', 'container', 'user2', 'user2'/*, true*/); -- /test2/f5 (rel: /f5) +INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, is_public, location_id) VALUES ('', NULL, 'test2', 'container', 'user2', 'user2', true, 1); -- /test2 +INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, is_public, location_id) VALUES ('6', '', 'f4', 'container', 'user2', 'user2', true, 1); -- /test2/f4 (rel: /f4) +INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, is_public, location_id) VALUES ('6', '', 'f5', 'container', 'user2', 'user2', true, 1); -- /test2/f5 (rel: /f5) DELETE FROM job; -- GitLab