Skip to content
Snippets Groups Projects
Commit a51cc46e authored by Nicola Fulvio Calabria's avatar Nicola Fulvio Calabria
Browse files

added add and remove node to collectionsDAO

parent b8f30cfe
No related branches found
No related tags found
No related merge requests found
...@@ -83,6 +83,32 @@ public class CollectionsDAO { ...@@ -83,6 +83,32 @@ public class CollectionsDAO {
return nc; 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) { public void deleteCollection(Long collectionId, String userId) {
String sql = "DELETE FROM collections WHERE collection_id = ? AND owner_id = ?"; String sql = "DELETE FROM collections WHERE collection_id = ? AND owner_id = ?";
......
...@@ -7,6 +7,7 @@ package it.inaf.oats.vospace.persistence; ...@@ -7,6 +7,7 @@ package it.inaf.oats.vospace.persistence;
import it.inaf.oats.vospace.datamodel.collections.NodeCollection; import it.inaf.oats.vospace.datamodel.collections.NodeCollection;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.sql.DataSource; import javax.sql.DataSource;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
...@@ -17,18 +18,21 @@ import org.springframework.test.context.ContextConfiguration; ...@@ -17,18 +18,21 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.util.ReflectionTestUtils;
/** /**
* *
* @author Nicola Fulvio Calabria <nicola.calabria at inaf.it> * @author Nicola Fulvio Calabria <nicola.calabria at inaf.it>
*/ */
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {DataSourceConfig.class}) @ContextConfiguration(classes = {DataSourceConfig.class})
@TestPropertySource(locations = "classpath:test.properties") @TestPropertySource(locations = "classpath:test.properties")
public class CollectionsDAOTest { public class CollectionsDAOTest {
private static final String AUTHORITY = "example.com!vospace";
private CollectionsDAO collectionsDAO; private CollectionsDAO collectionsDAO;
private NodeDAO nodeDAO;
@Autowired @Autowired
private DataSource dataSource; private DataSource dataSource;
...@@ -36,28 +40,49 @@ public class CollectionsDAOTest { ...@@ -36,28 +40,49 @@ public class CollectionsDAOTest {
@BeforeEach @BeforeEach
public void init() { public void init() {
collectionsDAO = new CollectionsDAO(dataSource); collectionsDAO = new CollectionsDAO(dataSource);
nodeDAO = new NodeDAO(dataSource);
ReflectionTestUtils.setField(nodeDAO, "authority", AUTHORITY);
} }
@Test @Test
public void testInsertAndDeleteCollection() { public void testInsertAndDeleteCollection() {
assertTrue(collectionsDAO.getUserNodeCollections("pippo").isEmpty()); assertTrue(collectionsDAO.getUserNodeCollections("pippo1").isEmpty());
collectionsDAO.createNewCollection("collection1", "pippo"); collectionsDAO.createNewCollection("collection1", "pippo1");
collectionsDAO.createNewCollection("collection2", "pippo"); collectionsDAO.createNewCollection("collection2", "pippo1");
List<NodeCollection> ncl = List<NodeCollection> ncl
collectionsDAO.getUserNodeCollections("pippo"); = collectionsDAO.getUserNodeCollections("pippo1");
assertEquals(2, ncl.size()); assertEquals(2, ncl.size());
for (NodeCollection nc : ncl) { for (NodeCollection nc : ncl) {
collectionsDAO.deleteCollection( collectionsDAO.deleteCollection(
nc.getId(), "pippo" nc.getId(), "pippo1"
); );
} }
assertTrue(collectionsDAO.getUserNodeCollections("pippo").isEmpty()); assertTrue(collectionsDAO.getUserNodeCollections("pippo1").isEmpty());
} }
@Test
public void testAddAndRemoveNode() {
List<NodeCollection> collections =
collectionsDAO.getUserNodeCollections("pippo");
assertEquals(1, collections.size());
NodeCollection nc = collections.get(0);
Optional<Long> maybeNodeId = nodeDAO.getNodeId("/test1/f1/f2_renamed");
assertTrue(maybeNodeId.isPresent());
Long nodeId = maybeNodeId.get();
collectionsDAO.addNodeToCollection(nodeId, nc.getId());
// Add some logic for checks
collectionsDAO.removeNodeFromCollection(nodeId, nc.getId());
}
} }
...@@ -51,3 +51,5 @@ INSERT INTO job (job_id, owner_id, job_type, phase, start_time, end_time, creati ...@@ -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 ('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 ('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 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment