Search in sources :

Example 1 with RxResponse

use of com.jakdor.labday.rx.RxResponse 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.TestUtils.readAssetFile) R(com.jakdor.labday.R) Instrumentation(android.app.Instrumentation) LabService(com.jakdor.labday.common.network.LabService) ApplicationProvider(androidx.test.core.app.ApplicationProvider) 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) InstrumentationRegistry(androidx.test.platform.app.InstrumentationRegistry) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Rule(org.junit.Rule) 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 2 with RxResponse

use of com.jakdor.labday.rx.RxResponse 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.TestUtils.readAssetFile) R(com.jakdor.labday.R) Instrumentation(android.app.Instrumentation) LabService(com.jakdor.labday.common.network.LabService) ApplicationProvider(androidx.test.core.app.ApplicationProvider) 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) InstrumentationRegistry(androidx.test.platform.app.InstrumentationRegistry) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Rule(org.junit.Rule) 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 3 with RxResponse

use of com.jakdor.labday.rx.RxResponse 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.TestUtils.readAssetFile) R(com.jakdor.labday.R) Instrumentation(android.app.Instrumentation) LabService(com.jakdor.labday.common.network.LabService) ApplicationProvider(androidx.test.core.app.ApplicationProvider) 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) InstrumentationRegistry(androidx.test.platform.app.InstrumentationRegistry) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Rule(org.junit.Rule) 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 4 with RxResponse

use of com.jakdor.labday.rx.RxResponse 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.TestUtils.readAssetFile) R(com.jakdor.labday.R) Instrumentation(android.app.Instrumentation) LabService(com.jakdor.labday.common.network.LabService) ApplicationProvider(androidx.test.core.app.ApplicationProvider) 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) InstrumentationRegistry(androidx.test.platform.app.InstrumentationRegistry) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Rule(org.junit.Rule) 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 5 with RxResponse

use of com.jakdor.labday.rx.RxResponse in project LabDayApp by jakdor.

the class ProjectRepository method getUpdate.

/**
 * Checks if update is necessary, then gets AppData from API or local db
 * @return {Single<RxResponse<AppData>>} appData wrapped with {@link RxResponse}
 */
public Observable<RxResponse<AppData>> getUpdate(String apiUrl, Context context) {
    if (accessToken == null) {
        if (!loadAccessToken(context)) {
            repositoryState = repositoryStates.ERROR;
            return Observable.just(RxResponse.loginError(new Throwable("no access token")));
        }
    } else if (accessToken.equals("-1")) {
        repositoryState = repositoryStates.ERROR;
        return Observable.just(RxResponse.loginError(new Throwable("bad access token")));
    }
    networkManager.configAuth(apiUrl, accessToken);
    networkManager.configAuth(apiUrl);
    return networkManager.getLastUpdate().subscribeOn(rxSchedulersFacade.io()).observeOn(rxSchedulersFacade.ui()).onErrorResumeNext(Observable.just(new LastUpdate("-1"))).onExceptionResumeNext(Observable.just(new LastUpdate("-1"))).flatMap(s -> isLocalDataCurrent(apiUpdateId = s.getUpdatedAt(), context) ? // load from local db
    localDbHandler.getAppDataFromDb() : // get appData from api
    apiRequest(networkManager.getAppData())).onErrorResumeNext(// last effort data retrieval
    localDbHandler.getAppDataFromDb()).flatMap(appDataRxResponse -> {
        // last effort data retrieval from local db
        if (appDataRxResponse.status == RxStatus.ERROR) {
            return localDbHandler.getAppDataFromDb();
        } else {
            return Observable.just(appDataRxResponse);
        }
    }).doOnNext(appDataRxResponse -> {
        if (appDataRxResponse.status == RxStatus.SUCCESS) {
            saveApiLastUpdateId(apiUpdateId, context);
        } else if (appDataRxResponse.status == RxStatus.SUCCESS_DB) {
            this.data = appDataRxResponse;
            this.repositoryState = repositoryStates.READY;
        }
    }).onErrorReturn(throwable -> {
        if (apiUpdateCurrent) {
            this.repositoryState = repositoryStates.NO_DB;
            return RxResponse.noDb(throwable);
        } else {
            this.repositoryState = repositoryStates.ERROR;
            return RxResponse.error(throwable);
        }
    });
}
Also used : Context(android.content.Context) Entity(com.facebook.crypto.Entity) R(com.jakdor.labday.R) MapPath(com.jakdor.labday.common.model.maps.MapPath) CryptoConfig(com.facebook.crypto.CryptoConfig) Singleton(javax.inject.Singleton) KeyChain(com.facebook.crypto.keychain.KeyChain) Inject(javax.inject.Inject) Crypto(com.facebook.crypto.Crypto) LocalDbHandler(com.jakdor.labday.common.localdb.LocalDbHandler) Charset(java.nio.charset.Charset) AccessToken(com.jakdor.labday.common.model.AccessToken) RxSchedulersFacade(com.jakdor.labday.rx.RxSchedulersFacade) MapService(com.jakdor.labday.common.network.MapService) Observable(io.reactivex.Observable) RxStatus(com.jakdor.labday.rx.RxStatus) AndroidConceal(com.facebook.android.crypto.keychain.AndroidConceal) AppData(com.jakdor.labday.common.model.AppData) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) LastUpdate(com.jakdor.labday.common.model.LastUpdate) FileInputStream(java.io.FileInputStream) File(java.io.File) Timber(timber.log.Timber) SharedPreferences(android.content.SharedPreferences) LastUpdate(com.jakdor.labday.common.model.LastUpdate)

Aggregations

AppData (com.jakdor.labday.common.model.AppData)9 RxResponse (com.jakdor.labday.rx.RxResponse)9 Gson (com.google.gson.Gson)7 Test (org.junit.Test)7 Context (android.content.Context)6 SharedPreferences (android.content.SharedPreferences)6 R (com.jakdor.labday.R)6 LocalDbHandler (com.jakdor.labday.common.localdb.LocalDbHandler)6 LastUpdate (com.jakdor.labday.common.model.LastUpdate)6 RxSchedulersFacade (com.jakdor.labday.rx.RxSchedulersFacade)6 RxStatus (com.jakdor.labday.rx.RxStatus)6 FileOutputStream (java.io.FileOutputStream)6 Charset (java.nio.charset.Charset)6 Instrumentation (android.app.Instrumentation)5 ApplicationProvider (androidx.test.core.app.ApplicationProvider)5 InstrumentationRegistry (androidx.test.platform.app.InstrumentationRegistry)5 SoLoader (com.facebook.soloader.SoLoader)5 TestApp (com.jakdor.labday.TestApp)5 TestUtils.readAssetFile (com.jakdor.labday.TestUtils.readAssetFile)5 LabService (com.jakdor.labday.common.network.LabService)5