@Test public void newPasswordsWrittenToNewFileIncludingEmptyLinesForExistingKeys() throws Exception { final Config config = mock(Config.class); final KeyConfiguration keyConfiguration = mock(KeyConfiguration.class); final List<KeyData> existingKeys = Arrays.asList(mock(KeyData.class), mock(KeyData.class)); when(config.getKeys()).thenReturn(keyConfiguration); when(keyConfiguration.getKeyData()).thenReturn(existingKeys); final Path pwdFile = mock(Path.class); final String path = "somepath"; when(pwdFile.toString()).thenReturn(path); final List<String> existingAndNewPasswords = new ArrayList<>(Arrays.asList("", "", "pwd1", "pwd2")); final List<char[]> newPasswords = new ArrayList<>(Arrays.asList("pwd1".toCharArray(), "pwd2".toCharArray())); writer.updateAndWrite(newPasswords, config, pwdFile); verify(filesDelegate).exists(pwdFile); verify(filesDelegate).createFile(pwdFile); verify(filesDelegate) .setPosixFilePermissions( pwdFile, Stream.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE) .collect(Collectors.toSet())); verify(filesDelegate).write(pwdFile, existingAndNewPasswords, APPEND); }
public void updateAndWrite(List<char[]> newPasswords, Config config, Path pwdDest) throws IOException { if (Optional.ofNullable(config).map(Config::getKeys).map(KeyConfiguration::getPasswords).isPresent() && !config.getKeys().getPasswords().isEmpty()) { throw new ConfigException(new RuntimeException(passwordsMessage)); } if (filesDelegate.exists(pwdDest)) { throw new FileAlreadyExistsException(pwdDest.toString()); } LOGGER.info("Writing updated passwords to {}", pwdDest); final List<String> passwords; if (Optional.ofNullable(config.getKeys()).map(KeyConfiguration::getPasswordFile).isPresent()) { passwords = filesDelegate.readAllLines(config.getKeys().getPasswordFile()); } else { LOGGER.info("No existing password file defined in config"); passwords = new ArrayList<>(); Optional.ofNullable(config.getKeys()) .map(KeyConfiguration::getKeyData) .ifPresent(k -> k.forEach(kk -> passwords.add(""))); } passwords.addAll( newPasswords.stream().map(p -> Optional.ofNullable(p) .map(String::valueOf) .orElse("")) .collect(Collectors.toList()) ); filesDelegate.createFile(pwdDest); LOGGER.info("Created empty file at {}", pwdDest); filesDelegate.setPosixFilePermissions(pwdDest, NEW_PASSWORD_FILE_PERMS); filesDelegate.write(pwdDest, passwords, APPEND); LOGGER.info("Updated passwords written to {}", pwdDest); }
PasswordFileUpdaterWriter { public void updateAndWrite(List<char[]> newPasswords, Config config, Path pwdDest) throws IOException { if (Optional.ofNullable(config).map(Config::getKeys).map(KeyConfiguration::getPasswords).isPresent() && !config.getKeys().getPasswords().isEmpty()) { throw new ConfigException(new RuntimeException(passwordsMessage)); } if (filesDelegate.exists(pwdDest)) { throw new FileAlreadyExistsException(pwdDest.toString()); } LOGGER.info("Writing updated passwords to {}", pwdDest); final List<String> passwords; if (Optional.ofNullable(config.getKeys()).map(KeyConfiguration::getPasswordFile).isPresent()) { passwords = filesDelegate.readAllLines(config.getKeys().getPasswordFile()); } else { LOGGER.info("No existing password file defined in config"); passwords = new ArrayList<>(); Optional.ofNullable(config.getKeys()) .map(KeyConfiguration::getKeyData) .ifPresent(k -> k.forEach(kk -> passwords.add(""))); } passwords.addAll( newPasswords.stream().map(p -> Optional.ofNullable(p) .map(String::valueOf) .orElse("")) .collect(Collectors.toList()) ); filesDelegate.createFile(pwdDest); LOGGER.info("Created empty file at {}", pwdDest); filesDelegate.setPosixFilePermissions(pwdDest, NEW_PASSWORD_FILE_PERMS); filesDelegate.write(pwdDest, passwords, APPEND); LOGGER.info("Updated passwords written to {}", pwdDest); } }
PasswordFileUpdaterWriter { public void updateAndWrite(List<char[]> newPasswords, Config config, Path pwdDest) throws IOException { if (Optional.ofNullable(config).map(Config::getKeys).map(KeyConfiguration::getPasswords).isPresent() && !config.getKeys().getPasswords().isEmpty()) { throw new ConfigException(new RuntimeException(passwordsMessage)); } if (filesDelegate.exists(pwdDest)) { throw new FileAlreadyExistsException(pwdDest.toString()); } LOGGER.info("Writing updated passwords to {}", pwdDest); final List<String> passwords; if (Optional.ofNullable(config.getKeys()).map(KeyConfiguration::getPasswordFile).isPresent()) { passwords = filesDelegate.readAllLines(config.getKeys().getPasswordFile()); } else { LOGGER.info("No existing password file defined in config"); passwords = new ArrayList<>(); Optional.ofNullable(config.getKeys()) .map(KeyConfiguration::getKeyData) .ifPresent(k -> k.forEach(kk -> passwords.add(""))); } passwords.addAll( newPasswords.stream().map(p -> Optional.ofNullable(p) .map(String::valueOf) .orElse("")) .collect(Collectors.toList()) ); filesDelegate.createFile(pwdDest); LOGGER.info("Created empty file at {}", pwdDest); filesDelegate.setPosixFilePermissions(pwdDest, NEW_PASSWORD_FILE_PERMS); filesDelegate.write(pwdDest, passwords, APPEND); LOGGER.info("Updated passwords written to {}", pwdDest); } PasswordFileUpdaterWriter(FilesDelegate filesDelegate); }
PasswordFileUpdaterWriter { public void updateAndWrite(List<char[]> newPasswords, Config config, Path pwdDest) throws IOException { if (Optional.ofNullable(config).map(Config::getKeys).map(KeyConfiguration::getPasswords).isPresent() && !config.getKeys().getPasswords().isEmpty()) { throw new ConfigException(new RuntimeException(passwordsMessage)); } if (filesDelegate.exists(pwdDest)) { throw new FileAlreadyExistsException(pwdDest.toString()); } LOGGER.info("Writing updated passwords to {}", pwdDest); final List<String> passwords; if (Optional.ofNullable(config.getKeys()).map(KeyConfiguration::getPasswordFile).isPresent()) { passwords = filesDelegate.readAllLines(config.getKeys().getPasswordFile()); } else { LOGGER.info("No existing password file defined in config"); passwords = new ArrayList<>(); Optional.ofNullable(config.getKeys()) .map(KeyConfiguration::getKeyData) .ifPresent(k -> k.forEach(kk -> passwords.add(""))); } passwords.addAll( newPasswords.stream().map(p -> Optional.ofNullable(p) .map(String::valueOf) .orElse("")) .collect(Collectors.toList()) ); filesDelegate.createFile(pwdDest); LOGGER.info("Created empty file at {}", pwdDest); filesDelegate.setPosixFilePermissions(pwdDest, NEW_PASSWORD_FILE_PERMS); filesDelegate.write(pwdDest, passwords, APPEND); LOGGER.info("Updated passwords written to {}", pwdDest); } PasswordFileUpdaterWriter(FilesDelegate filesDelegate); void updateAndWrite(List<char[]> newPasswords, Config config, Path pwdDest); }
PasswordFileUpdaterWriter { public void updateAndWrite(List<char[]> newPasswords, Config config, Path pwdDest) throws IOException { if (Optional.ofNullable(config).map(Config::getKeys).map(KeyConfiguration::getPasswords).isPresent() && !config.getKeys().getPasswords().isEmpty()) { throw new ConfigException(new RuntimeException(passwordsMessage)); } if (filesDelegate.exists(pwdDest)) { throw new FileAlreadyExistsException(pwdDest.toString()); } LOGGER.info("Writing updated passwords to {}", pwdDest); final List<String> passwords; if (Optional.ofNullable(config.getKeys()).map(KeyConfiguration::getPasswordFile).isPresent()) { passwords = filesDelegate.readAllLines(config.getKeys().getPasswordFile()); } else { LOGGER.info("No existing password file defined in config"); passwords = new ArrayList<>(); Optional.ofNullable(config.getKeys()) .map(KeyConfiguration::getKeyData) .ifPresent(k -> k.forEach(kk -> passwords.add(""))); } passwords.addAll( newPasswords.stream().map(p -> Optional.ofNullable(p) .map(String::valueOf) .orElse("")) .collect(Collectors.toList()) ); filesDelegate.createFile(pwdDest); LOGGER.info("Created empty file at {}", pwdDest); filesDelegate.setPosixFilePermissions(pwdDest, NEW_PASSWORD_FILE_PERMS); filesDelegate.write(pwdDest, passwords, APPEND); LOGGER.info("Updated passwords written to {}", pwdDest); } PasswordFileUpdaterWriter(FilesDelegate filesDelegate); void updateAndWrite(List<char[]> newPasswords, Config config, Path pwdDest);  }