Repository navigation
Expand file tree
/
Copy pathObservableMapTest.java
More file actions
330 lines (283 loc) · 11.6 KB
/
Copy pathObservableMapTest.java
File metadata and controls
330 lines (283 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Copyright 2016 Netflix, Inc.
*
* 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.reactivex.internal.operators.observable;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import java.util.*;
import org.junit.*;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.TestHelper;
import io.reactivex.functions.*;
import io.reactivex.schedulers.Schedulers;
public class ObservableMapTest {
Observer<String> stringObserver;
Observer<String> stringObserver2;
final static BiFunction<String, Integer, String> APPEND_INDEX = new BiFunction<String, Integer, String>() {
@Override
public String apply(String value, Integer index) {
return value + index;
}
};
@Before
public void before() {
stringObserver = TestHelper.mockObserver();
stringObserver2 = TestHelper.mockObserver();
}
@Test
public void testMap() {
Map<String, String> m1 = getMap("One");
Map<String, String> m2 = getMap("Two");
Observable<Map<String, String>> o = Observable.just(m1, m2);
Observable<String> m = o.map(new Function<Map<String, String>, String>() {
@Override
public String apply(Map<String, String> map) {
return map.get("firstName");
}
});
m.subscribe(stringObserver);
verify(stringObserver, never()).onError(any(Throwable.class));
verify(stringObserver, times(1)).onNext("OneFirst");
verify(stringObserver, times(1)).onNext("TwoFirst");
verify(stringObserver, times(1)).onComplete();
}
@Test
public void testMapMany() {
/* simulate a top-level async call which returns IDs */
Observable<Integer> ids = Observable.just(1, 2);
/* now simulate the behavior to take those IDs and perform nested async calls based on them */
Observable<String> m = ids.flatMap(new Function<Integer, Observable<String>>() {
@Override
public Observable<String> apply(Integer id) {
/* simulate making a nested async call which creates another NbpObservable */
Observable<Map<String, String>> subObservable = null;
if (id == 1) {
Map<String, String> m1 = getMap("One");
Map<String, String> m2 = getMap("Two");
subObservable = Observable.just(m1, m2);
} else {
Map<String, String> m3 = getMap("Three");
Map<String, String> m4 = getMap("Four");
subObservable = Observable.just(m3, m4);
}
/* simulate kicking off the async call and performing a select on it to transform the data */
return subObservable.map(new Function<Map<String, String>, String>() {
@Override
public String apply(Map<String, String> map) {
return map.get("firstName");
}
});
}
});
m.subscribe(stringObserver);
verify(stringObserver, never()).onError(any(Throwable.class));
verify(stringObserver, times(1)).onNext("OneFirst");
verify(stringObserver, times(1)).onNext("TwoFirst");
verify(stringObserver, times(1)).onNext("ThreeFirst");
verify(stringObserver, times(1)).onNext("FourFirst");
verify(stringObserver, times(1)).onComplete();
}
@Test
public void testMapMany2() {
Map<String, String> m1 = getMap("One");
Map<String, String> m2 = getMap("Two");
Observable<Map<String, String>> observable1 = Observable.just(m1, m2);
Map<String, String> m3 = getMap("Three");
Map<String, String> m4 = getMap("Four");
Observable<Map<String, String>> observable2 = Observable.just(m3, m4);
Observable<Observable<Map<String, String>>> o = Observable.just(observable1, observable2);
Observable<String> m = o.flatMap(new Function<Observable<Map<String, String>>, Observable<String>>() {
@Override
public Observable<String> apply(Observable<Map<String, String>> o) {
return o.map(new Function<Map<String, String>, String>() {
@Override
public String apply(Map<String, String> map) {
return map.get("firstName");
}
});
}
});
m.subscribe(stringObserver);
verify(stringObserver, never()).onError(any(Throwable.class));
verify(stringObserver, times(1)).onNext("OneFirst");
verify(stringObserver, times(1)).onNext("TwoFirst");
verify(stringObserver, times(1)).onNext("ThreeFirst");
verify(stringObserver, times(1)).onNext("FourFirst");
verify(stringObserver, times(1)).onComplete();
}
@Test
public void testMapWithError() {
Observable<String> w = Observable.just("one", "fail", "two", "three", "fail");
Observable<String> m = w.map(new Function<String, String>() {
@Override
public String apply(String s) {
if ("fail".equals(s)) {
throw new RuntimeException("Forced Failure");
}
return s;
}
}).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable t1) {
t1.printStackTrace();
}
});
m.subscribe(stringObserver);
verify(stringObserver, times(1)).onNext("one");
verify(stringObserver, never()).onNext("two");
verify(stringObserver, never()).onNext("three");
verify(stringObserver, never()).onComplete();
verify(stringObserver, times(1)).onError(any(Throwable.class));
}
@Test(expected = IllegalArgumentException.class)
public void testMapWithIssue417() {
Observable.just(1).observeOn(Schedulers.computation())
.map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer arg0) {
throw new IllegalArgumentException("any error");
}
}).blockingSingle();
}
@Test(expected = IllegalArgumentException.class)
public void testMapWithErrorInFuncAndThreadPoolScheduler() throws InterruptedException {
// The error will throw in one of threads in the thread pool.
// If map does not handle it, the error will disappear.
// so map needs to handle the error by itself.
Observable<String> m = Observable.just("one")
.observeOn(Schedulers.computation())
.map(new Function<String, String>() {
@Override
public String apply(String arg0) {
throw new IllegalArgumentException("any error");
}
});
// block for response, expecting exception thrown
m.blockingLast();
}
/**
* While mapping over range(1,0).last() we expect NoSuchElementException since the sequence is empty.
*/
@Test(expected = NoSuchElementException.class)
public void testErrorPassesThruMap() {
Observable.range(1, 0).last().map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer i) {
return i;
}
}).blockingSingle();
}
/**
* We expect IllegalStateException to pass thru map.
*/
@Test(expected = IllegalStateException.class)
public void testErrorPassesThruMap2() {
Observable.error(new IllegalStateException()).map(new Function<Object, Object>() {
@Override
public Object apply(Object i) {
return i;
}
}).blockingSingle();
}
/**
* We expect an ArithmeticException exception here because last() emits a single value
* but then we divide by 0.
*/
@Test(expected = ArithmeticException.class)
public void testMapWithErrorInFunc() {
Observable.range(1, 1).last().map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer i) {
return i / 0;
}
}).blockingSingle();
}
// FIXME RS subscribers can't throw
// @Test(expected = OnErrorNotImplementedException.class)
// public void verifyExceptionIsThrownIfThereIsNoExceptionHandler() {
//
// ObservableConsumable<Object> creator = new ObservableConsumable<Object>() {
//
// @Override
// public void accept(NbpSubscriber<? super Object> NbpObserver) {
// NbpObserver.onSubscribe(EmptyDisposable.INSTANCE);
// NbpObserver.onNext("a");
// NbpObserver.onNext("b");
// NbpObserver.onNext("c");
// NbpObserver.onComplete();
// }
// };
//
// Function<Object, NbpObservable<Object>> manyMapper = new Function<Object, NbpObservable<Object>>() {
//
// @Override
// public NbpObservable<Object> apply(Object object) {
// return NbpObservable.just(object);
// }
// };
//
// Function<Object, Object> mapper = new Function<Object, Object>() {
// private int count = 0;
//
// @Override
// public Object apply(Object object) {
// ++count;
// if (count > 2) {
// throw new RuntimeException();
// }
// return object;
// }
// };
//
// Consumer<Object> onNext = new Consumer<Object>() {
//
// @Override
// public void accept(Object object) {
// System.out.println(object.toString());
// }
// };
//
// try {
// NbpObservable.create(creator).flatMap(manyMapper).map(mapper).subscribe(onNext);
// } catch (RuntimeException e) {
// e.printStackTrace();
// throw e;
// }
// }
private static Map<String, String> getMap(String prefix) {
Map<String, String> m = new HashMap<String, String>();
m.put("firstName", prefix + "First");
m.put("lastName", prefix + "Last");
return m;
}
// FIXME RS subscribers can't throw
// @Test(expected = OnErrorNotImplementedException.class)
// public void testShouldNotSwallowOnErrorNotImplementedException() {
// NbpObservable.just("a", "b").flatMap(new Function<String, NbpObservable<String>>() {
// @Override
// public NbpObservable<String> apply(String s) {
// return NbpObservable.just(s + "1", s + "2");
// }
// }).flatMap(new Function<String, NbpObservable<String>>() {
// @Override
// public NbpObservable<String> apply(String s) {
// return NbpObservable.error(new Exception("test"));
// }
// }).forEach(new Consumer<String>() {
// @Override
// public void accept(String s) {
// System.out.println(s);
// }
// });
// }
}