Add 'data-mongodb-reactive' sample

This commit is contained in:
Moritz Halbritter
2022-07-27 15:46:26 +02:00
parent a8cc98b35a
commit 97e5d80872
12 changed files with 184 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ smoke_tests:
- data-jdbc
- data-jpa
- data-mongodb
- data-mongodb-reactive
- data-r2dbc
- data-redis
- event-listener

View File

@@ -0,0 +1 @@
Tests if Spring Data MongoDB Reactive works

View File

@@ -0,0 +1,16 @@
plugins {
id 'java'
id 'org.springframework.boot'
id 'org.springframework.aot.smoke-test'
id 'org.graalvm.buildtools.native'
}
dependencies {
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
testImplementation("org.springframework.boot:spring-boot-starter-test")
aotTestImplementation(project(":aot-smoke-test-support"))
aotTestImplementation("org.awaitility:awaitility:4.2.0")
}

View File

@@ -0,0 +1,6 @@
version: '3.1'
services:
mongo:
image: 'mongo:5'
ports:
- 27017

View File

@@ -0,0 +1,33 @@
package com.example.data.mongodb.reactive;
import java.time.Duration;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
import org.springframework.aot.smoketest.support.junit.AotSmokeTest;
import static org.assertj.core.api.Assertions.assertThat;
@AotSmokeTest
class DataMongoDbReactiveApplicationAotTests {
@Test
void findAll(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasSingleLineContaining("findAll(): Person{firstname='first-1', lastname='last-1'}")
.hasSingleLineContaining("findAll(): Person{firstname='first-2', lastname='last-2'}")
.hasSingleLineContaining("findAll(): Person{firstname='first-3', lastname='last-3'}");
});
}
@Test
void findByLastName(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output)
.hasSingleLineContaining("findByLastname(): Person{firstname='first-3', lastname='last-3'}");
});
}
}

View File

@@ -0,0 +1,30 @@
package com.example.data.mongodb.reactive;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
class CLR implements CommandLineRunner {
private final PersonRepository personRepository;
CLR(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@Override
public void run(String... args) {
this.personRepository.save(new Person("first-1", "last-1")).block();
this.personRepository.save(new Person("first-2", "last-2")).block();
this.personRepository.save(new Person("first-3", "last-3")).block();
this.personRepository.findAll().subscribe((person) -> {
System.out.printf("findAll(): %s%n", person);
}, ex -> ex.printStackTrace(System.out));
this.personRepository.findByLastname("last-3").subscribe((person) -> {
System.out.printf("findByLastname(): %s%n", person);
}, ex -> ex.printStackTrace(System.out));
}
}

View File

@@ -0,0 +1,14 @@
package com.example.data.mongodb.reactive;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DataMongoDbReactiveApplication {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(DataMongoDbReactiveApplication.class, args);
Thread.currentThread().join(); // To be able to measure memory consumption
}
}

View File

@@ -0,0 +1,55 @@
package com.example.data.mongodb.reactive;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Person {
@Id
private String id;
private String firstname;
@Indexed
private String lastname;
public Person() {
}
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Override
public String toString() {
return "Person{" + "firstname='" + firstname + '\'' + ", lastname='" + lastname + '\'' + '}';
}
}

View File

@@ -0,0 +1,11 @@
package com.example.data.mongodb.reactive;
import reactor.core.publisher.Flux;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
public interface PersonRepository extends ReactiveCrudRepository<Person, String> {
Flux<Person> findByLastname(String lastname);
}

View File

@@ -0,0 +1,2 @@
spring.data.mongodb.host=${MONGO_HOST:localhost}
spring.data.mongodb.port=${MONGO_PORT_27017:27017}

View File

@@ -0,0 +1,14 @@
package com.example.data.mongodb.reactive;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DataMongoDbReactiveApplicationTests {
@Test
void contextLoads() {
}
}

View File

@@ -41,6 +41,7 @@ include "configuration-properties"
include "data-jdbc"
include "data-jpa"
include "data-mongodb"
include "data-mongodb-reactive"
include "data-r2dbc"
include "data-redis"
include "event-listener"