diff --git a/src/main/java/it/inaf/oats/vospace/persistence/LinkedServiceDAO.java b/src/main/java/it/inaf/oats/vospace/persistence/LinkedServiceDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..e8adf2f3a9601978222645e1e160f5a31df21016 --- /dev/null +++ b/src/main/java/it/inaf/oats/vospace/persistence/LinkedServiceDAO.java @@ -0,0 +1,44 @@ +/* + * This file is part of vospace-rest + * Copyright (C) 2021 Istituto Nazionale di Astrofisica + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package it.inaf.oats.vospace.persistence; + +import com.fasterxml.jackson.databind.ObjectMapper; +import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Repository +public class LinkedServiceDAO { + + private static final Logger LOG = LoggerFactory.getLogger(LinkedServiceDAO.class); + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private final JdbcTemplate jdbcTemplate; + + @Autowired + public LinkedServiceDAO(DataSource dataSource) { + jdbcTemplate = new JdbcTemplate(dataSource); + } + + boolean isLinkedServiceUrl(String targetUrl) { + String sql = " SELECT COUNT(*) > 0\n" + + "FROM linked_service\n" + + "WHERE ? LIKE service_base_url || '%'"; + + return jdbcTemplate.query(sql, ps -> { + ps.setString(1, targetUrl); + }, row -> { + if (!row.next()) { + throw new IllegalStateException("Expected one result"); + } + return row.getBoolean(1); + }); + } +} diff --git a/src/test/java/it/inaf/oats/vospace/persistence/LinkedServiceDAOTest.java b/src/test/java/it/inaf/oats/vospace/persistence/LinkedServiceDAOTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c58ecbd96084ad3c529022f25c714074a60f9ff0 --- /dev/null +++ b/src/test/java/it/inaf/oats/vospace/persistence/LinkedServiceDAOTest.java @@ -0,0 +1,39 @@ +/* + * This file is part of vospace-rest + * Copyright (C) 2021 Istituto Nazionale di Astrofisica + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package it.inaf.oats.vospace.persistence; + +import javax.sql.DataSource; +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; +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 static org.junit.jupiter.api.Assertions.assertFalse; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = {DataSourceConfig.class}) +@TestPropertySource(locations = "classpath:test.properties") +public class LinkedServiceDAOTest { + + @Autowired + private DataSource dataSource; + private LinkedServiceDAO dao; + + @BeforeEach + public void init() { + dao = new LinkedServiceDAO(dataSource); + } + + @Test + void testIsLinkedService() { + assertTrue(dao.isLinkedServiceUrl("http://archives.ia2.inaf.it/files/aao/pippofile.fits.gz")); + assertFalse(dao.isLinkedServiceUrl("http://noportal.ia2.inaf.it/files/nop/nopippofile.tar.gz")); + } + +} diff --git a/src/test/resources/test-data.sql b/src/test/resources/test-data.sql index 3ae4a612a559bf2d4cdfa798e32f386c98f6a0a1..7827a3af033234d0933bc14a43b5393a98ce348f 100644 --- a/src/test/resources/test-data.sql +++ b/src/test/resources/test-data.sql @@ -1,3 +1,5 @@ +INSERT INTO linked_service(service_base_url) VALUES ('http://archives.ia2.inaf.it/files/aao'); + INSERT INTO storage (storage_type, base_path, base_url, hostname) VALUES ('cold', '/ia2_tape/users', NULL, 'tape-server'); INSERT INTO storage (storage_type, base_path, base_url, hostname) VALUES ('hot', '/mnt/hot_storage/users', NULL, 'server'); INSERT INTO storage (storage_type, base_path, base_url, hostname) VALUES ('local', '/home', NULL, 'localhost');