From c3f37a8f1bd3567e0c87d8536588b5648bafd68b Mon Sep 17 00:00:00 2001 From: Anatolii Karlov Date: Mon, 23 Sep 2024 19:12:38 +0700 Subject: [PATCH] experiments with Zonky (#12) --- pom.xml | 18 ++++++++++++++++ .../vality/disputes/config/DisableFlyway.java | 16 ++++++++++++++ .../disputes/config/DisableScheduling.java | 18 ++++++++++++++++ .../config/EmbeddedPostgresWithFlyway.java | 15 +++++++++++++ ...ddedPostgresWithFlywaySpringBootITest.java | 16 ++++++++++++++ .../config/PostgresqlSpringBootITest.java | 6 ++---- .../config/ZonkyEmbeddedPostgres.java | 14 +++++++++++++ .../ZonkyEmbeddedPostgresSpringBootITest.java | 16 ++++++++++++++ ...beddedPostgresWithFlywayConfiguration.java | 20 ++++++++++++++++++ .../vality/disputes/dao/DisputeDaoTest.java | 6 ++---- .../MockedUnimportantServicesConfig.java | 21 ------------------- ...eddedPostgresWithFlywayDisputeDaoTest.java | 8 +++++++ .../WithTestcontainerDisputeDaoTest.java | 8 +++++++ ...thZonkyEmbeddedPostgresDisputeDaoTest.java | 8 +++++++ 14 files changed, 161 insertions(+), 29 deletions(-) create mode 100644 src/test/java/dev/vality/disputes/config/DisableFlyway.java create mode 100644 src/test/java/dev/vality/disputes/config/DisableScheduling.java create mode 100644 src/test/java/dev/vality/disputes/config/EmbeddedPostgresWithFlyway.java create mode 100644 src/test/java/dev/vality/disputes/config/EmbeddedPostgresWithFlywaySpringBootITest.java rename src/test/java/dev/vality/disputes/{dao => }/config/PostgresqlSpringBootITest.java (68%) create mode 100644 src/test/java/dev/vality/disputes/config/ZonkyEmbeddedPostgres.java create mode 100644 src/test/java/dev/vality/disputes/config/ZonkyEmbeddedPostgresSpringBootITest.java create mode 100644 src/test/java/dev/vality/disputes/config/testconfiguration/EmbeddedPostgresWithFlywayConfiguration.java delete mode 100644 src/test/java/dev/vality/disputes/dao/config/testconfiguration/MockedUnimportantServicesConfig.java create mode 100644 src/test/java/dev/vality/disputes/dao/disputestartup/WithEmbeddedPostgresWithFlywayDisputeDaoTest.java create mode 100644 src/test/java/dev/vality/disputes/dao/disputestartup/WithTestcontainerDisputeDaoTest.java create mode 100644 src/test/java/dev/vality/disputes/dao/disputestartup/WithZonkyEmbeddedPostgresDisputeDaoTest.java diff --git a/pom.xml b/pom.xml index 78cfa03..361587e 100644 --- a/pom.xml +++ b/pom.xml @@ -260,6 +260,24 @@ 2.0.4 test + + io.zonky.test + embedded-postgres + 2.0.7 + test + + + io.zonky.test.postgres + embedded-postgres-binaries-darwin-amd64 + 16.2.0 + test + + + io.zonky.test + embedded-database-spring-test + 2.5.1 + test + diff --git a/src/test/java/dev/vality/disputes/config/DisableFlyway.java b/src/test/java/dev/vality/disputes/config/DisableFlyway.java new file mode 100644 index 0000000..0d1f3cc --- /dev/null +++ b/src/test/java/dev/vality/disputes/config/DisableFlyway.java @@ -0,0 +1,16 @@ +package dev.vality.disputes.config; + +import org.springframework.test.context.TestPropertySource; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@TestPropertySource(properties = { + "spring.flyway.enabled=false", +}) +public @interface DisableFlyway { +} diff --git a/src/test/java/dev/vality/disputes/config/DisableScheduling.java b/src/test/java/dev/vality/disputes/config/DisableScheduling.java new file mode 100644 index 0000000..90d0725 --- /dev/null +++ b/src/test/java/dev/vality/disputes/config/DisableScheduling.java @@ -0,0 +1,18 @@ +package dev.vality.disputes.config; + +import org.springframework.test.context.TestPropertySource; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@TestPropertySource(properties = { + "dispute.isScheduleCreatedEnabled=false", + "dispute.isSchedulePendingEnabled=false", + "dispute.isScheduleCreateAdjustmentsEnabled=false", +}) +public @interface DisableScheduling { +} diff --git a/src/test/java/dev/vality/disputes/config/EmbeddedPostgresWithFlyway.java b/src/test/java/dev/vality/disputes/config/EmbeddedPostgresWithFlyway.java new file mode 100644 index 0000000..19a26f9 --- /dev/null +++ b/src/test/java/dev/vality/disputes/config/EmbeddedPostgresWithFlyway.java @@ -0,0 +1,15 @@ +package dev.vality.disputes.config; + +import dev.vality.disputes.config.testconfiguration.EmbeddedPostgresWithFlywayConfiguration; +import org.springframework.context.annotation.Import; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Import(EmbeddedPostgresWithFlywayConfiguration.class) +public @interface EmbeddedPostgresWithFlyway { +} diff --git a/src/test/java/dev/vality/disputes/config/EmbeddedPostgresWithFlywaySpringBootITest.java b/src/test/java/dev/vality/disputes/config/EmbeddedPostgresWithFlywaySpringBootITest.java new file mode 100644 index 0000000..5a2f396 --- /dev/null +++ b/src/test/java/dev/vality/disputes/config/EmbeddedPostgresWithFlywaySpringBootITest.java @@ -0,0 +1,16 @@ +package dev.vality.disputes.config; + +import org.springframework.boot.test.context.SpringBootTest; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@EmbeddedPostgresWithFlyway +@DisableScheduling +@SpringBootTest +public @interface EmbeddedPostgresWithFlywaySpringBootITest { +} diff --git a/src/test/java/dev/vality/disputes/dao/config/PostgresqlSpringBootITest.java b/src/test/java/dev/vality/disputes/config/PostgresqlSpringBootITest.java similarity index 68% rename from src/test/java/dev/vality/disputes/dao/config/PostgresqlSpringBootITest.java rename to src/test/java/dev/vality/disputes/config/PostgresqlSpringBootITest.java index 369e0ba..2d7a4f9 100644 --- a/src/test/java/dev/vality/disputes/dao/config/PostgresqlSpringBootITest.java +++ b/src/test/java/dev/vality/disputes/config/PostgresqlSpringBootITest.java @@ -1,9 +1,7 @@ -package dev.vality.disputes.dao.config; +package dev.vality.disputes.config; -import dev.vality.disputes.dao.config.testconfiguration.MockedUnimportantServicesConfig; import dev.vality.testcontainers.annotations.DefaultSpringBootTest; import dev.vality.testcontainers.annotations.postgresql.PostgresqlTestcontainerSingleton; -import org.springframework.context.annotation.Import; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -13,7 +11,7 @@ import java.lang.annotation.Target; @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @PostgresqlTestcontainerSingleton +@DisableScheduling @DefaultSpringBootTest -@Import(MockedUnimportantServicesConfig.class) public @interface PostgresqlSpringBootITest { } diff --git a/src/test/java/dev/vality/disputes/config/ZonkyEmbeddedPostgres.java b/src/test/java/dev/vality/disputes/config/ZonkyEmbeddedPostgres.java new file mode 100644 index 0000000..c0256b1 --- /dev/null +++ b/src/test/java/dev/vality/disputes/config/ZonkyEmbeddedPostgres.java @@ -0,0 +1,14 @@ +package dev.vality.disputes.config; + +import io.zonky.test.db.AutoConfigureEmbeddedDatabase; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@AutoConfigureEmbeddedDatabase(provider = AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY) +public @interface ZonkyEmbeddedPostgres { +} diff --git a/src/test/java/dev/vality/disputes/config/ZonkyEmbeddedPostgresSpringBootITest.java b/src/test/java/dev/vality/disputes/config/ZonkyEmbeddedPostgresSpringBootITest.java new file mode 100644 index 0000000..5af6d48 --- /dev/null +++ b/src/test/java/dev/vality/disputes/config/ZonkyEmbeddedPostgresSpringBootITest.java @@ -0,0 +1,16 @@ +package dev.vality.disputes.config; + +import org.springframework.boot.test.context.SpringBootTest; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@ZonkyEmbeddedPostgres +@DisableScheduling +@SpringBootTest +public @interface ZonkyEmbeddedPostgresSpringBootITest { +} diff --git a/src/test/java/dev/vality/disputes/config/testconfiguration/EmbeddedPostgresWithFlywayConfiguration.java b/src/test/java/dev/vality/disputes/config/testconfiguration/EmbeddedPostgresWithFlywayConfiguration.java new file mode 100644 index 0000000..69f42b9 --- /dev/null +++ b/src/test/java/dev/vality/disputes/config/testconfiguration/EmbeddedPostgresWithFlywayConfiguration.java @@ -0,0 +1,20 @@ +package dev.vality.disputes.config.testconfiguration; + +import io.zonky.test.db.postgres.embedded.FlywayPreparer; +import io.zonky.test.db.postgres.embedded.PreparedDbProvider; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; +import java.sql.SQLException; + +@TestConfiguration +public class EmbeddedPostgresWithFlywayConfiguration { + + @Bean + public DataSource dataSource() throws SQLException { + return PreparedDbProvider + .forPreparer(FlywayPreparer.forClasspathLocation("db/migration")) + .createDataSource(); + } +} diff --git a/src/test/java/dev/vality/disputes/dao/DisputeDaoTest.java b/src/test/java/dev/vality/disputes/dao/DisputeDaoTest.java index 29f9960..201d128 100644 --- a/src/test/java/dev/vality/disputes/dao/DisputeDaoTest.java +++ b/src/test/java/dev/vality/disputes/dao/DisputeDaoTest.java @@ -1,6 +1,5 @@ package dev.vality.disputes.dao; -import dev.vality.disputes.dao.config.PostgresqlSpringBootITest; import dev.vality.disputes.domain.tables.pojos.Dispute; import dev.vality.disputes.exception.NotFoundException; import org.junit.jupiter.api.Test; @@ -14,8 +13,7 @@ import static dev.vality.testcontainers.annotations.util.ValuesGenerator.generat import static dev.vality.testcontainers.annotations.util.ValuesGenerator.generateLong; import static org.junit.jupiter.api.Assertions.*; -@PostgresqlSpringBootITest -public class DisputeDaoTest { +public abstract class DisputeDaoTest { @Autowired private DisputeDao disputeDao; @@ -48,7 +46,7 @@ public class DisputeDaoTest { } @Test - public void asd() { + public void testNextCheckAfter() { var random = random(Dispute.class); var createdAt = LocalDateTime.now(ZoneOffset.UTC); random.setCreatedAt(createdAt); diff --git a/src/test/java/dev/vality/disputes/dao/config/testconfiguration/MockedUnimportantServicesConfig.java b/src/test/java/dev/vality/disputes/dao/config/testconfiguration/MockedUnimportantServicesConfig.java deleted file mode 100644 index 9ccf13b..0000000 --- a/src/test/java/dev/vality/disputes/dao/config/testconfiguration/MockedUnimportantServicesConfig.java +++ /dev/null @@ -1,21 +0,0 @@ -package dev.vality.disputes.dao.config.testconfiguration; - -import dev.vality.disputes.schedule.TaskCreateAdjustmentsService; -import dev.vality.disputes.schedule.TaskCreatedDisputesService; -import dev.vality.disputes.schedule.TaskPendingDisputesService; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.boot.test.mock.mockito.MockBean; - -@TestConfiguration -public class MockedUnimportantServicesConfig { - - @MockBean - private TaskCreatedDisputesService taskCreatedDisputesService; - - @MockBean - private TaskPendingDisputesService taskPendingDisputesService; - - @MockBean - private TaskCreateAdjustmentsService taskCreateAdjustmentsService; - -} diff --git a/src/test/java/dev/vality/disputes/dao/disputestartup/WithEmbeddedPostgresWithFlywayDisputeDaoTest.java b/src/test/java/dev/vality/disputes/dao/disputestartup/WithEmbeddedPostgresWithFlywayDisputeDaoTest.java new file mode 100644 index 0000000..afa3293 --- /dev/null +++ b/src/test/java/dev/vality/disputes/dao/disputestartup/WithEmbeddedPostgresWithFlywayDisputeDaoTest.java @@ -0,0 +1,8 @@ +package dev.vality.disputes.dao.disputestartup; + +import dev.vality.disputes.config.EmbeddedPostgresWithFlywaySpringBootITest; +import dev.vality.disputes.dao.DisputeDaoTest; + +@EmbeddedPostgresWithFlywaySpringBootITest +public class WithEmbeddedPostgresWithFlywayDisputeDaoTest extends DisputeDaoTest { +} diff --git a/src/test/java/dev/vality/disputes/dao/disputestartup/WithTestcontainerDisputeDaoTest.java b/src/test/java/dev/vality/disputes/dao/disputestartup/WithTestcontainerDisputeDaoTest.java new file mode 100644 index 0000000..60541ae --- /dev/null +++ b/src/test/java/dev/vality/disputes/dao/disputestartup/WithTestcontainerDisputeDaoTest.java @@ -0,0 +1,8 @@ +package dev.vality.disputes.dao.disputestartup; + +import dev.vality.disputes.config.PostgresqlSpringBootITest; +import dev.vality.disputes.dao.DisputeDaoTest; + +@PostgresqlSpringBootITest +public class WithTestcontainerDisputeDaoTest extends DisputeDaoTest { +} diff --git a/src/test/java/dev/vality/disputes/dao/disputestartup/WithZonkyEmbeddedPostgresDisputeDaoTest.java b/src/test/java/dev/vality/disputes/dao/disputestartup/WithZonkyEmbeddedPostgresDisputeDaoTest.java new file mode 100644 index 0000000..f06aa81 --- /dev/null +++ b/src/test/java/dev/vality/disputes/dao/disputestartup/WithZonkyEmbeddedPostgresDisputeDaoTest.java @@ -0,0 +1,8 @@ +package dev.vality.disputes.dao.disputestartup; + +import dev.vality.disputes.config.ZonkyEmbeddedPostgresSpringBootITest; +import dev.vality.disputes.dao.DisputeDaoTest; + +@ZonkyEmbeddedPostgresSpringBootITest +public class WithZonkyEmbeddedPostgresDisputeDaoTest extends DisputeDaoTest { +}