Search in sources :

Example 46 with TestObserver

use of io.reactivex.observers.TestObserver in project open-event-orga-app by fossasia.

the class EventRepositoryTest method shouldNotUpdateToggledEventOnError.

@Test
public void shouldNotUpdateToggledEventOnError() {
    TestObserver testObserver = TestObserver.create();
    Completable completable = Completable.complete().doOnSubscribe(testObserver::onSubscribe);
    when(utilModel.isConnected()).thenReturn(true);
    when(utilModel.getToken()).thenReturn(TOKEN);
    when(eventService.patchEvent(EVENT.id, EVENT)).thenReturn(ObservableLift.error(Logger.TEST_ERROR));
    when(databaseRepository.update(Event.class, UPDATED_EVENT)).thenReturn(completable);
    eventRepository.updateEvent(EVENT).test();
    testObserver.assertNotSubscribed();
}
Also used : Completable(io.reactivex.Completable) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 47 with TestObserver

use of io.reactivex.observers.TestObserver in project open-event-orga-app by fossasia.

the class EventRepositoryTest method shouldSaveCreatedEventToDatabaseOnSuccess.

@Test
public void shouldSaveCreatedEventToDatabaseOnSuccess() {
    TestObserver testObserver = TestObserver.create();
    Completable completable = Completable.complete().doOnSubscribe(testObserver::onSubscribe);
    when(utilModel.isConnected()).thenReturn(true);
    when(eventService.postEvent(EVENT)).thenReturn(Observable.just(EVENT));
    when(databaseRepository.save(Event.class, EVENT)).thenReturn(completable);
    eventRepository.createEvent(EVENT).test();
    testObserver.assertSubscribed();
}
Also used : Completable(io.reactivex.Completable) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 48 with TestObserver

use of io.reactivex.observers.TestObserver in project open-event-orga-app by fossasia.

the class AttendeeRepositoryTest method shouldSaveAttendeesInCache.

@Test
public void shouldSaveAttendeesInCache() {
    TestObserver testObserver = TestObserver.create();
    Completable completable = Completable.complete().doOnSubscribe(testObserver::onSubscribe);
    when(utilModel.isConnected()).thenReturn(true);
    when(databaseRepository.getItems(eq(Attendee.class), any(SQLOperator.class))).thenReturn(Observable.empty());
    when(databaseRepository.deleteAll(Attendee.class)).thenReturn(completable);
    when(databaseRepository.saveList(Attendee.class, ATTENDEES)).thenReturn(completable);
    when(eventService.getAttendees(43)).thenReturn(Observable.just(ATTENDEES));
    // No force reload ensures use of cache
    attendeeRepository.getAttendees(43, false).test();
    testObserver.assertSubscribed();
    // Verify loads from network
    verify(eventService).getAttendees(43);
}
Also used : Completable(io.reactivex.Completable) SQLOperator(com.raizlabs.android.dbflow.sql.language.SQLOperator) TestObserver(io.reactivex.observers.TestObserver) Attendee(org.fossasia.openevent.app.data.models.Attendee) Test(org.junit.Test)

Example 49 with TestObserver

use of io.reactivex.observers.TestObserver in project open-event-orga-app by fossasia.

the class AttendeeRepositoryTest method shouldSaveToggledAttendeeCheck.

@Test
public void shouldSaveToggledAttendeeCheck() {
    Attendee attendee = Attendee.builder().id(89).build();
    attendee.setCheckedIn(true);
    TestObserver testObserver = TestObserver.create();
    Completable completable = Completable.complete().doOnSubscribe(testObserver::onSubscribe);
    when(utilModel.isConnected()).thenReturn(true);
    when(databaseRepository.getItems(eq(Attendee.class), any())).thenReturn(Observable.just(attendee));
    when(databaseRepository.update(Attendee.class, attendee)).thenReturn(completable);
    when(eventService.patchAttendee(89, attendee)).thenReturn(Observable.just(attendee));
    Observable<Attendee> attendeeObservable = attendeeRepository.toggleAttendeeCheckStatus(attendee);
    attendeeObservable.test();
    testObserver.assertSubscribed();
}
Also used : Completable(io.reactivex.Completable) Attendee(org.fossasia.openevent.app.data.models.Attendee) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 50 with TestObserver

use of io.reactivex.observers.TestObserver in project retrofit by square.

the class AsyncTest method bodyThrowingInOnErrorDeliveredToPlugin.

@Test
public void bodyThrowingInOnErrorDeliveredToPlugin() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404));
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> pluginRef = new AtomicReference<>();
    RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            if (!pluginRef.compareAndSet(null, throwable)) {
                // Don't swallow secondary errors!
                throw Exceptions.propagate(throwable);
            }
            latch.countDown();
        }
    });
    TestObserver<Void> observer = new TestObserver<>();
    final RuntimeException e = new RuntimeException();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    service.completable().subscribe(new ForwardingCompletableObserver(observer) {

        @Override
        public void onError(Throwable throwable) {
            errorRef.set(throwable);
            throw e;
        }
    });
    latch.await(1, SECONDS);
    // noinspection ThrowableResultOfMethodCallIgnored
    CompositeException composite = (CompositeException) pluginRef.get();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CompositeException(io.reactivex.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeException(io.reactivex.exceptions.CompositeException) IOException(java.io.IOException) TestObserver(io.reactivex.observers.TestObserver) ForwardingCompletableObserver(retrofit2.adapter.rxjava2.CompletableThrowingTest.ForwardingCompletableObserver) Test(org.junit.Test)

Aggregations

TestObserver (io.reactivex.observers.TestObserver)158 Test (org.junit.Test)128 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 TargetApi (android.annotation.TargetApi)21 Matchers.anyString (org.mockito.Matchers.anyString)21 StorIOException (com.pushtorefresh.storio3.StorIOException)19 TestException (io.reactivex.exceptions.TestException)19 Observable (io.reactivex.Observable)12 BaseTest (io.rx_cache2.internal.common.BaseTest)12 Completable (io.reactivex.Completable)11 Disposable (io.reactivex.disposables.Disposable)11 InOrder (org.mockito.InOrder)10 TestScheduler (io.reactivex.schedulers.TestScheduler)9 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)8 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)7 Schedulers (io.reactivex.schedulers.Schedulers)6 Activity (android.app.Activity)5 Instrumentation (android.app.Instrumentation)5 ContentValues (android.content.ContentValues)5 Context (android.content.Context)5