Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use test class with database #132

Merged
merged 10 commits into from
Jun 24, 2024
37 changes: 37 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven


- name: Build with Maven
run: cd server/eclipse-project; mvn -B -DskipTests package --file pom.xml

- name: Classic Tests
run: cd server/eclipse-project; mvn -Dtest='us.freeandfair.corla.**' test
Original file line number Diff line number Diff line change
@@ -131,6 +131,10 @@ public static synchronized void setProperties(final Properties the_properties) {
if (env.containsKey("HIBERNATE_URL")) {
system_properties.setProperty("hibernate.url", env.get("HIBERNATE_URL"));
}

// Properties have changed, we need to reinitialize our DB connectors
session_info.remove();
session_factory = null;
}

/**
Original file line number Diff line number Diff line change
@@ -243,7 +243,7 @@ public static int updateCVRContestInfos(final Long countyId, final Long contestI
newChoice = (newChoice.substring(0, newChoice.length() - 1));
newChoice = (newChoice.substring(1, newChoice.length()));

String escapedOldChoice = oldChoice.replaceAll("\"", Matcher.quoteReplacement("\\\\\""));
String escapedOldChoice = oldChoice.replaceAll("\"", Matcher.quoteReplacement("\\\\\"")).replace("?", "\\?");

final Query q = s
// this will only fix the first match, which is what we want, because
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package us.freeandfair.corla.controllers;

import org.testcontainers.containers.PostgreSQLContainer;
import org.testng.annotations.*;
import us.freeandfair.corla.controller.BallotSelection;
import us.freeandfair.corla.controller.BallotSelection.Selection;
import us.freeandfair.corla.controller.BallotSelection.Segment;
@@ -17,27 +19,16 @@

import java.time.Instant;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.*;

import java.util.function.Function;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import static org.testng.Assert.*;

import org.hibernate.Session;
import us.freeandfair.corla.query.CastVoteRecordQueries;
import us.freeandfair.corla.query.Setup;

@@ -48,18 +39,46 @@ public class BallotSelectionTest {

private Boolean return_cvr = true;

@BeforeTest()
public void setUp() {
Setup.setProperties();
/**
* Container for the mock-up database.
*/
static PostgreSQLContainer<?> postgres
= 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.
.withDatabaseName("corla")
.withUsername("corlaadmin")
.withPassword("corlasecret")
// .withInitScripts("corlaInit.sql","contest.sql");
.withInitScript("SQL/corlaInitEmpty.sql");

@BeforeClass
public static void beforeAll() {
postgres.start();
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");
Persistence.setProperties(hibernateProperties);
Persistence.beginTransaction();

}

@AfterTest()
public void tearDown() {
try {
Persistence.rollbackTransaction();
} catch (Exception e) {
}
@AfterClass
public static void afterAll() {
postgres.stop();
}

@BeforeMethod
public static void beforeEach() {
Persistence.beginTransaction();
}

@AfterMethod
public static void afterEach() {
Persistence.rollbackTransaction();
}

@Test()
@@ -112,6 +131,18 @@ public void testCombineSegmentsAuditSequence() {
CastVoteRecord cvr4 = fakeCVR(4);
List<CastVoteRecord> exampleCVRs = Stream.of(cvr1, cvr3, cvr2, cvr2).collect(Collectors.toList());
List<CastVoteRecord> exampleCVRs2 = Stream.of(cvr3, cvr4).collect(Collectors.toList());

// We have to create a BallotManifestInfo object so that the later
// sequence lookup code can match the batch_id.
BallotManifestInfo bmi = new BallotManifestInfo(64L,
1,
"Batch1",
64,
"Storage",
1L,
64L);
Persistence.saveOrUpdate(bmi);

// have to keep the raw data separate from the ordered, sorted data
segment.addCvrs(exampleCVRs);
segment.addCvrIds(exampleCVRs);
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package us.freeandfair.corla.query;

import java.lang.reflect.Method;
import java.util.List;
import java.util.ArrayList;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testng.annotations.*;

import static org.testng.Assert.*;

import java.time.Instant;
import java.util.Properties;

import us.freeandfair.corla.asm.PersistentASMState;
import us.freeandfair.corla.model.CastVoteRecord;
import us.freeandfair.corla.model.Contest;
import us.freeandfair.corla.model.Choice;
@@ -22,19 +27,39 @@
@Test(groups = {"integration"})
public class CastVoteRecordQueriesTest {


@BeforeTest()
public void setUp() {
Setup.setProperties();
/**
* Container for the mock-up database.
*/
private static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15-alpine")
.withDatabaseName("corla")
.withUsername("corlaadmin")
.withPassword("corlasecret")
.withInitScript("SQL/corlaInitEmpty.sql");

@BeforeClass
public static void beforeAll() {
postgres.start();
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");
Persistence.setProperties(hibernateProperties);
}
@BeforeMethod
public static void beforeEach() {
Persistence.beginTransaction();
}

@AfterTest()
public void tearDown() {
try {
@AfterMethod
public static void afterEach() {
Persistence.rollbackTransaction();
} catch (Exception e) {
}
}

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

public List<CVRContestInfo> noisyContestSetup(){
@@ -127,19 +152,25 @@ public void deleteAllTest() {

@Test()
public void canonicalChoicesTest() {
CastVoteRecord cvr = noisyCVRSetup(3);

Integer result = CastVoteRecordQueries.updateCVRContestInfos(1L,1L,"why?","because.");
assertEquals((int) result, (int) 1,
CastVoteRecord cvr = noisyCVRSetup(1);
// commit the transaction to populate the DB for debugging
// Persistence.commitTransaction();
// Persistence.beginTransaction();
// Note: the weird access to get the contest ID is because somehow it gets changed from 1 to 4 when run with other tests.
// TODO: THIS SHOULD NOT HAPPEN! WHY IS IT CHANGING FROM 1 TO 4 WHEN RUN WITH OTHER TESTS?!
int result = CastVoteRecordQueries.updateCVRContestInfos(cvr.countyID(),cvr.contestInfo().get(0).contest().id(), "why?","because.");
assertEquals(result, 1,
"a result of 1 means one choice was changed");
Persistence.currentSession().refresh(cvr);
assertEquals(cvr.contestInfo().toString().contains("choices=[because.]"), true);

Persistence.currentSession().refresh(cvr, LockMode.PESSIMISTIC_WRITE);
assertTrue(cvr.contestInfo().toString().contains("choices=[because.]"));

}


@Test()
public void activityReportTest() {
CastVoteRecord cvr = noisyCVRSetup(4);
CastVoteRecord cvr = noisyCVRSetup(1);
CastVoteRecord acvr = new CastVoteRecord(CastVoteRecord.RecordType.AUDITOR_ENTERED, Instant.now(),
cvr.countyID(), cvr.cvrNumber(), null, cvr.scannerID(),
cvr.batchID(), cvr.recordID(), cvr.imprintedID(),
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@

import java.util.List;
import java.util.ArrayList;
import java.util.Properties;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testng.annotations.*;
import org.testng.Assert;

import us.freeandfair.corla.persistence.Persistence;
@@ -18,21 +18,46 @@
@Test(groups = {"integration"})
public class ContestResultQueriesTest {

/**
* Container for the mock-up database.
*/
static PostgreSQLContainer<?> postgres
= 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.
.withDatabaseName("corla")
.withUsername("corlaadmin")
.withPassword("corlasecret")
// .withInitScripts("corlaInit.sql","contest.sql");
.withInitScript("SQL/corlaInitEmpty.sql");

@BeforeClass
public static void beforeAll() {
postgres.start();
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");
Persistence.setProperties(hibernateProperties);

@BeforeTest()
public void setUp() {
Setup.setProperties();
Persistence.beginTransaction();
}

@AfterTest()
public void tearDown() {
try {
Persistence.rollbackTransaction();
} catch (Exception e) {
}
@AfterClass
public static void afterAll() {
postgres.stop();
}

@BeforeMethod
public static void beforeEach() {
Persistence.beginTransaction();
}

@AfterMethod
public static void afterEach() { Persistence.rollbackTransaction(); }


@Test()
public void findOrCreateTest() {
County county = new County("abc", 321L);
Loading