LoginPresenter extends Presenter { public void startLogin(String userName, String password, boolean fromReSync) { hasGoneToNextPage = false; if (StringUtils.EMPTY.equals(userName.trim())) { view.showUserNameEmpty(); return; } if (StringUtils.EMPTY.equals(password)) { view.showPasswordEmpty(); return; } view.loading(); User user = new User(userName.trim(), password); if (!isRoboUniTest()) { new InternetCheck().execute(checkNetworkConnected(user, fromReSync)); } else { internetCheck.execute(checkNetworkConnected(user, fromReSync)); } }  @Override void attachView(BaseView v); void startLogin(String userName, String password, boolean fromReSync); void onLoginFailed(); void syncLocalUserData(Subscriber<SyncLocalUserProgress> subscriber); @NonNull Subscriber<SyncLocalUserProgress> getSyncLocalUserDataSubscriber();  }
@Test public void shouldSaveUserToLocalDBWhenSuccess() { presenter.startLogin("user", "password", false); verify(internetCheck1).execute(internetCheckCallBack.capture()); internetCheckCallBack.getValue().launchResponse(true); verify(mockActivity).loading(); verify(mockedApi).authorizeUser(any(User.class), loginCB.capture()); loginCB.getValue().success(userResponse, retrofitResponse); verify(userRepository).createOrUpdate(any(User.class)); }
@Test public void shouldSaveUserSupportedProgramsToLocalDBWhenSuccess() { presenter.startLogin("user", "password", false); verify(internetCheck1).execute(internetCheckCallBack.capture()); internetCheckCallBack.getValue().launchResponse(true); verify(mockActivity).loading(); verify(mockedApi).authorizeUser(any(User.class), loginCB.capture()); List<Program> supportedPrograms = newArrayList(new ProgramBuilder().build(), new ProgramBuilder().build()); userResponse.setFacilitySupportedPrograms(supportedPrograms); loginCB.getValue().success(userResponse, retrofitResponse); verify(userRepository).createOrUpdate(any(User.class)); }
@Test public void shouldCreateSyncAccountWhenLoginSuccess() { presenter.startLogin("user", "password", false); verify(internetCheck1).execute(internetCheckCallBack.capture()); internetCheckCallBack.getValue().launchResponse(true); verify(mockedApi).authorizeUser(any(User.class), loginCB.capture()); loginCB.getValue().success(userResponse, retrofitResponse); verify(syncService).createSyncAccount(userResponse.getUserInformation()); }
@Test public void shouldSaveUserInfoWhenLoginSuccess() { presenter.startLogin("user", "password", false); verify(internetCheck1).execute(internetCheckCallBack.capture()); internetCheckCallBack.getValue().launchResponse(true); verify(mockedApi).authorizeUser(any(User.class), loginCB.capture()); loginCB.getValue().success(userResponse, retrofitResponse); verify(userRepository).createOrUpdate(userResponse.getUserInformation()); assertThat(UserInfoMgr.getInstance().getUser()).isEqualTo(userResponse.getUserInformation()); verify(mockActivity).clearErrorAlerts(); }
@Test public void shouldSyncServerDataWhenLoginSuccessFromNet() { presenter.startLogin("user", "password", false); verify(internetCheck1).execute(internetCheckCallBack.capture()); internetCheckCallBack.getValue().launchResponse(true); verify(mockedApi).authorizeUser(any(User.class), loginCB.capture()); loginCB.getValue().success(userResponse, retrofitResponse); verify(syncDownManager).syncDownServerData(any(Subscriber.class)); }
@Test public void shouldDoOfflineLoginWhenNoConnectionAndHasSyncedData() { when(userRepository.mapUserFromLocal(any(User.class))).thenReturn(new User("user", "password")); when(mockActivity.needInitInventory()).thenReturn(false); SharedPreferenceMgr.getInstance().setLastMonthStockCardDataSynced(true); SharedPreferenceMgr.getInstance().setRequisitionDataSynced(true); SharedPreferenceMgr.getInstance().setLastSyncProductTime("time"); presenter.startLogin("user", "password", false); verify(internetCheck1).execute(internetCheckCallBack.capture()); internetCheckCallBack.getValue().launchResponse(false); verify(userRepository).mapUserFromLocal(any(User.class)); assertThat(UserInfoMgr.getInstance().getUser().getUsername()).isEqualTo("user"); }
@Test public void shouldShowMessageWhenNoConnectionAndHasNotGetProducts() { when(userRepository.mapUserFromLocal(any(User.class))).thenReturn(new User("user", "password")); when(mockActivity.needInitInventory()).thenReturn(false); presenter.startLogin("user", "password", false); verify(internetCheck1).execute(internetCheckCallBack.capture()); internetCheckCallBack.getValue().launchResponse(false); verify(userRepository).mapUserFromLocal(any(User.class)); assertThat(UserInfoMgr.getInstance().getUser().getUsername()).isEqualTo("user"); }
@Test public void shouldShowLoginFailErrorMsgWhenNoConnectionAndNoLocalCache() { when(userRepository.mapUserFromLocal(any(User.class))).thenReturn(null); presenter.startLogin("user", "password", false); InternetCheck internetCheck = mock(InternetCheck.class); verify(internetCheck1).execute(internetCheckCallBack.capture()); verify(internetCheck1).execute(internetCheckCallBack.capture()); internetCheckCallBack.getValue().launchResponse(false); verify(userRepository).mapUserFromLocal(any(User.class)); verify(mockActivity).loaded(); verify(mockActivity).showInvalidAlert(); verify(mockActivity).clearPassword(); }
@Test public void shouldShowUserNameEmptyErrorMessage() { presenter.startLogin("", "password1", false); verify(mockActivity).showUserNameEmpty(); }
@Test public void shouldShowPasswordEmptyErrorMessage() { presenter.startLogin("user", "", false); verify(mockActivity).showPasswordEmpty(); }