Switch from an in-memory HSQLDB to MariaDB during integration tests #57

Merged
ansv7779 merged 9 commits from testcontainers into develop 2024-12-16 13:55:34 +01:00
11 changed files with 53 additions and 25 deletions

View File

@ -86,8 +86,23 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hsqldb</groupId> <groupId>org.testcontainers</groupId>
<artifactId>hsqldb</artifactId> <artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>

View File

@ -4,9 +4,6 @@
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0"> version="3.0">
<!-- NOTE THAT THERE ARE TWO PERSISTENCE UNITS, one default and one test
used for either running or unit-tests -->
<!-- A JPA Persistence Unit --> <!-- A JPA Persistence Unit -->
<persistence-unit name="defaultPersistenceUnit" <persistence-unit name="defaultPersistenceUnit"
transaction-type="RESOURCE_LOCAL"> transaction-type="RESOURCE_LOCAL">
@ -17,13 +14,4 @@
</properties> </properties>
</persistence-unit> </persistence-unit>
<!-- A JPA Persistence Unit used for tests -->
<persistence-unit name="testPersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<properties>
<property name="jakarta.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:hsqldb:mem:test"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence> </persistence>

View File

@ -197,6 +197,7 @@ public class FinalSeminarSchedulingTest extends IntegrationTest {
NonWorkDayPeriod nonWorkDayPeriod = new NonWorkDayPeriod(); NonWorkDayPeriod nonWorkDayPeriod = new NonWorkDayPeriod();
nonWorkDayPeriod.setStartDate(date); nonWorkDayPeriod.setStartDate(date);
nonWorkDayPeriod.setEndDate(date); nonWorkDayPeriod.setEndDate(date);
nonWorkDayPeriod.setComment("test non work day");
save(nonWorkDayPeriod); save(nonWorkDayPeriod);
} }

View File

@ -12,6 +12,7 @@ import se.su.dsv.scipro.file.FileDescription;
import se.su.dsv.scipro.file.FileReference; import se.su.dsv.scipro.file.FileReference;
import se.su.dsv.scipro.project.Project; import se.su.dsv.scipro.project.Project;
import se.su.dsv.scipro.system.DegreeType; import se.su.dsv.scipro.system.DegreeType;
import se.su.dsv.scipro.system.Language;
import se.su.dsv.scipro.system.ProjectType; import se.su.dsv.scipro.system.ProjectType;
import se.su.dsv.scipro.system.User; import se.su.dsv.scipro.system.User;
import se.su.dsv.scipro.test.Dates; import se.su.dsv.scipro.test.Dates;
@ -66,6 +67,7 @@ public class CommentThreadServiceImplTest extends IntegrationTest {
PeerRequest peerRequest = new PeerRequest(); PeerRequest peerRequest = new PeerRequest();
peerRequest.setProject(project); peerRequest.setProject(project);
peerRequest.setRequester(createUser()); peerRequest.setRequester(createUser());
peerRequest.setLanguage(Language.ENGLISH);
final FileDescription fileDescription = save(new FileDescription()); final FileDescription fileDescription = save(new FileDescription());
final FileReference fileReference = new FileReference(); final FileReference fileReference = new FileReference();
fileReference.setFileDescription(fileDescription); fileReference.setFileDescription(fileDescription);

View File

@ -13,6 +13,7 @@ import se.su.dsv.scipro.file.FileDescription;
import se.su.dsv.scipro.file.FileReference; import se.su.dsv.scipro.file.FileReference;
import se.su.dsv.scipro.project.Project; import se.su.dsv.scipro.project.Project;
import se.su.dsv.scipro.system.DegreeType; import se.su.dsv.scipro.system.DegreeType;
import se.su.dsv.scipro.system.Language;
import se.su.dsv.scipro.system.PageRequest; import se.su.dsv.scipro.system.PageRequest;
import se.su.dsv.scipro.system.ProjectType; import se.su.dsv.scipro.system.ProjectType;
import se.su.dsv.scipro.system.User; import se.su.dsv.scipro.system.User;
@ -158,6 +159,7 @@ public class PeerRequestServiceImplTest extends IntegrationTest {
peerRequest.setProject(project); peerRequest.setProject(project);
peerRequest.setRequester(requester); peerRequest.setRequester(requester);
peerRequest.setStatus(status); peerRequest.setStatus(status);
peerRequest.setLanguage(Language.ENGLISH);
final FileDescription fileDescription = save(new FileDescription()); final FileDescription fileDescription = save(new FileDescription());
final FileReference fileReference = new FileReference(); final FileReference fileReference = new FileReference();
fileReference.setFileDescription(fileDescription); fileReference.setFileDescription(fileDescription);

View File

@ -14,6 +14,7 @@ import se.su.dsv.scipro.file.FileDescription;
import se.su.dsv.scipro.file.FileReference; import se.su.dsv.scipro.file.FileReference;
import se.su.dsv.scipro.project.Project; import se.su.dsv.scipro.project.Project;
import se.su.dsv.scipro.system.DegreeType; import se.su.dsv.scipro.system.DegreeType;
import se.su.dsv.scipro.system.Language;
import se.su.dsv.scipro.system.PageRequest; import se.su.dsv.scipro.system.PageRequest;
import se.su.dsv.scipro.system.ProjectType; import se.su.dsv.scipro.system.ProjectType;
import se.su.dsv.scipro.system.User; import se.su.dsv.scipro.system.User;
@ -102,6 +103,7 @@ public class PeerReviewServiceImplIntegrationTest extends IntegrationTest {
PeerRequest peerRequest = new PeerRequest(); PeerRequest peerRequest = new PeerRequest();
peerRequest.setProject(createProject()); peerRequest.setProject(createProject());
peerRequest.setRequester(createUser()); peerRequest.setRequester(createUser());
peerRequest.setLanguage(Language.ENGLISH);
final FileDescription fileDescription = save(new FileDescription()); final FileDescription fileDescription = save(new FileDescription());
final FileReference fileReference = new FileReference(); final FileReference fileReference = new FileReference();
fileReference.setFileDescription(fileDescription); fileReference.setFileDescription(fileDescription);

View File

@ -14,6 +14,7 @@ import se.su.dsv.scipro.file.FileDescription;
import se.su.dsv.scipro.file.FileReference; import se.su.dsv.scipro.file.FileReference;
import se.su.dsv.scipro.project.Project; import se.su.dsv.scipro.project.Project;
import se.su.dsv.scipro.system.DegreeType; import se.su.dsv.scipro.system.DegreeType;
import se.su.dsv.scipro.system.Language;
import se.su.dsv.scipro.system.PageRequest; import se.su.dsv.scipro.system.PageRequest;
import se.su.dsv.scipro.system.ProjectType; import se.su.dsv.scipro.system.ProjectType;
import se.su.dsv.scipro.system.User; import se.su.dsv.scipro.system.User;
@ -95,6 +96,7 @@ public class PeerReviewServiceImplTest extends IntegrationTest {
peerRequest.setRequester( peerRequest.setRequester(
save(User.builder().firstName("Bob").lastName("Sponge").emailAddress("bob@example.com").build()) save(User.builder().firstName("Bob").lastName("Sponge").emailAddress("bob@example.com").build())
); );
peerRequest.setLanguage(Language.ENGLISH);
final FileDescription fileDescription = save(new FileDescription()); final FileDescription fileDescription = save(new FileDescription());
final FileReference fileReference = new FileReference(); final FileReference fileReference = new FileReference();
fileReference.setFileDescription(fileDescription); fileReference.setFileDescription(fileDescription);

View File

@ -19,6 +19,7 @@ import se.su.dsv.scipro.file.FileDescription;
import se.su.dsv.scipro.file.FileReference; import se.su.dsv.scipro.file.FileReference;
import se.su.dsv.scipro.project.Project; import se.su.dsv.scipro.project.Project;
import se.su.dsv.scipro.project.ProjectStatus; import se.su.dsv.scipro.project.ProjectStatus;
import se.su.dsv.scipro.system.Language;
import se.su.dsv.scipro.system.PageRequest; import se.su.dsv.scipro.system.PageRequest;
import se.su.dsv.scipro.system.ProjectType; import se.su.dsv.scipro.system.ProjectType;
import se.su.dsv.scipro.system.User; import se.su.dsv.scipro.system.User;
@ -196,6 +197,7 @@ public class TestPeerReview extends IntegrationTest {
request.setComment(comment); request.setComment(comment);
request.setRequester(requester); request.setRequester(requester);
request.setProject(project); request.setProject(project);
request.setLanguage(Language.ENGLISH);
final FileDescription fileDescription = save(new FileDescription()); final FileDescription fileDescription = save(new FileDescription());
final FileReference fileReference = new FileReference(); final FileReference fileReference = new FileReference();
fileReference.setFileDescription(fileDescription); fileReference.setFileDescription(fileDescription);

View File

@ -13,6 +13,7 @@ import org.hamcrest.TypeSafeMatcher;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import se.su.dsv.scipro.file.FileDescription; import se.su.dsv.scipro.file.FileDescription;
import se.su.dsv.scipro.file.FileReference; import se.su.dsv.scipro.file.FileReference;
import se.su.dsv.scipro.system.User;
import se.su.dsv.scipro.test.SpringTest; import se.su.dsv.scipro.test.SpringTest;
public class UrkundSubmissionRepositoryTest extends SpringTest { public class UrkundSubmissionRepositoryTest extends SpringTest {
@ -23,11 +24,14 @@ public class UrkundSubmissionRepositoryTest extends SpringTest {
@Test @Test
public void save() { public void save() {
final Instant submitted = Instant.now(); final Instant submitted = Instant.now();
User bob = User.builder().firstName("Bob").lastName("Sponge").emailAddress("bob@example.com").build();
save(bob);
final UrkundSubmission submission = new UrkundSubmission(); final UrkundSubmission submission = new UrkundSubmission();
submission.setState(UrkundSubmission.State.SUBMITTED); submission.setState(UrkundSubmission.State.SUBMITTED);
submission.setMessage("Hi"); submission.setMessage("Hi");
submission.setSubmitted(submitted); submission.setSubmitted(submitted);
submission.setNextPoll(submitted); submission.setNextPoll(submitted);
submission.setReceiver(bob);
final FileDescription file = save(new FileDescription()); final FileDescription file = save(new FileDescription());
final FileReference fileReference = new FileReference(); final FileReference fileReference = new FileReference();
fileReference.setFileDescription(file); fileReference.setFileDescription(file);

View File

@ -4,28 +4,47 @@ import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction; import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Persistence; import jakarta.persistence.Persistence;
import java.sql.SQLException;
import java.time.Clock; import java.time.Clock;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import org.flywaydb.core.Flyway;
import org.hibernate.cfg.Environment;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.mariadb.jdbc.MariaDbDataSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import se.su.dsv.scipro.CoreConfig; import se.su.dsv.scipro.CoreConfig;
import se.su.dsv.scipro.RepositoryConfiguration; import se.su.dsv.scipro.RepositoryConfiguration;
import se.su.dsv.scipro.profiles.CurrentProfile; import se.su.dsv.scipro.profiles.CurrentProfile;
import se.su.dsv.scipro.sukat.Sukat; import se.su.dsv.scipro.sukat.Sukat;
import se.su.dsv.scipro.system.CurrentUser; import se.su.dsv.scipro.system.CurrentUser;
@Testcontainers
public abstract class SpringTest { public abstract class SpringTest {
private EntityManager entityManager; private EntityManager entityManager;
private EntityManagerFactory entityManagerFactory; private EntityManagerFactory entityManagerFactory;
@Container
static MariaDBContainer<?> mariaDBContainer = new MariaDBContainer<>("mariadb:10.11");
@BeforeEach @BeforeEach
public final void prepareSpring() { public final void prepareSpring() throws SQLException {
entityManagerFactory = Persistence.createEntityManagerFactory("testPersistenceUnit"); MariaDbDataSource dataSource = new MariaDbDataSource(mariaDBContainer.getJdbcUrl());
dataSource.setUser(mariaDBContainer.getUsername());
dataSource.setPassword(mariaDBContainer.getPassword());
Flyway.configure().dataSource(dataSource).load().migrate();
Map<String, Object> jpaProperties = Map.of(Environment.JAKARTA_JTA_DATASOURCE, dataSource);
entityManagerFactory = Persistence.createEntityManagerFactory("defaultPersistenceUnit", jpaProperties);
this.entityManager = entityManagerFactory.createEntityManager(); this.entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction(); EntityTransaction transaction = entityManager.getTransaction();
transaction.begin(); transaction.begin();

View File

@ -34,7 +34,6 @@
<querydsl.version>5.0.0</querydsl.version> <querydsl.version>5.0.0</querydsl.version>
<jakarta.servlet.version>5.0.0</jakarta.servlet.version> <jakarta.servlet.version>5.0.0</jakarta.servlet.version>
<junit.version>5.9.3</junit.version> <junit.version>5.9.3</junit.version>
<hsqldb.version>2.7.1</hsqldb.version>
<mockito.version>5.3.1</mockito.version> <mockito.version>5.3.1</mockito.version>
<flyway.version>9.19.1</flyway.version> <flyway.version>9.19.1</flyway.version>
<jersey.version>3.1.6</jersey.version> <jersey.version>3.1.6</jersey.version>
@ -133,14 +132,6 @@
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<!-- Database stuff -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.mariadb.jdbc</groupId> <groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId> <artifactId>mariadb-java-client</artifactId>