From fe928b9e046ec98b63eb9195026894382b947331 Mon Sep 17 00:00:00 2001 From: lpandzic Date: Thu, 31 Oct 2024 07:18:07 +0100 Subject: [PATCH] added example for issue 105 --- .../pom.xml | 42 ++++++++++++++++ .../src/test/java/com/infobip/test/Main.java | 14 ++++++ .../test/java/com/infobip/test/Person.java | 16 +++++++ .../com/infobip/test/PersonRepository.java | 6 +++ .../test/QuerydslR2dbcRepositoryTest.java | 15 ++++++ .../test/ReactivePagingRepository.java | 8 ++++ .../test/ReactivePagingRepositoryImpl.java | 19 ++++++++ .../test/java/com/infobip/test/TestBase.java | 48 +++++++++++++++++++ .../com/infobip/test/TestConfiguration.java | 26 ++++++++++ .../src/test/resources/application-mssql.yaml | 6 +++ .../migration/mssql/V1_0_0__base_schema.sql | 37 ++++++++++++++ pom.xml | 1 + 12 files changed, 238 insertions(+) create mode 100644 infobip-spring-data-r2dbc-custom-fragment/pom.xml create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/Main.java create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/Person.java create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/PersonRepository.java create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/QuerydslR2dbcRepositoryTest.java create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/ReactivePagingRepository.java create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/ReactivePagingRepositoryImpl.java create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/TestBase.java create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/TestConfiguration.java create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/resources/application-mssql.yaml create mode 100644 infobip-spring-data-r2dbc-custom-fragment/src/test/resources/db/migration/mssql/V1_0_0__base_schema.sql diff --git a/infobip-spring-data-r2dbc-custom-fragment/pom.xml b/infobip-spring-data-r2dbc-custom-fragment/pom.xml new file mode 100644 index 0000000..6fa3301 --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + + com.infobip + infobip-spring-data-querydsl + 9.0.8-SNAPSHOT + + + infobip-spring-data-r2dbc-custom-fragment + + + + ${project.groupId} + infobip-spring-data-r2dbc-querydsl-boot-starter + ${project.version} + + + + + io.r2dbc + r2dbc-mssql + test + + + + io.projectreactor + reactor-test + test + + + + com.zaxxer + HikariCP + test + + + + \ No newline at end of file diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/Main.java b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/Main.java new file mode 100644 index 0000000..a8fa075 --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/Main.java @@ -0,0 +1,14 @@ +package com.infobip.test; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +@SpringBootApplication +public class Main { + + public static void main(String[] args) { + + new SpringApplicationBuilder(Main.class).run(args); + } + +} diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/Person.java b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/Person.java new file mode 100644 index 0000000..6dde86d --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/Person.java @@ -0,0 +1,16 @@ +package com.infobip.test; + +import lombok.With; +import org.springframework.data.annotation.Id; + +public record Person( + @With + @Id + Long id, + + String firstName, + + String lastName +) { + +} diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/PersonRepository.java b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/PersonRepository.java new file mode 100644 index 0000000..178296c --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/PersonRepository.java @@ -0,0 +1,6 @@ +package com.infobip.test; + +import com.infobip.spring.data.r2dbc.QuerydslR2dbcRepository; + +public interface PersonRepository extends QuerydslR2dbcRepository, ReactivePagingRepository { +} diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/QuerydslR2dbcRepositoryTest.java b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/QuerydslR2dbcRepositoryTest.java new file mode 100644 index 0000000..9fd4117 --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/QuerydslR2dbcRepositoryTest.java @@ -0,0 +1,15 @@ +package com.infobip.test; + +import lombok.AllArgsConstructor; +import org.junit.jupiter.api.Test; + +@AllArgsConstructor +public class QuerydslR2dbcRepositoryTest extends TestBase { + + private final PersonRepository repository; + + @Test + void shouldSaveWithVarArgs() { + repository.simplePaging(""); + } +} diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/ReactivePagingRepository.java b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/ReactivePagingRepository.java new file mode 100644 index 0000000..33f5fd7 --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/ReactivePagingRepository.java @@ -0,0 +1,8 @@ +package com.infobip.test; + +import reactor.core.publisher.Mono; + +public interface ReactivePagingRepository { + + Mono simplePaging(String string); +} diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/ReactivePagingRepositoryImpl.java b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/ReactivePagingRepositoryImpl.java new file mode 100644 index 0000000..058e83b --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/ReactivePagingRepositoryImpl.java @@ -0,0 +1,19 @@ +package com.infobip.test; + +import com.infobip.spring.data.r2dbc.QuerydslR2dbcFragment; +import org.springframework.context.annotation.Lazy; +import reactor.core.publisher.Mono; + +public class ReactivePagingRepositoryImpl implements ReactivePagingRepository { + + private final QuerydslR2dbcFragment querydslR2dbcFragment; + + public ReactivePagingRepositoryImpl(@Lazy QuerydslR2dbcFragment querydslR2dbcFragment) { + this.querydslR2dbcFragment = querydslR2dbcFragment; + } + + @Override + public Mono simplePaging(String string) { + throw new UnsupportedOperationException(); + } +} diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/TestBase.java b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/TestBase.java new file mode 100644 index 0000000..111290e --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/TestBase.java @@ -0,0 +1,48 @@ +package com.infobip.test; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.TestInstance; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.repository.reactive.ReactiveCrudRepository; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestConstructor; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.time.Duration; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@ActiveProfiles("mssql") +@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL) +@TestInstance(PER_CLASS) +@SpringBootTest(classes = Main.class) +public abstract class TestBase { + + @Autowired + private List> repositories; + + @AfterEach + public void clearRepositories() { + block(Flux.concat(repositories.stream() + .map(ReactiveCrudRepository::deleteAll) + .collect(Collectors.toList())) + .collectList()); + } + + private T block(Mono mono) { + return mono.block(Duration.ofSeconds(10)); + } + + List block(Flux flux) { + return block(flux.collectList()); + } + + protected Mono given(Mono... ts) { + return Flux.concat(Stream.of(ts).collect(Collectors.toList())).last().then(); + } +} diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/TestConfiguration.java b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/TestConfiguration.java new file mode 100644 index 0000000..eccde7c --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/java/com/infobip/test/TestConfiguration.java @@ -0,0 +1,26 @@ +package com.infobip.test; + +import lombok.AllArgsConstructor; +import org.flywaydb.core.Flyway; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; + +@AllArgsConstructor +@Configuration +public class TestConfiguration { + + private final Environment env; + + @Bean(initMethod = "migrate") + public Flyway flyway() { + return new Flyway(Flyway.configure() + .baselineOnMigrate(true) + .locations(env.getRequiredProperty("spring.flyway.locations")) + .dataSource( + env.getRequiredProperty("spring.flyway.url"), + env.getRequiredProperty("spring.flyway.username"), + env.getRequiredProperty("spring.flyway.password")) + ); + } +} diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/resources/application-mssql.yaml b/infobip-spring-data-r2dbc-custom-fragment/src/test/resources/application-mssql.yaml new file mode 100644 index 0000000..010fb04 --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/resources/application-mssql.yaml @@ -0,0 +1,6 @@ +spring: + r2dbc: + url: r2dbc:pool:mssql://:/InfobipSpringDataJdbcQuerydslTest + flyway: + locations: "classpath:db/migration/mssql" + url: jdbc:sqlserver://:;database=InfobipSpringDataJdbcQuerydslTest;trustServerCertificate=true diff --git a/infobip-spring-data-r2dbc-custom-fragment/src/test/resources/db/migration/mssql/V1_0_0__base_schema.sql b/infobip-spring-data-r2dbc-custom-fragment/src/test/resources/db/migration/mssql/V1_0_0__base_schema.sql new file mode 100644 index 0000000..fede9dc --- /dev/null +++ b/infobip-spring-data-r2dbc-custom-fragment/src/test/resources/db/migration/mssql/V1_0_0__base_schema.sql @@ -0,0 +1,37 @@ +CREATE TABLE Person ( + Id BIGINT IDENTITY, + FirstName NVARCHAR(20) NOT NULL, + LastName NVARCHAR(50) NOT NULL, + CONSTRAINT PK_Person PRIMARY KEY (Id) +); + +CREATE TABLE PersonSettings ( + Id BIGINT IDENTITY, + PersonId BIGINT NOT NULL, + CONSTRAINT PK_PersonSettings PRIMARY KEY (Id), + CONSTRAINT FK_PersonSettings_PersonId FOREIGN KEY (PersonId) REFERENCES Person(Id) ON DELETE CASCADE +); + +CREATE TABLE NoArgsEntity ( + Id BIGINT IDENTITY, + Value NVARCHAR(20), + CONSTRAINT PK_NoArgsEntity PRIMARY KEY (Id), +); + +CREATE TABLE TransientEntity ( + Id BIGINT IDENTITY, + Value NVARCHAR(20), + CONSTRAINT PK_TransientEntity PRIMARY KEY (Id), +); + +CREATE TABLE PagingEntity ( + Id BIGINT IDENTITY, + Value NVARCHAR(20), + CONSTRAINT PK_PagingEntity PRIMARY KEY (Id), +); + +CREATE TABLE sorting_entity ( + id BIGINT IDENTITY, + foo_bar NVARCHAR(20), + CONSTRAINT PK_sorting_entity PRIMARY KEY (id), +); \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3ddaf3e..e76a370 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ infobip-spring-data-jdbc-annotation-processor-common infobip-spring-data-r2dbc-querydsl infobip-spring-data-r2dbc-querydsl-boot-starter + infobip-spring-data-r2dbc-custom-fragment