How to use retrieveFile method of com.consol.citrus.ftp.client.SftpClient class

Best Citrus code snippet using com.consol.citrus.ftp.client.SftpClient.retrieveFile

Source:SftpClient.java Github

copy

Full Screen

...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) {169 try {170 String remoteFilePath = context.replaceDynamicContentInString(command.getFile().getPath());171 String localFilePath = addFileNameToTargetPath(remoteFilePath, context.replaceDynamicContentInString(command.getTarget().getPath()));172 try (InputStream inputStream = sftp.get(remoteFilePath)) {173 byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);174 // create intermediate directories if necessary175 Path localFilePathObj = Paths.get(localFilePath);176 Files.createDirectories(localFilePathObj.getParent());177 Files.write(localFilePathObj, bytes);178 } catch (SftpException e) {179 throw new CitrusRuntimeException(String.format("Failed to get file from FTP server. Remote path: %s. Local file path: %s. Error: %s",180 remoteFilePath, localFilePath, e.getMessage()));181 }182 if (getEndpointConfiguration().isAutoReadFiles()) {...

Full Screen

Full Screen

Source:SftpClientTest.java Github

copy

Full Screen

...89 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 @Test...

Full Screen

Full Screen

retrieveFile

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.ftp.client.SftpClient;2import com.consol.citrus.ftp.message.FtpMessage;3import com.consol.citrus.ftp.message.FtpMessageType;4public class 3 {5 public static void main(String[] args) {6 SftpClient sftpClient = new SftpClient();7 sftpClient.remoteHost("localhost");8 sftpClient.port(22);9 sftpClient.username("user");10 sftpClient.password("password");11 sftpClient.connect();12 FtpMessage ftpMessage = sftpClient.retrieveFile("test.txt");13 if (ftpMessage.getType() == FtpMessageType.FILE) {14 System.out.println(ftpMessage.getPayload(String.class));15 }16 sftpClient.disconnect();17 }18}19import com.consol.citrus.ftp.client.SftpClient;20import com.consol.citrus.ftp.message.FtpMessage;21import com.consol.citrus.ftp.message.FtpMessageType;22public class 4 {23 public static void main(String[] args) {24 SftpClient sftpClient = new SftpClient();25 sftpClient.remoteHost("localhost");26 sftpClient.port(22);27 sftpClient.username("user");28 sftpClient.password("password");29 sftpClient.connect();30 FtpMessage ftpMessage = sftpClient.retrieveFile("test.txt", "newfile.txt");31 if (ftpMessage.getType() == FtpMessageType.FILE) {32 System.out.println(ftpMessage.getPayload(String.class));33 }34 sftpClient.disconnect();35 }36}37import com.consol.citrus.ftp.client.SftpClient;38import com.consol.citrus.ftp.message.FtpMessage;39import com.consol.citrus.ftp.message.FtpMessageType;40public class 5 {41 public static void main(String[] args) {42 SftpClient sftpClient = new SftpClient();43 sftpClient.remoteHost("localhost");44 sftpClient.port(22);45 sftpClient.username("user");46 sftpClient.password("password");47 sftpClient.connect();

Full Screen

Full Screen

retrieveFile

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ftp.client;2import com.consol.citrus.testng.AbstractTestNGUnitTest;3import org.testng.annotations.Test;4import org.springframework.core.io.ClassPathResource;5import org.springframework.core.io.FileSystemResource;6import org.springframework.core.io.Resource;7import static org.testng.Assert.assertEquals;8public class SftpClientTest extends AbstractTestNGUnitTest {9 public void testSftpClient() {10 SftpClient sftpClient = new SftpClient();11 sftpClient.setServerHost("localhost");12 sftpClient.setServerPort(22);13 sftpClient.setUserName("mukesh");14 sftpClient.setPassword("password");15 sftpClient.setPrivateKeyPath("src/test/resources/rsa_key");16 sftpClient.setPrivateKeyPassphrase("passphrase");17 sftpClient.setKnownHostsPath("src/test/resources/known_hosts");18 sftpClient.setStrictHostKeyChecking("no");19 sftpClient.setSftpClientFactory(new DefaultSftpClientFactory());20 sftpClient.createConnection();21 Resource resource = new ClassPathResource("com/consol/citrus/ftp/client/test.txt");22 sftpClient.send(resource);23 Resource targetResource = new FileSystemResource("target/test.txt");24 sftpClient.retrieveFile("test.txt", targetResource);25 assertEquals(targetResource.getFile().length(), resource.getFile().length());26 }27}

Full Screen

Full Screen

retrieveFile

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ftp;2import com.consol.citrus.context.TestContext;3import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;4import com.consol.citrus.exceptions.CitrusRuntimeException;5import org.springframework.context.annotation.Bean;6import org.springframework.context.annotation.Configuration;7import org.springframework.context.annotation.Import;8import org.springframework.ftp.core.FtpClient;9import org.springframework.ftp.core.FtpClientFactory;10import org.springframework.ftp.core.FtpClientTemplate;11import org.testng.annotations.Test;12import java.io.File;13import java.io.IOException;14public class SftpClientRetrieveFileIT extends TestNGCitrusTestDesigner {15 @Import({FtpServerConfig.class})16 public static class Config {17 public FtpClient ftpClient(FtpClientFactory ftpClientFactory) {18 return new FtpClientTemplate(ftpClientFactory);19 }20 }21 public void sftpClientRetrieveFileIT() {22 description("Test to retrieve a file from an SFTP server");23 variable("localFilePath", "target/test-classes/files/3.txt");24 variable("remoteFilePath", "target/test-classes/files/3.txt");25 variable("remoteDirectory", "/citrus/remote");26 variable("localDirectory", "target/test-classes/files/");27 context(SftpClient.class, "sftpClient", new TestContext())28 .host("localhost")29 .port(2222)30 .username("admin")31 .password("admin");32 echo("Creating local file ${localFilePath}");33 createFile("${localFilePath}");34 echo("Creating remote directory ${remoteDirectory}");35 createRemoteDirectory("${remoteDirectory}");36 echo("Uploading local file ${localFilePath} to remote directory ${remoteDirectory}");37 send("sftpClient")38 .put("${localFilePath}")39 .remoteDir("${remoteDirectory}");40 echo("Retrieving remote file ${remoteFilePath} from remote directory ${remoteDirectory} to local directory ${localDirectory}");41 receive("sftpClient")42 .retrieveFile("${remoteFilePath}")43 .remoteDir("${remoteDirectory}")44 .localDir("${localDirectory}");45 echo("Checking local file ${localDirectory}${remoteFilePath} exists");46 validateFileExists("${localDirectory}${remoteFilePath}", true);47 }48 private void createFile(String filePath

Full Screen

Full Screen

retrieveFile

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ftp;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import com.consol.citrus.ftp.client.SftpClient;4import com.consol.citrus.ftp.message.FtpMessageHeaders;5import org.springframework.beans.factory.annotation.Autowired;6import org.springframework.core.io.ClassPathResource;7import org.springframework.core.io.Resource;8import org.testng.annotations.Test;9import java.io.IOException;10import static com.consol.citrus.actions.EchoAction.Builder.echo;11public class SftpClientJavaITest extends TestNGCitrusTestDesigner {12 private SftpClient sftpClient;13 public void sftpClientJavaITest() throws IOException {14 send(sftpClient)15 .message()16 .body(new ClassPathResource("com/consol/citrus/ftp/SftpClientJavaITest.txt"));17 receive(sftpClient)18 .message()19 .body(new ClassPathResource("com/consol/citrus/ftp/SftpClientJavaITest.txt"));20 Resource localFile = new ClassPathResource("com/consol/citrus/ftp/SftpClientJavaITest.txt");21 receive(sftpClient)22 .message()23 .body(localFile);24 Resource localFile2 = new ClassPathResource("com/consol/citrus/ftp/SftpClientJavaITest2.txt");25 receive(sftpClient)26 .message()27 .header(FtpMessageHeaders.FILE_NAME, "SftpClientJavaITest.txt")28 .body(localFile2);29 Resource localFile3 = new ClassPathResource("com/consol/citrus/ftp/SftpClientJavaITest3.txt");30 receive(sftpClient)31 .message()32 .header(FtpMessageHeaders.FILE_NAME, "SftpClientJavaITest.txt")33 .body(localFile3);

Full Screen

Full Screen

retrieveFile

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.samples;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.springframework.ftp.core.FtpClient;7import org.springframework.ftp.core.FtpClientFactory;8import org.springframework.ftp.core.FtpClientTemplate;9import org.springframework.ftp.core.FtpException;10import org.testng.annotations.Test;11import java.io.IOException;12public class SftpClientIT extends TestNGCitrusTestDesigner {13 private FtpClientFactory ftpClientFactory;14 public void sftpClient() {15 SftpClient sftpClient = new SftpClient();16 sftpClient.setFtpClientFactory(ftpClientFactory);17 sftpClient.setRemoteDirectory("upload");18 sftpClient.setFileName("test.txt");19 sftpClient.setLocalFileName("test.txt");20 sftpClient.setLocalDirectory("upload");21 sftpClient.setFtpClient(ftpClientFactory.createFtpClient());22 sftpClient.retrieveFile();23 }24}25package com.consol.citrus.samples;26import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;27import org.springframework.beans.factory.annotation.Autowired;28import org.springframework.core.io.ClassPathResource;29import org.springframework.core.io.Resource;30import org.springframework.ftp.core.FtpClient;31import org.springframework.ftp.core.FtpClientFactory;32import org.springframework.ftp.core.FtpClientTemplate;33import org.springframework.ftp.core.FtpException;34import org.testng.annotations.Test;35import java.io.IOException;36public class SftpClientIT extends TestNGCitrusTestDesigner {37 private FtpClientFactory ftpClientFactory;38 public void sftpClient() {39 SftpClient sftpClient = new SftpClient();

Full Screen

Full Screen

retrieveFile

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ftp.client;2import java.io.File;3import org.testng.annotations.Test;4import org.testng.annotations.BeforeTest;5import org.testng.annotations.AfterTest;6import org.testng.annotations.DataProvider;7import com.consol.citrus.annotations.CitrusXmlTest;8import com.consol.citrus.testng.CitrusParameters;9public class SftpClientTest {10 @CitrusXmlTest(name = "SftpClientTest")11 @Test(dataProvider = "sftpDataProvider")12 @CitrusParameters({"context"})13 public void sftpClientTest(String context) {14 }15 @DataProvider(name = "sftpDataProvider")16 public Object[][] sftpDataProvider() {17 return new Object[][] {18 new Object[] {19 }20 };21 }22 public void beforeTest() {23 SftpClient client = new SftpClient();24 client.setHost("localhost");25 client.setPort(22);26 client.setUser("user");27 client.setPassword("password");28 client.setPrivateKeyFile(new File("src/test/resources/id_rsa"));29 client.setPrivateKeyPassphrase("passphrase");30 client.setKnownHostsFile(new File("src/test/resources/known_hosts"));31 client.setStrictHostKeyChecking("no");32 client.init();33 }34 public void afterTest() {35 SftpClient client = new SftpClient();36 client.destroy();37 }38}39package com.consol.citrus.ftp.client;40import java.io.File;41import org.testng.annotations.Test;42import org.testng.annotations.BeforeTest;43import org.testng.annotations.AfterTest;44import org.testng.annotations.DataProvider;45import com.consol.citrus.annotations.CitrusXmlTest;46import com.consol.citrus.testng.CitrusParameters;47public class SftpClientTest {48 @CitrusXmlTest(name = "SftpClientTest")49 @Test(dataProvider = "sftpDataProvider")

Full Screen

Full Screen

retrieveFile

Using AI Code Generation

copy

Full Screen

1public class 3 extends TestNGCitrusTestDesigner {2 private SftpClient sftpClient;3 public void 3() {4 variable("localDirectory", "C:/Users/Downloads");5 variable("remoteDirectory", "/home/user");6 variable("fileName", "test.txt");7 variable("fileContent", "Hello World!");8 parallel(9 sequential(10 send(sftpClient)11 .command("put")12 .localDirectory("${localDirectory}")13 .remoteDirectory("${remoteDirectory}")14 .fileName("${fileName}")15 .fileContent("${fileContent}")16 sequential(17 send(sftpClient)18 .command("get")19 .localDirectory("${localDirectory}")20 .remoteDirectory("${remoteDirectory}")21 .fileName("${fileName}")22 );23 }24}25public class 4 extends TestNGCitrusTestDesigner {26 private SftpClient sftpClient;27 public void 4() {28 variable("localDirectory", "C:/Users/Downloads");29 variable("remoteDirectory", "/home/user");30 variable("fileName", "test.txt");31 variable("fileContent", "Hello World!");32 parallel(33 sequential(34 send(sftpClient)35 .command("put")36 .localDirectory("${localDirectory}")37 .remoteDirectory("${remoteDirectory}")38 .fileName("${fileName}")39 .fileContent("${fileContent}")40 sequential(41 send(sftpClient)42 .command("get")43 .localDirectory("${localDirectory}")44 .remoteDirectory("${remoteDirectory}")45 .fileName("${fileName}")46 );47 }48}49public class 5 extends TestNGCitrusTestDesigner {50 private SftpClient sftpClient;51 public void 5() {52 variable("localDirectory", "C:/Users/Downloads");53 variable("remoteDirectory", "/home/user");54 variable("fileName", "test.txt");55 variable("fileContent", "Hello World!");56 parallel(57 sequential(58 send(sftpClient)59 .command("put")

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful