LoginServiceImpl extends BaseRetrofitService<LoginRepository> implements org.unicef.rapidreg.service
        .LoginService { @Override public void loginOffline(String username, String password, String url, LoginCallback callback) { User user = userDao.getUser(username, url); if (user == null) { callback.onFailed(null); return; } if (EncryptHelper.isMatched(password, user.getPassword())) { callback.onSuccessful("", user); return; } callback.onError(); } LoginServiceImpl(ConnectivityManager connectivityManager,
                            TelephonyManager telephonyManager,
                            UserDao userDao); @Override boolean isOnline(); @Override void loginOnline(final String username,
                            final String password,
                            final String url,
                            String imei,
                            final LoginCallback callback); @Override void loginOffline(String username, String password, String url, LoginCallback callback); @Override void destroy(); @Override String loadLastLoginServerUrl(); @Override boolean isUsernameValid(String username); @Override boolean isPasswordValid(String password); @Override boolean isUrlValid(String url);  }
@Test public void should_login_offline_when_fail() { LoginService.LoginCallback loginCallback = mock(LoginService.LoginCallback.class); when(userDao.getUser(anyString(), anyString())).thenReturn(null); loginService.loginOffline(username, password, expectedUrl, loginCallback); verify(loginCallback, times(1)).onFailed(null); verify(userDao, times(1)).getUser(username, expectedUrl); }
@Test public void should_login_offline_when_error() throws Exception { LoginService.LoginCallback loginCallback = mock(LoginService.LoginCallback.class); when(userDao.getUser(anyString(), anyString())).thenReturn(jack); loginService.loginOffline(username, "wrong pass workd", expectedUrl, loginCallback); verify(loginCallback, times(1)).onError(); verify(userDao, times(1)).getUser(username, expectedUrl); }
@Test public void should_login_offline_successfully() throws Exception { LoginService.LoginCallback loginCallback = mock(LoginService.LoginCallback.class); when(userDao.getUser(anyString(), anyString())).thenReturn(jack); loginService.loginOffline(username, password, expectedUrl, loginCallback); verify(loginCallback, times(1)).onSuccessful("", jack); verify(userDao, times(1)).getUser(username, expectedUrl); }