Search in sources :

Example 41 with TestObserver

use of io.reactivex.observers.TestObserver in project LabDayApp by jakdor.

the class ProjectRepositoryIntegrationTest method integrationLoginTestScenario1.

/**
 * {@link ProjectRepository} login() api call
 * - get accessToken after successful login
 * - get AppData
 * - check ProjectRepository after successful login/load
 */
@Test
public void integrationLoginTestScenario1() throws Exception {
    SharedPreferences sharedPreferences = targetContext.getSharedPreferences(targetContext.getString(R.string.pref_file_name), Context.MODE_PRIVATE);
    sharedPreferences.edit().putString(targetContext.getString(R.string.pref_api_last_update_id), "null").commit();
    Assert.assertEquals(projectRepository.getRepositoryState(), ProjectRepository.repositoryStates.INIT);
    Assert.assertNull(projectRepository.getData());
    Gson gson = new Gson();
    AppData appData = gson.fromJson(readAssetFile(testContext, "api/app_data.json"), AppData.class);
    TestObserver<RxResponse<AppData>> testObserver = projectRepository.login(dummyApiUrl, targetContext, dummyLogin, dummyPassword).subscribeOn(Schedulers.io()).doOnError(throwable -> Assert.fail()).test();
    testObserver.assertSubscribed();
    testObserver.awaitCount(1);
    testObserver.assertNoErrors();
    testObserver.assertValue(appDataRxResponse -> {
        Assert.assertNotNull(appDataRxResponse);
        Assert.assertNotNull(appDataRxResponse.data);
        Assert.assertNull(appDataRxResponse.error);
        Assert.assertEquals(RxStatus.SUCCESS, appDataRxResponse.status);
        Assert.assertEquals(appData, appDataRxResponse.data);
        Assert.assertEquals(appData.hashCode(), appDataRxResponse.data.hashCode());
        Assert.assertEquals(projectRepository.getRepositoryState(), ProjectRepository.repositoryStates.READY);
        Assert.assertNotNull(projectRepository.getData());
        Assert.assertEquals(projectRepository.getData().data, appData);
        LastUpdate expectedLastUpdate = gson.fromJson(readAssetFile(testContext, "api/last_update.json"), LastUpdate.class);
        Assert.assertNotNull(expectedLastUpdate.getUpdatedAt());
        Assert.assertEquals(new String(expectedLastUpdate.getUpdatedAt().toCharArray()), sharedPreferences.getString(targetContext.getString(R.string.pref_api_last_update_id), null));
        Assert.assertTrue(projectRepository.isLoggedIn(targetContext));
        return true;
    });
    testObserver.onComplete();
}
Also used : Context(android.content.Context) TestUtils.readAssetFile(com.jakdor.labday.androidjunit.TestUtils.readAssetFile) R(com.jakdor.labday.R) Instrumentation(android.app.Instrumentation) LabService(com.jakdor.labday.common.network.LabService) ProjectRepository(com.jakdor.labday.common.repository.ProjectRepository) LocalDbHandler(com.jakdor.labday.common.localdb.LocalDbHandler) Charset(java.nio.charset.Charset) Gson(com.google.gson.Gson) RxSchedulersFacade(com.jakdor.labday.rx.RxSchedulersFacade) After(org.junit.After) Schedulers(io.reactivex.schedulers.Schedulers) ExpectedException(org.junit.rules.ExpectedException) RxStatus(com.jakdor.labday.rx.RxStatus) Before(org.junit.Before) NetworkManager(com.jakdor.labday.common.repository.NetworkManager) AppData(com.jakdor.labday.common.model.AppData) RetrofitBuilder(com.jakdor.labday.common.network.RetrofitBuilder) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) InstrumentationRegistry(android.support.test.InstrumentationRegistry) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Rule(org.junit.Rule) RESTMockServer(io.appflate.restmock.RESTMockServer) SharedPreferences(android.content.SharedPreferences) SoLoader(com.facebook.soloader.SoLoader) Assert(org.junit.Assert) TestApp(com.jakdor.labday.TestApp) SharedPreferences(android.content.SharedPreferences) AppData(com.jakdor.labday.common.model.AppData) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Gson(com.google.gson.Gson) RxResponse(com.jakdor.labday.rx.RxResponse) Test(org.junit.Test)

Example 42 with TestObserver

use of io.reactivex.observers.TestObserver in project LabDayApp by jakdor.

the class ProjectRepositoryIntegrationTest method integrationUpdateTestScenario2.

/**
 * {@link ProjectRepository} getUpdate() integration test scenario 2
 * - local last update id matches API id
 * - get API last update id
 * - load AppData from local db
 * - check ProjectRepository after successful call/load
 */
@Test
public void integrationUpdateTestScenario2() throws Exception {
    SharedPreferences sharedPreferences = targetContext.getSharedPreferences(targetContext.getString(R.string.pref_file_name), Context.MODE_PRIVATE);
    Gson gson = new Gson();
    LastUpdate expectedLastUpdate = gson.fromJson(readAssetFile(testContext, "api/last_update.json"), LastUpdate.class);
    sharedPreferences.edit().putString(targetContext.getString(R.string.pref_api_last_update_id), expectedLastUpdate.getUpdatedAt()).commit();
    Assert.assertEquals(projectRepository.getRepositoryState(), ProjectRepository.repositoryStates.INIT);
    Assert.assertNull(projectRepository.getData());
    projectRepository.setAccessToken(dummyToken);
    AppData appData = gson.fromJson(readAssetFile(testContext, "api/app_data.json"), AppData.class);
    localDbHandler.pushAppDataToDb(appData);
    TestObserver<RxResponse<AppData>> testObserver = projectRepository.getUpdate(dummyApiUrl, targetContext).subscribeOn(Schedulers.io()).doOnError(throwable -> Assert.fail()).test();
    testObserver.assertSubscribed();
    testObserver.awaitCount(1);
    testObserver.assertNoErrors();
    testObserver.assertValue(appDataRxResponse -> {
        Assert.assertNotNull(appDataRxResponse);
        Assert.assertNotNull(appDataRxResponse.data);
        Assert.assertNull(appDataRxResponse.error);
        Assert.assertEquals(RxStatus.SUCCESS_DB, appDataRxResponse.status);
        Assert.assertEquals(appData, appDataRxResponse.data);
        Assert.assertEquals(appData.hashCode(), appDataRxResponse.data.hashCode());
        Assert.assertEquals(ProjectRepository.repositoryStates.READY, projectRepository.getRepositoryState());
        Assert.assertNotNull(projectRepository.getData());
        Assert.assertEquals(appData, projectRepository.getData().data);
        Assert.assertNotNull(expectedLastUpdate.getUpdatedAt());
        Assert.assertEquals(new String(expectedLastUpdate.getUpdatedAt().toCharArray()), sharedPreferences.getString(targetContext.getString(R.string.pref_api_last_update_id), null));
        return true;
    });
    testObserver.onComplete();
}
Also used : Context(android.content.Context) TestUtils.readAssetFile(com.jakdor.labday.androidjunit.TestUtils.readAssetFile) R(com.jakdor.labday.R) Instrumentation(android.app.Instrumentation) LabService(com.jakdor.labday.common.network.LabService) ProjectRepository(com.jakdor.labday.common.repository.ProjectRepository) LocalDbHandler(com.jakdor.labday.common.localdb.LocalDbHandler) Charset(java.nio.charset.Charset) Gson(com.google.gson.Gson) RxSchedulersFacade(com.jakdor.labday.rx.RxSchedulersFacade) After(org.junit.After) Schedulers(io.reactivex.schedulers.Schedulers) ExpectedException(org.junit.rules.ExpectedException) RxStatus(com.jakdor.labday.rx.RxStatus) Before(org.junit.Before) NetworkManager(com.jakdor.labday.common.repository.NetworkManager) AppData(com.jakdor.labday.common.model.AppData) RetrofitBuilder(com.jakdor.labday.common.network.RetrofitBuilder) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) InstrumentationRegistry(android.support.test.InstrumentationRegistry) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Rule(org.junit.Rule) RESTMockServer(io.appflate.restmock.RESTMockServer) SharedPreferences(android.content.SharedPreferences) SoLoader(com.facebook.soloader.SoLoader) Assert(org.junit.Assert) TestApp(com.jakdor.labday.TestApp) SharedPreferences(android.content.SharedPreferences) LastUpdate(com.jakdor.labday.common.model.LastUpdate) AppData(com.jakdor.labday.common.model.AppData) Gson(com.google.gson.Gson) RxResponse(com.jakdor.labday.rx.RxResponse) Test(org.junit.Test)

Example 43 with TestObserver

use of io.reactivex.observers.TestObserver in project LabDayApp by jakdor.

the class ProjectRepositoryIntegrationTest method integrationDataTestScenario1.

/**
 * {@link ProjectRepository} getAppData() / getData() integration test scenario 1
 * - check init ProjectRepository state
 * - get appData API response (successful)
 * - check ProjectRepository after successful call
 */
@Test
public void integrationDataTestScenario1() throws Exception {
    Assert.assertEquals(projectRepository.getRepositoryState(), ProjectRepository.repositoryStates.INIT);
    Assert.assertNull(projectRepository.getData());
    projectRepository.setAccessToken(dummyToken);
    Gson gson = new Gson();
    AppData appData = gson.fromJson(readAssetFile(testContext, "api/app_data.json"), AppData.class);
    TestObserver<RxResponse<AppData>> testObserver = projectRepository.getAppData(dummyApiUrl, targetContext).subscribeOn(Schedulers.io()).doOnError(throwable -> Assert.fail()).test();
    testObserver.assertSubscribed();
    testObserver.awaitCount(1);
    testObserver.assertNoErrors();
    testObserver.assertValue(appDataRxResponse -> {
        Assert.assertNotNull(appDataRxResponse);
        Assert.assertNotNull(appDataRxResponse.data);
        Assert.assertNull(appDataRxResponse.error);
        Assert.assertEquals(RxStatus.SUCCESS, appDataRxResponse.status);
        Assert.assertEquals(appData, appDataRxResponse.data);
        Assert.assertEquals(appData.hashCode(), appDataRxResponse.data.hashCode());
        Assert.assertEquals(projectRepository.getRepositoryState(), ProjectRepository.repositoryStates.READY);
        Assert.assertNotNull(projectRepository.getData());
        Assert.assertEquals(projectRepository.getData().data, appData);
        return true;
    });
    testObserver.onComplete();
}
Also used : Context(android.content.Context) TestUtils.readAssetFile(com.jakdor.labday.androidjunit.TestUtils.readAssetFile) R(com.jakdor.labday.R) Instrumentation(android.app.Instrumentation) LabService(com.jakdor.labday.common.network.LabService) ProjectRepository(com.jakdor.labday.common.repository.ProjectRepository) LocalDbHandler(com.jakdor.labday.common.localdb.LocalDbHandler) Charset(java.nio.charset.Charset) Gson(com.google.gson.Gson) RxSchedulersFacade(com.jakdor.labday.rx.RxSchedulersFacade) After(org.junit.After) Schedulers(io.reactivex.schedulers.Schedulers) ExpectedException(org.junit.rules.ExpectedException) RxStatus(com.jakdor.labday.rx.RxStatus) Before(org.junit.Before) NetworkManager(com.jakdor.labday.common.repository.NetworkManager) AppData(com.jakdor.labday.common.model.AppData) RetrofitBuilder(com.jakdor.labday.common.network.RetrofitBuilder) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) InstrumentationRegistry(android.support.test.InstrumentationRegistry) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Rule(org.junit.Rule) RESTMockServer(io.appflate.restmock.RESTMockServer) SharedPreferences(android.content.SharedPreferences) SoLoader(com.facebook.soloader.SoLoader) Assert(org.junit.Assert) TestApp(com.jakdor.labday.TestApp) AppData(com.jakdor.labday.common.model.AppData) Gson(com.google.gson.Gson) RxResponse(com.jakdor.labday.rx.RxResponse) Test(org.junit.Test)

Example 44 with TestObserver

use of io.reactivex.observers.TestObserver in project LabDayApp by jakdor.

the class ProjectRepositoryIntegrationTest method integrationUpdateTestScenario3.

/**
 * {@link ProjectRepository} getUpdate() integration test scenario 3
 * - get API last update id (failed)
 * - load AppData from local db
 * - check ProjectRepository after successful load
 */
@Test
public void integrationUpdateTestScenario3() throws Exception {
    Assert.assertEquals(projectRepository.getRepositoryState(), ProjectRepository.repositoryStates.INIT);
    Assert.assertNull(projectRepository.getData());
    projectRepository.setAccessToken(dummyToken);
    Gson gson = new Gson();
    AppData appData = gson.fromJson(readAssetFile(testContext, "api/app_data.json"), AppData.class);
    localDbHandler.pushAppDataToDb(appData);
    TestObserver<RxResponse<AppData>> testObserver = projectRepository.getUpdate(dummyApiBadUrl, targetContext).subscribeOn(Schedulers.io()).doOnError(throwable -> Assert.fail()).test();
    testObserver.assertSubscribed();
    testObserver.awaitCount(1);
    testObserver.assertNoErrors();
    testObserver.assertValue(appDataRxResponse -> {
        Assert.assertNotNull(appDataRxResponse);
        Assert.assertNotNull(appDataRxResponse.data);
        Assert.assertNull(appDataRxResponse.error);
        Assert.assertEquals(RxStatus.SUCCESS_DB, appDataRxResponse.status);
        Assert.assertEquals(appData, appDataRxResponse.data);
        Assert.assertEquals(appData.hashCode(), appDataRxResponse.data.hashCode());
        Assert.assertEquals(ProjectRepository.repositoryStates.READY, projectRepository.getRepositoryState());
        Assert.assertNotNull(projectRepository.getData());
        Assert.assertEquals(appData, projectRepository.getData().data);
        return true;
    });
    testObserver.onComplete();
}
Also used : Context(android.content.Context) TestUtils.readAssetFile(com.jakdor.labday.androidjunit.TestUtils.readAssetFile) R(com.jakdor.labday.R) Instrumentation(android.app.Instrumentation) LabService(com.jakdor.labday.common.network.LabService) ProjectRepository(com.jakdor.labday.common.repository.ProjectRepository) LocalDbHandler(com.jakdor.labday.common.localdb.LocalDbHandler) Charset(java.nio.charset.Charset) Gson(com.google.gson.Gson) RxSchedulersFacade(com.jakdor.labday.rx.RxSchedulersFacade) After(org.junit.After) Schedulers(io.reactivex.schedulers.Schedulers) ExpectedException(org.junit.rules.ExpectedException) RxStatus(com.jakdor.labday.rx.RxStatus) Before(org.junit.Before) NetworkManager(com.jakdor.labday.common.repository.NetworkManager) AppData(com.jakdor.labday.common.model.AppData) RetrofitBuilder(com.jakdor.labday.common.network.RetrofitBuilder) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) InstrumentationRegistry(android.support.test.InstrumentationRegistry) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Rule(org.junit.Rule) RESTMockServer(io.appflate.restmock.RESTMockServer) SharedPreferences(android.content.SharedPreferences) SoLoader(com.facebook.soloader.SoLoader) Assert(org.junit.Assert) TestApp(com.jakdor.labday.TestApp) AppData(com.jakdor.labday.common.model.AppData) Gson(com.google.gson.Gson) RxResponse(com.jakdor.labday.rx.RxResponse) Test(org.junit.Test)

Example 45 with TestObserver

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

the class EventRepositoryTest method shouldUpdateToggledEventToDatabaseOnSuccess.

@Test
public void shouldUpdateToggledEventToDatabaseOnSuccess() {
    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(Observable.just(UPDATED_EVENT));
    when(databaseRepository.update(Event.class, UPDATED_EVENT)).thenReturn(completable);
    eventRepository.updateEvent(EVENT).test();
    testObserver.assertSubscribed();
}
Also used : Completable(io.reactivex.Completable) TestObserver(io.reactivex.observers.TestObserver) 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