Best Citrus code snippet using com.consol.citrus.ftp.client.SftpClient.deleteFile
Source:SftpClient.java
...66 return createDir(ftpCommand);67 } else if (ftpCommand.getSignal().equals(FTPCmd.LIST.getCommand())) {68 return listFiles(FtpMessage.list(ftpCommand.getArguments()).getPayload(ListCommand.class), context);69 } else if (ftpCommand.getSignal().equals(FTPCmd.DELE.getCommand())) {70 return deleteFile(FtpMessage.delete(ftpCommand.getArguments()).getPayload(DeleteCommand.class), context);71 } else if (ftpCommand.getSignal().equals(FTPCmd.STOR.getCommand())) {72 return storeFile(FtpMessage.put(ftpCommand.getArguments()).getPayload(PutCommand.class), context);73 } else if (ftpCommand.getSignal().equals(FTPCmd.RETR.getCommand())) {74 return retrieveFile(FtpMessage.get(ftpCommand.getArguments()).getPayload(GetCommand.class), context);75 } else {76 throw new CitrusRuntimeException(String.format("Unsupported ftp command '%s'", ftpCommand.getSignal()));77 }78 }79 /**80 * Execute mkDir command and create new directory.81 * @param ftpCommand82 * @return83 */84 protected FtpMessage createDir(CommandType ftpCommand) {85 try {86 sftp.mkdir(ftpCommand.getArguments());87 return FtpMessage.result(FTPReply.PATHNAME_CREATED, "Pathname created", true);88 } catch (SftpException e) {89 throw new CitrusRuntimeException("Failed to execute ftp command", e);90 }91 }92 @Override93 protected FtpMessage listFiles(ListCommand list, TestContext context) {94 String remoteFilePath = Optional.ofNullable(list.getTarget())95 .map(ListCommand.Target::getPath)96 .map(context::replaceDynamicContentInString)97 .orElse("");98 try {99 List<String> fileNames = new ArrayList<>();100 Vector<ChannelSftp.LsEntry> entries = sftp.ls(remoteFilePath);101 for (ChannelSftp.LsEntry entry : entries) {102 fileNames.add(entry.getFilename());103 }104 return FtpMessage.result(FTPReply.FILE_STATUS_OK, "List files complete", fileNames);105 } catch (SftpException e) {106 throw new CitrusRuntimeException(String.format("Failed to list files in path '%s'", remoteFilePath), e);107 }108 }109 @Override110 protected FtpMessage deleteFile(DeleteCommand delete, TestContext context) {111 String remoteFilePath = context.replaceDynamicContentInString(delete.getTarget().getPath());112 try {113 if (!StringUtils.hasText(remoteFilePath)) {114 return null;115 }116 if (isDirectory(remoteFilePath)) {117 sftp.cd(remoteFilePath);118 if (delete.isRecursive()) {119 Vector<ChannelSftp.LsEntry> entries = sftp.ls(".");120 List<String> excludedDirs = Arrays.asList(".", "..");121 for (ChannelSftp.LsEntry entry : entries) {122 if (!excludedDirs.contains(entry.getFilename())) {123 DeleteCommand recursiveDelete = new DeleteCommand();124 DeleteCommand.Target target = new DeleteCommand.Target();125 target.setPath(remoteFilePath + "/" + entry.getFilename());126 recursiveDelete.setTarget(target);127 recursiveDelete.setIncludeCurrent(true);128 deleteFile(recursiveDelete, context);129 }130 }131 }132 if (delete.isIncludeCurrent()) {133 // we cannot delete the current working directory, so go to root directory and delete from there134 sftp.cd("..");135 sftp.rmdir(remoteFilePath);136 }137 } else {138 sftp.rm(remoteFilePath);139 }140 } catch (SftpException e) {141 throw new CitrusRuntimeException("Failed to delete file from FTP server", e);142 }...
Source:SftpClientTest.java
...120 public void testDeleteFile() {121 FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);122 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");123 assertTrue(Paths.get(remoteFilePath).toFile().exists());124 ftpMessage = sftpClient.deleteFile(deleteCommand(remoteFilePath), context);125 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");126 Assert.assertFalse(Paths.get(remoteFilePath).toFile().exists());127 }128 @Test129 public void testDeleteGlob() {130 String remoteFilePathCopy = remoteFilePath.replace(FileUtils.FILE_EXTENSION_XML, "_copy.xml");131 FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);132 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");133 ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePathCopy), context);134 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");135 assertTrue(Paths.get(remoteFilePath).toFile().exists());136 assertTrue(Paths.get(remoteFilePathCopy).toFile().exists());137 ftpMessage = sftpClient.deleteFile(deleteCommand(targetPath + "/hello*.xml"), context);138 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");139 Assert.assertFalse(Paths.get(remoteFilePath).toFile().exists());140 Assert.assertFalse(Paths.get(remoteFilePathCopy).toFile().exists());141 }142 @Test143 public void testDeleteDirIncludeCurrent() throws Exception {144 // the following dir structure and let is delete recursively via sftp:145 // tmpDir/146 // âââ subDir147 // âââ testfile148 Path tmpDir = Paths.get(targetPath, "tmpDir");149 Path subDir = Files.createDirectories(tmpDir.resolve("subDir"));150 writeToFile("test file\n", subDir.resolve("testfile"));151 assertTrue(Files.exists(tmpDir));152 DeleteCommand deleteCommand = deleteCommand(tmpDir.toAbsolutePath().toString());153 deleteCommand.setIncludeCurrent(true);154 FtpMessage ftpMessage = sftpClient.deleteFile(deleteCommand, context);155 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");156 Assert.assertFalse(Files.exists(tmpDir));157 }158 @Test159 public void testDeleteDir() throws Exception {160 // the following dir structure and let is delete recursively via sftp:161 // tmpDir/162 // âââ subDir163 // âââ testfile164 Path tmpDir = Paths.get(targetPath, "tmpDir");165 Path subDir = Files.createDirectories(tmpDir.resolve("subDir"));166 writeToFile("test file\n", subDir.resolve("testfile"));167 assertTrue(Files.exists(tmpDir));168 FtpMessage ftpMessage = sftpClient.deleteFile(deleteCommand(tmpDir.toAbsolutePath().toString()), context);169 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");170 assertTrue(tmpDir.toFile().list().length == 0);171 assertTrue(Files.exists(tmpDir));172 }173 @Test174 public void testDeleteNoMatches() {175 // this should not throw an exception, even though no files match176 FtpMessage ftpMessage = sftpClient.deleteFile(deleteCommand(targetPath + "/1234*1234"), context);177 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");178 }179 private SshServer startSftpMockServer() throws IOException {180 // SFTP mock server without authentication181 SshServer sshd = SshServer.setUpDefaultServer();182 sshd.setPort(2223);183 ClassLoadableResourceKeyPairProvider resourceKeyPairProvider = new ClassLoadableResourceKeyPairProvider();184 resourceKeyPairProvider.setResources(Collections.singletonList("com/consol/citrus/ssh/citrus.pem"));185 sshd.setKeyPairProvider(resourceKeyPairProvider);186 sshd.setPasswordAuthenticator((username, password, session) -> true);187 List<SubsystemFactory> subsystemFactories = new ArrayList<>();188 SftpSubsystemFactory sftpSubsystemFactory = new SftpSubsystemFactory.Builder().build();189 subsystemFactories.add(sftpSubsystemFactory);190 sshd.setSubsystemFactories(subsystemFactories);...
deleteFile
Using AI Code Generation
1package com.consol.citrus.ftp.client;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;4import com.consol.citrus.testng.CitrusParameters;5import org.testng.annotations.Test;6public class DeleteFileIT extends JUnit4CitrusTestRunner {7 @CitrusParameters({"ftpServerPort"})8 public void deleteFileTest() {9 variable("ftpServerPort", "2222");10 variable("tempFilePath", "target/ftp/temp");11 variable("tempFileName", "test.txt");12 variable("tempFile", "${tempFilePath}/${tempFileName}");13 variable("remoteDir", "test");14 variable("remoteFile", "${remoteDir}/${tempFileName}");15 echo("Creating test file on local filesystem");16 create().directory("${tempFilePath}");17 create().file("${tempFile}");18 echo("Uploading test file to remote server");19 send(ftp().server("localhost")20 .port("${ftpServerPort}")21 .user("citrus")22 .password("citrus")23 .put("${tempFile}", "${remoteFile}"));24 echo("Deleting test file from remote server");25 send(ftp().server("localhost")26 .port("${ftpServerPort}")27 .user("citrus")28 .password("citrus")29 .delete("${remoteFile}"));30 echo("Delete local test file");31 delete().file("${tempFile}");32 echo("Delete local test directory");33 delete().directory("${tempFilePath}");34 }35}36package com.consol.citrus.ftp.client;37import com.consol.citrus.annotations.CitrusTest;38import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;39import com.consol.citrus.testng.CitrusParameters;40import org.testng.annotations.Test;41public class DeleteFilesIT extends JUnit4CitrusTestRunner {42 @CitrusParameters({"ftpServerPort"})43 public void deleteFilesTest() {44 variable("ftpServerPort", "2222");45 variable("tempFilePath", "target/ftp/temp");46 variable("tempFileName", "test.txt");
deleteFile
Using AI Code Generation
1package com.consol.citrus.ftp.client;2import com.consol.citrus.context.TestContext;3import com.consol.citrus.exceptions.CitrusRuntimeException;4import com.consol.citrus.testng.AbstractTestNGUnitTest;5import org.mockito.Mockito;6import org.springframework.core.io.Resource;7import org.springframework.ftp.core.FtpTemplate;8import org.springframework.ftp.core.SessionCallback;9import org.testng.Assert;10import org.testng.annotations.Test;11import java.io.IOException;12public class SftpClientTest extends AbstractTestNGUnitTest {13 private SftpClient sftpClient = new SftpClient();14 private FtpTemplate ftpTemplate = Mockito.mock(FtpTemplate.class);15 private TestContext context = Mockito.mock(TestContext.class);16 public void testDeleteFile() {17 Resource resource = Mockito.mock(Resource.class);18 Mockito.when(resource.getFilename()).thenReturn("test.txt");19 sftpClient.setFtpTemplate(ftpTemplate);20 sftpClient.deleteFile(resource, context);21 Mockito.verify(ftpTemplate, Mockito.times(1)).execute(Mockito.any(SessionCallback.class));22 }23 public void testDeleteFileFail() {24 Resource resource = Mockito.mock(Resource.class);25 Mockito.when(resource.getFilename()).thenReturn("test.txt");26 Mockito.when(ftpTemplate.execute(Mockito.any(SessionCallback.class))).thenThrow(new IOException());27 sftpClient.setFtpTemplate(ftpTemplate);28 try {29 sftpClient.deleteFile(resource, context);30 } catch (CitrusRuntimeException e) {31 Assert.assertEquals(e.getMessage(), "Failed to delete file from sftp server");32 }33 }34}
deleteFile
Using AI Code Generation
1package com.consol.citrus.ftp.client;2import com.consol.citrus.context.TestContext;3import com.consol.citrus.testng.AbstractTestNGUnitTest;4import org.mockito.Mockito;5import org.springframework.beans.factory.annotation.Autowired;6import org.springframework.beans.factory.annotation.Qualifier;7import org.springframework.context.ApplicationContext;8import org.springframework.core.io.Resource;9import org.testng.annotations.Test;10import static org.mockito.Mockito.*;11public class SftpClientTest extends AbstractTestNGUnitTest {12 @Qualifier("sftpClient")13 private SftpClient sftpClient;14 private ApplicationContext applicationContext;15 public void testDeleteFile() throws Exception {16 TestContext context = new TestContext();17 Resource resource = Mockito.mock(Resource.class);18 when(resource.getInputStream()).thenReturn(getClass().getResourceAsStream("test.txt"));19 sftpClient.deleteFile(resource, context);20 verify(sftpClient.getEndpointConfiguration().getSftpClient(), times(1)).delete("test.txt");21 }22}
deleteFile
Using AI Code Generation
1{2 public static void main(String[] args)3 {4 SftpClient client = new SftpClient();5 client.setDefaultPort(22);6 client.setHost("localhost");7 client.setUser("user");8 client.setPassword("password");9 client.connect();10 client.deleteFile("C:/test.txt");11 client.disconnect();12 }13}
deleteFile
Using AI Code Generation
1package com.consol.citrus.ftp.client;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.testng.CitrusParameters;4import com.consol.citrus.testng.spring.TestNGCitrusSpringSupport;5import org.springframework.beans.factory.annotation.Autowired;6import org.springframework.beans.factory.annotation.Qualifier;7import org.springframework.context.annotation.Bean;8import org.springframework.context.annotation.Configuration;9import org.springframework.context.annotation.Import;10import org.springframework.core.io.ClassPathResource;11import org.springframework.core.io.Resource;12import org.springframework.integration.annotation.IntegrationComponentScan;13import org.springframework.integration.annotation.MessagingGateway;14import org.springframework.integration.annotation.ServiceActivator;15import org.springframework.integration.channel.DirectChannel;16import org.springframework.integration.config.EnableIntegration;17import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;18import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;19import org.springframework.integration.sftp.session.SftpSession;20import org.springframework.messaging.MessageChannel;21import org.springframework.messaging.MessageHandler;22import org.testng.annotations.Test;23import java.io.IOException;24public class SftpClientJavaIT extends TestNGCitrusSpringSupport {25 @Qualifier("sftpClient")26 private SftpClient sftpClient;27 @CitrusParameters({"fileName", "fileContent"})28 public void sftpClientJavaIT() {29 echo("SFTP client test");30 variable("fileName", "testfile.txt");31 variable("fileContent", "Hello World!");32 echo("Creating file on SFTP server");33 sftp(template -> {34 return template.execute(session -> {35 session.write(new ClassPathResource("testfiles/testfile.txt"), "testfile.txt");36 return null;37 });38 });39 echo("Delete file on SFTP server");40 sftpClient.deleteFile("${fileName}");41 echo("Check if file was deleted on SFTP server");42 sftp(template -> {43 return template.execute(session -> {44 return session.exists("testfile.txt");45 });46 });47 echo("Check if file was deleted on SFTP server");48 sftpClient.deleteFile("${fileName}");49 echo("Check if file was deleted on SFTP server");50 sftp(template -> {51 return template.execute(session -> {52 return session.exists("testfile.txt");53 });54 });55 }56 @MessagingGateway(defaultRequestChannel = "sftpChannel
deleteFile
Using AI Code Generation
1package com.consol.citrus.samples.ftp;2import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;3import com.consol.citrus.ftp.client.SftpClient;4import org.springframework.beans.factory.annotation.Autowired;5import org.springframework.beans.factory.annotation.Qualifier;6import org.testng.annotations.Test;7public class SftpDeleteFileIT extends TestNGCitrusTestRunner {8 @Qualifier("sftpClient")9 private SftpClient sftpClient;10 public void sftpDeleteFileIT() {11 sftpClient.deleteFile("test.txt");12 }13}14package com.consol.citrus.samples.ftp;15import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;16import com.consol.citrus.ftp.client.SftpClient;17import org.springframework.beans.factory.annotation.Autowired;18import org.springframework.beans.factory.annotation.Qualifier;19import org.testng.annotations.Test;20public class SftpDeleteFileIT extends TestNGCitrusTestRunner {21 @Qualifier("sftpClient")22 private SftpClient sftpClient;23 public void sftpDeleteFileIT() {24 sftpClient.deleteFile("test.txt");25 }26}27package com.consol.citrus.samples.ftp;28import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;29import com.consol.citrus.ftp.client.SftpClient;30import org.springframework.beans.factory.annotation.Autowired;31import org.springframework.beans.factory.annotation.Qualifier;32import org.testng.annotations.Test;33public class SftpDeleteFileIT extends TestNGCitrusTestRunner {34 @Qualifier("sftpClient")35 private SftpClient sftpClient;36 public void sftpDeleteFileIT() {37 sftpClient.deleteFile("test.txt");38 }39}
deleteFile
Using AI Code Generation
1SftpClient sftpClient = new SftpClient();2FtpClient ftpClient = new FtpClient();3FtpsClient ftpsClient = new FtpsClient();4SftpClient sftpClient = new SftpClient();5FtpClient ftpClient = new FtpClient();6FtpsClient ftpsClient = new FtpsClient();7SftpClient sftpClient = new SftpClient();8FtpClient ftpClient = new FtpClient();
deleteFile
Using AI Code Generation
1public void testDeleteFile() {2 context.setVariable("fileName", "test.txt");3 run(new DeleteFileAction().client("sftpClient").remotePath("test.txt"));4}5public void testDeleteFile() {6 context.setVariable("fileName", "test.txt");7 run(new DeleteFileAction().client("sftpClient").remotePath("test.txt"));8}9public void testDeleteFile() {10 context.setVariable("fileName", "test.txt");11 run(new DeleteFileAction().client("sftpClient").remotePath("test.txt"));12}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!