SimpleBasicAuthenticator implements Authenticator<BasicCredentials, User> { public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { if (userAccountRepository.containsKey(credentials.getUsername()) && EncryptorFactory.getPasswordEncryptor().checkPassword(credentials.getPassword(), userAccountRepository.get(credentials.getUsername()).getEncryptedPassword())) { UserAccount userAccount = userAccountRepository.get(credentials.getUsername()); return Optional.of(new User(userAccount)); } else { return Optional.absent(); } } SimpleBasicAuthenticator(SimpleConfig config); Optional<User> authenticate(BasicCredentials credentials);  }
@Test public void testNormal() { try { BasicCredentials credentials = new BasicCredentials(TEST_USERNAME, TEST_SECRET_PHRASE); Optional<User> result = authenticator.authenticate(credentials); Assert.assertTrue("result isn't present when passed correct credentials", result.isPresent()); User user = result.get(); Assert.assertEquals("authenticated user is not expected", TEST_USERNAME, user.getName()); } catch (AuthenticationException e) { Assert.fail("unexpected error occurs: " + e.getMessage()); } }
@Test public void testUnexistingUsername() { try { Optional<User> result = authenticator.authenticate(new BasicCredentials(TEST_UNEXISTING_USERNAME, TEST_SECRET_PHRASE)); Assert.assertFalse("result is present when passed unexisting username", result.isPresent()); } catch (AuthenticationException e) { Assert.fail("unexpected error occurs: " + e.getMessage()); } }
@Test public void testWrongPassword() { try { Optional<User> result = authenticator.authenticate(new BasicCredentials(TEST_USERNAME, TEST_WRONG_SECRET_PHRASE)); Assert.assertFalse("result is present when passed wrong password", result.isPresent()); } catch (AuthenticationException e) { Assert.fail("unexpected error occurs: " + e.getMessage()); } }