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); } } 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..bd51cbe --- /dev/null +++ b/src/test/java/guru/springframework/reactiveexamples/PersonRepositoryImplTest.java @@ -0,0 +1,110 @@ +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.Flux; +import reactor.core.publisher.Mono; + +import java.util.List; + +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); + }); + } + + @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()); + }); + }); + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + +