use of com.jakdor.labday.common.model.AccessToken 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);
}
});
}
use of com.jakdor.labday.common.model.AccessToken in project LabDayApp by jakdor.
the class NetworkManagerIntegrationTest method loginTest.
/**
* {@link NetworkManager} integration test: login request to, local REST API server mock
*/
@Test
public void loginTest() throws Exception {
networkManager.configAuth(dummyApiUrl);
Gson gson = new Gson();
AccessToken expectedAccessToken = gson.fromJson(readAssetFile(testContext, "api/login.json"), AccessToken.class);
TestObserver<AccessToken> testObserver = networkManager.getAccessToken(dummyLogin, dummyPassword).test();
testObserver.awaitCount(1);
testObserver.assertSubscribed();
testObserver.assertNoErrors();
testObserver.assertValue(accessToken -> {
Assert.assertEquals(expectedAccessToken, accessToken);
Assert.assertEquals(expectedAccessToken.hashCode(), accessToken.hashCode());
return true;
});
testObserver.onComplete();
}
use of com.jakdor.labday.common.model.AccessToken in project LabDayApp by jakdor.
the class RetrofitAPICallsIntegrationTest method loginTest.
/**
* Tests login API call
*/
@Test
public void loginTest() throws Exception {
LabService labService = retrofitBuilder.createService(dummyApiUrl, LabService.class);
TestObserver<AccessToken> testObserver = labService.getAccessToken(dummyLogin, dummyPassword).test();
testObserver.assertSubscribed();
testObserver.awaitCount(1);
testObserver.assertNoErrors();
testObserver.assertValue(accessToken1 -> {
assertNotNull(accessToken1);
assertNotNull(accessToken1.getAccessToken());
return true;
});
testObserver.onComplete();
}
Aggregations