Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableFirstTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18
19 import java.util.NoSuchElementException;
20
21 import org.junit.*;
22 import org.mockito.InOrder;
23 import org.reactivestreams.Subscriber;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.functions.Predicate;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class FlowableFirstTest extends RxJavaTest {
30
31 Subscriber<String> w;
32
33 SingleObserver<Object> wo;
34
35 MaybeObserver<Object> wm;
36
37 private static final Predicate<String> IS_D = new Predicate<String>() {
38 @Override
39 public boolean test(String value) {
40 return "d".equals(value);
41 }
42 };
43
44 @Before
45 public void before() {
46 w = TestHelper.mockSubscriber();
47 wo = TestHelper.mockSingleObserver();
48 wm = TestHelper.mockMaybeObserver();
49 }
50
51 @Test
52 public void firstOrElseOfNoneFlowable() {
53 Flowable<String> src = Flowable.empty();
54 src.first("default").toFlowable().subscribe(w);
55
56 verify(w, times(1)).onNext(anyString());
57 verify(w, times(1)).onNext("default");
58 verify(w, never()).onError(any(Throwable.class));
59 verify(w, times(1)).onComplete();
60 }
61
62 @Test
63 public void firstOrElseOfSomeFlowable() {
64 Flowable<String> src = Flowable.just("a", "b", "c");
65 src.first("default").toFlowable().subscribe(w);
66
67 verify(w, times(1)).onNext(anyString());
68 verify(w, times(1)).onNext("a");
69 verify(w, never()).onError(any(Throwable.class));
70 verify(w, times(1)).onComplete();
71 }
72
73 @Test
74 public void firstOrElseWithPredicateOfNoneMatchingThePredicateFlowable() {
75 Flowable<String> src = Flowable.just("a", "b", "c");
76 src.filter(IS_D).first("default").toFlowable().subscribe(w);
77
78 verify(w, times(1)).onNext(anyString());
79 verify(w, times(1)).onNext("default");
80 verify(w, never()).onError(any(Throwable.class));
81 verify(w, times(1)).onComplete();
82 }
83
84 @Test
85 public void firstOrElseWithPredicateOfSomeFlowable() {
86 Flowable<String> src = Flowable.just("a", "b", "c", "d", "e", "f");
87 src.filter(IS_D).first("default").toFlowable().subscribe(w);
88
89 verify(w, times(1)).onNext(anyString());
90 verify(w, times(1)).onNext("d");
91 verify(w, never()).onError(any(Throwable.class));
92 verify(w, times(1)).onComplete();
93 }
94
95 @Test
96 public void firstFlowable() {
97 Flowable<Integer> flowable = Flowable.just(1, 2, 3).firstElement().toFlowable();
98
99 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
100 flowable.subscribe(subscriber);
101
102 InOrder inOrder = inOrder(subscriber);
103 inOrder.verify(subscriber, times(1)).onNext(1);
104 inOrder.verify(subscriber, times(1)).onComplete();
105 inOrder.verifyNoMoreInteractions();
106 }
107
108 @Test
109 public void firstWithOneElementFlowable() {
110 Flowable<Integer> flowable = Flowable.just(1).firstElement().toFlowable();
111
112 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
113 flowable.subscribe(subscriber);
114
115 InOrder inOrder = inOrder(subscriber);
116 inOrder.verify(subscriber, times(1)).onNext(1);
117 inOrder.verify(subscriber, times(1)).onComplete();
118 inOrder.verifyNoMoreInteractions();
119 }
120
121 @Test
122 public void firstWithEmptyFlowable() {
123 Flowable<Integer> flowable = Flowable.<Integer> empty().firstElement().toFlowable();
124
125 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
126 flowable.subscribe(subscriber);
127
128 InOrder inOrder = inOrder(subscriber);
129 inOrder.verify(subscriber).onComplete();
130 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
131 inOrder.verifyNoMoreInteractions();
132 }
133
134 @Test
135 public void firstWithPredicateFlowable() {
136 Flowable<Integer> flowable = Flowable.just(1, 2, 3, 4, 5, 6)
137 .filter(new Predicate<Integer>() {
138 @Override
139 public boolean test(Integer t1) {
140 return t1 % 2 == 0;
141 }
142 })
143 .firstElement().toFlowable();
144
145 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
146 flowable.subscribe(subscriber);
147
148 InOrder inOrder = inOrder(subscriber);
149 inOrder.verify(subscriber, times(1)).onNext(2);
150 inOrder.verify(subscriber, times(1)).onComplete();
151 inOrder.verifyNoMoreInteractions();
152 }
153
154 @Test
155 public void firstWithPredicateAndOneElementFlowable() {
156 Flowable<Integer> flowable = Flowable.just(1, 2)
157 .filter(new Predicate<Integer>() {
158 @Override
159 public boolean test(Integer t1) {
160 return t1 % 2 == 0;
161 }
162 })
163 .firstElement().toFlowable();
164
165 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
166 flowable.subscribe(subscriber);
167
168 InOrder inOrder = inOrder(subscriber);
169 inOrder.verify(subscriber, times(1)).onNext(2);
170 inOrder.verify(subscriber, times(1)).onComplete();
171 inOrder.verifyNoMoreInteractions();
172 }
173
174 @Test
175 public void firstWithPredicateAndEmptyFlowable() {
176 Flowable<Integer> flowable = Flowable.just(1)
177 .filter(new Predicate<Integer>() {
178 @Override
179 public boolean test(Integer t1) {
180 return t1 % 2 == 0;
181 }
182 })
183 .firstElement().toFlowable();
184
185 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
186 flowable.subscribe(subscriber);
187
188 InOrder inOrder = inOrder(subscriber);
189 inOrder.verify(subscriber).onComplete();
190 inOrder.verify(subscriber, never()).onError(any(Throwable.class));
191 inOrder.verifyNoMoreInteractions();
192 }
193
194 @Test
195 public void firstOrDefaultFlowable() {
196 Flowable<Integer> flowable = Flowable.just(1, 2, 3)
197 .first(4).toFlowable();
198
199 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
200 flowable.subscribe(subscriber);
201
202 InOrder inOrder = inOrder(subscriber);
203 inOrder.verify(subscriber, times(1)).onNext(1);
204 inOrder.verify(subscriber, times(1)).onComplete();
205 inOrder.verifyNoMoreInteractions();
206 }
207
208 @Test
209 public void firstOrDefaultWithOneElementFlowable() {
210 Flowable<Integer> flowable = Flowable.just(1).first(2).toFlowable();
211
212 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
213 flowable.subscribe(subscriber);
214
215 InOrder inOrder = inOrder(subscriber);
216 inOrder.verify(subscriber, times(1)).onNext(1);
217 inOrder.verify(subscriber, times(1)).onComplete();
218 inOrder.verifyNoMoreInteractions();
219 }
220
221 @Test
222 public void firstOrDefaultWithEmptyFlowable() {
223 Flowable<Integer> flowable = Flowable.<Integer> empty()
224 .first(1).toFlowable();
225
226 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
227 flowable.subscribe(subscriber);
228
229 InOrder inOrder = inOrder(subscriber);
230 inOrder.verify(subscriber, times(1)).onNext(1);
231 inOrder.verify(subscriber, times(1)).onComplete();
232 inOrder.verifyNoMoreInteractions();
233 }
234
235 @Test
236 public void firstOrDefaultWithPredicateFlowable() {
237 Flowable<Integer> flowable = Flowable.just(1, 2, 3, 4, 5, 6)
238 .filter(new Predicate<Integer>() {
239 @Override
240 public boolean test(Integer t1) {
241 return t1 % 2 == 0;
242 }
243 })
244 .first(8).toFlowable();
245
246 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
247 flowable.subscribe(subscriber);
248
249 InOrder inOrder = inOrder(subscriber);
250 inOrder.verify(subscriber, times(1)).onNext(2);
251 inOrder.verify(subscriber, times(1)).onComplete();
252 inOrder.verifyNoMoreInteractions();
253 }
254
255 @Test
256 public void firstOrDefaultWithPredicateAndOneElementFlowable() {
257 Flowable<Integer> flowable = Flowable.just(1, 2)
258 .filter(new Predicate<Integer>() {
259 @Override
260 public boolean test(Integer t1) {
261 return t1 % 2 == 0;
262 }
263 })
264 .first(4).toFlowable();
265
266 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
267 flowable.subscribe(subscriber);
268
269 InOrder inOrder = inOrder(subscriber);
270 inOrder.verify(subscriber, times(1)).onNext(2);
271 inOrder.verify(subscriber, times(1)).onComplete();
272 inOrder.verifyNoMoreInteractions();
273 }
274
275 @Test
276 public void firstOrDefaultWithPredicateAndEmptyFlowable() {
277 Flowable<Integer> flowable = Flowable.just(1)
278 .filter(new Predicate<Integer>() {
279 @Override
280 public boolean test(Integer t1) {
281 return t1 % 2 == 0;
282 }
283 })
284 .first(2).toFlowable();
285
286 Subscriber<Integer> subscriber = TestHelper.mockSubscriber();
287 flowable.subscribe(subscriber);
288
289 InOrder inOrder = inOrder(subscriber);
290 inOrder.verify(subscriber, times(1)).onNext(2);
291 inOrder.verify(subscriber, times(1)).onComplete();
292 inOrder.verifyNoMoreInteractions();
293 }
294
295 @Test
296 public void firstOrElseOfNone() {
297 Flowable<String> src = Flowable.empty();
298 src.first("default").subscribe(wo);
299
300 verify(wo, times(1)).onSuccess(anyString());
301 verify(wo, times(1)).onSuccess("default");
302 verify(wo, never()).onError(any(Throwable.class));
303 }
304
305 @Test
306 public void firstOrElseOfSome() {
307 Flowable<String> src = Flowable.just("a", "b", "c");
308 src.first("default").subscribe(wo);
309
310 verify(wo, times(1)).onSuccess(anyString());
311 verify(wo, times(1)).onSuccess("a");
312 verify(wo, never()).onError(any(Throwable.class));
313 }
314
315 @Test
316 public void firstOrElseWithPredicateOfNoneMatchingThePredicate() {
317 Flowable<String> src = Flowable.just("a", "b", "c");
318 src.filter(IS_D).first("default").subscribe(wo);
319
320 verify(wo, times(1)).onSuccess(anyString());
321 verify(wo, times(1)).onSuccess("default");
322 verify(wo, never()).onError(any(Throwable.class));
323 }
324
325 @Test
326 public void firstOrElseWithPredicateOfSome() {
327 Flowable<String> src = Flowable.just("a", "b", "c", "d", "e", "f");
328 src.filter(IS_D).first("default").subscribe(wo);
329
330 verify(wo, times(1)).onSuccess(anyString());
331 verify(wo, times(1)).onSuccess("d");
332 verify(wo, never()).onError(any(Throwable.class));
333 }
334
335 @Test
336 public void first() {
337 Maybe<Integer> maybe = Flowable.just(1, 2, 3).firstElement();
338
339 maybe.subscribe(wm);
340
341 InOrder inOrder = inOrder(wm);
342 inOrder.verify(wm, times(1)).onSuccess(1);
343 inOrder.verifyNoMoreInteractions();
344 }
345
346 @Test
347 public void firstWithOneElement() {
348 Maybe<Integer> maybe = Flowable.just(1).firstElement();
349
350 maybe.subscribe(wm);
351
352 InOrder inOrder = inOrder(wm);
353 inOrder.verify(wm, times(1)).onSuccess(1);
354 inOrder.verifyNoMoreInteractions();
355 }
356
357 @Test
358 public void firstWithEmpty() {
359 Maybe<Integer> maybe = Flowable.<Integer> empty().firstElement();
360
361 maybe.subscribe(wm);
362
363 InOrder inOrder = inOrder(wm);
364 inOrder.verify(wm).onComplete();
365 inOrder.verify(wm, never()).onError(any(Throwable.class));
366 inOrder.verifyNoMoreInteractions();
367 }
368
369 @Test
370 public void firstWithPredicate() {
371 Maybe<Integer> maybe = Flowable.just(1, 2, 3, 4, 5, 6)
372 .filter(new Predicate<Integer>() {
373 @Override
374 public boolean test(Integer t1) {
375 return t1 % 2 == 0;
376 }
377 })
378 .firstElement();
379
380 maybe.subscribe(wm);
381
382 InOrder inOrder = inOrder(wm);
383 inOrder.verify(wm, times(1)).onSuccess(2);
384 inOrder.verifyNoMoreInteractions();
385 }
386
387 @Test
388 public void firstWithPredicateAndOneElement() {
389 Maybe<Integer> maybe = Flowable.just(1, 2)
390 .filter(new Predicate<Integer>() {
391 @Override
392 public boolean test(Integer t1) {
393 return t1 % 2 == 0;
394 }
395 })
396 .firstElement();
397
398 maybe.subscribe(wm);
399
400 InOrder inOrder = inOrder(wm);
401 inOrder.verify(wm, times(1)).onSuccess(2);
402 inOrder.verifyNoMoreInteractions();
403 }
404
405 @Test
406 public void firstWithPredicateAndEmpty() {
407 Maybe<Integer> maybe = Flowable.just(1)
408 .filter(new Predicate<Integer>() {
409 @Override
410 public boolean test(Integer t1) {
411 return t1 % 2 == 0;
412 }
413 })
414 .firstElement();
415
416 maybe.subscribe(wm);
417
418 InOrder inOrder = inOrder(wm);
419 inOrder.verify(wm).onComplete();
420 inOrder.verify(wm, never()).onError(any(Throwable.class));
421 inOrder.verifyNoMoreInteractions();
422 }
423
424 @Test
425 public void firstOrDefault() {
426 Single<Integer> single = Flowable.just(1, 2, 3)
427 .first(4);
428
429 single.subscribe(wo);
430
431 InOrder inOrder = inOrder(wo);
432 inOrder.verify(wo, times(1)).onSuccess(1);
433 inOrder.verifyNoMoreInteractions();
434 }
435
436 @Test
437 public void firstOrDefaultWithOneElement() {
438 Single<Integer> single = Flowable.just(1).first(2);
439
440 single.subscribe(wo);
441
442 InOrder inOrder = inOrder(wo);
443 inOrder.verify(wo, times(1)).onSuccess(1);
444 inOrder.verifyNoMoreInteractions();
445 }
446
447 @Test
448 public void firstOrDefaultWithEmpty() {
449 Single<Integer> single = Flowable.<Integer> empty()
450 .first(1);
451
452 single.subscribe(wo);
453
454 InOrder inOrder = inOrder(wo);
455 inOrder.verify(wo, times(1)).onSuccess(1);
456 inOrder.verifyNoMoreInteractions();
457 }
458
459 @Test
460 public void firstOrDefaultWithPredicate() {
461 Single<Integer> single = Flowable.just(1, 2, 3, 4, 5, 6)
462 .filter(new Predicate<Integer>() {
463 @Override
464 public boolean test(Integer t1) {
465 return t1 % 2 == 0;
466 }
467 })
468 .first(8);
469
470 single.subscribe(wo);
471
472 InOrder inOrder = inOrder(wo);
473 inOrder.verify(wo, times(1)).onSuccess(2);
474 inOrder.verifyNoMoreInteractions();
475 }
476
477 @Test
478 public void firstOrDefaultWithPredicateAndOneElement() {
479 Single<Integer> single = Flowable.just(1, 2)
480 .filter(new Predicate<Integer>() {
481 @Override
482 public boolean test(Integer t1) {
483 return t1 % 2 == 0;
484 }
485 })
486 .first(4);
487
488 single.subscribe(wo);
489
490 InOrder inOrder = inOrder(wo);
491 inOrder.verify(wo, times(1)).onSuccess(2);
492 inOrder.verifyNoMoreInteractions();
493 }
494
495 @Test
496 public void firstOrDefaultWithPredicateAndEmpty() {
497 Single<Integer> single = Flowable.just(1)
498 .filter(new Predicate<Integer>() {
499 @Override
500 public boolean test(Integer t1) {
501 return t1 % 2 == 0;
502 }
503 })
504 .first(2);
505
506 single.subscribe(wo);
507
508 InOrder inOrder = inOrder(wo);
509 inOrder.verify(wo, times(1)).onSuccess(2);
510 inOrder.verifyNoMoreInteractions();
511 }
512
513 @Test
514 public void firstOrErrorNoElement() {
515 Flowable.empty()
516 .firstOrError()
517 .test()
518 .assertNoValues()
519 .assertError(NoSuchElementException.class);
520 }
521
522 @Test
523 public void firstOrErrorOneElement() {
524 Flowable.just(1)
525 .firstOrError()
526 .test()
527 .assertNoErrors()
528 .assertValue(1);
529 }
530
531 @Test
532 public void firstOrErrorMultipleElements() {
533 Flowable.just(1, 2, 3)
534 .firstOrError()
535 .test()
536 .assertNoErrors()
537 .assertValue(1);
538 }
539
540 @Test
541 public void firstOrErrorError() {
542 Flowable.error(new RuntimeException("error"))
543 .firstOrError()
544 .to(TestHelper.testConsumer())
545 .assertNoValues()
546 .assertErrorMessage("error")
547 .assertError(RuntimeException.class);
548 }
549
550 @Test
551 public void firstOrErrorNoElementFlowable() {
552 Flowable.empty()
553 .firstOrError()
554 .toFlowable()
555 .test()
556 .assertNoValues()
557 .assertError(NoSuchElementException.class);
558 }
559
560 @Test
561 public void firstOrErrorOneElementFlowable() {
562 Flowable.just(1)
563 .firstOrError()
564 .toFlowable()
565 .test()
566 .assertNoErrors()
567 .assertValue(1);
568 }
569
570 @Test
571 public void firstOrErrorMultipleElementsFlowable() {
572 Flowable.just(1, 2, 3)
573 .firstOrError()
574 .toFlowable()
575 .test()
576 .assertNoErrors()
577 .assertValue(1);
578 }
579
580 @Test
581 public void firstOrErrorErrorFlowable() {
582 Flowable.error(new RuntimeException("error"))
583 .firstOrError()
584 .toFlowable()
585 .to(TestHelper.testConsumer())
586 .assertNoValues()
587 .assertErrorMessage("error")
588 .assertError(RuntimeException.class);
589 }
590 }