BAEL-3018 (#7230)
* BAEL-3018 * BAEL-3018 * BAEL-3018 * BAEL-3018 * Replace Postgres by H2 * Resolve MongoDB conflict * Method name incorrect * Method name incorrect * Add batch example * Remove unnecesssary code at pom.xml * Organize the code in packages * Replaced Unit with Integration tests
This commit is contained in:
parent
fc260052b7
commit
9f5986963c
|
@ -1,9 +1,9 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>spring-5-data-reactive</artifactId>
|
<artifactId>spring-5-data-reactive</artifactId>
|
||||||
<name>spring-5-data-reactive</name>
|
<name>spring-5-data-reactive</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
@ -49,7 +49,7 @@
|
||||||
<version>${kotlin.version}</version>
|
<version>${kotlin.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.reactivex.rxjava2</groupId>
|
<groupId>io.reactivex.rxjava2</groupId>
|
||||||
<artifactId>rxjava</artifactId>
|
<artifactId>rxjava</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
@ -68,6 +68,39 @@
|
||||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-tx</artifactId>
|
||||||
|
<version>5.2.0.M2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-r2dbc</artifactId>
|
||||||
|
<version>1.0.0.M2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.r2dbc</groupId>
|
||||||
|
<artifactId>r2dbc-h2</artifactId>
|
||||||
|
<version>0.8.0.M8</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>1.4.199</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -79,7 +112,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>kotlin-maven-plugin</artifactId>
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
<version>${kotlin-maven-plugin.version}</version>
|
<version>${kotlin-maven-plugin.version}</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>compile</id>
|
<id>compile</id>
|
||||||
<goals>
|
<goals>
|
||||||
|
@ -123,7 +156,7 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<configuration>
|
<configuration>
|
||||||
|
@ -167,5 +200,21 @@
|
||||||
<kotlin-maven-plugin.version>1.2.40</kotlin-maven-plugin.version>
|
<kotlin-maven-plugin.version>1.2.40</kotlin-maven-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.r2dbc;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@ComponentScan(basePackages = "com.baeldung.r2dbc")
|
||||||
|
public class R2dbcApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(R2dbcApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.r2dbc.configuration;
|
||||||
|
|
||||||
|
import io.r2dbc.h2.H2ConnectionConfiguration;
|
||||||
|
import io.r2dbc.h2.H2ConnectionFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
|
||||||
|
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
//@EnableR2dbcRepositories(basePackages = "com.baeldung.r2dbc.repository")
|
||||||
|
public class R2DBCConfiguration extends AbstractR2dbcConfiguration {
|
||||||
|
@Bean
|
||||||
|
public H2ConnectionFactory connectionFactory() {
|
||||||
|
return new H2ConnectionFactory(
|
||||||
|
H2ConnectionConfiguration.builder()
|
||||||
|
.url("mem:testdb;DB_CLOSE_DELAY=-1;TRACE_LEVEL_FILE=4")
|
||||||
|
.username("sa")
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.baeldung.r2dbc.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Player {
|
||||||
|
@Id
|
||||||
|
Integer id;
|
||||||
|
String name;
|
||||||
|
Integer age;
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.r2dbc.repository;
|
||||||
|
|
||||||
|
import com.baeldung.r2dbc.model.Player;
|
||||||
|
import org.springframework.data.r2dbc.repository.query.Query;
|
||||||
|
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
public interface PlayerRepository extends ReactiveCrudRepository<Player, Integer> {
|
||||||
|
|
||||||
|
@Query("select id, name, age from player where name = $1")
|
||||||
|
Flux<Player> findAllByName(String name);
|
||||||
|
|
||||||
|
@Query("select * from player where age = $1")
|
||||||
|
Flux<Player> findByAge(int age);
|
||||||
|
}
|
|
@ -0,0 +1,125 @@
|
||||||
|
package com.baeldung.r2dbc;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baeldung.r2dbc.model.Player;
|
||||||
|
import com.baeldung.r2dbc.repository.PlayerRepository;
|
||||||
|
import io.r2dbc.h2.H2ConnectionFactory;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.data.r2dbc.core.DatabaseClient;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Hooks;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class R2dbcApplicationIntegrationTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
PlayerRepository playerRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
DatabaseClient client;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
H2ConnectionFactory factory;
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
|
||||||
|
Hooks.onOperatorDebug();
|
||||||
|
|
||||||
|
List<String> statements = Arrays.asList(//
|
||||||
|
"DROP TABLE IF EXISTS player;",
|
||||||
|
"CREATE table player (id INT AUTO_INCREMENT NOT NULL, name VARCHAR2, age INT NOT NULL);");
|
||||||
|
|
||||||
|
statements.forEach(it -> client.execute() //
|
||||||
|
.sql(it) //
|
||||||
|
.fetch() //
|
||||||
|
.rowsUpdated() //
|
||||||
|
.as(StepVerifier::create) //
|
||||||
|
.expectNextCount(1) //
|
||||||
|
.verifyComplete());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenDeleteAll_then0IsExpected() {
|
||||||
|
|
||||||
|
|
||||||
|
playerRepository.deleteAll()
|
||||||
|
.as(StepVerifier::create)
|
||||||
|
.expectNextCount(0)
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenInsert6_then6AreExpected() {
|
||||||
|
|
||||||
|
insertPlayers();
|
||||||
|
|
||||||
|
playerRepository.findAll()
|
||||||
|
.as(StepVerifier::create)
|
||||||
|
.expectNextCount(6)
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSearchForCR7_then1IsExpected() {
|
||||||
|
|
||||||
|
insertPlayers();
|
||||||
|
|
||||||
|
playerRepository.findAllByName("CR7")
|
||||||
|
.as(StepVerifier::create)
|
||||||
|
.expectNextCount(1)
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSearchFor32YearsOld_then2AreExpected() {
|
||||||
|
insertPlayers();
|
||||||
|
|
||||||
|
playerRepository.findByAge(32)
|
||||||
|
.as(StepVerifier::create)
|
||||||
|
.expectNextCount(2)
|
||||||
|
.verifyComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenBatchHas2Operations_then2AreExpected() {
|
||||||
|
Mono.from(factory.create())
|
||||||
|
.flatMapMany(connection -> Flux.from(connection
|
||||||
|
.createBatch()
|
||||||
|
.add("select * from player")
|
||||||
|
.add("select * from player")
|
||||||
|
.execute()))
|
||||||
|
.as(StepVerifier::create)
|
||||||
|
.expectNextCount(2)
|
||||||
|
.verifyComplete();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertPlayers() {
|
||||||
|
List<Player> players = Arrays.asList(
|
||||||
|
new Player(1, "Kaka", 37),
|
||||||
|
new Player(2, "Messi", 32),
|
||||||
|
new Player(3, "Mbappé", 20),
|
||||||
|
new Player(4, "CR7", 34),
|
||||||
|
new Player(5, "Lewandowski", 30),
|
||||||
|
new Player(6, "Cavani", 32)
|
||||||
|
);
|
||||||
|
|
||||||
|
playerRepository.saveAll(players).subscribe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue