BackupFilesManager { public Optional<File> rolloverBackupFileIfExists() { if (!backupFile.exists()) { logger.info("Backup file doesn't exist."); return Optional.empty(); } File timestampedBackupFile = new File(format("%s/%s-%s.dat", baseDir, FILE_NAME, clock.millis())); try { FileUtils.moveFile(backupFile, timestampedBackupFile); } catch (IOException e) { logger.error("Error while moving backup file from path {} to path {}.", backupFile.getAbsolutePath(), timestampedBackupFile.getAbsolutePath(), e); return Optional.empty(); } return Optional.of(timestampedBackupFile); } BackupFilesManager(String baseDir, Clock clock); File getCurrentBackupFile(); Optional<File> rolloverBackupFileIfExists(); List<File> getTemporaryBackupV2Files(String temporaryDir); void delete(File file); List<File> getRolledBackupFiles();  }
@Test public void shouldRolloverExistingBackupFile() throws IOException { new File(tempDir, "hermes-buffer-v3.dat").createNewFile(); BackupFilesManager backupFilesManager = new BackupFilesManager(tempDir.getAbsolutePath(), clock); Optional<File> backupFile = backupFilesManager.rolloverBackupFileIfExists(); assertThat(backupFile.get().getName()).isEqualTo("hermes-buffer-v3-12345.dat"); }
@Test public void shouldNotRolloverNotExistsBackupFile() { BackupFilesManager backupFilesManager = new BackupFilesManager(tempDir.getAbsolutePath(), clock); Optional<File> backupFile = backupFilesManager.rolloverBackupFileIfExists(); assertThat(backupFile.isPresent()).isFalse(); }