### Question:
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);  }### Answer:
@Test(expected = NullPointerException.class) public void testGetUserDetailsByIdThrowsNPEIfArgsNull() throws IOException { final FilesystemUserDAO dao = new FilesystemUserDAO(tmpFile()); dao.getUserCredentialsById(null); }
@Test public void testGetUserDetailsByIdReturnsEmptyOptionalForABogusUserId() throws IOException { final FilesystemUserDAO dao = new FilesystemUserDAO(tmpFile()); assertThat(dao.getUserCredentialsById(generateUserId())).isEmpty(); }
@Test public void testGetUserDetailsByIdReturnsUserDetailsFromFilesystem() throws IOException { final UserCredentials userCredentials = generateUserDetails(); final File usersFile = tmpFile(); Files.write(usersFile.toPath(), userCredentials.toUserFileLine().getBytes()); final FilesystemUserDAO dao = new FilesystemUserDAO(usersFile); final Optional<UserCredentials> maybeCredentials = dao.getUserCredentialsById(userCredentials.getId()); assertThat(maybeCredentials).isNotEmpty(); assertThat(maybeCredentials.get()).isEqualTo(userCredentials); }
@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); }