LoginInteractor { @NonNull public synchronized Observable<UiIndicator<String>> loginOrSignUp(@NonNull final UserCredentialsPayload userCredentialsPayload) { Logger.info(this, "Initiating user login (or sign up)"); if (this.uiIndicatorReplaySubject == null) { this.userCredentialsPayload = userCredentialsPayload; this.uiIndicatorReplaySubject = ReplaySubject.create(); identityManager.logInOrSignUp(userCredentialsPayload) .map(loginResponse -> UiIndicator.success(getSuccessMessage(userCredentialsPayload))) .onErrorReturn(throwable -> UiIndicator.error(getErrorMessage(userCredentialsPayload, throwable))) .startWith(UiIndicator.loading()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(uiIndicatorReplaySubject); } return uiIndicatorReplaySubject; } @Inject  LoginInteractor(@NonNull Context context, @NonNull IdentityManagerImpl identityManager); @NonNull synchronized Observable<UiIndicator<String>> loginOrSignUp(@NonNull final UserCredentialsPayload userCredentialsPayload); @NonNull synchronized Maybe<UserCredentialsPayload> getLastUserCredentialsPayload(); synchronized void onLoginResultsConsumed();  }
@Test public void loginSuccess() { when(identityManager.logInOrSignUp(loginPayload)).thenReturn(Observable.just(loginResponse)); final TestObserver<UiIndicator<String>> testObserver = interactor.loginOrSignUp(loginPayload).test(); testObserver.assertValues(UiIndicator.loading(), UiIndicator.success(context.getString(R.string.login_success_toast))); testObserver.assertComplete(); testObserver.assertNoErrors(); }
@Test public void signUpSuccess() { when(identityManager.logInOrSignUp(signUpPayload)).thenReturn(Observable.just(loginResponse)); final TestObserver<UiIndicator<String>> testObserver = interactor.loginOrSignUp(signUpPayload).test(); testObserver.assertValues(UiIndicator.loading(), UiIndicator.success(context.getString(R.string.sign_up_success_toast))); testObserver.assertComplete(); testObserver.assertNoErrors(); }
@Test public void loginErrorGeneric() { when(identityManager.logInOrSignUp(loginPayload)).thenReturn(Observable.error(new Exception())); final TestObserver<UiIndicator<String>> testObserver = interactor.loginOrSignUp(loginPayload).test(); testObserver.assertValues(UiIndicator.loading(), UiIndicator.error(context.getString(R.string.login_failure_toast))); testObserver.assertComplete(); testObserver.assertNoErrors(); }
@Test public void signUpErrorGeneric() { when(identityManager.logInOrSignUp(signUpPayload)).thenReturn(Observable.error(new Exception())); final TestObserver<UiIndicator<String>> testObserver = interactor.loginOrSignUp(signUpPayload).test(); testObserver.assertValues(UiIndicator.loading(), UiIndicator.error(context.getString(R.string.sign_up_failure_toast))); testObserver.assertComplete(); testObserver.assertNoErrors(); }
@Test public void loginErrorBadCredentials() { Response response = Response.error(401, responseBody); when(identityManager.logInOrSignUp(loginPayload)).thenReturn(Observable.error(new HttpException(response))); final TestObserver<UiIndicator<String>> testObserver = interactor.loginOrSignUp(loginPayload).test(); testObserver.assertValues(UiIndicator.loading(), UiIndicator.error(context.getString(R.string.login_failure_credentials_toast))); testObserver.assertComplete(); testObserver.assertNoErrors(); }
@Test public void signUpErrorExistingAccount() { Response response = Response.error(420, responseBody); when(identityManager.logInOrSignUp(signUpPayload)).thenReturn(Observable.error(new HttpException(response))); final TestObserver<UiIndicator<String>> testObserver = interactor.loginOrSignUp(signUpPayload).test(); testObserver.assertValues(UiIndicator.loading(), UiIndicator.error(context.getString(R.string.sign_up_failure_account_exists_toast))); testObserver.assertComplete(); testObserver.assertNoErrors(); }