How to use storeFile method of com.consol.citrus.ftp.client.FtpClient class

Best Citrus code snippet using com.consol.citrus.ftp.client.FtpClient.storeFile

Source:FtpClient.java Github

copy

Full Screen

...92 FtpMessage response;93 if (ftpCommand instanceof GetCommand) {94 response = retrieveFile((GetCommand) ftpCommand, context);95 } else if (ftpCommand instanceof PutCommand) {96 response = storeFile((PutCommand) ftpCommand, context);97 } else if (ftpCommand instanceof ListCommand) {98 response = listFiles((ListCommand) ftpCommand, context);99 } else if (ftpCommand instanceof DeleteCommand) {100 response = deleteFile((DeleteCommand) ftpCommand, context);101 } else {102 response = executeCommand(ftpCommand, context);103 }104 if (getEndpointConfiguration().getErrorHandlingStrategy().equals(ErrorHandlingStrategy.THROWS_EXCEPTION)) {105 if (!isPositive(response.getReplyCode())) {106 throw new CitrusRuntimeException(String.format("Failed to send FTP command - reply is: %s:%s", response.getReplyCode(), response.getReplyString()));107 }108 }109 log.info(String.format("FTP message was sent to: '%s:%s'", getEndpointConfiguration().getHost(), getEndpointConfiguration().getPort()));110 correlationManager.store(correlationKey, response);111 } catch (IOException e) {112 throw new CitrusRuntimeException("Failed to execute ftp command", e);113 }114 }115 protected FtpMessage executeCommand(CommandType ftpCommand, TestContext context) {116 try {117 int reply = ftpClient.sendCommand(ftpCommand.getSignal(), ftpCommand.getArguments());118 return FtpMessage.result(reply, ftpClient.getReplyString(), isPositive(reply));119 } catch (IOException e) {120 throw new CitrusRuntimeException("Failed to execute ftp command", e);121 }122 }123 private boolean isPositive(int reply) {124 return FTPReply.isPositiveCompletion(reply) || FTPReply.isPositivePreliminary(reply);125 }126 /**127 * Perform list files operation and provide file information as response.128 * @param list129 * @param context130 * @return131 */132 protected FtpMessage listFiles(ListCommand list, TestContext context) {133 String remoteFilePath = Optional.ofNullable(list.getTarget())134 .map(ListCommand.Target::getPath)135 .map(context::replaceDynamicContentInString)136 .orElse("");137 try {138 List<String> fileNames = new ArrayList<>();139 FTPFile[] ftpFiles;140 if (StringUtils.hasText(remoteFilePath)) {141 ftpFiles = ftpClient.listFiles(remoteFilePath);142 } else {143 ftpFiles = ftpClient.listFiles(remoteFilePath);144 }145 for (FTPFile ftpFile : ftpFiles) {146 fileNames.add(ftpFile.getName());147 }148 return FtpMessage.result(ftpClient.getReplyCode(), ftpClient.getReplyString(), fileNames);149 } catch (IOException e) {150 throw new CitrusRuntimeException(String.format("Failed to list files in path '%s'", remoteFilePath), e);151 }152 }153 /**154 * Performs delete file operation.155 * @param delete156 * @param context157 */158 protected FtpMessage deleteFile(DeleteCommand delete, TestContext context) {159 String remoteFilePath = context.replaceDynamicContentInString(delete.getTarget().getPath());160 try {161 if (!StringUtils.hasText(remoteFilePath)) {162 return null;163 }164 boolean success = true;165 if (isDirectory(remoteFilePath)) {166 if (!ftpClient.changeWorkingDirectory(remoteFilePath)) {167 throw new CitrusRuntimeException("Failed to change working directory to " + remoteFilePath + ". FTP reply code: " + ftpClient.getReplyString());168 }169 if (delete.isRecursive()) {170 FTPFile[] ftpFiles = ftpClient.listFiles();171 for (FTPFile ftpFile : ftpFiles) {172 DeleteCommand recursiveDelete = new DeleteCommand();173 DeleteCommand.Target target = new DeleteCommand.Target();174 target.setPath(remoteFilePath + "/" + ftpFile.getName());175 recursiveDelete.setTarget(target);176 recursiveDelete.setIncludeCurrent(true);177 deleteFile(recursiveDelete, context);178 }179 }180 if (delete.isIncludeCurrent()) {181 // we cannot delete the current working directory, so go to root directory and delete from there182 ftpClient.changeWorkingDirectory("/");183 success = ftpClient.removeDirectory(remoteFilePath);184 }185 } else {186 success = ftpClient.deleteFile(remoteFilePath);187 }188 if (!success) {189 throw new CitrusRuntimeException("Failed to delete path " + remoteFilePath + ". FTP reply code: " + ftpClient.getReplyString());190 }191 } catch (IOException e) {192 throw new CitrusRuntimeException("Failed to delete file from FTP server", e);193 }194 // If there was no file to delete, the ftpClient has the reply code from the previously executed195 // operation. Since we want to have a deterministic behaviour, we need to set the reply code and196 // reply string on our own!197 if (ftpClient.getReplyCode() != FILE_ACTION_OK) {198 return FtpMessage.deleteResult(FILE_ACTION_OK, String.format("%s No files to delete.", FILE_ACTION_OK), true);199 }200 return FtpMessage.deleteResult(ftpClient.getReplyCode(), ftpClient.getReplyString(), isPositive(ftpClient.getReplyCode()));201 }202 /**203 * Check file path type directory or file.204 * @param remoteFilePath205 * @return206 * @throws IOException207 */208 protected boolean isDirectory(String remoteFilePath) throws IOException {209 if (!ftpClient.changeWorkingDirectory(remoteFilePath)) { // not a directory or not accessible210 switch (ftpClient.listFiles(remoteFilePath).length) {211 case 0:212 throw new CitrusRuntimeException("Remote file path does not exist or is not accessible: " + remoteFilePath);213 case 1:214 return false;215 default:216 throw new CitrusRuntimeException("Unexpected file type result for file path: " + remoteFilePath);217 }218 } else {219 return true;220 }221 }222 /**223 * Performs store file operation.224 * @param command225 * @param context226 */227 protected FtpMessage storeFile(PutCommand command, TestContext context) {228 try {229 String localFilePath = context.replaceDynamicContentInString(command.getFile().getPath());230 String remoteFilePath = addFileNameToTargetPath(localFilePath, context.replaceDynamicContentInString(command.getTarget().getPath()));231 String dataType = context.replaceDynamicContentInString(Optional.ofNullable(command.getFile().getType()).orElse(DataType.BINARY.name()));232 try (InputStream localFileInputStream = getLocalFileInputStream(command.getFile().getPath(), dataType, context)) {233 ftpClient.setFileType(getFileType(dataType));234 if (!ftpClient.storeFile(remoteFilePath, localFileInputStream)) {235 throw new IOException("Failed to put file to FTP server. Remote path: " + remoteFilePath236 + ". Local file path: " + localFilePath + ". FTP reply: " + ftpClient.getReplyString());237 }238 }239 } catch (IOException e) {240 throw new CitrusRuntimeException("Failed to put file to FTP server", e);241 }242 return FtpMessage.putResult(ftpClient.getReplyCode(), ftpClient.getReplyString(), isPositive(ftpClient.getReplyCode()));243 }244 /**245 * Constructs local file input stream. When using ASCII data type the test variable replacement is activated otherwise246 * plain byte stream is used.247 *248 * @param path...

Full Screen

Full Screen

Source:FtpClientTest.java Github

copy

Full Screen

...126 public void testStoreFile() throws Exception {127 assertFalse(fakeFtpServer.getFileSystem().exists("/" + UPLOAD_FILE));128 Path uploadFile = Paths.get(targetPath, UPLOAD_FILE);129 Files.write(uploadFile, "Upload content\n".getBytes());130 FtpMessage ftpMessage = ftpClient.storeFile(putCommand(Paths.get(targetPath, UPLOAD_FILE).toString(), "/" + UPLOAD_FILE), context);131 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "226 Created file /upload_file.");132 assertTrue(fakeFtpServer.getFileSystem().exists("/" + UPLOAD_FILE));133 fakeFtpServer.getFileSystem().delete("/" + UPLOAD_FILE);134 }135 @Test136 public void testStoreFileImplicitFilename() throws Exception {137 assertFalse(fakeFtpServer.getFileSystem().exists("/" + UPLOAD_FILE));138 Path uploadFile = Paths.get(targetPath, UPLOAD_FILE);139 Files.write(uploadFile, "Upload content\n".getBytes());140 FtpMessage ftpMessage = ftpClient.storeFile(putCommand(Paths.get(targetPath, UPLOAD_FILE).toString(), "/"), context);141 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "226 Created file /upload_file.");142 assertTrue(fakeFtpServer.getFileSystem().exists("/" + UPLOAD_FILE));143 fakeFtpServer.getFileSystem().delete("/" + UPLOAD_FILE);144 }145 @Test146 public void testDeleteCurrentDirectory() {147 assertTrue(fakeFtpServer.getFileSystem().exists(COMPLETELY_DELETE_FOLDER));148 DeleteCommand deleteCommand = deleteCommand(COMPLETELY_DELETE_FOLDER);149 deleteCommand.setIncludeCurrent(true);150 FtpMessage ftpMessage = ftpClient.deleteFile(deleteCommand, context);151 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "250 \"/completely_delete\" removed.");152 assertFalse(fakeFtpServer.getFileSystem().exists(COMPLETELY_DELETE_FOLDER));153 }154 @Test...

Full Screen

Full Screen

storeFile

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.samples;2import com.consol.citrus.annotations.CitrusTest;3import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;4import com.consol.citrus.testng.CitrusParameters;5import org.testng.annotations.Test;6public class StoreFile extends TestNGCitrusTestRunner {7 @CitrusParameters({ "ftpServerPort", "ftpServerHost" })8 public void storeFile(String ftpServerPort, String ftpServerHost) {9 variable("ftpServerPort", ftpServerPort);10 variable("ftpServerHost", ftpServerHost);11 echo("Store file from local to remote FTP server");12 given(ftp(ftpClient())13 .client(ftpClient())14 .serverPort("${ftpServerPort}")15 .serverHost("${ftpServerHost}")16 .username("citrus")17 .password("citrus")18 .autoReadFiles(true)19 .autoCreateLocalDirectory(true)20 .autoCreateRemoteDirectory(true)21 .autoDeleteLocalFiles(true)22 .autoDeleteRemoteFiles(true)23 .autoDeleteEmptyRemoteDirectories(true));24 when(ftp().client(ftpClient())25 .send()26 .file("classpath:com/consol/citrus/samples/ftp/ftp-file.txt"));27 then(ftp().client(ftpClient())28 .receive()29 .message("FTP file transfer complete"));30 }31 public FtpClient ftpClient() {32 return new FtpClient();33 }34}35package com.consol.citrus.samples;36import com.consol.citrus.annotations.CitrusTest;37import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;38import com.consol.citrus.testng.CitrusParameters;39import org.testng.annotations.Test;40public class StoreFile extends TestNGCitrusTestRunner {41 @CitrusParameters({ "ftpServerPort", "ftpServerHost" })42 public void storeFile(String ftpServerPort, String ftpServerHost) {43 variable("ftpServerPort", ftpServerPort);44 variable("ftpServerHost", ftpServer

Full Screen

Full Screen

storeFile

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ftp.client;2import com.consol.citrus.dsl.endpoint.CitrusEndpoints;3import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;4import com.consol.citrus.ftp.client.FtpClient;5import com.consol.citrus.testng.CitrusParameters;6import org.testng.annotations.Test;7import java.io.File;8public class StoreFileTest extends TestNGCitrusTestDesigner {9 @CitrusParameters({"ftpHost", "ftpPort", "ftpUsername", "ftpPassword"})10 public void ftpStoreFile(String ftpHost, String ftpPort, String ftpUsername, String ftpPassword) {11 FtpClient ftpClient = CitrusEndpoints.ftp()12 .client()13 .host(ftpHost)14 .port(Integer.valueOf(ftpPort))15 .username(ftpUsername)16 .password(ftpPassword)17 .build();18 variable("localFile", getClass().getClassLoader().getResource("com/consol/citrus/ftp/client/test.txt").getFile());19 echo("FTP store file test");20 storeFile(ftpClient)21 .localPath("${localFile}")22 .remotePath("test.txt");23 }24}25package com.consol.citrus.ftp.client;26import com.consol.citrus.dsl.endpoint.CitrusEndpoints;27import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;28import com.consol.citrus.ftp.client.FtpClient;29import com.consol.citrus.testng.CitrusParameters;30import org.testng.annotations.Test;31import java.io.File;32public class StoreFileTest extends TestNGCitrusTestDesigner {33 @CitrusParameters({"ftpHost", "ftpPort", "ftpUsername", "ftpPassword"})34 public void ftpStoreFile(String ftpHost, String ftpPort, String ftpUsername, String ftpPassword) {35 FtpClient ftpClient = CitrusEndpoints.ftp()36 .client()37 .host(ftpHost)38 .port(Integer.valueOf(ftpPort))39 .username(ftpUsername)40 .password(ftpPassword)41 .build();42 variable("localFile", getClass().getClassLoader().getResource("com/consol/citrus/ftp/client/test.txt

Full Screen

Full Screen

storeFile

Using AI Code Generation

copy

Full Screen

1public class 3 {2 public static void main(String[] args) {3 FtpClient ftpClient = new FtpClient();4 ftpClient.setHost("localhost");5 ftpClient.setPort(21);6 ftpClient.setUsername("user");7 ftpClient.setPassword("password");8 ftpClient.setBinaryTransfer(true);9 ftpClient.setPassiveMode(true);10 ftpClient.setAutoLogin(true);11 ftpClient.setClientMode(FTPClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE);12 ftpClient.createClient();13 ftpClient.storeFile("/home/user/Desktop/test.txt", new FileSystemResource("/home/user/Desktop/test.txt"));14 }15}16public class 4 {17 public static void main(String[] args) {18 FtpClient ftpClient = new FtpClient();19 ftpClient.setHost("localhost");20 ftpClient.setPort(21);21 ftpClient.setUsername("user");22 ftpClient.setPassword("password");23 ftpClient.setBinaryTransfer(true);24 ftpClient.setPassiveMode(true);25 ftpClient.setAutoLogin(true);26 ftpClient.setClientMode(FTPClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE);27 ftpClient.createClient();28 ftpClient.retrieveFile("/home/user/Desktop/test.txt", new FileSystemResource("/home/user/Desktop/test.txt"));29 }30}31public class 5 {32 public static void main(String[] args) {33 FtpClient ftpClient = new FtpClient();34 ftpClient.setHost("localhost");35 ftpClient.setPort(21);36 ftpClient.setUsername("user");37 ftpClient.setPassword("password");38 ftpClient.setBinaryTransfer(true);39 ftpClient.setPassiveMode(true);40 ftpClient.setAutoLogin(true);41 ftpClient.setClientMode(FTPClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE);42 ftpClient.createClient();43 ftpClient.deleteFile("/home/user/Desktop/test.txt");44 }45}46public class 6 {47 public static void main(String[] args) {48 FtpClient ftpClient = new FtpClient();49 ftpClient.setHost("localhost");

Full Screen

Full Screen

storeFile

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.ftp.client;2import java.io.File;3import java.io.IOException;4import org.apache.commons.net.ftp.FTPClient;5import org.slf4j.Logger;6import org.slf4j.LoggerFactory;7import org.springframework.integration.Message;8import org.springframework.integration.support.MessageBuilder;9import org.springframework.util.Assert;10import com.consol.citrus.context.TestContext;11import com.consol.citrus.exceptions.CitrusRuntimeException;12import com.consol.citrus.message.MessageHandler;13import com.consol.citrus.message.MessageHeaders;14import com.consol.citrus.messaging.Producer;15import com.consol.citrus.util.FileUtils;16import com.consol.citrus.util.FileUtils.FileType;17import com.consol.citrus.validation.MessageValidator;18import com.consol.citrus.validation.context.ValidationContext;19public class FtpClient implements Producer, MessageHandler {20 private static Logger log = LoggerFactory.getLogger(FtpClient.class);21 private FtpClientConfiguration configuration;22 private FTPClient ftp;23 private FileType fileType = FileType.BINARY;24 private MessageValidator<? super Message<?>> messageValidator;25 private ValidationContext validationContext;26 public FtpClient(FtpClientConfiguration configuration) {27 this.configuration = configuration;28 }29 public void send(Message<?> message, TestContext context) {30 Assert.notNull(message.getPayload(), "Message payload must not be null");31 Assert.isTrue(message.getPayload() instanceof File, "Message payload must be of type File");32 File file = (File) message.getPayload();33 try {34 if (message.getHeaders().containsKey(MessageHeaders.FILE_NAME)) {35 storeFile(file, message.getHeaders().get(MessageHeaders.FILE_NAME).toString());36 } else {37 storeFile(file);38 }39 } catch (IOException e) {40 throw new CitrusRuntimeException("Failed to send file", e);41 }42 }43 public Message<?> receive(TestContext context) {44 return MessageBuilder.withPayload(FileUtils.getFileResource(configuration.getFtpServer().getHomeDirectory(), context)).build();45 }46 public void validateMessage(Message<?> receivedMessage, TestContext context) {47 if (

Full Screen

Full Screen

storeFile

Using AI Code Generation

copy

Full Screen

1public class 3 {2 public static void main(String[] args) {3 FtpClient ftpClient = new FtpClient();4 ftpClient.setServer("localhost");5 ftpClient.setPort(21);6 ftpClient.setUsername("user");7 ftpClient.setPassword("password");8 ftpClient.setBinary(true);9 ftpClient.setPassiveMode(true);10 ftpClient.setControlEncoding("UTF-8");11 ftpClient.storeFile("test.txt", new File("test.txt"));12 }13}14public class 4 {15 public static void main(String[] args) {16 FtpClient ftpClient = new FtpClient();17 ftpClient.setServer("localhost");18 ftpClient.setPort(21);19 ftpClient.setUsername("user");20 ftpClient.setPassword("password");21 ftpClient.setBinary(true);22 ftpClient.setPassiveMode(true);23 ftpClient.setControlEncoding("UTF-8");24 ftpClient.storeFile("test.txt", new File("test.txt"));25 }26}27public class 5 {28 public static void main(String[] args) {29 FtpClient ftpClient = new FtpClient();30 ftpClient.setServer("localhost");31 ftpClient.setPort(21);32 ftpClient.setUsername("user");33 ftpClient.setPassword("password");34 ftpClient.setBinary(true);35 ftpClient.setPassiveMode(true);36 ftpClient.setControlEncoding("UTF-8");37 ftpClient.storeFile("test.txt", new File("test.txt"));38 }39}40public class 6 {41 public static void main(String[] args) {42 FtpClient ftpClient = new FtpClient();43 ftpClient.setServer("localhost");44 ftpClient.setPort(21);45 ftpClient.setUsername("user");46 ftpClient.setPassword("password");47 ftpClient.setBinary(true);48 ftpClient.setPassiveMode(true);49 ftpClient.setControlEncoding("UTF-8");50 ftpClient.storeFile("test.txt", new File("test.txt"));51 }52}

Full Screen

Full Screen

storeFile

Using AI Code Generation

copy

Full Screen

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;5import java.io.IOException;6public class StoreFile extends TestNGCitrusTestDesigner {7 private FtpClient ftpClient;8 public void storeFile() throws IOException {9 ftpClient.storeFile("src/test/resources/ftp/file.txt");10 }11}12package com.consol.citrus.ftp.client;13import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;14import org.springframework.beans.factory.annotation.Autowired;15import org.testng.annotations.Test;16import java.io.IOException;17public class StoreFileWithCustomFileName extends TestNGCitrusTestDesigner {18 private FtpClient ftpClient;19 public void storeFileWithCustomFileName() throws IOException {20 ftpClient.storeFile("src/test/resources/ftp/file.txt", "custom-file-name.txt");21 }22}

Full Screen

Full Screen

storeFile

Using AI Code Generation

copy

Full Screen

1public class 3 extends TestCase {2 public void 3() {3 variable("ftpServer", "localhost");4 variable("ftpPort", "2221");5 variable("ftpUser", "user");6 variable("ftpPassword", "password");7 variable("ftpLocalFile", "src/test/resources/3.txt");8 variable("ftpRemoteFile", "3.txt");9 variable("ftpRemoteDir", "/upload");10 variable("ftpMode", "ASCII");11 variable("ftpTimeout", "5000");12 echo("FTP: Storing local file: ${ftpLocalFile} on remote server");13 echo("FTP: Remote server address: ${ftpServer}:${ftpPort}");14 echo("FTP: Remote server user: ${ftpUser}");15 echo("FTP: Remote server password: ${ftpPassword}");16 echo("FTP: Remote server directory: ${ftpRemoteDir}");17 echo("FTP: Remote server file: ${ftpRemoteFile}");18 echo("FTP: Transfer mode: ${ftpMode}");19 echo("FTP: Transfer timeout: ${ftpTimeout}");20 echo("FTP: Store file");21 http()22 .client("ftpClient")23 .send()24 .storeFile()25 .localFile("${ftpLocalFile}")26 .remoteFile("${ftpRemoteFile}")27 .remoteDirectory("${ftpRemoteDir}")28 .mode("${ftpMode}")29 .timeout("${ftpTimeout}");30 echo("FTP: Store file OK");31 }32}33public class 4 extends TestCase {34 public void 4() {35 variable("ftpServer", "localhost");36 variable("ftpPort", "2221");37 variable("ftpUser", "user");38 variable("ftpPassword", "password");39 variable("ftpLocalFile", "src/test/resources/4.txt");40 variable("ftpRemoteFile", "4.txt");41 variable("ftpRemoteDir", "/upload");42 variable("ftpMode", "ASCII");43 variable("ftpTimeout", "5000");44 echo("FTP: Retrieving remote file: ${ftpRemoteFile} from remote server");45 echo("FTP: Remote server address: ${ftpServer}:${ftpPort}");46 echo("FTP: Remote server user: ${ftpUser}");47 echo("FTP

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