Skip to content

Commit 9667f06

Browse files
arjaynacionKevinGilmore
authored andcommitted
BAEL-1510 - RxJava - Combining Observables (eugenp#3605)
* BAEL-1510 - RxJava - Combining Observables Signed-off-by: Arjay Nacion <arjaycnacion@gmail.com> * BAEL-1510 - RxJava - Combining Observables * BAEL-1510 - RxJava - Combining Observables * Fixed indentations, used assertJ assertions, renamed test class to end in UnitTest * BAEL-1510 - RxJava - Combining Observables * Amendments based on PR review
1 parent 24cc2e0 commit 9667f06

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.baeldung.rxjava.combine;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.concurrent.Callable;
8+
import java.util.concurrent.ExecutionException;
9+
import java.util.concurrent.ExecutorService;
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.Future;
12+
13+
import org.junit.AfterClass;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
import rx.Observable;
18+
import rx.observers.TestSubscriber;
19+
20+
public class ObservableCombineUnitTest {
21+
22+
private static ExecutorService executor;
23+
24+
@BeforeClass
25+
public static void setupClass() {
26+
executor = Executors.newFixedThreadPool(10);
27+
}
28+
29+
@AfterClass
30+
public static void tearDownClass() {
31+
executor.shutdown();
32+
}
33+
34+
@Test
35+
public void givenTwoObservables_whenMerged_shouldEmitCombinedResults() {
36+
List<String> results = new ArrayList<>();
37+
38+
//@formatter:off
39+
Observable.merge(
40+
Observable.from(new String[] {"Hello", "World"}),
41+
Observable.from(new String[]{ "I love", "RxJava"})
42+
).subscribe(data -> {
43+
results.add(data);
44+
});
45+
//@formatter:on
46+
47+
assertThat(results).isNotEmpty();
48+
assertThat(results.size()).isEqualTo(4);
49+
assertThat(results).contains("Hello", "World", "I love", "RxJava");
50+
}
51+
52+
@Test
53+
public void givenAnObservable_whenStartWith_thenPrependEmittedResults() {
54+
StringBuffer buffer = new StringBuffer();
55+
56+
//@formatter:off
57+
Observable
58+
.from(new String[] { "RxJava", "Observables" })
59+
.startWith("Buzzwords of Reactive Programming")
60+
.subscribe(data -> {
61+
buffer.append(data).append(" ");
62+
});
63+
//@formatter:on
64+
65+
assertThat(buffer.toString().trim()).isEqualTo("Buzzwords of Reactive Programming RxJava Observables");
66+
}
67+
68+
@Test
69+
public void givenTwoObservables_whenZipped_thenReturnCombinedResults() {
70+
List<String> zippedStrings = new ArrayList<>();
71+
72+
//@formatter:off
73+
Observable.zip(
74+
Observable.from(new String[] { "Simple", "Moderate", "Complex" }),
75+
Observable.from(new String[] { "Solutions", "Success", "Heirarchy"}),
76+
(str1, str2) -> {
77+
return str1 + " " + str2;
78+
}).subscribe(zipped -> {
79+
zippedStrings.add(zipped);
80+
});
81+
//formatter:on
82+
83+
assertThat(zippedStrings).isNotEmpty();
84+
assertThat(zippedStrings.size()).isEqualTo(3);
85+
assertThat(zippedStrings).contains("Simple Solutions", "Moderate Success", "Complex Heirarchy");
86+
}
87+
88+
@Test
89+
public void givenMutipleObservablesOneThrows_whenMerged_thenCombineBeforePropagatingError() {
90+
Future<String> f1 = executor.submit(createCallable("Hello"));
91+
Future<String> f2 = executor.submit(createCallable("World"));
92+
Future<String> f3 = executor.submit(createCallable(null));
93+
Future<String> f4 = executor.submit(createCallable("RxJava"));
94+
95+
TestSubscriber<String> testSubscriber = new TestSubscriber<>();
96+
97+
//@formatter:off
98+
Observable.mergeDelayError(
99+
Observable.from(f1),
100+
Observable.from(f2),
101+
Observable.from(f3),
102+
Observable.from(f4)
103+
).subscribe(testSubscriber);
104+
//@formatter:on
105+
106+
testSubscriber.assertValues("hello", "world", "rxjava");
107+
testSubscriber.assertError(ExecutionException.class);
108+
}
109+
110+
private Callable<String> createCallable(final String data) {
111+
return new Callable<String>() {
112+
@Override
113+
public String call() throws Exception {
114+
if (data == null) {
115+
throw new IllegalArgumentException("Data should not be null.");
116+
}
117+
return data.toLowerCase();
118+
}
119+
};
120+
}
121+
}

0 commit comments

Comments
 (0)