JAVA-24463 Cleanup spring-reactive-modules (#14811)

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1
2023-09-25 06:42:56 +03:00
committed by GitHub
parent c689708383
commit a3ab8131c9
354 changed files with 377 additions and 371 deletions
@@ -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,18 @@
package com.baeldung.r2dbc.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table
public class Player {
@Id
Integer id;
String name;
Integer age;
}
@@ -0,0 +1,17 @@
package com.baeldung.r2dbc.repository;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import com.baeldung.r2dbc.model.Player;
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,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,17 @@
package com.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.r2dbc.R2dbcApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = R2dbcApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,125 @@
package com.baeldung.r2dbc;
import com.baeldung.r2dbc.configuration.R2DBCConfiguration;
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.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(classes = R2DBCConfiguration.class)
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.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(null, "Kaka", 37),
new Player(null, "Messi", 32),
new Player(null, "Mbappé", 20),
new Player(null, "CR7", 34),
new Player(null, "Lewandowski", 30),
new Player(null, "Cavani", 32)
);
playerRepository.saveAll(players).subscribe();
}
}