From ff424e96184d1262738f092c9fbc968f593d4ca0 Mon Sep 17 00:00:00 2001 From: Nicola Fulvio Calabria Date: Sat, 13 Mar 2021 11:09:39 +0100 Subject: [PATCH] #3824: JobDAO tests added --- .../inaf/ia2/transfer/persistence/JobDAO.java | 18 ++++++++ .../ia2/transfer/persistence/JobDAOTest.java | 45 +++++++++++++++++++ src/test/resources/test-data.sql | 9 ++++ 3 files changed, 72 insertions(+) create mode 100644 src/test/java/it/inaf/ia2/transfer/persistence/JobDAOTest.java diff --git a/src/main/java/it/inaf/ia2/transfer/persistence/JobDAO.java b/src/main/java/it/inaf/ia2/transfer/persistence/JobDAO.java index f25bb68..8a345fe 100644 --- a/src/main/java/it/inaf/ia2/transfer/persistence/JobDAO.java +++ b/src/main/java/it/inaf/ia2/transfer/persistence/JobDAO.java @@ -42,5 +42,23 @@ public class JobDAO { ps.setString(2, jobId); }); } + + public ExecutionPhase getJobPhase(String jobId) { + String sql = "SELECT phase FROM job WHERE job_id = ?"; + + ExecutionPhase result = jdbcTemplate.query(sql, + ps -> { + ps.setString(1, jobId); + }, rs -> { + if(rs.next()) + { + return ExecutionPhase.fromValue(rs.getString("phase")); + } else { + return null; + } + }); + + return result; + } } diff --git a/src/test/java/it/inaf/ia2/transfer/persistence/JobDAOTest.java b/src/test/java/it/inaf/ia2/transfer/persistence/JobDAOTest.java new file mode 100644 index 0000000..1eaf974 --- /dev/null +++ b/src/test/java/it/inaf/ia2/transfer/persistence/JobDAOTest.java @@ -0,0 +1,45 @@ +package it.inaf.ia2.transfer.persistence; + +import javax.sql.DataSource; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import net.ivoa.xml.uws.v1.ExecutionPhase; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = {DataSourceConfig.class}) +@TestPropertySource(locations = "classpath:test.properties") +public class JobDAOTest { + + @Autowired + private DataSource dataSource; + private JobDAO dao; + + @BeforeEach + public void init() { + dao = new JobDAO(dataSource); + } + + @Test + public void testNodeDAO() + { + assertTrue(dao.isJobExisting("pippo5")); + assertFalse(dao.isJobExisting("pippo22")); + + ExecutionPhase phase = dao.getJobPhase("pippo5"); + assertEquals(ExecutionPhase.EXECUTING, phase); + dao.updateJobPhase(ExecutionPhase.COMPLETED, "pippo5"); + phase = dao.getJobPhase("pippo5"); + assertEquals(ExecutionPhase.COMPLETED, phase); + } + + + +} diff --git a/src/test/resources/test-data.sql b/src/test/resources/test-data.sql index cf0fb58..df51224 100644 --- a/src/test/resources/test-data.sql +++ b/src/test/resources/test-data.sql @@ -10,3 +10,12 @@ INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creat INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, group_read, group_write) VALUES ('2', NULL, '.tmp-123.txt', 'structured', 'user1', 'user1', '{"group1","group2"}','{"group2"}'); -- /test1/.tmp-123.txt INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, group_read, group_write, location_id) VALUES ('2', '', 'file1.txt', 'data', 'user1', 'user1', '{"group1","group2"}','{"group2"}', 1); -- /test1/file1.txt INSERT INTO node (parent_path, parent_relative_path, name, type, owner_id, creator_id, group_read, group_write, location_id) VALUES ('2', '', 'file2.txt', 'data', 'user1', 'user1', '{"group1","group2"}','{"group2"}', 1); -- /test1/file2.txt + +DELETE FROM job; + +INSERT INTO job (job_id, owner_id, job_type, phase, start_time, end_time, creation_time, job_info, results) VALUES ('pippo1', 'user1', 'pullFromVoSpace', 'ARCHIVED', NULL, NULL, '2011-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 ('pippo2', 'user1', 'pullToVoSpace', 'PENDING', NULL, NULL, '2012-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 ('pippo3', 'user1', 'pullFromVoSpace', 'QUEUED', NULL, NULL, '2013-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 ('pippo6', 'user2', 'pullFromVoSpace', 'PENDING', NULL, NULL, '2015-06-22 19:10:25', NULL, NULL); \ No newline at end of file -- GitLab