### Question:
JsonBasedAuthenticator implements Authenticator, LoginCreator { @Override public void createLogin(String userPid, String userName, String password, String givenName, String surname, String email, String organization) throws LoginCreationException { try { Login login = create(userPid, userName, password, givenName, surname, email, organization); loginAccess.addLogin(login); } catch (NoSuchAlgorithmException e) { LOG.error("Encryption algorithm can not be found.", e); throw new LoginCreationException(e.getMessage()); } } 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 createLoginIgnoresTheAdditionOfLoginOfAKnownUserPid() throws Exception { Login[] logins = new Login[0]; Path emptyLoginsFile = FileHelpers.makeTempFilePath(true); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValue(emptyLoginsFile.toFile(), logins); JsonBasedAuthenticator instance = backedByFile(emptyLoginsFile); String userPid = "userPid"; instance.createLogin(userPid, "userName", "password", "givenName", "surname", "email", "org"); instance.createLogin(userPid, "userName1", "password1", "givenName1", "surname1", "email1", "org1"); List<Login> loginList = objectMapper.readValue(emptyLoginsFile.toFile(), new TypeReference<List<Login>>() { }); long count = loginList.stream().filter(login -> Objects.equals(login.getUserPid(), userPid)).count(); assertThat(count, is(1L)); Files.delete(emptyLoginsFile); }
@Test public void createLoginIgnoresTheAdditionOfLoginOfAKnownUserName() throws Exception { Login[] logins = new Login[0]; Path emptyLoginsFile = FileHelpers.makeTempFilePath(true); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValue(emptyLoginsFile.toFile(), logins); JsonBasedAuthenticator instance = backedByFile(emptyLoginsFile); String userName = "userName"; instance.createLogin("userPid", userName, "password", "givenName", "surname", "email", "org"); instance.createLogin("userPid2", userName, "password1", "givenName2", "surname2", "email2", "org2"); List<Login> loginList = objectMapper.readValue(emptyLoginsFile.toFile(), new TypeReference<List<Login>>() { }); long count = loginList.stream().filter(login -> Objects.equals(login.getUsername(), userName)).count(); assertThat(count, is(1L)); Files.delete(emptyLoginsFile); }
@Test(expected = LoginCreationException.class) public void createLoginThrowsLoginCreationExceptionWhenTheLoginsFileCannotBeRead() throws Exception { Path pathToNonExistingFile = FileHelpers.makeTempFilePath(false); JsonBasedAuthenticator instance = backedByFile(pathToNonExistingFile); instance.createLogin("userPid", "userName", "password", "givenName", "surname", "email", "org"); }