Best Citrus code snippet using com.consol.citrus.ftp.client.SftpClient.storeFile
Source:SftpClient.java  
...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        }143        return FtpMessage.deleteResult(FTPReply.FILE_ACTION_OK, "Delete file complete", true);144    }145    @Override146    protected boolean isDirectory(String remoteFilePath) {147        try {148            return !remoteFilePath.contains("*") && sftp.stat(remoteFilePath).isDir();149        } catch (SftpException e) {150            throw new CitrusRuntimeException("Failed to check file state", e);151        }152    }153    @Override154    protected FtpMessage storeFile(PutCommand command, TestContext context) {155        try {156            String localFilePath = context.replaceDynamicContentInString(command.getFile().getPath());157            String remoteFilePath = addFileNameToTargetPath(localFilePath, context.replaceDynamicContentInString(command.getTarget().getPath()));158            String dataType = context.replaceDynamicContentInString(Optional.ofNullable(command.getFile().getType()).orElse(DataType.BINARY.name()));159            try (InputStream localFileInputStream = getLocalFileInputStream(command.getFile().getPath(), dataType, context)) {160                sftp.put(localFileInputStream, remoteFilePath);161            }162        } catch (IOException | SftpException e) {163            throw new CitrusRuntimeException("Failed to put file to FTP server", e);164        }165        return FtpMessage.putResult(FTPReply.CLOSING_DATA_CONNECTION, "Transfer complete", true);166    }167    @Override168    protected FtpMessage retrieveFile(GetCommand command, TestContext context) {...Source:SftpClientTest.java  
...71    }72    @Test73    public void testListFiles() {74        String remoteFilePath = targetPath + "/file1";75        FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);76        verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");77        assertTrue(Paths.get(remoteFilePath).toFile().exists());78        remoteFilePath = targetPath + "/file2";79        ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);80        verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");81        assertTrue(Paths.get(remoteFilePath).toFile().exists());82        ftpMessage = sftpClient.listFiles(listCommand(targetPath + "/file*"), context);83        verifyMessage(ftpMessage, ListCommandResult.class, FILE_STATUS_OK,84                "List files complete", Arrays.asList("file1", "file2"));85        assertTrue(Paths.get(targetPath + "/file1").toFile().exists());86        assertTrue(Paths.get(targetPath + "/file2").toFile().exists());87    }88    @Test89    public void testRetrieveFile() {90        FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);91        verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");92        assertTrue(Paths.get(remoteFilePath).toFile().exists());93        FtpMessage response = sftpClient.retrieveFile(getCommand(remoteFilePath), context);94        verifyMessage(response, GetCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");95        Assert.assertEquals(response.getPayload(GetCommandResult.class).getFile().getData(), inputFileAsString);96    }97    @Test98    public void testRetrieveFileToLocalPath() throws Exception {99        Path localDownloadFilePath = Paths.get(targetPath, "local_download.xml");100        FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);101        verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");102        assertTrue(Paths.get(remoteFilePath).toFile().exists());103        ftpMessage = sftpClient.retrieveFile(getCommand(remoteFilePath, localDownloadFilePath.toString()), context);104        verifyMessage(ftpMessage, GetCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");105        Assert.assertEquals(inputFileAsString,106                new String(Files.readAllBytes(localDownloadFilePath), "UTF-8"));107    }108    @Test109    public void testRetrieveFileToLocalPathWithoutFilename() throws Exception {110        Path localDownloadFilePath = Paths.get(targetPath, "local_download.xml");111        FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, targetPath + "/"), context);112        verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");113        assertTrue(Paths.get(remoteFilePath).toFile().exists());114        ftpMessage = sftpClient.retrieveFile(getCommand(remoteFilePath, localDownloadFilePath.toString()), context);115        verifyMessage(ftpMessage, GetCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");116        Assert.assertEquals(inputFileAsString,117                new String(Files.readAllBytes(localDownloadFilePath), "UTF-8"));118    }119    @Test120    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        //     âââ testfile...storeFile
Using AI Code Generation
1package com.consol.citrus.ftp.client;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import org.springframework.beans.factory.annotation.Autowired;4import org.springframework.core.io.ClassPathResource;5import org.springframework.core.io.Resource;6import org.testng.annotations.Test;7import java.io.IOException;8public class StoreFile extends TestNGCitrusTestDesigner {9    private SftpClient sftpClient;10    public void storeFile() throws IOException {11        Resource localFile = new ClassPathResource("testfile.txt");12        sftpClient.storeFile(localFile, "testfile.txt");13    }14}15package com.consol.citrus.ftp.client;16import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;17import org.springframework.beans.factory.annotation.Autowired;18import org.springframework.core.io.ClassPathResource;19import org.springframework.core.io.Resource;20import org.testng.annotations.Test;21import java.io.IOException;22public class RetrieveFile extends TestNGCitrusTestDesigner {23    private SftpClient sftpClient;24    public void retrieveFile() throws IOException {25        Resource localFile = new ClassPathResource("testfile.txt");26        sftpClient.retrieveFile("testfile.txt", localFile);27    }28}29package com.consol.citrus.ftp.client;30import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;31import org.springframework.beans.factory.annotation.Autowired;32import org.springframework.core.io.ClassPathResource;33import org.springframework.core.io.Resource;34import org.testng.annotations.Test;35import java.io.IOException;36public class DeleteFile extends TestNGCitrusTestDesigner {37    private SftpClient sftpClient;38    public void deleteFile() throws IOException {39        sftpClient.deleteFile("testfile.txt");40    }41}42package com.consol.citrus.ftp.client;43import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;44import org.springframework.beans.factory.annotation.Autowired;storeFile
Using AI Code Generation
1package com.consol.citrus.ftp.client;2import org.testng.annotations.Test;3import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;4import com.consol.citrus.ftp.client.SftpClient;5public class StoreFile extends TestNGCitrusTestDesigner {6	public void storeFile() {7		SftpClient sftpClient = new SftpClient();8		sftpClient.setHost("localhost");9		sftpClient.setPort(22);10		sftpClient.setUsername("user");11		sftpClient.setPassword("password");12		sftpClient.setRemoteDir("/home/user");13		sftpClient.setLocalDir("/home/user");14		sftpClient.setFilename("file.txt");15		sftpClient.storeFile();16	}17}18package com.consol.citrus.ftp.client;19import org.testng.annotations.Test;20import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;21import com.consol.citrus.ftp.client.SftpClient;22public class StoreFile extends TestNGCitrusTestDesigner {23	public void storeFile() {24		SftpClient sftpClient = new SftpClient();25		sftpClient.setHost("localhost");26		sftpClient.setPort(22);27		sftpClient.setUsername("user");28		sftpClient.setPassword("password");29		sftpClient.setRemoteDir("/home/user");30		sftpClient.setLocalDir("/home/user");31		sftpClient.setFilename("file.txt");32		sftpClient.setCharset("UTF-8");storeFile
Using AI Code Generation
1package com.consol.citrus.ftp.client;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import org.springframework.beans.factory.annotation.Autowired;4import org.testng.annotations.Test;5public class storeFile extends TestNGCitrusTestDesigner {6private SftpClient sftpClient;7public void storeFile() {8sftpClient.storeFile("localFile", "remoteFile");9}10}11package com.consol.citrus.ftp.client;12import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;13import org.springframework.beans.factory.annotation.Autowired;14import org.testng.annotations.Test;15public class storeFile extends TestNGCitrusTestDesigner {16private SftpClient sftpClient;17public void storeFile() {18sftpClient.storeFile("localFile", "remoteFile", true);19}20}21package com.consol.citrus.ftp.client;22import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;23import org.springframework.beans.factory.annotation.Autowired;24import org.testng.annotations.Test;25public class storeFile extends TestNGCitrusTestDesigner {26private SftpClient sftpClient;27public void storeFile() {28sftpClient.storeFile("localFile", "remoteFile", true, "UTF-8");29}30}31package com.consol.citrus.ftp.client;32import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;33import org.springframework.beans.factory.annotation.Autowired;34import org.testng.annotations.Test;35public class storeFile extends TestNGCitrusTestDesigner {36private SftpClient sftpClient;37public void storeFile() {38sftpClient.storeFile("localFile", "remoteFile", true, "UTF-8", 10000L);39}40}41package com.consol.citrus.ftp.client;42import com.consol.citstoreFile
Using AI Code Generation
1package com.consol.citrus.ftp.client;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;4import com.consol.citrus.testng.CitrusParameters;5import org.springframework.beans.factory.annotation.Autowired;6import org.springframework.core.io.ClassPathResource;7import org.springframework.core.io.Resource;8import org.testng.annotations.Test;9public class SftpClientStoreFileIT extends TestNGCitrusTestRunner {10    private SftpClient sftpClient;11    @CitrusParameters({"localFile", "remoteFile"})12    public void sftpClientStoreFileIT() {13        Resource localFile = new ClassPathResource("com/consol/citrus/ftp/client/sftp-client-store-file-it.txt");14        String remoteFile = "sftp-client-store-file-it.txt";15        sftpClient.storeFile(localFile, remoteFile);16    }17}18package com.consol.citrus.ftp.client;19import com.consol.citrus.annotations.CitrusTest;20import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;21import com.consol.citrus.testng.CitrusParameters;22import org.springframework.beans.factory.annotation.Autowired;23import org.springframework.core.io.ClassPathResource;24import org.springframework.core.io.Resource;25import org.testng.annotations.Test;26public class SftpClientStoreFileIT extends TestNGCitrusTestRunner {27    private SftpClient sftpClient;28    @CitrusParameters({"localFile", "remoteFile"})29    public void sftpClientStoreFileIT() {30        Resource localFile = new ClassPathResource("com/consol/citrus/ftp/client/sftp-client-store-file-it.txt");31        String remoteFile = "sftp-client-store-file-it.txt";32        sftpClient.storeFile(localFile, remoteFile);33    }34}storeFile
Using AI Code Generation
1SftpClient sftpClient = new SftpClient();2sftpClient.setUsername("user");3sftpClient.setPassword("password");4sftpClient.setPrivateKeyPath("/path/to/private/key");5sftpClient.setPrivateKeyPassphrase("passphrase");6sftpClient.setKnownHostsPath("/path/to/known/hosts");7sftpClient.setKnownHostsResourcePath("/path/to/known/hosts/resource");8sftpClient.setStrictHostKeyChecking("no");9sftpClient.setConnectTimeout(5000);10sftpClient.setSessionTimeout(10000);11sftpClient.setClientVersion("2");12sftpClient.setClientAliveInterval(5000);13sftpClient.setClientAliveCountMax(5);14sftpClient.setCompression("yes");15sftpClient.setCompressionLevel(6);16sftpClient.setMaxPacketSize(2048);17sftpClient.setPortForwardingL("localhost:2222:localhost:2222");18sftpClient.setPortForwardingR("localhost:2222:localhost:2222");19sftpClient.setPreferredAuthentications("publickey,password");20sftpClient.setProxyHost("localhost");21sftpClient.setProxyPort(2222);22sftpClient.setProxyUser("user");23sftpClient.setProxyPassword("password");24sftpClient.setProxyType("http");25sftpClient.setProxyCommand("ssh -q -W %h:%p gateway.example.com");26sftpClient.setServerAliveInterval(5000);27sftpClient.setServerAliveCountMax(5);28sftpClient.setStrictHostKeyChecking("no");29sftpClient.setSendEnv("LANG en_US");30sftpClient.setSendEnv("LC_CTYPE en_US");31sftpClient.setSendEnv("LC_ALL en_US");32sftpClient.setSendEnv("LC_MESSAGES en_US");33sftpClient.setSendEnv("LC_COLLATE en_US");34sftpClient.setSendEnv("LC_TIME en_US");35sftpClient.setSendEnv("LC_NUMERIC en_US");36sftpClient.setSendEnv("LC_MONETARY en_US");37sftpClient.setSendEnv("LC_PAPER en_US");38sftpClient.setSendEnv("LC_NAME en_US");39sftpClient.setSendEnv("LC_ADDRESS en_US");40sftpClient.setSendEnv("LC_TELEPHONE en_US");41sftpClient.setSendEnv("LC_MEASUREMENTstoreFile
Using AI Code Generation
1package com.consol.citrus.ftp.samples;2import org.springframework.context.support.ClassPathXmlApplicationContext;3import org.testng.annotations.Test;4import com.consol.citrus.ftp.client.SftpClient;5public class SftpClientStoreFileTest {6    public void sftpClientStoreFileTest() {7        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/consol/citrus/ftp/samples/sftpClientStoreFileTest.xml");8        SftpClient sftpClient = context.getBean("sftpClient", SftpClient.class);9        sftpClient.storeFile("src/test/resources/test.txt", "test.txt");10    }11}12The SftpClient bean configured in sftpClientStoreFileTest.xml file is used to store a file on the SFTP server. The storeFile() method of SftpClient class is used to store a file on FTP server. The storeFile() method has two parameters:storeFile
Using AI Code Generation
1import com.consol.citrus.dsl.runner.TestRunner;2import com.consol.citrus.dsl.testng.TestNGCitrusTest;3import org.springframework.context.annotation.Bean;4import org.springframework.context.annotation.Import;5import org.springframework.core.io.ClassPathResource;6import org.testng.annotations.Test;7public class 3 extends TestNGCitrusTest {8    public void 3() {9        TestRunner runner = citrus.createTestRunner();10        runner.sftp(action -> action.client("sftpClient")11                .send()12                .storeFile(new ClassPathResource("test.txt"), "/test.txt")13        );14    }15    public SftpClient sftpClient() {16        return CitrusEndpoints.sftp()17                .host("localhost")18                .port(2222)19                .autoCreateLocalDirectory(true)20                .username("sftpuser")21                .password("sftppassword")22                .build();23    }24}25import com.consol.citrus.dsl.runner.TestRunner;26import com.consol.citrus.dsl.testng.TestNGCitrusTest;27import org.springframework.context.annotation.Bean;28import org.springframework.context.annotation.Import;29import org.springframework.core.io.ClassPathResource;30import org.testng.annotations.Test;31public class 4 extends TestNGCitrusTest {32    public void 4() {33        TestRunner runner = citrus.createTestRunner();34        runner.sftp(action -> action.client("sftpClient")35                .send()36                .storeFile(new ClassPathResource("test.txt"), "/test.txt")37        );38    }39    public SftpClient sftpClient() {40        return CitrusEndpoints.sftp()41                .host("localhost")42                .port(2222)43                .autoCreateLocalDirectory(true)44                .username("sftpuser")45                .password("sftppassword")46                .build();47    }48}49import com.consol.citrus.dsl.runner.TestRunner;50import com.consol.citrus.dsl.testng.TestNGCitrusTest;51import orgstoreFile
Using AI Code Generation
1public class 3 {2    private SftpClient sftpClient;3    public void test() throws IOException {4        sftpClient.storeFile("src/test/resources/3.txt", "3.txt");5    }6}7public class 4 {8    private SftpClient sftpClient;9    public void test() throws IOException {10        sftpClient.retrieveFile("4.txt", "src/test/resources/4.txt");11    }12}13public class 5 {14    private SftpClient sftpClient;15    public void test() {16        sftpClient.deleteFile("5.txt");17    }18}19public class 6 {20    private SftpClient sftpClient;21    public void test() {22        sftpClient.deleteDirectory("6");23    }24}25public class 7 {26    private SftpClient sftpClient;27    public void test() {28        sftpClient.createDirectory("7");29    }30}31public class 8 {32    private SftpClient sftpClient;33    public void test() {34        sftpClient.renameFile("8.txt", "8_new.txt");35    }36}37public class 9 {38    private SftpClient sftpClient;39    public void test() {40        Collection<String> fileNames = sftpClient.listFiles("9");41        for (String fileName : fileNames) {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!!
