From d5c0b684b553944d4d3fb90b9df786ea776e5c87 Mon Sep 17 00:00:00 2001 From: vteague Date: Tue, 24 Dec 2024 11:52:10 +1100 Subject: [PATCH] Adopted most of the refactoring of TestClassWithDatabase.java; added a small function to load other database scripts. --- .../corla/endpoint/GetAssertionsTests.java | 13 +----- .../corla/util/TestClassWithDatabase.java | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/server/eclipse-project/src/test/java/au/org/democracydevelopers/corla/endpoint/GetAssertionsTests.java b/server/eclipse-project/src/test/java/au/org/democracydevelopers/corla/endpoint/GetAssertionsTests.java index 970fcfda..29ecf8b9 100644 --- a/server/eclipse-project/src/test/java/au/org/democracydevelopers/corla/endpoint/GetAssertionsTests.java +++ b/server/eclipse-project/src/test/java/au/org/democracydevelopers/corla/endpoint/GetAssertionsTests.java @@ -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; @@ -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. */ @@ -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"); } /** diff --git a/server/eclipse-project/src/test/java/au/org/democracydevelopers/corla/util/TestClassWithDatabase.java b/server/eclipse-project/src/test/java/au/org/democracydevelopers/corla/util/TestClassWithDatabase.java index c70c2448..f06c489b 100644 --- a/server/eclipse-project/src/test/java/au/org/democracydevelopers/corla/util/TestClassWithDatabase.java +++ b/server/eclipse-project/src/test/java/au/org/democracydevelopers/corla/util/TestClassWithDatabase.java @@ -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; @@ -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. */ @@ -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") @@ -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(); @@ -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); + } }