Skip to content

Commit

Permalink
Merge pull request eugenp#10701 from singhkaushal01/master
Browse files Browse the repository at this point in the history
BAEL-4885: How to convert Mono<List<T>> into Flux<T>
  • Loading branch information
glmartin authored May 11, 2021
2 parents f61cc6a + fe6440a commit e4078a9
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions reactor-core/src/test/java/com/baeldung/mono/MonoUnitTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,4 +44,40 @@ private Mono<String> blockingHelloWorld() {
// 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);
}
}

0 comments on commit e4078a9

Please sign in to comment.