diff --git a/src/main/java/it/inaf/oats/vospace/persistence/CollectionsDAO.java b/src/main/java/it/inaf/oats/vospace/persistence/CollectionsDAO.java index 66a00838e2eaf031a0a7099506c1ce0da9c97d2f..cd1cfd005df8c9f7220481935309f7d16a6d4bc8 100644 --- a/src/main/java/it/inaf/oats/vospace/persistence/CollectionsDAO.java +++ b/src/main/java/it/inaf/oats/vospace/persistence/CollectionsDAO.java @@ -83,6 +83,32 @@ public class CollectionsDAO { return nc; } + public void addNodeToCollection(Long nodeId, Long collectionId) { + String sql = "INSERT INTO collections_node (collection_id, node_id)\n" + + "VALUES (?,?)"; + + jdbcTemplate.update(conn -> { + PreparedStatement ps = conn.prepareStatement(sql); + int i = 0; + ps.setLong(++i, collectionId); + ps.setLong(++i, nodeId); + return ps; + }); + } + + public void removeNodeFromCollection(Long nodeId, Long collectionId) { + String sql = "DELETE FROM collections_node WHERE collection_id = ? and node_id = ?"; + + jdbcTemplate.update(sql, collectionId, nodeId); + } + + // TODO complete stub + public List<NodeCollection> getCollectionsOfNode(Long nodeId) { + throw new UnsupportedOperationException(); + } + + // TODO complete stub: add list of nodes in collection (path list?) + public void deleteCollection(Long collectionId, String userId) { String sql = "DELETE FROM collections WHERE collection_id = ? AND owner_id = ?"; diff --git a/src/test/java/it/inaf/oats/vospace/persistence/CollectionsDAOTest.java b/src/test/java/it/inaf/oats/vospace/persistence/CollectionsDAOTest.java index 7b921292df679815974df6dfa86b73224f7c03de..99bff8100674559a13e4b27f43321260efc6bfde 100644 --- a/src/test/java/it/inaf/oats/vospace/persistence/CollectionsDAOTest.java +++ b/src/test/java/it/inaf/oats/vospace/persistence/CollectionsDAOTest.java @@ -7,6 +7,7 @@ package it.inaf.oats.vospace.persistence; import it.inaf.oats.vospace.datamodel.collections.NodeCollection; import java.util.List; +import java.util.Optional; import javax.sql.DataSource; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -17,47 +18,71 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.util.ReflectionTestUtils; /** * * @author Nicola Fulvio Calabria <nicola.calabria at inaf.it> */ - @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = {DataSourceConfig.class}) @TestPropertySource(locations = "classpath:test.properties") public class CollectionsDAOTest { - + + private static final String AUTHORITY = "example.com!vospace"; + private CollectionsDAO collectionsDAO; - + private NodeDAO nodeDAO; + @Autowired private DataSource dataSource; - + @BeforeEach - public void init(){ - collectionsDAO = new CollectionsDAO(dataSource); + public void init() { + collectionsDAO = new CollectionsDAO(dataSource); + nodeDAO = new NodeDAO(dataSource); + ReflectionTestUtils.setField(nodeDAO, "authority", AUTHORITY); } - + @Test public void testInsertAndDeleteCollection() { - assertTrue(collectionsDAO.getUserNodeCollections("pippo").isEmpty()); + assertTrue(collectionsDAO.getUserNodeCollections("pippo1").isEmpty()); + + collectionsDAO.createNewCollection("collection1", "pippo1"); + collectionsDAO.createNewCollection("collection2", "pippo1"); + + List<NodeCollection> ncl + = collectionsDAO.getUserNodeCollections("pippo1"); + + assertEquals(2, ncl.size()); + + for (NodeCollection nc : ncl) { + collectionsDAO.deleteCollection( + nc.getId(), "pippo1" + ); + } + + assertTrue(collectionsDAO.getUserNodeCollections("pippo1").isEmpty()); + } + + @Test + public void testAddAndRemoveNode() { + List<NodeCollection> collections = + collectionsDAO.getUserNodeCollections("pippo"); - collectionsDAO.createNewCollection("collection1", "pippo"); - collectionsDAO.createNewCollection("collection2", "pippo"); + assertEquals(1, collections.size()); - List<NodeCollection> ncl = - collectionsDAO.getUserNodeCollections("pippo"); + NodeCollection nc = collections.get(0); - assertEquals(2, ncl.size()); + Optional<Long> maybeNodeId = nodeDAO.getNodeId("/test1/f1/f2_renamed"); + assertTrue(maybeNodeId.isPresent()); + Long nodeId = maybeNodeId.get(); - for(NodeCollection nc : ncl) { - collectionsDAO.deleteCollection( - nc.getId(), "pippo" - ); - } + collectionsDAO.addNodeToCollection(nodeId, nc.getId()); - assertTrue(collectionsDAO.getUserNodeCollections("pippo").isEmpty()); - } + // Add some logic for checks - + collectionsDAO.removeNodeFromCollection(nodeId, nc.getId()); + } + } diff --git a/src/test/resources/test-data.sql b/src/test/resources/test-data.sql index a2daaf141ebd3960b9b34c276d76bd04a8b8105a..caa7bd736f50213a7f37292d5aa9d903ac8d83aa 100644 --- a/src/test/resources/test-data.sql +++ b/src/test/resources/test-data.sql @@ -51,3 +51,5 @@ INSERT INTO job (job_id, owner_id, job_type, phase, start_time, end_time, creati INSERT INTO job (job_id, owner_id, job_type, phase, start_time, end_time, creation_time, job_info, results) VALUES ('pippo4', 'user2', 'copyNode', 'PENDING', NULL, NULL, '2014-06-22 19:10:25', NULL, NULL); INSERT INTO job (job_id, owner_id, job_type, phase, start_time, end_time, creation_time, job_info, results) VALUES ('pippo5', 'user1', 'pushToVoSpace', 'EXECUTING', NULL, NULL, '2015-06-22 19:10:25', NULL, NULL); INSERT INTO job (job_id, owner_id, job_type, phase, start_time, end_time, creation_time, job_info, results) VALUES ('pippo6', 'user2', 'pullFromVoSpace', 'PENDING', NULL, NULL, '2015-06-22 19:10:25', NULL, NULL); + +INSERT INTO collections (title, owner_id) VALUES ('collection', 'pippo'); \ No newline at end of file