From 2f4b247b21f9e35e20ba59c06c1aa812a9bf8a79 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 2 Nov 2016 22:10:36 +0100 Subject: [PATCH] Add StepVerifier and Errors parts + global update --- README.md | 6 +- pom.xml | 5 - ...{Part01CreateFlux.java => Part01Flux.java} | 17 ++- ...{Part02CreateMono.java => Part02Mono.java} | 11 +- .../io/pivotal/literx/Part03StepVerifier.java | 80 +++++++++++++ ...t03Transform.java => Part04Transform.java} | 17 ++- .../{Part04Merge.java => Part05Merge.java} | 13 +-- ...{Part05Request.java => Part06Request.java} | 20 ++-- .../java/io/pivotal/literx/Part07Errors.java | 109 ++++++++++++++++++ ...ations.java => Part08OtherOperations.java} | 74 ++++-------- ...8Conversion.java => Part09Conversion.java} | 35 +++--- ...ing.java => Part10ReactiveToBlocking.java} | 5 +- ...ive.java => Part11BlockingToReactive.java} | 9 +- 13 files changed, 272 insertions(+), 129 deletions(-) rename src/test/java/io/pivotal/literx/{Part01CreateFlux.java => Part01Flux.java} (85%) rename src/test/java/io/pivotal/literx/{Part02CreateMono.java => Part02Mono.java} (80%) create mode 100644 src/test/java/io/pivotal/literx/Part03StepVerifier.java rename src/test/java/io/pivotal/literx/{Part03Transform.java => Part04Transform.java} (76%) rename src/test/java/io/pivotal/literx/{Part04Merge.java => Part05Merge.java} (81%) rename src/test/java/io/pivotal/literx/{Part05Request.java => Part06Request.java} (82%) create mode 100644 src/test/java/io/pivotal/literx/Part07Errors.java rename src/test/java/io/pivotal/literx/{Part06OtherOperations.java => Part08OtherOperations.java} (70%) rename src/test/java/io/pivotal/literx/{Part08Conversion.java => Part09Conversion.java} (73%) rename src/test/java/io/pivotal/literx/{Part07ReactiveToBlocking.java => Part10ReactiveToBlocking.java} (78%) rename src/test/java/io/pivotal/literx/{Part09BlockingToReactive.java => Part11BlockingToReactive.java} (90%) diff --git a/README.md b/README.md index 6d60635c..5787564d 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Lite Rx API Hands-on -This Hands-on (slides available [here](https://speakerdeck.com/sdeleuze/a-lite-rx-api-for-the-jvm)) is designed to help you to learn easily the lite Rx API provided by [Reactor Core 3.x](https://github.com/reactor/reactor-core/). +This Hands-on is designed to help you to learn easily the lite Rx API provided by [Reactor Core 3.x](https://github.com/reactor/reactor-core/). You will mostly need these 3 classes Javadoc: - [Flux](http://projectreactor.io/core/docs/api/reactor/core/publisher/Flux.html) - [Mono](http://projectreactor.io/core/docs/api/reactor/core/publisher/Mono.html) - - [Verifier](http://next.projectreactor.io/ext/docs/api/reactor/test/subscriber/Verifier.html) + - [StepVerifier](http://next.projectreactor.io/ext/docs/api/reactor/test/StepVerifier.html) To do this Hands-on, you just have to: @@ -14,7 +14,7 @@ To do this Hands-on, you just have to: - Clone this repository (or your fork) - Import the project as a Maven one in your IDE - Make sure that the language level is set to Java 8 in your IDE project settings - - Fix the TODO one by one in Part01 to Part09 test classes to make unit tests green + - Fix the TODO one by one in Part01 to Part11 test classes to make unit tests green The solution is available in the `complete` branch to compare, when you have finished, with what you have done. diff --git a/pom.xml b/pom.xml index 15041f87..d1897f12 100644 --- a/pom.xml +++ b/pom.xml @@ -22,11 +22,6 @@ reactor-core 3.0.3.RELEASE - - io.projectreactor.addons - reactor-adapter - 3.0.3.BUILD-SNAPSHOT - io.projectreactor.addons reactor-test diff --git a/src/test/java/io/pivotal/literx/Part01CreateFlux.java b/src/test/java/io/pivotal/literx/Part01Flux.java similarity index 85% rename from src/test/java/io/pivotal/literx/Part01CreateFlux.java rename to src/test/java/io/pivotal/literx/Part01Flux.java index f416c6e0..6e2b74c9 100644 --- a/src/test/java/io/pivotal/literx/Part01CreateFlux.java +++ b/src/test/java/io/pivotal/literx/Part01Flux.java @@ -5,16 +5,15 @@ import org.junit.Test; import reactor.core.publisher.Flux; -import reactor.test.subscriber.Verifier; +import reactor.test.StepVerifier; /** * Learn how to create Flux instances. * * @author Sebastien Deleuze * @see Flux Javadoc - * @see fooBarFluxFromValues() { @Test public void fromList() { Flux flux = fooBarFluxFromList(); - Verifier.create(flux) + StepVerifier.create(flux) .expectNext("foo", "bar") .expectComplete() .verify(); @@ -70,7 +68,7 @@ Flux fooBarFluxFromList() { @Test public void error() { Flux flux = errorFlux(); - Verifier.create(flux) + StepVerifier.create(flux) .expectError(IllegalStateException.class) .verify(); } @@ -84,7 +82,7 @@ Flux errorFlux() { @Test public void countEachSecond() { Flux flux = counter(); - Verifier.create(flux) + StepVerifier.create(flux) .expectNext(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L) .expectComplete() .verify(); @@ -95,4 +93,5 @@ Flux counter() { return null; } + } diff --git a/src/test/java/io/pivotal/literx/Part02CreateMono.java b/src/test/java/io/pivotal/literx/Part02Mono.java similarity index 80% rename from src/test/java/io/pivotal/literx/Part02CreateMono.java rename to src/test/java/io/pivotal/literx/Part02Mono.java index 35edf7f1..8f8cfe65 100644 --- a/src/test/java/io/pivotal/literx/Part02CreateMono.java +++ b/src/test/java/io/pivotal/literx/Part02Mono.java @@ -2,23 +2,22 @@ import org.junit.Test; import reactor.core.publisher.Mono; -import reactor.test.subscriber.Verifier; +import reactor.test.StepVerifier; /** * Learn how to create Mono instances. * * @author Sebastien Deleuze * @see Mono Javadoc - * @see fooMono() { @Test public void error() { Mono mono = errorMono(); - Verifier.create(mono) + StepVerifier.create(mono) .expectError(IllegalStateException.class) .verify(); } diff --git a/src/test/java/io/pivotal/literx/Part03StepVerifier.java b/src/test/java/io/pivotal/literx/Part03StepVerifier.java new file mode 100644 index 00000000..e4336f41 --- /dev/null +++ b/src/test/java/io/pivotal/literx/Part03StepVerifier.java @@ -0,0 +1,80 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.pivotal.literx; + +import io.pivotal.literx.domain.User; +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import reactor.core.Exceptions; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +/** + * Learn how to use StepVerifier to test Mono, Flux or any other kind of Reactive Streams Publisher. + * + * @author Sebastien Deleuze + * @see StepVerifier Javadoc + */ +public class Part03StepVerifier { + +//======================================================================================== + + @Test + public void expect2ElementsThenComplete() { + expect2Elements(Flux.just("foo", "bar")); + } + + // TODO Use StepVerifier to check that the flux parameter emits 2 elements and then completes successfully. + void expect2Elements(Flux flux) { + } + + +//======================================================================================== + + @Test + public void expectElementsThenComplete() { + expectFooBarComplete(Flux.just("foo", "bar")); + } + + // TODO Use StepVerifier to check that the flux parameter emits "foo" and "bar" elements then completes successfully. + void expectFooBarComplete(Flux flux) { + } + +//======================================================================================== + + @Test + public void expect2ElementsThenError() { + expectFooBarError(Flux.just("foo", "bar").concatWith(Mono.error(new RuntimeException()))); + } + + // TODO Use StepVerifier to check that the flux parameter emits "foo" and "bar" elements then a RuntimeException error. + void expectFooBarError(Flux flux) { + } + +//======================================================================================== + + @Test + public void expectElementsWithThenComplete() { + expectSkylerJesseComplete(Flux.just(new User("swhite", null, null), new User("jpinkman", null, null))); + } + + // TODO Use StepVerifier to check that the flux parameter emits a User with "swhite" username and another one with "jpinkman" then completes successfully. + void expectSkylerJesseComplete(Flux flux) { + } + +} diff --git a/src/test/java/io/pivotal/literx/Part03Transform.java b/src/test/java/io/pivotal/literx/Part04Transform.java similarity index 76% rename from src/test/java/io/pivotal/literx/Part03Transform.java rename to src/test/java/io/pivotal/literx/Part04Transform.java index 415781d0..873238c9 100644 --- a/src/test/java/io/pivotal/literx/Part03Transform.java +++ b/src/test/java/io/pivotal/literx/Part04Transform.java @@ -6,17 +6,14 @@ import org.junit.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.test.subscriber.Verifier; +import reactor.test.StepVerifier; /** * Learn how to transform values. * - * @author Sebastien Deleuze - * @see Flux Javadoc - * @see Mono Javadoc - * @see capitalizeOne(Mono mono) { @Test public void transformFlux() { Flux flux = repository.findAll(); - Verifier.create(capitalizeMany(flux)) + StepVerifier.create(capitalizeMany(flux)) .expectNext( new User("SWHITE", "SKYLER", "WHITE"), new User("JPINKMAN", "JESSE", "PINKMAN"), @@ -61,7 +58,7 @@ Flux capitalizeMany(Flux flux) { @Test public void asyncTransformFlux() { Flux flux = repository.findAll(); - Verifier.create(asyncCapitalizeMany(flux)) + StepVerifier.create(asyncCapitalizeMany(flux)) .expectNext( new User("SWHITE", "SKYLER", "WHITE"), new User("JPINKMAN", "JESSE", "PINKMAN"), @@ -77,7 +74,7 @@ Flux asyncCapitalizeMany(Flux flux) { } Mono asyncCapitalizeUser(User u) { - return null; + return Mono.just(new User(u.getUsername().toUpperCase(), u.getFirstname().toUpperCase(), u.getLastname().toUpperCase())); } } diff --git a/src/test/java/io/pivotal/literx/Part04Merge.java b/src/test/java/io/pivotal/literx/Part05Merge.java similarity index 81% rename from src/test/java/io/pivotal/literx/Part04Merge.java rename to src/test/java/io/pivotal/literx/Part05Merge.java index 8a937b9f..b340da9b 100644 --- a/src/test/java/io/pivotal/literx/Part04Merge.java +++ b/src/test/java/io/pivotal/literx/Part05Merge.java @@ -6,17 +6,14 @@ import org.junit.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.test.subscriber.Verifier; +import reactor.test.StepVerifier; /** * Learn how to merge flux. * * @author Sebastien Deleuze - * @see Flux Javadoc - * @see Mono Javadoc - * @see flux = mergeFluxWithInterleave(repository1.findAll(), repository2.findAll()); - Verifier.create(flux) + StepVerifier.create(flux) .expectNext(MARIE, MIKE, User.SKYLER, User.JESSE, User.WALTER, User.SAUL) .expectComplete() .verify(); @@ -45,7 +42,7 @@ Flux mergeFluxWithInterleave(Flux flux1, Flux flux2) { @Test public void mergeWithNoInterleave() { Flux flux = mergeFluxWithNoInterleave(repository1.findAll(), repository2.findAll()); - Verifier.create(flux) + StepVerifier.create(flux) .expectNext(User.SKYLER, User.JESSE, User.WALTER, User.SAUL, MARIE, MIKE) .expectComplete() .verify(); @@ -63,7 +60,7 @@ public void multipleMonoToFlux() { Mono skylerMono = repository1.findFirst(); Mono marieMono = repository2.findFirst(); Flux flux = createFluxFromMultipleMono(skylerMono, marieMono); - Verifier.create(flux) + StepVerifier.create(flux) .expectNext(User.SKYLER, MARIE) .expectComplete() .verify(); diff --git a/src/test/java/io/pivotal/literx/Part05Request.java b/src/test/java/io/pivotal/literx/Part06Request.java similarity index 82% rename from src/test/java/io/pivotal/literx/Part05Request.java rename to src/test/java/io/pivotal/literx/Part06Request.java index 6ae57eea..980b6fbd 100644 --- a/src/test/java/io/pivotal/literx/Part05Request.java +++ b/src/test/java/io/pivotal/literx/Part06Request.java @@ -5,14 +5,14 @@ import io.pivotal.literx.repository.ReactiveUserRepository; import org.junit.Test; import reactor.core.publisher.Flux; -import reactor.test.subscriber.Verifier; +import reactor.test.StepVerifier; /** * Learn how to control the demand. * * @author Sebastien Deleuze */ -public class Part05Request { +public class Part06Request { ReactiveRepository repository = new ReactiveUserRepository(); @@ -21,12 +21,12 @@ public class Part05Request { @Test public void requestAll() { Flux flux = repository.findAll(); - Verifier subscriber = requestAllExpectFour(flux); - subscriber.verify(); + StepVerifier verifier = requestAllExpectFour(flux); + verifier.verify(); } // TODO Create a Verifier that requests initially all values and expect a 4 values to be received - Verifier requestAllExpectFour(Flux flux) { + StepVerifier requestAllExpectFour(Flux flux) { return null; } @@ -35,12 +35,12 @@ Verifier requestAllExpectFour(Flux flux) { @Test public void requestOneByOne() { Flux flux = repository.findAll(); - Verifier subscriber = requestOneExpectSkylerThenRequestOneExpectJesse(flux); - subscriber.verify(); + StepVerifier verifier = requestOneExpectSkylerThenRequestOneExpectJesse(flux); + verifier.verify(); } // TODO Create a Verifier that requests initially 1 value and expects {@link User.SKYLER} then requests another value and expects {@link User.JESSE}. - Verifier requestOneExpectSkylerThenRequestOneExpectJesse(Flux flux) { + StepVerifier requestOneExpectSkylerThenRequestOneExpectJesse(Flux flux) { return null; } @@ -49,7 +49,7 @@ Verifier requestOneExpectSkylerThenRequestOneExpectJesse(Flux flux) { @Test public void experimentWithLog() { Flux flux = fluxWithLog(); - Verifier.create(flux, 0) + StepVerifier.create(flux, 0) .thenRequest(1) .expectNextWith(u -> true) .thenRequest(1) @@ -72,7 +72,7 @@ Flux fluxWithLog() { @Test public void experimentWithDoOn() { Flux flux = fluxWithDoOnPrintln(); - Verifier.create(flux) + StepVerifier.create(flux) .expectNextCount(4) .expectComplete() .verify(); diff --git a/src/test/java/io/pivotal/literx/Part07Errors.java b/src/test/java/io/pivotal/literx/Part07Errors.java new file mode 100644 index 00000000..dbe3b226 --- /dev/null +++ b/src/test/java/io/pivotal/literx/Part07Errors.java @@ -0,0 +1,109 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.pivotal.literx; + +import java.time.Duration; +import java.util.function.Function; + +import io.pivotal.literx.domain.User; +import org.junit.Test; +import reactor.core.Exceptions; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Hooks; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +/** + * Learn how to deal with errors. + * + * @author Sebastien Deleuze + * @see Exceptions#propagate(Throwable) + * @see Hooks#onOperator(Function) + */ +public class Part07Errors { + +//======================================================================================== + + @Test + public void monoWithValueInsteadOfError() { + Mono mono = betterCallSaulForBogusMono(Mono.error(new IllegalStateException())); + StepVerifier.create(mono) + .expectNext(User.SAUL) + .expectComplete() + .verify(); + + mono = betterCallSaulForBogusMono(Mono.just(User.SKYLER)); + StepVerifier.create(mono) + .expectNext(User.SKYLER) + .expectComplete() + .verify(); + } + + // TODO Return a Mono containing Saul when an error occurs in the input Mono, else do not change the input Mono. + Mono betterCallSaulForBogusMono(Mono mono) { + return null; + } + +//======================================================================================== + + @Test + public void fluxWithValueInsteadOfError() { + Flux flux = betterCallSaulAndJesseForBogusFlux(Flux.error(new IllegalStateException())); + StepVerifier.create(flux) + .expectNext(User.SAUL, User.JESSE) + .expectComplete() + .verify(); + + flux = betterCallSaulAndJesseForBogusFlux(Flux.just(User.SKYLER, User.WALTER)); + StepVerifier.create(flux) + .expectNext(User.SKYLER, User.WALTER) + .expectComplete() + .verify(); + } + + // TODO Return a Flux containing Saul and Jesse when an error occurs in the input Flux, else do not change the input Flux. + Flux betterCallSaulAndJesseForBogusFlux(Flux flux) { + return null; + } + +//======================================================================================== + + @Test + public void handleCheckedExceptions() { + Flux flux = capitalizeMany(Flux.just(User.SAUL, User.JESSE)); + + StepVerifier.create(flux) + .expectError(GetOutOfHereException.class) + .verify(); + } + + // TODO Implement a method that capitalize each user of the incoming flux using the capitalizeUser() method and emit an error containing a GetOutOfHereException exception + Flux capitalizeMany(Flux users) { + return null; + } + + User capitalizeUser(User user) throws GetOutOfHereException { + if (user.equals(User.SAUL)) { + throw new GetOutOfHereException(); + } + return new User(user.getUsername(), user.getFirstname(), user.getLastname()); + } + + private class GetOutOfHereException extends Exception { + } + +} diff --git a/src/test/java/io/pivotal/literx/Part06OtherOperations.java b/src/test/java/io/pivotal/literx/Part08OtherOperations.java similarity index 70% rename from src/test/java/io/pivotal/literx/Part06OtherOperations.java rename to src/test/java/io/pivotal/literx/Part08OtherOperations.java index bce3af0a..4396e944 100644 --- a/src/test/java/io/pivotal/literx/Part06OtherOperations.java +++ b/src/test/java/io/pivotal/literx/Part08OtherOperations.java @@ -6,17 +6,14 @@ import org.junit.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.test.subscriber.Verifier; +import reactor.test.StepVerifier; /** * Learn how to use various other operators. * * @author Sebastien Deleuze - * @see Flux Javadoc - * @see Mono Javadoc - * @see firstnameFlux = Flux.just(User.SKYLER.getFirstname(), User.JESSE.getFirstname(), User.WALTER.getFirstname(), User.SAUL.getFirstname()); Flux lastnameFlux = Flux.just(User.SKYLER.getLastname(), User.JESSE.getLastname(), User.WALTER.getLastname(), User.SAUL.getLastname()); Flux userFlux = userFluxFromStringFlux(usernameFlux, firstnameFlux, lastnameFlux); - Verifier.create(userFlux) + StepVerifier.create(userFlux) .expectNext(User.SKYLER, User.JESSE, User.WALTER, User.SAUL) .expectComplete() .verify(); @@ -47,7 +44,7 @@ public void fastestMono() { ReactiveRepository repository1 = new ReactiveUserRepository(MARIE); ReactiveRepository repository2 = new ReactiveUserRepository(250, MIKE); Mono mono = useFastestMono(repository1.findFirst(), repository2.findFirst()); - Verifier.create(mono) + StepVerifier.create(mono) .expectNext(MARIE) .expectComplete() .verify(); @@ -55,7 +52,7 @@ public void fastestMono() { repository1 = new ReactiveUserRepository(250, MARIE); repository2 = new ReactiveUserRepository(MIKE); mono = useFastestMono(repository1.findFirst(), repository2.findFirst()); - Verifier.create(mono) + StepVerifier.create(mono) .expectNext(MIKE) .expectComplete() .verify(); @@ -73,7 +70,7 @@ public void fastestFlux() { ReactiveRepository repository1 = new ReactiveUserRepository(MARIE, MIKE); ReactiveRepository repository2 = new ReactiveUserRepository(250); Flux flux = useFastestFlux(repository1.findAll(), repository2.findAll()); - Verifier.create(flux) + StepVerifier.create(flux) .expectNext(MARIE, MIKE) .expectComplete() .verify(); @@ -81,7 +78,7 @@ public void fastestFlux() { repository1 = new ReactiveUserRepository(250, MARIE, MIKE); repository2 = new ReactiveUserRepository(); flux = useFastestFlux(repository1.findAll(), repository2.findAll()); - Verifier.create(flux) + StepVerifier.create(flux) .expectNext(User.SKYLER, User.JESSE, User.WALTER, User.SAUL) .expectComplete() .verify(); @@ -98,7 +95,7 @@ Flux useFastestFlux(Flux flux1, Flux flux2) { public void complete() { ReactiveRepository repository = new ReactiveUserRepository(); Mono completion = fluxCompletion(repository.findAll()); - Verifier.create(completion) + StepVerifier.create(completion) .expectComplete() .verify(); } @@ -111,64 +108,41 @@ Mono fluxCompletion(Flux flux) { //======================================================================================== @Test - public void monoWithValueInsteadOfError() { - Mono mono = betterCallSaulForBogusMono(Mono.error(new IllegalStateException())); - Verifier.create(mono) - .expectNext(User.SAUL) + public void nullHandling() { + Mono mono = nullAwareUserToMono(User.SKYLER); + StepVerifier.create(mono) + .expectNext(User.SKYLER) .expectComplete() .verify(); - - mono = betterCallSaulForBogusMono(Mono.just(User.SKYLER)); - Verifier.create(mono) - .expectNext(User.SKYLER) + mono = nullAwareUserToMono(null); + StepVerifier.create(mono) .expectComplete() .verify(); } - // TODO Return a Mono containing Saul when an error occurs in the input Mono, else do not change the input Mono. - Mono betterCallSaulForBogusMono(Mono mono) { + // TODO Return a valid Mono of user for null input and non null input user (hint: Reactive Streams does not accept null values) + Mono nullAwareUserToMono(User user) { return null; } //======================================================================================== @Test - public void fluxWithValueInsteadOfError() { - Flux flux = betterCallSaulAndJesseForBogusFlux(Flux.error(new IllegalStateException())); - Verifier.create(flux) - .expectNext(User.SAUL, User.JESSE) - .expectComplete() - .verify(); - - flux = betterCallSaulAndJesseForBogusFlux(Flux.just(User.SKYLER, User.WALTER)); - Verifier.create(flux) - .expectNext(User.SKYLER, User.WALTER) + public void emptyHandling() { + Mono mono = emptyToSkyler(Mono.just(User.WALTER)); + StepVerifier.create(mono) + .expectNext(User.WALTER) .expectComplete() .verify(); - } - - // TODO Return a Flux containing Saul and Jesse when an error occurs in the input Flux, else do not change the input Flux. - Flux betterCallSaulAndJesseForBogusFlux(Flux flux) { - return null; - } - - //======================================================================================== - - @Test - public void nullHandling() { - Mono mono = nullAwareUserToMono(User.SKYLER); - Verifier.create(mono) + mono = emptyToSkyler(Mono.empty()); + StepVerifier.create(mono) .expectNext(User.SKYLER) .expectComplete() .verify(); - mono = nullAwareUserToMono(null); - Verifier.create(mono) - .expectComplete() - .verify(); } - // TODO Return a valid Mono of user for null input and non null input user (hint: Reactive Streams does not accept null values) - Mono nullAwareUserToMono(User user) { + // TODO Return the same mono passed as input parameter, expect that it will emit User.SKYLER when empty + Mono emptyToSkyler(Mono mono) { return null; } diff --git a/src/test/java/io/pivotal/literx/Part08Conversion.java b/src/test/java/io/pivotal/literx/Part09Conversion.java similarity index 73% rename from src/test/java/io/pivotal/literx/Part08Conversion.java rename to src/test/java/io/pivotal/literx/Part09Conversion.java index 5d39a023..93f4f141 100644 --- a/src/test/java/io/pivotal/literx/Part08Conversion.java +++ b/src/test/java/io/pivotal/literx/Part09Conversion.java @@ -28,10 +28,9 @@ import io.reactivex.Single; import org.junit.Test; import org.reactivestreams.Publisher; -import reactor.adapter.rxjava.RxJava2Adapter; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import reactor.test.subscriber.Verifier; +import reactor.test.StepVerifier; /** * Learn how to adapt from/to RxJava 2 Observable/Single/Flowable and transform from/to @@ -41,13 +40,11 @@ * Reactive Streams compliant + there are {@link Mono#from(Publisher)} and {@link Flux#from(Publisher)} * factory methods. * + * For RxJava 2, you should not use Reactor Adapter but only RxJava 2 and Reactor Core. + * * @author Sebastien Deleuze - * @see RxJava2Adapter - * @see Flux Javadoc - * @see Mono Javadoc - * @see Flux Javadoc - * @see Mono Javadoc - * @see Verifier */ -public class Part09BlockingToReactive { +public class Part11BlockingToReactive { //======================================================================================== @@ -39,7 +38,7 @@ public void slowPublisherFastSubscriber() { BlockingUserRepository repository = new BlockingUserRepository(); Flux flux = blockingRepositoryToFlux(repository); assertEquals(0, repository.getCallCount()); - Verifier.create(flux) + StepVerifier.create(flux) .expectNext(User.SKYLER, User.JESSE, User.WALTER, User.SAUL) .expectComplete() .verify(); @@ -58,7 +57,7 @@ public void fastPublisherSlowSubscriber() { BlockingUserRepository blockingRepository = new BlockingUserRepository(new User[]{}); Mono complete = fluxToBlockingRepository(reactiveRepository.findAll(), blockingRepository); assertEquals(0, blockingRepository.getCallCount()); - Verifier.create(complete) + StepVerifier.create(complete) .expectComplete() .verify(); Iterator it = blockingRepository.findAll().iterator();