### Question:
JsonBasedAuthenticator implements Authenticator, LoginCreator { @Override public Optional<String> authenticate(String username, String password) throws LocalLoginUnavailableException { try { Optional<Login> first = loginAccess.getLogin(username); if (first.isPresent()) { Login login = first.get(); return isCorrectPassword(password, login) ? Optional.of(login.getUserPid()) : Optional.empty(); } } catch (NoSuchAlgorithmException e) { LOG.error("Encryption algorithm can not be found.", e); throw new LocalLoginUnavailableException(e.getMessage()); } return Optional.empty(); } JsonBasedAuthenticator(LoginAccess loginAccess, String encryptionAlgorithm); @Override Optional<String> authenticate(String username, String password); @Override void createLogin(String userPid, String userName, String password, String givenName, String surname,
                          String email, String organization); Login create(String userPid, String userName, String password, String givenName, String surname, String email,
                      String organization); static final Logger LOG; }### Answer:
@Test public void authenticateReturnsNullWhenTheUsernameIsUnknown() throws Exception { Optional<String> authenticate = instance.authenticate("unknownUser", "password"); assertThat(authenticate, is(not(present()))); }
@Test public void authenticateReturnsThePidWhenTheUsernameAndPasswordAreCorrect() throws Exception { Optional<String> authenticate = instance.authenticate(KNOWN_USER, CORRECT_PASSWORD); assertThat(authenticate, is(present())); }
@Test public void authenticateReturnsNullWhenThePasswordIsIncorrect() throws Exception { Optional<String> authenticate = instance.authenticate(KNOWN_USER, "incorrectPassword"); assertThat(authenticate, is(not(present()))); }
@Test public void authenticateThrowsALocalLoginUnavailableExceptionWhenTheLoginsFileCouldBeRead() throws LocalLoginUnavailableException { JsonBasedAuthenticator instance = backedByFile(Paths.get("unavailableLoginsFile")); expectedException.expect(LocalLoginUnavailableException.class); instance.authenticate("user", "password"); }