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 e89ea652..c3865f6c 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 @@ -38,6 +38,11 @@ * This class is designed to be extended by any test class that needs to interact with a test * instantiation of the colorado-rla database. It provides convenience methods for instantiating * a postgres container (initialised with one a given SQL script) and hibernate properties. + * Database configuration properties are loaded from test.properties, except that the Hibernate URL + * is overridden with the test container's URL. + * Important gotcha: although you can extend this class and write a new @BeforeClass method, which + * will be executed after any @BeforeClass methods in this class, you *must* give the method a new + * name - otherwise it seems to silently not run. */ public abstract class TestClassWithDatabase { diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/controllers/BallotSelectionTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/controllers/BallotSelectionTest.java index 0998ba1b..88935623 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/controllers/BallotSelectionTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/controllers/BallotSelectionTest.java @@ -31,7 +31,7 @@ import org.hibernate.Session; import us.freeandfair.corla.query.CastVoteRecordQueries; import us.freeandfair.corla.query.Setup; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; @Test(groups = {"integration"}) public class BallotSelectionTest extends TestClassWithDatabase { diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/endpoint/StartAuditRoundTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/endpoint/StartAuditRoundTest.java index b645ff0d..3f69ea19 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/endpoint/StartAuditRoundTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/endpoint/StartAuditRoundTest.java @@ -10,7 +10,7 @@ import us.freeandfair.corla.model.CountyDashboard; import us.freeandfair.corla.persistence.Persistence; import us.freeandfair.corla.query.Setup; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; import java.util.Properties; diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AdministratorTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AdministratorTest.java index 66e4723c..8bb233d9 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AdministratorTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AdministratorTest.java @@ -2,7 +2,7 @@ import org.testng.annotations.Test; import us.freeandfair.corla.persistence.Persistence; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; import java.time.Clock; import java.time.Instant; diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AuditBoardTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AuditBoardTest.java index 6e8a3633..6df7782e 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AuditBoardTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AuditBoardTest.java @@ -1,7 +1,7 @@ package us.freeandfair.corla.model; import org.testng.annotations.Test; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; import java.lang.reflect.Array; import java.time.Instant; diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AuditInfoTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AuditInfoTest.java index 147cffd2..90938282 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AuditInfoTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/model/AuditInfoTest.java @@ -23,7 +23,7 @@ import us.freeandfair.corla.csv.ContestNameParser; import us.freeandfair.corla.json.VersionExclusionStrategy; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; public class AuditInfoTest extends TestClassWithDatabase { public Gson gson; diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/AdministratorQueriesTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/AdministratorQueriesTest.java index 503ed8dd..3eea1547 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/AdministratorQueriesTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/AdministratorQueriesTest.java @@ -4,7 +4,7 @@ import us.freeandfair.corla.model.Administrator; import us.freeandfair.corla.model.County; import us.freeandfair.corla.persistence.Persistence; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/BallotManifestInfoQueriesTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/BallotManifestInfoQueriesTest.java index 17fdfa9c..50b021dc 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/BallotManifestInfoQueriesTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/BallotManifestInfoQueriesTest.java @@ -6,7 +6,7 @@ import org.testng.annotations.*; import us.freeandfair.corla.model.*; import us.freeandfair.corla.persistence.Persistence; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; import javax.swing.text.html.Option; import java.util.*; diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/CastVoteRecordQueriesTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/CastVoteRecordQueriesTest.java index 1d06e9c1..77ab48a1 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/CastVoteRecordQueriesTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/CastVoteRecordQueriesTest.java @@ -23,7 +23,7 @@ import us.freeandfair.corla.model.*; import us.freeandfair.corla.persistence.Persistence; import us.freeandfair.corla.query.Setup; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; @Test(groups = {"integration"}) public class CastVoteRecordQueriesTest extends TestClassWithDatabase { diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ComparisonAuditQueriesTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ComparisonAuditQueriesTest.java index aaeb7d5d..7e13b607 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ComparisonAuditQueriesTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ComparisonAuditQueriesTest.java @@ -6,7 +6,7 @@ import us.freeandfair.corla.model.ComparisonAudit; import us.freeandfair.corla.model.ContestResult; import us.freeandfair.corla.persistence.Persistence; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; import java.math.BigDecimal; import java.util.ArrayList; diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ContestQueriesTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ContestQueriesTest.java index 504ba437..4f3e9761 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ContestQueriesTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ContestQueriesTest.java @@ -8,7 +8,7 @@ import us.freeandfair.corla.model.*; import us.freeandfair.corla.model.Choice; import us.freeandfair.corla.persistence.Persistence; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; import java.awt.*; import java.util.*; diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ContestResultQueriesTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ContestResultQueriesTest.java index 963cc926..0898ce26 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ContestResultQueriesTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ContestResultQueriesTest.java @@ -14,7 +14,7 @@ import us.freeandfair.corla.model.ContestResult; import us.freeandfair.corla.model.Contest; import us.freeandfair.corla.model.County; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; @Test(groups = {"integration"}) public class ContestResultQueriesTest extends TestClassWithDatabase { diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ExportQueriesTest.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ExportQueriesTest.java index b24b27e0..756e5915 100644 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ExportQueriesTest.java +++ b/server/eclipse-project/src/test/java/us/freeandfair/corla/query/ExportQueriesTest.java @@ -14,7 +14,7 @@ import org.hibernate.Session; -import us.freeandfair.corla.util.TestClassWithDatabase; +import au.org.democracydevelopers.corla.util.TestClassWithDatabase; @Test(groups = {"integration"}) diff --git a/server/eclipse-project/src/test/java/us/freeandfair/corla/util/TestClassWithDatabase.java b/server/eclipse-project/src/test/java/us/freeandfair/corla/util/TestClassWithDatabase.java deleted file mode 100644 index a3c8ae94..00000000 --- a/server/eclipse-project/src/test/java/us/freeandfair/corla/util/TestClassWithDatabase.java +++ /dev/null @@ -1,109 +0,0 @@ -/* -Democracy Developers IRV extensions to colorado-rla. - -@copyright 2024 Colorado Department of State - -These IRV extensions are designed to connect to a running instance of the raire -service (https://github.com/DemocracyDevelopers/raire-service), in order to -generate assertions that can be audited using colorado-rla. - -The colorado-rla IRV extensions are free software: you can redistribute it and/or modify it under the terms -of the GNU Affero General Public License as published by the Free Software Foundation, either -version 3 of the License, or (at your option) any later version. - -The colorado-rla IRV extensions are distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -See the GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License along with -raire-service. If not, see . -*/ - -package us.freeandfair.corla.util; - -import java.util.Properties; -import org.testcontainers.containers.PostgreSQLContainer; -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; - -/** - * This class is designed to be extended by any test class that needs to interact with a test - * instantiation of the colorado-rla database. It provides convenience methods for instantiating - * a postgres container (initialised with one a given SQL script) and hibernate properties. - */ -public abstract class TestClassWithDatabase { - - - /** - * 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. - */ - @BeforeMethod - public static void beforeTest(){ - Persistence.beginTransaction(); - } - - /** - * Rollback any changes to the (test) database after each test method is run. - */ - @AfterMethod - public static void afterTest(){ - try { - Persistence.rollbackTransaction(); - } catch (Exception ignored) { - } - } - - /** - * 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. - */ - public static PostgreSQLContainer createTestContainer() { - return new PostgreSQLContainer<>("postgres:15-alpine") - // None of these actually have to be the same as the real database (except its name), - // but this makes it easy to match the setup scripts. - // .withExposedPorts(port) - .withDatabaseName("corla") - .withUsername("corlaadmin") - .withPassword("corlasecret") - .withInitScript("SQL/corla.sql"); - } - - /** - * 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; - } - -} \ No newline at end of file