Skip to content

Commit

Permalink
refactored boot-data-r2dbc-postgresql to use testContainers for @Data…
Browse files Browse the repository at this point in the history
…R2dbcTest
  • Loading branch information
hantsy committed May 28, 2020
1 parent 127aa6b commit 9e5f56f
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 35 deletions.
31 changes: 11 additions & 20 deletions boot-data-r2dbc-postgresql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
Expand All @@ -17,27 +17,16 @@

<properties>
<java.version>11</java.version>
<testcontainers.version>1.14.2</testcontainers.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-bom-r2dbc</artifactId>
<version>0.1.0.M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
Expand All @@ -61,18 +50,20 @@
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.example.demo;


import io.r2dbc.spi.ConnectionFactory;
import org.junit.Before;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.r2dbc.connectionfactory.init.CompositeDatabasePopulator;
import org.springframework.data.r2dbc.connectionfactory.init.ConnectionFactoryInitializer;
import org.springframework.data.r2dbc.connectionfactory.init.ResourceDatabasePopulator;
import org.springframework.data.r2dbc.core.DatabaseClient;
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 reactor.test.StepVerifier;

import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

// see: https://github.com/spring-projects-experimental/spring-boot-r2dbc/issues/68
@DataR2dbcTest
@Testcontainers
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
public class PostRepositoryTest {

@Container
static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();

@DynamicPropertySource
static void resgiterDynamicProperties(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());
}

@TestConfiguration
static class TestConfig {
@Bean
public ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {

ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
initializer.setConnectionFactory(connectionFactory);

CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("schema.sql")));
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("data.sql")));
initializer.setDatabasePopulator(populator);

return initializer;
}
}

@Autowired
DatabaseClient client;

@Autowired
PostRepository posts;

@Test
public void testDatabaseClientExisted() {
assertNotNull(client);
}

@Test
public void testPostRepositoryExisted() {
assertNotNull(posts);
}


@Test
public void existedOneItemInPosts() {
assertThat(this.posts.count().block()).isEqualTo(1);
}

@Test
public void testInsertAndQuery() {
this.client.insert()
.into("posts")
//.nullValue("id", Integer.class)
.value("title", "mytesttitle")
.value("content", "testcontent")
.then().block(Duration.ofSeconds(5));

this.posts.findByTitleContains("%testtitle")
.take(1)
.as(StepVerifier::create)
.consumeNextWith(p -> assertEquals("mytesttitle", p.getTitle()))
.verifyComplete();

}
}
2 changes: 2 additions & 0 deletions boot-data-r2dbc-postgresql/src/test/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DELETE FROM posts;
INSERT INTO posts (title, content) VALUES ('post one in data.sql', 'content of post one in data.sql');
9 changes: 9 additions & 0 deletions boot-data-r2dbc-postgresql/src/test/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE SEQUENCE table_name_id_seq;

CREATE TABLE IF NOT EXISTS posts(
id integer NOT NULL PRIMARY KEY DEFAULT nextval('table_name_id_seq') ,
title VARCHAR(255) NOT NULL,
content VARCHAR(255) NOT NULL
);

ALTER SEQUENCE table_name_id_seq OWNED BY posts.id;
8 changes: 1 addition & 7 deletions data-r2dbc-postgresql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,23 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<spring-data-r2dbc.version>1.0.0.RELEASE</spring-data-r2dbc.version>
<r2dbc-postgresql.version>0.8.0.RELEASE</r2dbc-postgresql.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>${spring-data-r2dbc.version}</version>
</dependency>

<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>${r2dbc-postgresql.version}</version>
</dependency>

<dependency>
Expand Down
9 changes: 1 addition & 8 deletions data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,23 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<spring.framework.version>5.2.2.RELEASE</spring.framework.version>
<spring-data-r2dbc.version>1.0.0.RELEASE</spring-data-r2dbc.version>
<r2dbc.version>0.8.1.RELEASE</r2dbc.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>${spring-data-r2dbc.version}</version>
</dependency>

<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<version>${r2dbc.version}</version>
</dependency>

<dependency>
Expand Down

0 comments on commit 9e5f56f

Please sign in to comment.