Merge pull request #10701 from singhkaushal01/master
BAEL-4885: How to convert Mono<List<T>> into Flux<T>
This commit is contained in:
commit
5a4c4c6331
|
@ -1,10 +1,14 @@
|
|||
package com.baeldung.mono;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -40,4 +44,40 @@ public class MonoUnitTest {
|
|||
// blocking
|
||||
return Mono.just("Hello world!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMonoProducesListOfElements_thenConvertToFluxofElements() {
|
||||
|
||||
Mono<List<String>> monoList = monoOfList();
|
||||
|
||||
StepVerifier.create(monoTofluxUsingFlatMapIterable(monoList))
|
||||
.expectNext("one", "two", "three", "four")
|
||||
.verifyComplete();
|
||||
|
||||
StepVerifier.create(monoTofluxUsingFlatMapMany(monoList))
|
||||
.expectNext("one", "two", "three", "four")
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
private <T> Flux<T> monoTofluxUsingFlatMapIterable(Mono<List<T>> monoList) {
|
||||
return monoList
|
||||
.flatMapIterable(list -> list)
|
||||
.log();
|
||||
}
|
||||
|
||||
private <T> Flux<T> monoTofluxUsingFlatMapMany(Mono<List<T>> monoList) {
|
||||
return monoList
|
||||
.flatMapMany(Flux::fromIterable)
|
||||
.log();
|
||||
}
|
||||
|
||||
private Mono<List<String>> monoOfList() {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("one");
|
||||
list.add("two");
|
||||
list.add("three");
|
||||
list.add("four");
|
||||
|
||||
return Mono.just(list);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue