Skip to content

Commit

Permalink
Add integration testing for DbSettingConfigSource to ensure data retr…
Browse files Browse the repository at this point in the history
…ieval works. Fixing a typo, too. IQSS#7457
  • Loading branch information
poikilotherm committed Dec 8, 2020
1 parent e1cb339 commit 8790313
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Expand Up @@ -643,6 +643,11 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down Expand Up @@ -855,6 +860,7 @@
<id>tc</id>
<properties>
<skipUnitTests>true</skipUnitTests>
<postgresql.server.version>9.6</postgresql.server.version>
</properties>
<build>
<plugins>
Expand All @@ -864,6 +870,9 @@
<version>2.22.2</version>
<configuration>
<groups>testcontainers</groups>
<systemPropertyVariables>
<postgresql.server.version>${postgresql.server.version}</postgresql.server.version>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
Expand Down
Expand Up @@ -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.
Expand All @@ -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();

Expand Down Expand Up @@ -70,7 +76,7 @@ public Set<String> getPropertyNames() {

@Override
public int getOrdinal() {
return 0;
return 50;
}

@Override
Expand Down
@@ -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"));
}

}
11 changes: 11 additions & 0 deletions 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;

0 comments on commit 8790313

Please sign in to comment.