@Test public void testGetUserDetailsByIdReturnsUserDetailsEvenWhenFileContainsBlankLines() throws IOException { final UserCredentials userCredentials = generateUserDetails(); final File usersFile = tmpFile(); final String userLine = userCredentials.toUserFileLine(); final String fileContent = "#somecomment\n\n\n" + userLine + "\n#anothercomment\n"; Files.write(usersFile.toPath(), fileContent.getBytes()); final FilesystemUserDAO dao = new FilesystemUserDAO(usersFile); final Optional<UserCredentials> maybeCredentials = dao.getUserCredentialsById(userCredentials.getId()); assertThat(maybeCredentials).isNotEmpty(); assertThat(maybeCredentials.get()).isEqualTo(userCredentials); }
@Override public Optional<UserCredentials> getUserCredentialsById(UserId id) { requireNonNull(id); try { return readUserCredentials() .filter(c -> c.getId().equals(id)) .findFirst(); } catch (IOException e) { throw new RuntimeException(e); } }
FilesystemUserDAO implements UserDAO { @Override public Optional<UserCredentials> getUserCredentialsById(UserId id) { requireNonNull(id); try { return readUserCredentials() .filter(c -> c.getId().equals(id)) .findFirst(); } catch (IOException e) { throw new RuntimeException(e); } } }
FilesystemUserDAO implements UserDAO { @Override public Optional<UserCredentials> getUserCredentialsById(UserId id) { requireNonNull(id); try { return readUserCredentials() .filter(c -> c.getId().equals(id)) .findFirst(); } catch (IOException e) { throw new RuntimeException(e); } } FilesystemUserDAO(File usersFile); }
FilesystemUserDAO implements UserDAO { @Override public Optional<UserCredentials> getUserCredentialsById(UserId id) { requireNonNull(id); try { return readUserCredentials() .filter(c -> c.getId().equals(id)) .findFirst(); } catch (IOException e) { throw new RuntimeException(e); } } FilesystemUserDAO(File usersFile); @Override Optional<UserCredentials> getUserCredentialsById(UserId id); @Override boolean addNewUser(UserId id, String authName, String authField); @Override boolean updateUserAuth(UserId id, String authName, String authField); }
FilesystemUserDAO implements UserDAO { @Override public Optional<UserCredentials> getUserCredentialsById(UserId id) { requireNonNull(id); try { return readUserCredentials() .filter(c -> c.getId().equals(id)) .findFirst(); } catch (IOException e) { throw new RuntimeException(e); } } FilesystemUserDAO(File usersFile); @Override Optional<UserCredentials> getUserCredentialsById(UserId id); @Override boolean addNewUser(UserId id, String authName, String authField); @Override boolean updateUserAuth(UserId id, String authName, String authField);  }