I get from a reactive repository a Mono<FooBar>
based on it's value I have to create two other objects save them using reactive repositories, modify FooBar
object and save it as well.
As I'm new to reactive programming I landed with the following solution, which is working, but I'm not sure if I'm using correctly the reactive API:
@Test
void createAndSave() {
Mono<FooBar> fooBarMono = findFooBar() // returns Mono<FooBar>
.map(fooBar -> {
createAndSaveLoremBar(fooBar).subscribe(); // returns Mono<LoremBar>
createAndSaveDoloremBar(fooBar).subscribe(); // returns Mono<DoloremBar>
fooBar.setActive(true);
return saveFooBar(fooBar); // returns Mono<FooBar>
}).flatMap(Function.identity());
StepVerifier.create(fooBarMono)
.expectNextMatches(Objects::nonNull)
.expectComplete()
.verify();
}
from console log:
saved lorem bar
saved dolorem bar
saved foo bar