From ae42ad21774f436ebfe3814250c51b260590c68c Mon Sep 17 00:00:00 2001 From: John Thompson Date: Sat, 27 Feb 2021 14:53:20 -0500 Subject: [PATCH 1/3] updating README.md --- .../reactiveexamples/PersonRepositoryImpl.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/guru/springframework/reactiveexamples/PersonRepositoryImpl.java b/src/main/java/guru/springframework/reactiveexamples/PersonRepositoryImpl.java index 9fdbba2..0ea4ea1 100644 --- a/src/main/java/guru/springframework/reactiveexamples/PersonRepositoryImpl.java +++ b/src/main/java/guru/springframework/reactiveexamples/PersonRepositoryImpl.java @@ -8,13 +8,19 @@ * Created by jt on 2/27/21. */ public class PersonRepositoryImpl implements PersonRepository { + + Person michael = new Person(1, "Michael", "Weston"); + Person fiona = new Person(2, "Fiona", "Glenanne"); + Person sam = new Person(3, "Sam", "Axe"); + Person jesse = new Person(3, "Jesse", "Porter"); + @Override public Mono getById(Integer id) { - return null; + return Mono.just(michael); } @Override public Flux findAll() { - return null; + return Flux.just(michael, fiona, sam, jesse); } } From 113bf024ec7d523573a4b94a2f6b0c3ec156be38 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Sun, 28 Feb 2021 11:53:03 -0500 Subject: [PATCH 2/3] adding mono examples --- .../PersonRepositoryImplTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/java/guru/springframework/reactiveexamples/PersonRepositoryImplTest.java diff --git a/src/test/java/guru/springframework/reactiveexamples/PersonRepositoryImplTest.java b/src/test/java/guru/springframework/reactiveexamples/PersonRepositoryImplTest.java new file mode 100644 index 0000000..64a644f --- /dev/null +++ b/src/test/java/guru/springframework/reactiveexamples/PersonRepositoryImplTest.java @@ -0,0 +1,58 @@ +package guru.springframework.reactiveexamples; + +import guru.springframework.reactiveexamples.domain.Person; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; + +class PersonRepositoryImplTest { + + PersonRepositoryImpl personRepository; + + @BeforeEach + void setUp() { + personRepository = new PersonRepositoryImpl(); + } + + @Test + void getByIdBlock() { + Mono personMono = personRepository.getById(1); + + Person person = personMono.block(); + + System.out.println(person.toString()); + } + + @Test + void getByIdSubscribe() { + Mono personMono = personRepository.getById(1); + + personMono.subscribe(person -> { + System.out.println(person.toString()); + }); + } + + @Test + void getByIdMapFunction() { + Mono personMono = personRepository.getById(1); + + personMono.map(person -> { + System.out.println(person.toString()); + + return person.getFirstName(); + }).subscribe(firstName -> { + System.out.println("from map: " + firstName); + }); + } +} + + + + + + + + + + + From d7b0b27f48dd43f01a1043e6a7779fcf469e3ee4 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Sun, 28 Feb 2021 12:08:37 -0500 Subject: [PATCH 3/3] adding flux examples --- .../PersonRepositoryImplTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/test/java/guru/springframework/reactiveexamples/PersonRepositoryImplTest.java b/src/test/java/guru/springframework/reactiveexamples/PersonRepositoryImplTest.java index 64a644f..bd51cbe 100644 --- a/src/test/java/guru/springframework/reactiveexamples/PersonRepositoryImplTest.java +++ b/src/test/java/guru/springframework/reactiveexamples/PersonRepositoryImplTest.java @@ -3,8 +3,11 @@ import guru.springframework.reactiveexamples.domain.Person; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.util.List; + class PersonRepositoryImplTest { PersonRepositoryImpl personRepository; @@ -44,6 +47,37 @@ void getByIdMapFunction() { System.out.println("from map: " + firstName); }); } + + @Test + void fluxTestBlockFirst() { + Flux personFlux = personRepository.findAll(); + + Person person = personFlux.blockFirst(); + + System.out.println(person.toString()); + } + + @Test + void testFluxSubscribe() { + Flux personFlux = personRepository.findAll(); + + personFlux.subscribe(person -> { + System.out.println(person.toString()); + }); + } + + @Test + void testFluxToListMono() { + Flux personFlux = personRepository.findAll(); + + Mono> personListMono = personFlux.collectList(); + + personListMono.subscribe(list -> { + list.forEach(person -> { + System.out.println(person.toString()); + }); + }); + } } @@ -54,5 +88,23 @@ void getByIdMapFunction() { + + + + + + + + + + + + + + + + + +