SftpClient extends FtpClient { @Override protected FtpMessage deleteFile(DeleteCommand delete, TestContext context) { String remoteFilePath = context.replaceDynamicContentInString(delete.getTarget().getPath()); try { if (!StringUtils.hasText(remoteFilePath)) { return null; } if (isDirectory(remoteFilePath)) { sftp.cd(remoteFilePath); if (delete.isRecursive()) { Vector<ChannelSftp.LsEntry> entries = sftp.ls("."); List<String> excludedDirs = Arrays.asList(".", ".."); for (ChannelSftp.LsEntry entry : entries) { if (!excludedDirs.contains(entry.getFilename())) { DeleteCommand recursiveDelete = new DeleteCommand(); DeleteCommand.Target target = new DeleteCommand.Target(); target.setPath(remoteFilePath + "/" + entry.getFilename()); recursiveDelete.setTarget(target); recursiveDelete.setIncludeCurrent(true); deleteFile(recursiveDelete, context); } } } if (delete.isIncludeCurrent()) { sftp.cd(".."); sftp.rmdir(remoteFilePath); } } else { sftp.rm(remoteFilePath); } } catch (SftpException e) { throw new CitrusRuntimeException("Failed to delete file from FTP server", e); } return FtpMessage.deleteResult(FTPReply.FILE_ACTION_OK, "Delete file complete", true); } SftpClient(); protected  SftpClient(SftpEndpointConfiguration endpointConfiguration); @Override SftpEndpointConfiguration getEndpointConfiguration(); @Override void afterPropertiesSet(); @Override void destroy(); JSch getSsh(); void setSsh(JSch ssh);  }
@Test public void testDeleteFile() { FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context); verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete"); assertTrue(Paths.get(remoteFilePath).toFile().exists()); ftpMessage = sftpClient.deleteFile(deleteCommand(remoteFilePath), context); verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete"); Assert.assertFalse(Paths.get(remoteFilePath).toFile().exists()); }
@Test public void testDeleteDirIncludeCurrent() throws Exception { Path tmpDir = Paths.get(targetPath, "tmpDir"); Path subDir = Files.createDirectories(tmpDir.resolve("subDir")); writeToFile("test file\n", subDir.resolve("testfile")); assertTrue(Files.exists(tmpDir)); DeleteCommand deleteCommand = deleteCommand(tmpDir.toAbsolutePath().toString()); deleteCommand.setIncludeCurrent(true); FtpMessage ftpMessage = sftpClient.deleteFile(deleteCommand, context); verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete"); Assert.assertFalse(Files.exists(tmpDir)); }
@Test public void testDeleteDir() throws Exception { Path tmpDir = Paths.get(targetPath, "tmpDir"); Path subDir = Files.createDirectories(tmpDir.resolve("subDir")); writeToFile("test file\n", subDir.resolve("testfile")); assertTrue(Files.exists(tmpDir)); FtpMessage ftpMessage = sftpClient.deleteFile(deleteCommand(tmpDir.toAbsolutePath().toString()), context); verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete"); assertTrue(tmpDir.toFile().list().length == 0); assertTrue(Files.exists(tmpDir)); }
@Test public void testDeleteNoMatches() { FtpMessage ftpMessage = sftpClient.deleteFile(deleteCommand(targetPath + "/1234*1234"), context); verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete"); }