Skip to content

Commit

Permalink
Adopted most of the refactoring of TestClassWithDatabase.java; added …
Browse files Browse the repository at this point in the history
…a small function to load other database scripts.
  • Loading branch information
vteague committed Dec 24, 2024
1 parent 5c54f39 commit d5c0b68
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.ext.ScriptUtils;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
Expand Down Expand Up @@ -72,11 +70,6 @@ public class GetAssertionsTests extends TestClassWithDatabase {

private static final Logger LOGGER = LogManager.getLogger(GetAssertionsTests.class);

/**
* Container for the mock-up database.
*/
private final static PostgreSQLContainer<?> postgres = createTestContainer();

/**
* Endpoint for getting assertions.
*/
Expand All @@ -103,12 +96,10 @@ public class GetAssertionsTests extends TestClassWithDatabase {
* Database init.
*/
@BeforeClass
public static void beforeAll() throws IOException {

var containerDelegate = setupContainerStartPostgres(postgres);
public static void beforeAllThisClass() {

// Load in the counties data, actually just for basic setup such as DoSDashboard.
ScriptUtils.runInitScript(containerDelegate, "SQL/co-counties.sql");
runSQLSetupScript("SQL/co-counties.sql");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import java.util.Properties;

import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.ext.ScriptUtils;
import org.testcontainers.jdbc.JdbcDatabaseDelegate;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import us.freeandfair.corla.persistence.Persistence;

Expand Down Expand Up @@ -58,6 +61,23 @@ public abstract class TestClassWithDatabase {
*/
protected final static String getAssertionsPortNumberString = "get_assertions_mock_port";

/**
* Container for the mock-up database.
*/
static PostgreSQLContainer<?> postgres = createTestContainer();


@BeforeClass
public static void beforeAll() {
postgres.start();
Persistence.setProperties(createHibernateProperties(postgres));
}

@AfterClass
public static void afterAll() {
postgres.stop();
}

/**
* Begin a new transaction before each test method in the class is run.
*/
Expand All @@ -81,6 +101,7 @@ public static void afterTest(){
* Create and return a postgres test container for the purposes of testing functionality that
* interacts with the database.
* @return a postgres test container representing a test database.
* FIXME This is more general than Matt's edits - suggest retaining this version.
*/
public static PostgreSQLContainer<?> createTestContainer() {
return new PostgreSQLContainer<>("postgres:15-alpine")
Expand All @@ -97,6 +118,7 @@ public static PostgreSQLContainer<?> createTestContainer() {
* generated at init.
* @param postgres the database container.
* @return the database delegate.
* FIXME possibly no longer needed.
*/
protected static JdbcDatabaseDelegate setupContainerStartPostgres(PostgreSQLContainer<?> postgres) {
postgres.start();
Expand All @@ -108,4 +130,26 @@ protected static JdbcDatabaseDelegate setupContainerStartPostgres(PostgreSQLCont

return new JdbcDatabaseDelegate(postgres, "");
}

/**
* Create and return a hibernate properties object for use in testing functionality that
* interacts with the database.
* @param postgres Postgres test container representing a test version of the database.
* @return Hibernate persistence properties.
*/
public static Properties createHibernateProperties(PostgreSQLContainer<?> postgres) {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.driver", "org.postgresql.Driver");
hibernateProperties.setProperty("hibernate.url", postgres.getJdbcUrl());
hibernateProperties.setProperty("hibernate.user", postgres.getUsername());
hibernateProperties.setProperty("hibernate.pass", postgres.getPassword());
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");

return hibernateProperties;
}

protected static void runSQLSetupScript(String initScriptPath) {
var containerDelegate = new JdbcDatabaseDelegate(postgres, "");
ScriptUtils.runInitScript(containerDelegate, initScriptPath);
}
}

0 comments on commit d5c0b68

Please sign in to comment.