@Test public void MonoProcessorRejectedSubscribeCallback() { MonoProcessor<String> mp = MonoProcessor.create(); AtomicReference<Throwable> ref = new AtomicReference<>(); mp.subscribe(v -> {}, ref::set); mp.onError(new Exception("test")); assertThat(ref.get()).hasMessage("test"); assertThat(mp.isSuccess()).isFalse(); assertThat(mp.isError()).isTrue(); }
@Test public void MonoProcessorRejectedDoOnSuccessOrError() { MonoProcessor<String> mp = MonoProcessor.create(); AtomicReference<Throwable> ref = new AtomicReference<>(); mp.doOnSuccessOrError((s, f) -> ref.set(f)).subscribe(); mp.onError(new Exception("test")); assertThat(ref.get()).hasMessage("test"); assertThat(mp.isSuccess()).isFalse(); assertThat(mp.isError()).isTrue(); }
@Test public void MonoProcessorRejectedDoOnTerminate() { MonoProcessor<String> mp = MonoProcessor.create(); AtomicInteger invoked = new AtomicInteger(); mp.doOnTerminate(invoked::incrementAndGet).subscribe(); mp.onError(new Exception("test")); assertThat(invoked.get()).isEqualTo(1); assertThat(mp.isSuccess()).isFalse(); assertThat(mp.isError()).isTrue(); }
@Test public void MonoProcessorRejectedDoOnError() { MonoProcessor<String> mp = MonoProcessor.create(); AtomicReference<Throwable> ref = new AtomicReference<>(); mp.doOnError(ref::set).subscribe(); mp.onError(new Exception("test")); assertThat(ref.get()).hasMessage("test"); assertThat(mp.isSuccess()).isFalse(); assertThat(mp.isError()).isTrue(); }
@Test public void MonoProcessorSuccessDoOnTerminate() { MonoProcessor<String> mp = MonoProcessor.create(); AtomicInteger invoked = new AtomicInteger(); mp.doOnTerminate(invoked::incrementAndGet).subscribe(); mp.onNext("test"); assertThat(invoked.get()).isEqualTo(1); assertThat(mp.isSuccess()).isTrue(); assertThat(mp.isError()).isFalse(); }
@Test public void MonoProcessorSuccessSubscribeCallback() { MonoProcessor<String> mp = MonoProcessor.create(); AtomicReference<String> ref = new AtomicReference<>(); mp.subscribe(ref::set); mp.onNext("test"); assertThat(ref.get()).isEqualToIgnoringCase("test"); assertThat(mp.isSuccess()).isTrue(); assertThat(mp.isError()).isFalse(); }
@Test public void MonoProcessorSuccessDoOnSuccessOrError() { MonoProcessor<String> mp = MonoProcessor.create(); AtomicReference<String> ref = new AtomicReference<>(); mp.doOnSuccessOrError((s, f) -> ref.set(s)).subscribe(); mp.onNext("test"); assertThat(ref.get()).isEqualToIgnoringCase("test"); assertThat(mp.isSuccess()).isTrue(); assertThat(mp.isError()).isFalse(); }
@Test public void MonoProcessorRejectedChainTogether() { MonoProcessor<String> mp = MonoProcessor.create(); MonoProcessor<String> mp2 = MonoProcessor.create(); mp.subscribe(mp2); mp.onError(new Exception("test")); assertThat(mp2.getError()).hasMessage("test"); assertThat(mp.isSuccess()).isFalse(); assertThat(mp.isError()).isTrue(); }
@Test public void MonoProcessorThenFulfill() { MonoProcessor<Integer> mp = MonoProcessor.create(); mp.onNext(1); MonoProcessor<Integer> mp2 = mp.flatMap(s -> Mono.just(s * 2)) .toProcessor(); mp2.subscribe(); assertThat(mp2.isTerminated()).isTrue(); assertThat(mp2.isSuccess()).isTrue(); assertThat(mp2.peek()).isEqualTo(2); }
@Test public void whenMonoError() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.<Integer>error(new Exception("test1")), Mono.<Integer>error(new Exception("test2"))) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isTrue()) .then(() -> assertThat(mp.isSuccess()).isFalse()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .verifyErrorSatisfies(e -> assertThat(e).hasMessage("test1")); }
@Test public void whenDelayJustMono3() { MonoProcessor<Tuple3<Integer, Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zipDelayError(Mono.just(1), Mono.just(2), Mono.just(3)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2 && v.getT3() == 3).isTrue()) .verifyComplete(); }
@Test public void firstMonoJust() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.first(Mono.just(1), Mono.just(2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .expectNext(1) .verifyComplete(); }
@Test public void whenMonoJust() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void whenDelayJustMono() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zipDelayError(Mono.just(1), Mono.just(2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void filterMonoNot() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.just(1).filter(s -> s % 2 == 0).subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.peek()).isNull()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .verifyComplete(); } }
@Test public void otherwiseReturnErrorUnfilter2() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.<Integer>error(new TestException()) .onErrorReturn(RuntimeException.class::isInstance, 1) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isTrue()) .then(() -> assertThat(mp.isSuccess()).isFalse()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .verifyError(TestException.class); } }
@Test public void otherwiseErrorFilter() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.<Integer>error(new TestException()) .onErrorResume(TestException.class, e -> Mono.just(1)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .expectNext(1) .verifyComplete(); }
@Test public void whenMonoCallable() { MonoProcessor<Tuple2<Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.fromCallable(() -> 1), Mono.fromCallable(() -> 2)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2).isTrue()) .verifyComplete(); }
@Test public void otherwiseReturnErrorUnfilter() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.<Integer>error(new TestException()) .onErrorReturn(RuntimeException.class, 1) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isTrue()) .then(() -> assertThat(mp.isSuccess()).isFalse()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .verifyError(TestException.class); }
@Test public void otherwiseReturnErrorFilter() { MonoProcessor<Integer> mp = MonoProcessor.create(); StepVerifier.create(Mono.<Integer>error(new TestException()) .onErrorReturn(TestException.class, 1) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .expectNext(1) .verifyComplete(); }