diff --git a/.gitignore b/.gitignore index cdff0bf46977a6a2055f0f613dd12dcff9118344..90fe81dc6b66f83a4a6bd693cecbf1e170a800cb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ certs/ *.p12 *.db +!BudgetMasterDatabaseMigrator/src/test/resources/*.mv.db *.iml */build/[0-9]*.[0-9]*.[0-9]* diff --git a/BudgetMasterDatabaseMigrator/pom.xml b/BudgetMasterDatabaseMigrator/pom.xml index 9e084b805eafbb4503526e85c38dcf7ac739155c..9603d82a93a2b2df7afe2903c0e6be93535e821a 100644 --- a/BudgetMasterDatabaseMigrator/pom.xml +++ b/BudgetMasterDatabaseMigrator/pom.xml @@ -25,6 +25,12 @@ <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> + <dependency> + <groupId>org.springframework.batch</groupId> + <artifactId>spring-batch-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> diff --git a/BudgetMasterDatabaseMigrator/src/test/java/de/deadlocker8/budgetmaster/databasemigrator/MigrateDefaultDatabaseTest.java b/BudgetMasterDatabaseMigrator/src/test/java/de/deadlocker8/budgetmaster/databasemigrator/MigrateDefaultDatabaseTest.java new file mode 100644 index 0000000000000000000000000000000000000000..246d47a961e124301c9b883a0b2d99c29f05fb5c --- /dev/null +++ b/BudgetMasterDatabaseMigrator/src/test/java/de/deadlocker8/budgetmaster/databasemigrator/MigrateDefaultDatabaseTest.java @@ -0,0 +1,36 @@ +package de.deadlocker8.budgetmaster.databasemigrator; + +import org.junit.jupiter.api.Test; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.StepExecution; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class MigrateDefaultDatabaseTest extends MigratorTestBase +{ + @Test + void test_jobMigrate() throws Exception + { + final JobExecution jobExecution = jobLauncherTestUtils.launchJob(DEFAULT_JOB_PARAMETERS); + + assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED); + } + + @Test + void test_stepMigrateImages_noImages() + { + final JobExecution jobExecution = jobLauncherTestUtils.launchStep("Migrate images", DEFAULT_JOB_PARAMETERS); + final List<StepExecution> stepExecutions = new ArrayList<>(jobExecution.getStepExecutions()); + + assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED); + + assertThat(stepExecutions).hasSize(1); + final StepExecution stepExecution = stepExecutions.get(0); + assertThat(stepExecution.getReadCount()).isZero(); + assertThat(stepExecution.getCommitCount()).isOne(); + } +} \ No newline at end of file diff --git a/BudgetMasterDatabaseMigrator/src/test/java/de/deadlocker8/budgetmaster/databasemigrator/MigratorTestBase.java b/BudgetMasterDatabaseMigrator/src/test/java/de/deadlocker8/budgetmaster/databasemigrator/MigratorTestBase.java new file mode 100644 index 0000000000000000000000000000000000000000..abf8383ef804c5889afe53210f07fed2f875c493 --- /dev/null +++ b/BudgetMasterDatabaseMigrator/src/test/java/de/deadlocker8/budgetmaster/databasemigrator/MigratorTestBase.java @@ -0,0 +1,90 @@ +package de.deadlocker8.budgetmaster.databasemigrator; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.batch.test.JobRepositoryTestUtils; +import org.springframework.batch.test.context.SpringBatchTest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Primary; +import org.springframework.core.io.Resource; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import javax.sql.DataSource; +import java.io.IOException; + + +@SpringBatchTest +@SpringBootTest +@ExtendWith(SpringExtension.class) +@Testcontainers +@Import(MigratorTestBase.TestDatabaseConfiguration.class) +@ActiveProfiles("test") +@EnableAutoConfiguration +@ContextConfiguration(classes = {BatchConfiguration.class}) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) +public abstract class MigratorTestBase +{ + protected static final JobParameters DEFAULT_JOB_PARAMETERS = new JobParametersBuilder().toJobParameters(); + + @Autowired + protected JobLauncherTestUtils jobLauncherTestUtils; + + @Autowired + protected JobRepositoryTestUtils jobRepositoryTestUtils; + + @AfterEach + public void afterEach() + { + jobRepositoryTestUtils.removeJobExecutions(); + } + + @Container + static PostgreSQLContainer<?> postgresDB = new PostgreSQLContainer<>("postgres:14.2") + .withDatabaseName("budgetmaster-tests-db") + .withUsername("budgetmaster") + .withPassword("BudgetMaster"); + + @TestConfiguration + static class TestDatabaseConfiguration + { + @Value("classpath:default_database_after_first_start.mv.db") + private Resource databaseResource; + + @Bean + @Primary + public DataSource dataSource() throws IOException + { + final String folderName = databaseResource.getFile().getAbsolutePath().replace(".mv.db", ""); + String jdbcString = "jdbc:h2:/" + folderName + ";DB_CLOSE_ON_EXIT=TRUE"; + return DataSourceBuilder.create().username("sa").password("").url(jdbcString).driverClassName("org.h2.Driver").build(); + } + } + + @DynamicPropertySource + static void properties(DynamicPropertyRegistry registry) + { + registry.add("spring.seconddatasource.url", postgresDB::getJdbcUrl); + registry.add("spring.seconddatasource.username", postgresDB::getUsername); + registry.add("spring.seconddatasource.password", postgresDB::getPassword); + } +} diff --git a/BudgetMasterDatabaseMigrator/src/test/resources/default_database_after_first_start.mv.db b/BudgetMasterDatabaseMigrator/src/test/resources/default_database_after_first_start.mv.db new file mode 100644 index 0000000000000000000000000000000000000000..36ca61ae144e56adb8d0d616095a9c96a576d8df Binary files /dev/null and b/BudgetMasterDatabaseMigrator/src/test/resources/default_database_after_first_start.mv.db differ diff --git a/BudgetMasterServer/pom.xml b/BudgetMasterServer/pom.xml index 42eb9c36dba696236871348d23f7b1b8fb2bb420..982d787d386a12ec1c5dbe3cdb9be28917a80ff6 100644 --- a/BudgetMasterServer/pom.xml +++ b/BudgetMasterServer/pom.xml @@ -34,14 +34,12 @@ <mousetrap.version>1.6.5</mousetrap.version> <codemirror.version>5.62.2</codemirror.version> <selenium.version>4.1.2</selenium.version> - <assertj-core.version>3.22.0</assertj-core.version> <jgit.version>6.0.0.202111291000-r</jgit.version> <natorder.version>1.1.2</natorder.version> <itextpdf.version>5.5.13.2</itextpdf.version> <vanilla-picker.version>2.12.1</vanilla-picker.version> <jacoco-maven-plugin.version>0.8.7</jacoco-maven-plugin.version> <dependency-check-maven.version>6.5.3</dependency-check-maven.version> - <testcontainer.version>1.16.3</testcontainer.version> <project.outputDirectory>build/${project.version}</project.outputDirectory> <project.artifactName>${project.artifactId}-v${project.version}</project.artifactName> @@ -63,12 +61,6 @@ <artifactId>spring-boot-starter-web</artifactId> </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-test</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> @@ -197,31 +189,11 @@ <scope>test</scope> </dependency> - <dependency> - <groupId>org.assertj</groupId> - <artifactId>assertj-core</artifactId> - <version>${assertj-core.version}</version> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-inline</artifactId> <scope>test</scope> </dependency> - - <dependency> - <groupId>org.testcontainers</groupId> - <artifactId>junit-jupiter</artifactId> - <version>${testcontainer.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.testcontainers</groupId> - <artifactId>postgresql</artifactId> - <version>${testcontainer.version}</version> - <scope>test</scope> - </dependency> </dependencies> <build> diff --git a/pom.xml b/pom.xml index 109be0af2e7218a678ca1e641db430d585004a79..b6c34edd48ed8b664af254ba861707129b622a75 100644 --- a/pom.xml +++ b/pom.xml @@ -54,8 +54,40 @@ <maven.build.timestamp.format>dd.MM.yy</maven.build.timestamp.format> <app.versionCode>38</app.versionCode> <app.author>Robert Goldmann</app.author> + + <testcontainer.version>1.16.3</testcontainer.version> + <assertj-core.version>3.22.0</assertj-core.version> </properties> + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj-core.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>junit-jupiter</artifactId> + <version>${testcontainer.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>postgresql</artifactId> + <version>${testcontainer.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + <distributionManagement> <repository> <id>release</id>