Skip to content

Instantly share code, notes, and snippets.

@RaviH
Created October 28, 2015 20:38
Show Gist options
  • Save RaviH/f7d222663c495f8191ce to your computer and use it in GitHub Desktop.
Save RaviH/f7d222663c495f8191ce to your computer and use it in GitHub Desktop.
Test the concept that if first Observable doesn't contain satisfactory value then try the next one.
package com.charter.aesd.deviceactivation.middle.services
import rx.Observable
import rx.functions.Func1
import spock.lang.Specification
/**
* Created by rhasija on 10/28/15.
*/
class ObservableTest extends Specification {
def "happy path"() {
given:
def observable1 = Observable.just(Optional.of(1))
def observable2 = Observable.just(Optional.of(2))
def observable3 = Observable.just(Optional.of(3))
when:
def single = observable1.flatMap(new Func1<Optional<Integer>, Observable<? extends Optional<Integer>>>() {
@Override
Observable<? extends Optional<Integer>> call(Optional<Integer> integer) {
if (integer.get() != 1) {
return integer
} else {
return observable2
}
}
}).flatMap(new Func1<Optional<Integer>, Observable<? extends Optional<Integer>>>() {
@Override
Observable<? extends Optional<Integer>> call(Optional<Integer> integer) {
if (integer.get() != 2)
return integer
else
return observable3
}
}).toBlocking().single()
then:
single.isPresent()
single.get() == 3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment