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 b6021f217d757681a799e23bebdc15b143bf4646..6c2c6cd4dd097b86194281366a32e5cdc3d57889 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,7 @@ public class NodeDAO { } select += "*{1}'"; return select; - } + } private Node getNodeFromResultSet(ResultSet rs) throws SQLException { @@ -180,6 +180,38 @@ public class NodeDAO { node.setProperties(properties); return node; } + + // just a stub + public void deleteNode(String path) + { + int nodesWithPath = countNodesWithPath(path); + if(nodesWithPath == 0) + { + throw new IllegalStateException("Node at path " + + path + " not found"); + } + + if(nodesWithPath > 1) + { + throw new IllegalStateException("Multiple nodes at path " + path); + } + + // Complete stub... + + } + + // utility method for deleteNode + 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); + } private String getPropertyURI(String propertyName) { return "ivo://ivoa.net/vospace/core#".concat(propertyName); 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 ee7bbe23edb92f3c538f71bc6e85279d728b63c0..0eb14dfb75a520d0b971ecc11a6e07d7210cc656 100644 --- a/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java +++ b/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java @@ -9,6 +9,7 @@ import net.ivoa.xml.vospace.v2.Node; import net.ivoa.xml.vospace.v2.Property; import net.ivoa.xml.vospace.v2.View; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -56,6 +57,28 @@ public class NodeDAOTest { assertEquals("group1 group2", getProperty(root.getNodes().get(0), "ivo://ivoa.net/vospace/core#groupread")); } + @Test + public void testCountNodeWithPath() { + assertEquals(1, dao.countNodesWithPath("/")); + assertEquals(1, dao.countNodesWithPath("/test1"), "Test db has been changed"); + assertEquals(1, dao.countNodesWithPath("/test1/f1"), "Test db has been changed"); + assertEquals(1, dao.countNodesWithPath("/test1/f1/f2_renamed"), "Test db has been changed"); + assertEquals(1, dao.countNodesWithPath("/test1/f1/f2_renamed/f3"), "Test db has been changed"); + + assertEquals(1, dao.countNodesWithPath("/test2"), "Test db has been changed"); + + assertEquals(1, dao.countNodesWithPath("/test2/f4")); + assertEquals(1, dao.countNodesWithPath("/test2/f5")); + + assertEquals(0, dao.countNodesWithPath("/pippooo")); + } + + // Test stub + //@Test + public void testDeleteNode() { + + } + private String getProperty(Node node, String uri) { for (Property property : node.getProperties()) { if (uri.equals(property.getUri())) { diff --git a/src/test/resources/test-data.sql b/src/test/resources/test-data.sql index 67aa878e0f8597cc445c87f9eb1fe332ad47ae54..3f032aa22229d467db82c93fc31c6a8bdefa87cc 100644 --- a/src/test/resources/test-data.sql +++ b/src/test/resources/test-data.sql @@ -8,9 +8,9 @@ INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creat 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) +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) DELETE FROM job;