Authenticator { public UserDto authenticateCredentials(String username, String password) { try { User user = userDao.findByUsername(username); if (passwordSecurity.check(password, user.getHashPassword())) { UserDto userDto = User.createDto(user); persistHttpSessionCookie(userDto); return userDto; } else { throw new AuthenticationException(); } } catch (Exception e) { throw new AuthenticationException(); } } @Inject  Authenticator(
            UserDao userDao,
            Provider<HttpSession> sessionProvider,
            PasswordSecurity passwordSecurity,
            CurrentUserDtoProvider currentUserDtoProvider,
            UserSessionDao userSessionDao); UserDto authenticateCredentials(String username, String password); UserDto authenticatCookie(String loggedInCookie); void logout(); Boolean isUserLoggedIn();  }
@Test public void aValidUserShouldBeAbleToConnect() { User user = mock(User.class); given(user.getId()).willReturn(A_USER_ID); given(userDao.findByUsername(A_VALID_USER)).willReturn(user); given(passwordSecurity.check(anyString(), anyString())).willReturn(true); UserDto fetchUser = authenticator.authenticateCredentials(A_VALID_USER, A_VALID_PASSWORD); assertNotNull(fetchUser); verify(httpSession).setAttribute(SecurityParameters.getUserSessionKey(), A_USER_ID); }
@Test(expected = AuthenticationException.class) public void aValidUserWithAnInvalidPasswordShouldntBeAbleToConnect(User user) { given(userDao.findByUsername(A_VALID_USER)).willReturn(user); given(passwordSecurity.check(anyString(), anyString())).willReturn(false); authenticator.authenticateCredentials(A_VALID_USER, AN_INVALID_PASSWORD); verify(httpSession, never()).setAttribute(anyString(), any()); }
@Test(expected = AuthenticationException.class) public void anInvalidUserShouldntBeAbleToConnect() { given(userDao.findByUsername(AN_INVALID_USER)).willThrow(new NoResultException()); authenticator.authenticateCredentials(AN_INVALID_USER, AN_INVALID_PASSWORD); verify(httpSession, never()).setAttribute(anyString(), any()); }