diff --git a/pom.xml b/pom.xml index db96ae29f4c..6da5755517b 100644 --- a/pom.xml +++ b/pom.xml @@ -643,6 +643,11 @@ junit-jupiter test + + org.testcontainers + postgresql + test + org.mockito mockito-core @@ -855,6 +860,7 @@ tc true + 9.6 @@ -864,6 +870,9 @@ 2.22.2 testcontainers + + ${postgresql.server.version} + diff --git a/src/main/java/edu/harvard/iq/dataverse/settings/source/DbSettingConfigSource.java b/src/main/java/edu/harvard/iq/dataverse/settings/source/DbSettingConfigSource.java index 1820d246859..5038e5c7714 100644 --- a/src/main/java/edu/harvard/iq/dataverse/settings/source/DbSettingConfigSource.java +++ b/src/main/java/edu/harvard/iq/dataverse/settings/source/DbSettingConfigSource.java @@ -32,6 +32,12 @@ public DbSettingConfigSource() { } } + // Test usage (no JNDI context) + public DbSettingConfigSource(DataSource ds) { + dataSource = ds; + updateProperties(); + } + public void updateProperties() { // Do brutal JDBC retrieval over the wire, to be available right from the start of app deployment. // Injecting the EntityManager or the SettingsServiceBean is hard, as MPCONFIG sources are POJOs. @@ -42,7 +48,7 @@ public void updateProperties() { final ResultSet props = query.executeQuery(); while (props.next()) { - propertiesCache.put(PREFIX+"."+props.getString(0), props.getString(1)); + propertiesCache.put(PREFIX+"."+props.getString(1), props.getString(2)); } lastUpdate = Instant.now(); @@ -70,7 +76,7 @@ public Set getPropertyNames() { @Override public int getOrdinal() { - return 0; + return 50; } @Override diff --git a/src/test/java/edu/harvard/iq/dataverse/settings/source/DbSettingConfigSourceIT.java b/src/test/java/edu/harvard/iq/dataverse/settings/source/DbSettingConfigSourceIT.java new file mode 100644 index 00000000000..5a31c815e6d --- /dev/null +++ b/src/test/java/edu/harvard/iq/dataverse/settings/source/DbSettingConfigSourceIT.java @@ -0,0 +1,47 @@ +package edu.harvard.iq.dataverse.settings.source; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.postgresql.ds.PGSimpleDataSource; +import org.testcontainers.containers.JdbcDatabaseContainer; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; + +import static org.junit.jupiter.api.Assertions.*; + +@Testcontainers +@Tag("testcontainers") +class DbSettingConfigSourceTest { + + @Container + static JdbcDatabaseContainer dbContainer = new PostgreSQLContainer("postgres:"+System.getProperty("postgresql.server.version", "9.6")) + .withInitScript("sql/dbsetting-config-source-testdata.sql") + .withDatabaseName("dataverse"); + + static DbSettingConfigSource dbSource; + + @BeforeAll + static void setUp() throws NamingException { + // create the datasource + PGSimpleDataSource ds = new PGSimpleDataSource(); + ds.setURL(dbContainer.getJdbcUrl()); + ds.setUser(dbContainer.getUsername()); + ds.setPassword(dbContainer.getPassword()); + + // create the config source + dbSource = new DbSettingConfigSource(ds); + } + + @Test + void testDataRetrieval() { + assertEquals("foobar@example.org", dbSource.getValue("dataverse.settings.fromdb.SystemEmail")); + } + +} \ No newline at end of file diff --git a/src/test/resources/sql/dbsetting-config-source-testdata.sql b/src/test/resources/sql/dbsetting-config-source-testdata.sql new file mode 100644 index 00000000000..c55162d8707 --- /dev/null +++ b/src/test/resources/sql/dbsetting-config-source-testdata.sql @@ -0,0 +1,11 @@ +CREATE TABLE Setting ( + id char(5) CONSTRAINT firstkey PRIMARY KEY, + name varchar(40) NOT NULL, + content varchar(40) NOT NULL, + lang varchar(40) DEFAULT NULL +); + +INSERT INTO Setting (id, name, content) + VALUES + (1, 'SystemEmail', 'foobar@example.org') + ON CONFLICT DO NOTHING; \ No newline at end of file