From 58471802871c0a1ccbda70290a41bed8ed6f303f Mon Sep 17 00:00:00 2001 From: hantsy Date: Mon, 31 Jan 2022 11:34:02 +0800 Subject: [PATCH] refactor: refactor Github actions workflows --- .github/workflows/auditing.yml | 41 ++++ .github/workflows/boot.yml | 41 ++++ .github/workflows/build.yml | 222 ------------------ .github/workflows/connection-factories.yml | 35 +++ .../workflows/data-r2dbc-entitytemplate.yml | 39 +++ .github/workflows/data-r2dbc-repositories.yml | 39 +++ .github/workflows/database-client.yml | 39 +++ .github/workflows/entitycallbacks.yml | 39 +++ .github/workflows/testconainers.yml | 39 +++ auditing/pom.xml | 2 +- boot/pom.xml | 2 +- data-r2dbc-entitytemplate/pom.xml | 2 +- data-r2dbc-repositories/pom.xml | 2 +- database-client/pom.xml | 2 +- entitycallbacks/pom.xml | 2 +- testcontainers/pom.xml | 110 +++------ ...oryWithDockerContainerInitializerTest.java | 92 ++++++++ ...positoryWithDynamicPropertySourceTest.java | 79 +++++++ ...ostRepositoryWithR2dbcSupportUrlTest.java} | 4 +- testcontainers/src/test/resources/init.sql | 19 ++ 20 files changed, 540 insertions(+), 310 deletions(-) create mode 100644 .github/workflows/auditing.yml create mode 100644 .github/workflows/boot.yml delete mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/connection-factories.yml create mode 100644 .github/workflows/data-r2dbc-entitytemplate.yml create mode 100644 .github/workflows/data-r2dbc-repositories.yml create mode 100644 .github/workflows/database-client.yml create mode 100644 .github/workflows/entitycallbacks.yml create mode 100644 .github/workflows/testconainers.yml create mode 100644 testcontainers/src/test/java/com/example/demo/PostRepositoryWithDockerContainerInitializerTest.java create mode 100644 testcontainers/src/test/java/com/example/demo/PostRepositoryWithDynamicPropertySourceTest.java rename testcontainers/src/test/java/com/example/demo/{PostRepositoryTest.java => PostRepositoryWithR2dbcSupportUrlTest.java} (91%) create mode 100644 testcontainers/src/test/resources/init.sql diff --git a/.github/workflows/auditing.yml b/.github/workflows/auditing.yml new file mode 100644 index 00000000..b056534e --- /dev/null +++ b/.github/workflows/auditing.yml @@ -0,0 +1,41 @@ +name: auditing + +on: + push: + paths: + - "auditing/**" + branches: + - master + pull_request: + paths: + - "auditing/**" + types: + - opened + - synchronize + - reopened + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Java + uses: actions/setup-java@v2 + with: + distribution: "zulu" + java-version: 17 + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Start up PostgreSQL in Docker + run: | + docker-compose up -d postgres + sleep 10 + docker ps -a + - name: Build with Maven + run: | + mvn clean install --file auditing/pom.xml + mvn clean verify -Pit --file auditing/pom.xml \ No newline at end of file diff --git a/.github/workflows/boot.yml b/.github/workflows/boot.yml new file mode 100644 index 00000000..4f2cf01b --- /dev/null +++ b/.github/workflows/boot.yml @@ -0,0 +1,41 @@ +name: boot + +on: + push: + paths: + - "boot/**" + branches: + - master + pull_request: + paths: + - "boot/**" + types: + - opened + - synchronize + - reopened + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Java + uses: actions/setup-java@v2 + with: + distribution: "zulu" + java-version: 17 + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Start up PostgreSQL in Docker + run: | + docker-compose up -d postgres + sleep 10 + docker ps -a + - name: Build with Maven + run: | + mvn clean install --file boot/pom.xml + mvn clean verify -Pit --file boot/pom.xml \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index cd8b42f6..00000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,222 +0,0 @@ -name: build - -on: - push: - paths-ignore: - - "docs/**" - branches: - - master - pull_request: - types: - - opened - - synchronize - - reopened - -jobs: - connection-factories: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Set up Java - uses: actions/setup-java@v2 - with: - distribution: "adopt" - java-version: 16 -# - name: Start up PostgreSQL in Docker -# run: | -# docker-compose up -d postgres -# docker ps -a - - name: Cache Maven packages - uses: actions/cache@v2 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Build connectionfactories - run: mvn clean install --file connection-factories/pom.xml -Dmaven.test.skip=true -DskipTests - - data-r2dbc-repositories: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - with: - # Disabling shallow clone is recommended for improving relevancy of reporting - fetch-depth: 0 - - - name: Set up JDK 16 - uses: actions/setup-java@v2 - with: - java-version: 16 - distribution: "adopt" - - name: Start up PostgreSQL in Docker - run: | - docker-compose up -d postgres - docker ps -a - - name: Cache Maven packages - uses: actions/cache@v2.1.7 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Build with Maven - run: mvn clean install --file data-r2dbc-repositories/pom.xml - data-r2dbc-entitytemplate: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - with: - # Disabling shallow clone is recommended for improving relevancy of reporting - fetch-depth: 0 - - - name: Set up JDK 16 - uses: actions/setup-java@v2 - with: - java-version: 16 - distribution: "adopt" - - name: Start up PostgreSQL in Docker - run: | - docker-compose up -d postgres - docker ps -a - - name: Cache Maven packages - uses: actions/cache@v2.1.7 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Build with Maven - run: mvn clean install --file data-r2dbc-entitytemplate/pom.xml - database-client: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - with: - # Disabling shallow clone is recommended for improving relevancy of reporting - fetch-depth: 0 - - - name: Set up JDK 16 - uses: actions/setup-java@v2 - with: - java-version: 16 - distribution: "adopt" - - name: Start up PostgreSQL in Docker - run: | - docker-compose up -d postgres - docker ps -a - - name: Cache Maven packages - uses: actions/cache@v2.1.7 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Build with Maven - run: mvn clean install --file database-client/pom.xml - entitycallbacks: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - with: - # Disabling shallow clone is recommended for improving relevancy of reporting - fetch-depth: 0 - - - name: Set up JDK 16 - uses: actions/setup-java@v2 - with: - java-version: 16 - distribution: "adopt" -# - name: Start up PostgreSQL in Docker -# run: | -# docker-compose up -d postgres -# docker ps -a - - name: Cache Maven packages - uses: actions/cache@v2.1.7 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Build with Maven - run: mvn clean install --file entitycallbacks/pom.xml -Dmaven.test.skip=true -DskipTests - boot: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - with: - # Disabling shallow clone is recommended for improving relevancy of reporting - fetch-depth: 0 - - - name: Set up JDK 16 - uses: actions/setup-java@v2 - with: - java-version: 16 - distribution: "adopt" - - name: Start up PostgreSQL in Docker - run: | - docker-compose up -d postgres - docker ps -a - - name: Cache Maven packages - uses: actions/cache@v2.1.7 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Build with Maven - run: | - mvn clean install --file boot/pom.xml - mvn clean verify -Pit --file boot/pom.xml - auditing: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - with: - # Disabling shallow clone is recommended for improving relevancy of reporting - fetch-depth: 0 - - - name: Set up JDK 16 - uses: actions/setup-java@v2 - with: - java-version: 16 - distribution: "adopt" - - name: Start up PostgreSQL in Docker - run: | - docker-compose up -d postgres - docker ps -a - - name: Cache Maven packages - uses: actions/cache@v2.1.7 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Build with Maven - run: | - mvn clean install --file auditing/pom.xml - mvn clean verify -Pit --file auditing/pom.xml - testcontainers: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - with: - # Disabling shallow clone is recommended for improving relevancy of reporting - fetch-depth: 0 - - - name: Set up JDK 16 - uses: actions/setup-java@v2 - with: - java-version: 16 - distribution: "adopt" - - - name: Cache Maven packages - uses: actions/cache@v2.1.7 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - - name: Build with Maven - run: | - mvn clean install --file testcontainers/pom.xml diff --git a/.github/workflows/connection-factories.yml b/.github/workflows/connection-factories.yml new file mode 100644 index 00000000..db43ccbc --- /dev/null +++ b/.github/workflows/connection-factories.yml @@ -0,0 +1,35 @@ +name: connection-factories + +on: + push: + paths: + - "connection-factories/**" + branches: + - master + pull_request: + paths: + - "connection-factories/**" + types: + - opened + - synchronize + - reopened + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Java + uses: actions/setup-java@v2 + with: + distribution: "zulu" + java-version: 17 + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + + - name: Build connectionfactories + run: mvn clean install --file connection-factories/pom.xml -Dmaven.test.skip=true -DskipTests \ No newline at end of file diff --git a/.github/workflows/data-r2dbc-entitytemplate.yml b/.github/workflows/data-r2dbc-entitytemplate.yml new file mode 100644 index 00000000..41d43827 --- /dev/null +++ b/.github/workflows/data-r2dbc-entitytemplate.yml @@ -0,0 +1,39 @@ +name: data-r2dbc-entitytemplate + +on: + push: + paths: + - "data-r2dbc-entitytemplate/**" + branches: + - master + pull_request: + paths: + - "data-r2dbc-entitytemplate/**" + types: + - opened + - synchronize + - reopened + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Java + uses: actions/setup-java@v2 + with: + distribution: "zulu" + java-version: 17 + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Start up PostgreSQL in Docker + run: | + docker-compose up -d postgres + sleep 10 + docker ps -a + - name: Build with Maven + run: mvn clean install --file data-r2dbc-entitytemplate/pom.xml \ No newline at end of file diff --git a/.github/workflows/data-r2dbc-repositories.yml b/.github/workflows/data-r2dbc-repositories.yml new file mode 100644 index 00000000..daf55a51 --- /dev/null +++ b/.github/workflows/data-r2dbc-repositories.yml @@ -0,0 +1,39 @@ +name: data-r2dbc-repositories + +on: + push: + paths: + - "data-r2dbc-repositories/**" + branches: + - master + pull_request: + paths: + - "data-r2dbc-repositories/**" + types: + - opened + - synchronize + - reopened + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Java + uses: actions/setup-java@v2 + with: + distribution: "zulu" + java-version: 17 + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Start up PostgreSQL in Docker + run: | + docker-compose up -d postgres + sleep 10 + docker ps -a + - name: Build with Maven + run: mvn clean install --file data-r2dbc-repositories/pom.xml \ No newline at end of file diff --git a/.github/workflows/database-client.yml b/.github/workflows/database-client.yml new file mode 100644 index 00000000..d2e96440 --- /dev/null +++ b/.github/workflows/database-client.yml @@ -0,0 +1,39 @@ +name: database-client + +on: + push: + paths: + - "database-client/**" + branches: + - master + pull_request: + paths: + - "database-client/**" + types: + - opened + - synchronize + - reopened + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Java + uses: actions/setup-java@v2 + with: + distribution: "zulu" + java-version: 17 + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Start up PostgreSQL in Docker + run: | + docker-compose up -d postgres + sleep 10 + docker ps -a + - name: Build with Maven + run: mvn clean install --file database-client/pom.xml \ No newline at end of file diff --git a/.github/workflows/entitycallbacks.yml b/.github/workflows/entitycallbacks.yml new file mode 100644 index 00000000..5d16d9f8 --- /dev/null +++ b/.github/workflows/entitycallbacks.yml @@ -0,0 +1,39 @@ +name: entitycallbacks + +on: + push: + paths: + - "entitycallbacks/**" + branches: + - master + pull_request: + paths: + - "entitycallbacks/**" + types: + - opened + - synchronize + - reopened + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Java + uses: actions/setup-java@v2 + with: + distribution: "zulu" + java-version: 17 + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Start up PostgreSQL in Docker + run: | + docker-compose up -d postgres + sleep 10 + docker ps -a + - name: Build with Maven + run: mvn clean install --file entitycallbacks/pom.xml \ No newline at end of file diff --git a/.github/workflows/testconainers.yml b/.github/workflows/testconainers.yml new file mode 100644 index 00000000..00a866cf --- /dev/null +++ b/.github/workflows/testconainers.yml @@ -0,0 +1,39 @@ +name: testcontainers + +on: + push: + paths: + - "testcontainers/**" + branches: + - master + pull_request: + paths: + - "testcontainers/**" + types: + - opened + - synchronize + - reopened + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Java + uses: actions/setup-java@v2 + with: + distribution: "zulu" + java-version: 17 + - name: Cache Maven packages + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Start up PostgreSQL in Docker + run: | + docker-compose up -d postgres + sleep 10 + docker ps -a + - name: Build with Maven + run: mvn clean install --file testcontainers/pom.xml \ No newline at end of file diff --git a/auditing/pom.xml b/auditing/pom.xml index 75d226b9..e951b28e 100644 --- a/auditing/pom.xml +++ b/auditing/pom.xml @@ -15,7 +15,7 @@ Demo project for Spring Boot - 11 + 17 3.1.2 diff --git a/boot/pom.xml b/boot/pom.xml index 76373a83..305722b8 100644 --- a/boot/pom.xml +++ b/boot/pom.xml @@ -15,7 +15,7 @@ Demo project for Spring Boot - 11 + 17 3.1.2 diff --git a/data-r2dbc-entitytemplate/pom.xml b/data-r2dbc-entitytemplate/pom.xml index e45cc1ae..e3489fdc 100644 --- a/data-r2dbc-entitytemplate/pom.xml +++ b/data-r2dbc-entitytemplate/pom.xml @@ -19,7 +19,7 @@ - 11 + 17 diff --git a/data-r2dbc-repositories/pom.xml b/data-r2dbc-repositories/pom.xml index eb91bc73..2d8d20c3 100644 --- a/data-r2dbc-repositories/pom.xml +++ b/data-r2dbc-repositories/pom.xml @@ -19,7 +19,7 @@ - 11 + 17 diff --git a/database-client/pom.xml b/database-client/pom.xml index fcac607a..32da46a2 100644 --- a/database-client/pom.xml +++ b/database-client/pom.xml @@ -19,7 +19,7 @@ - 11 + 17 diff --git a/entitycallbacks/pom.xml b/entitycallbacks/pom.xml index 26860c3a..22d64dfc 100644 --- a/entitycallbacks/pom.xml +++ b/entitycallbacks/pom.xml @@ -15,7 +15,7 @@ Demo project for Spring Boot - 11 + 17 3.1.2 diff --git a/testcontainers/pom.xml b/testcontainers/pom.xml index 5897ad1f..a8404e8c 100644 --- a/testcontainers/pom.xml +++ b/testcontainers/pom.xml @@ -15,20 +15,26 @@ Demo project for Spring Boot - 11 + 17 3.1.2 3.0.0-M5 3.0.0-M5 - - - true - true + + + + org.testcontainers + testcontainers-bom + 1.16.3 + pom + import + + + - org.springframework.boot spring-boot-starter-security @@ -41,17 +47,17 @@ org.springframework.boot spring-boot-starter-data-r2dbc - io.r2dbc r2dbc-postgresql - org.projectlombok lombok true + + org.springframework.boot spring-boot-starter-test @@ -67,56 +73,29 @@ spring-security-test test + + org.testcontainers + junit-jupiter + test + + + org.testcontainers + postgresql + test + + + org.postgresql + postgresql + test + - - org.apache.maven.plugins maven-surefire-plugin ${maven-surefire-plugin.version} - - ${skip.unit.tests} - - - - test - unit-test - - test - - - - **/*IT.java - - - - - - - org.apache.maven.plugins - maven-failsafe-plugin - ${maven-failsafe-plugin.version} - - ${skip.integration.tests} - - - - integration-test - integration-test - - integration-test - verify - - - - **/*IT.java - - - - org.springframework.boot @@ -124,37 +103,6 @@ - - - it - - true - false - - - - - org.springframework.boot - spring-boot-maven-plugin - - - pre-integration-test - - start - - - - post-integration-test - - stop - - - - - - - - spring-milestones diff --git a/testcontainers/src/test/java/com/example/demo/PostRepositoryWithDockerContainerInitializerTest.java b/testcontainers/src/test/java/com/example/demo/PostRepositoryWithDockerContainerInitializerTest.java new file mode 100644 index 00000000..d6e0d4fd --- /dev/null +++ b/testcontainers/src/test/java/com/example/demo/PostRepositoryWithDockerContainerInitializerTest.java @@ -0,0 +1,92 @@ +package com.example.demo; + + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ApplicationListener; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.data.r2dbc.core.R2dbcEntityTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.utility.MountableFile; +import reactor.test.StepVerifier; + +import java.time.Duration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@DataR2dbcTest() +@Slf4j +@ContextConfiguration(initializers = PostRepositoryWithDockerContainerInitializerTest.TestContainerInitializer.class) +public class PostRepositoryWithDockerContainerInitializerTest { + + static class TestContainerInitializer implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer<>("postgres:12") + .withCopyFileToContainer(MountableFile.forClasspathResource("init.sql"), "/docker-entrypoint-initdb.d/init.sql"); + postgreSQLContainer.start(); + log.info(" container.getFirstMappedPort():: {}", postgreSQLContainer.getFirstMappedPort()); + configurableApplicationContext + .addApplicationListener((ApplicationListener) event -> postgreSQLContainer.stop()); + TestPropertyValues + .of( + "spring.r2dbc.url=" + "r2dbc:postgresql://" + + postgreSQLContainer.getHost() + ":" + postgreSQLContainer.getFirstMappedPort() + + "/" + postgreSQLContainer.getDatabaseName(), + "spring.r2dbc.username=" + postgreSQLContainer.getUsername(), + "spring.r2dbc.password=" + postgreSQLContainer.getPassword() + ) + .applyTo(configurableApplicationContext); + } + } + + @Autowired + R2dbcEntityTemplate template; + + @Autowired + PostRepository posts; + + @BeforeEach + public void setup() { + this.template.delete(Post.class).all().block(Duration.ofSeconds(5)); + } + + @Test + public void testDatabaseClientExisted() { + assertNotNull(template); + } + + @Test + public void testPostRepositoryExisted() { + assertNotNull(posts); + } + + @Test + public void testInsertAndQuery() { + var data = Post.builder().title("test title").content("content of test").build(); + this.template.insert(data) + .thenMany( + this.posts.findByTitleContains("test%") + ) + .log() + .as(StepVerifier::create) + .consumeNextWith(p -> { + log.info("saved post: {}", p); + assertThat(p.getTitle()).isEqualTo("test title"); + } + ) + .verifyComplete(); + + } + + +} diff --git a/testcontainers/src/test/java/com/example/demo/PostRepositoryWithDynamicPropertySourceTest.java b/testcontainers/src/test/java/com/example/demo/PostRepositoryWithDynamicPropertySourceTest.java new file mode 100644 index 00000000..596a4573 --- /dev/null +++ b/testcontainers/src/test/java/com/example/demo/PostRepositoryWithDynamicPropertySourceTest.java @@ -0,0 +1,79 @@ +package com.example.demo; + + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest; +import org.springframework.data.r2dbc.core.R2dbcEntityTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.MountableFile; +import reactor.test.StepVerifier; + +import java.time.Duration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@Testcontainers +@DataR2dbcTest() +@Slf4j +public class PostRepositoryWithDynamicPropertySourceTest { + + @Container + static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer<>("postgres:12") + .withCopyFileToContainer(MountableFile.forClasspathResource("init.sql"), "/docker-entrypoint-initdb.d/init.sql"); + + @DynamicPropertySource + static void registerDynamicProperties(DynamicPropertyRegistry registry) { + registry.add("spring.r2dbc.url", () -> "r2dbc:postgresql://" + + postgreSQLContainer.getHost() + ":" + postgreSQLContainer.getFirstMappedPort() + + "/" + postgreSQLContainer.getDatabaseName()); + registry.add("spring.r2dbc.username", () -> postgreSQLContainer.getUsername()); + registry.add("spring.r2dbc.password", () -> postgreSQLContainer.getPassword()); + } + + @Autowired + R2dbcEntityTemplate template; + + @Autowired + PostRepository posts; + + @BeforeEach + public void setup() { + this.template.delete(Post.class).all().block(Duration.ofSeconds(5)); + } + + @Test + public void testDatabaseClientExisted() { + assertNotNull(template); + } + + @Test + public void testPostRepositoryExisted() { + assertNotNull(posts); + } + + @Test + public void testInsertAndQuery() { + var data = Post.builder().title("test title").content("content of test").build(); + this.template.insert(data) + .thenMany( + this.posts.findByTitleContains("test%") + ) + .log() + .as(StepVerifier::create) + .consumeNextWith(p -> { + log.info("saved post: {}", p); + assertThat(p.getTitle()).isEqualTo("test title"); + } + ) + .verifyComplete(); + + } +} diff --git a/testcontainers/src/test/java/com/example/demo/PostRepositoryTest.java b/testcontainers/src/test/java/com/example/demo/PostRepositoryWithR2dbcSupportUrlTest.java similarity index 91% rename from testcontainers/src/test/java/com/example/demo/PostRepositoryTest.java rename to testcontainers/src/test/java/com/example/demo/PostRepositoryWithR2dbcSupportUrlTest.java index 83e21529..411c93c8 100644 --- a/testcontainers/src/test/java/com/example/demo/PostRepositoryTest.java +++ b/testcontainers/src/test/java/com/example/demo/PostRepositoryWithR2dbcSupportUrlTest.java @@ -3,6 +3,7 @@ import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest; @@ -21,7 +22,8 @@ @Slf4j @Import(DataConfig.class) @ActiveProfiles("tc") -public class PostRepositoryTest { +@Disabled // see: https://github.com/testcontainers/testcontainers-java/discussions/4961 +public class PostRepositoryWithR2dbcSupportUrlTest { @Autowired R2dbcEntityTemplate template; diff --git a/testcontainers/src/test/resources/init.sql b/testcontainers/src/test/resources/init.sql new file mode 100644 index 00000000..e4561d8c --- /dev/null +++ b/testcontainers/src/test/resources/init.sql @@ -0,0 +1,19 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +-- There is no 'if not exists' for type +-- But use a 'Drop' will cause exception if the type is applied in table schema. +-- DROP TYPE post_status; + +-- The value only accept single quotes. +-- CREATE TYPE post_status AS ENUM( 'DRAFT', 'PENDING_MODERATION', 'PUBLISHED'); + +-- A simple way to skip exception when creating type if it is existed. +DO $$ BEGIN + CREATE TYPE post_status AS ENUM( 'DRAFT', 'PENDING_MODERATION', 'PUBLISHED'); +EXCEPTION + WHEN duplicate_object THEN null; +END $$; + +-- Use EnumCodec to handle enum between Java and pg. +-- see: https://github.com/pgjdbc/r2dbc-postgresql#postgres-enum-types +-- CREATE CAST (varchar AS post_status) WITH INOUT AS IMPLICIT; \ No newline at end of file