@Test public void bodySuccess200() { server.enqueue(new MockResponse().setBody("Hi")); BlockingObservable<String> o = service.body().toBlocking(); assertThat(o.first()).isEqualTo("Hi"); }
@Test public void bodyFailure() { server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AFTER_REQUEST)); BlockingObservable<String> o = service.body().toBlocking(); try { o.first(); fail(); } catch (RuntimeException e) { assertThat(e.getCause()).isInstanceOf(IOException.class); } }
@Test public void bodySuccess404() { server.enqueue(new MockResponse().setResponseCode(404)); BlockingObservable<String> o = service.body().toBlocking(); try { o.first(); fail(); } catch (RuntimeException e) { // TODO assert on some indicator of 404. } }
@Test public void testShouldReturnGuitarsOfGivenType() { // given final int limit = 3; final GuitarType type = GuitarType.ACOUSTIC; // when BlockingObservable<Guitar> observable = provider.observeGuitars(type, limit).toBlocking(); // then observable.forEach( new Action1<Guitar>() { @Override public void call(Guitar guitar) { assertThat(guitar.type).isEqualTo(type); } }); }
@Test public void testShouldReturnLimitedGuitars() { // given final int limit = 2; final GuitarType type = GuitarType.ELECTRIC; final List<Guitar> guitars = new ArrayList<>(); // when BlockingObservable<Guitar> observable = provider.observeGuitars(type, limit).toBlocking(); observable.forEach( new Action1<Guitar>() { @Override public void call(Guitar guitar) { guitars.add(guitar); } }); // then assertThat(guitars.size()).isEqualTo(limit); }
@Test /* (timeout = 8000) */ public void testSingleSourceManyIterators() throws InterruptedException { PublishSubject<Long> ps = PublishSubject.create(); BlockingObservable<Long> source = ps.take(10).toBlockingObservable(); Iterable<Long> iter = source.next(); for (int j = 0; j < 3; j++) { BlockingOperatorNext.NextIterator<Long> it = (BlockingOperatorNext.NextIterator<Long>) iter.iterator(); for (long i = 0; i < 9; i++) { // hasNext has to set the waiting to true, otherwise, all onNext will be skipped it.setWaiting(true); ps.onNext(i); Assert.assertEquals(true, it.hasNext()); Assert.assertEquals(j + "th iteration", Long.valueOf(i), it.next()); } it.setWaiting(true); ps.onNext(9L); Assert.assertEquals(j + "th iteration", false, it.hasNext()); } }