Skip to content

Commit

Permalink
Add StepVerifier and Errors parts + global update
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Nov 2, 2016
1 parent dd3ab2d commit 2f4b247
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 129 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# 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:

- Have [Java 8](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) and a Java IDE ([IntelliJ IDEA](https://www.jetbrains.com/idea/) for example) installed with Maven support
- 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.

Expand Down
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
<artifactId>reactor-core</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>io.projectreactor.addons</groupId>
<artifactId>reactor-adapter</artifactId>
<version>3.0.3.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.projectreactor.addons</groupId>
<artifactId>reactor-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@

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 <a href="http://projectreactor.io/core/docs/api/reactor/core/publisher/Flux.html">Flux Javadoc</a>
* @see <a href="https://github.com/reactor/reactor-addons/blob/master/reactor-test/src/main/java/reactor/test/subscriber/Verifier.java>Verifier</a>
*/
public class Part01CreateFlux {
public class Part01Flux {

//========================================================================================

@Test
public void empty() {
Flux<String> flux = emptyFlux();

Verifier.create(flux)
.expectNextCount(0)
StepVerifier.create(flux)
.expectComplete()
.verify();
}
Expand All @@ -38,7 +36,7 @@ Flux<String> emptyFlux() {
@Test
public void fromValues() {
Flux<String> flux = fooBarFluxFromValues();
Verifier.create(flux)
StepVerifier.create(flux)
.expectNext("foo", "bar")
.expectComplete()
.verify();
Expand All @@ -54,7 +52,7 @@ Flux<String> fooBarFluxFromValues() {
@Test
public void fromList() {
Flux<String> flux = fooBarFluxFromList();
Verifier.create(flux)
StepVerifier.create(flux)
.expectNext("foo", "bar")
.expectComplete()
.verify();
Expand All @@ -70,7 +68,7 @@ Flux<String> fooBarFluxFromList() {
@Test
public void error() {
Flux<String> flux = errorFlux();
Verifier.create(flux)
StepVerifier.create(flux)
.expectError(IllegalStateException.class)
.verify();
}
Expand All @@ -84,7 +82,7 @@ Flux<String> errorFlux() {
@Test
public void countEachSecond() {
Flux<Long> flux = counter();
Verifier.create(flux)
StepVerifier.create(flux)
.expectNext(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)
.expectComplete()
.verify();
Expand All @@ -95,4 +93,5 @@ Flux<Long> counter() {
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://projectreactor.io/core/docs/api/reactor/core/publisher/Mono.html">Mono Javadoc</a>
* @see <a href="https://github.com/reactor/reactor-addons/blob/master/reactor-test/src/main/java/reactor/test/subscriber/Verifier.java>Verifier</a>
*/
public class Part02CreateMono {
public class Part02Mono {

//========================================================================================

@Test
public void empty() {
Mono<String> mono = emptyMono();
Verifier.create(mono)
StepVerifier.create(mono)
.expectNextCount(0)
.expectComplete()
.verify();
Expand All @@ -34,7 +33,7 @@ Mono<String> emptyMono() {
@Test
public void fromValue() {
Mono<String> mono = fooMono();
Verifier.create(mono)
StepVerifier.create(mono)
.expectNext("foo")
.expectComplete()
.verify();
Expand All @@ -50,7 +49,7 @@ Mono<String> fooMono() {
@Test
public void error() {
Mono<String> mono = errorMono();
Verifier.create(mono)
StepVerifier.create(mono)
.expectError(IllegalStateException.class)
.verify();
}
Expand Down
80 changes: 80 additions & 0 deletions src/test/java/io/pivotal/literx/Part03StepVerifier.java
Original file line number Diff line number Diff line change
@@ -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 <a href="http://next.projectreactor.io/ext/docs/api/reactor/test/StepVerifier.html">StepVerifier Javadoc</a>
*/
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<String> 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<String> 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<String> 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<User> flux) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://projectreactor.io/core/docs/api/reactor/core/publisher/Flux.html">Flux Javadoc</a>
* @see <a href="http://projectreactor.io/core/docs/api/reactor/core/publisher/Mono.html">Mono Javadoc</a>
* @see <a href="https://github.com/reactor/reactor-addons/blob/master/reactor-test/src/main/java/reactor/test/subscriber/Verifier.java>Verifier</a>
* @author Sebastien Deleuze*
*/
public class Part03Transform {
public class Part04Transform {

ReactiveRepository<User> repository = new ReactiveUserRepository();

Expand All @@ -25,7 +22,7 @@ public class Part03Transform {
@Test
public void transformMono() {
Mono<User> mono = repository.findFirst();
Verifier.create(capitalizeOne(mono))
StepVerifier.create(capitalizeOne(mono))
.expectNext(new User("SWHITE", "SKYLER", "WHITE"))
.expectComplete()
.verify();
Expand All @@ -41,7 +38,7 @@ Mono<User> capitalizeOne(Mono<User> mono) {
@Test
public void transformFlux() {
Flux<User> flux = repository.findAll();
Verifier.create(capitalizeMany(flux))
StepVerifier.create(capitalizeMany(flux))
.expectNext(
new User("SWHITE", "SKYLER", "WHITE"),
new User("JPINKMAN", "JESSE", "PINKMAN"),
Expand All @@ -61,7 +58,7 @@ Flux<User> capitalizeMany(Flux<User> flux) {
@Test
public void asyncTransformFlux() {
Flux<User> flux = repository.findAll();
Verifier.create(asyncCapitalizeMany(flux))
StepVerifier.create(asyncCapitalizeMany(flux))
.expectNext(
new User("SWHITE", "SKYLER", "WHITE"),
new User("JPINKMAN", "JESSE", "PINKMAN"),
Expand All @@ -77,7 +74,7 @@ Flux<User> asyncCapitalizeMany(Flux<User> flux) {
}

Mono<User> asyncCapitalizeUser(User u) {
return null;
return Mono.just(new User(u.getUsername().toUpperCase(), u.getFirstname().toUpperCase(), u.getLastname().toUpperCase()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://projectreactor.io/core/docs/api/reactor/core/publisher/Flux.html">Flux Javadoc</a>
* @see <a href="http://projectreactor.io/core/docs/api/reactor/core/publisher/Mono.html">Mono Javadoc</a>
* @see <a href="https://github.com/reactor/reactor-addons/blob/master/reactor-test/src/main/java/reactor/test/subscriber/Verifier.java>Verifier</a>
*/
public class Part04Merge {
public class Part05Merge {

final static User MARIE = new User("mschrader", "Marie", "Schrader");
final static User MIKE = new User("mehrmantraut", "Mike", "Ehrmantraut");
Expand All @@ -29,7 +26,7 @@ public class Part04Merge {
@Test
public void mergeWithInterleave() {
Flux<User> 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();
Expand All @@ -45,7 +42,7 @@ Flux<User> mergeFluxWithInterleave(Flux<User> flux1, Flux<User> flux2) {
@Test
public void mergeWithNoInterleave() {
Flux<User> 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();
Expand All @@ -63,7 +60,7 @@ public void multipleMonoToFlux() {
Mono<User> skylerMono = repository1.findFirst();
Mono<User> marieMono = repository2.findFirst();
Flux<User> flux = createFluxFromMultipleMono(skylerMono, marieMono);
Verifier.create(flux)
StepVerifier.create(flux)
.expectNext(User.SKYLER, MARIE)
.expectComplete()
.verify();
Expand Down
Loading

0 comments on commit 2f4b247

Please sign in to comment.