Best Citrus code snippet using com.consol.citrus.ftp.client.SftpClientTest.startSftpMockServer
Source:SftpClientTest.java
...49 targetPath = System.getProperty("project.build.directory");50 localFilePath = "classpath:ftp/input/hello.xml";51 remoteFilePath = targetPath + "/hello.xml";52 inputFileAsString = FileUtils.readToString(new ClassPathResource("ftp/input/hello.xml"), StandardCharsets.UTF_8);53 sshServer = startSftpMockServer();54 sftpClient = createSftpClient();55 }56 @AfterClass57 public void tearDown() throws Exception {58 sftpClient.destroy();59 sshServer.close();60 }61 @Test62 public void testListFiles() {63 String remoteFilePath = targetPath + "/file1";64 FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);65 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");66 assertTrue(Paths.get(remoteFilePath).toFile().exists());67 remoteFilePath = targetPath + "/file2";68 ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);69 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");70 assertTrue(Paths.get(remoteFilePath).toFile().exists());71 ftpMessage = sftpClient.listFiles(listCommand(targetPath + "/file*"), context);72 verifyMessage(ftpMessage, ListCommandResult.class, FILE_STATUS_OK,73 "List files complete", Arrays.asList("file1", "file2"));74 assertTrue(Paths.get(targetPath + "/file1").toFile().exists());75 assertTrue(Paths.get(targetPath + "/file2").toFile().exists());76 }77 @Test78 public void testRetrieveFile() {79 FtpMessage 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 response = sftpClient.retrieveFile(getCommand(remoteFilePath), context);83 verifyMessage(response, GetCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");84 Assert.assertEquals(response.getPayload(GetCommandResult.class).getFile().getData(), inputFileAsString);85 }86 @Test87 public void testRetrieveFileToLocalPath() throws Exception {88 Path localDownloadFilePath = Paths.get(targetPath, "local_download.xml");89 FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);90 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");91 assertTrue(Paths.get(remoteFilePath).toFile().exists());92 ftpMessage = sftpClient.retrieveFile(getCommand(remoteFilePath, localDownloadFilePath.toString()), context);93 verifyMessage(ftpMessage, GetCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");94 Assert.assertEquals(inputFileAsString,95 new String(Files.readAllBytes(localDownloadFilePath), "UTF-8"));96 }97 @Test98 public void testRetrieveFileToLocalPathWithoutFilename() throws Exception {99 Path localDownloadFilePath = Paths.get(targetPath, "local_download.xml");100 FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, targetPath + "/"), 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 testDeleteFile() {110 FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);111 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");112 assertTrue(Paths.get(remoteFilePath).toFile().exists());113 ftpMessage = sftpClient.deleteFile(deleteCommand(remoteFilePath), context);114 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");115 Assert.assertFalse(Paths.get(remoteFilePath).toFile().exists());116 }117 @Test118 public void testDeleteGlob() {119 String remoteFilePathCopy = remoteFilePath.replace(".xml", "_copy.xml");120 FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context);121 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");122 ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePathCopy), context);123 verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete");124 assertTrue(Paths.get(remoteFilePath).toFile().exists());125 assertTrue(Paths.get(remoteFilePathCopy).toFile().exists());126 ftpMessage = sftpClient.deleteFile(deleteCommand(targetPath + "/hello*.xml"), context);127 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");128 Assert.assertFalse(Paths.get(remoteFilePath).toFile().exists());129 Assert.assertFalse(Paths.get(remoteFilePathCopy).toFile().exists());130 }131 @Test132 public void testDeleteDirIncludeCurrent() throws Exception {133 // the following dir structure and let is delete recursively via sftp:134 // tmpDir/135 // âââ subDir136 // âââ testfile137 Path tmpDir = Paths.get(targetPath, "tmpDir");138 Path subDir = Files.createDirectories(tmpDir.resolve("subDir"));139 writeToFile("test file\n", subDir.resolve("testfile"));140 assertTrue(Files.exists(tmpDir));141 DeleteCommand deleteCommand = deleteCommand(tmpDir.toAbsolutePath().toString());142 deleteCommand.setIncludeCurrent(true);143 FtpMessage ftpMessage = sftpClient.deleteFile(deleteCommand, context);144 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");145 Assert.assertFalse(Files.exists(tmpDir));146 }147 @Test148 public void testDeleteDir() throws Exception {149 // the following dir structure and let is delete recursively via sftp:150 // tmpDir/151 // âââ subDir152 // âââ testfile153 Path tmpDir = Paths.get(targetPath, "tmpDir");154 Path subDir = Files.createDirectories(tmpDir.resolve("subDir"));155 writeToFile("test file\n", subDir.resolve("testfile"));156 assertTrue(Files.exists(tmpDir));157 FtpMessage ftpMessage = sftpClient.deleteFile(deleteCommand(tmpDir.toAbsolutePath().toString()), context);158 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");159 assertTrue(tmpDir.toFile().list().length == 0);160 assertTrue(Files.exists(tmpDir));161 }162 @Test163 public void testDeleteNoMatches() {164 // this should not throw an exception, even though no files match165 FtpMessage ftpMessage = sftpClient.deleteFile(deleteCommand(targetPath + "/1234*1234"), context);166 verifyMessage(ftpMessage, DeleteCommandResult.class, FILE_ACTION_OK, "Delete file complete");167 }168 private SshServer startSftpMockServer() throws IOException {169 // SFTP mock server without authentication170 SshServer sshd = SshServer.setUpDefaultServer();171 sshd.setPort(2223);172 ClassLoadableResourceKeyPairProvider resourceKeyPairProvider = new ClassLoadableResourceKeyPairProvider();173 resourceKeyPairProvider.setResources(Collections.singletonList("com/consol/citrus/ssh/citrus.pem"));174 sshd.setKeyPairProvider(resourceKeyPairProvider);175 sshd.setPasswordAuthenticator((username, password, session) -> true);176 ArrayList<NamedFactory<Command>> subsystemFactories = new ArrayList<>();177 SftpSubsystemFactory sftpSubsystemFactory = new SftpSubsystemFactory.Builder().build();178 subsystemFactories.add(sftpSubsystemFactory);179 sshd.setSubsystemFactories(subsystemFactories);180 sshd.start();181 return sshd;182 }...
startSftpMockServer
Using AI Code Generation
1package com.consol.citrus.ftp;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.Import;9import org.springframework.context.annotation.PropertySource;10import org.springframework.core.io.ClassPathResource;11import org.springframework.ftp.core.FtpTemplate;12import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;13import org.springframework.test.context.ContextConfiguration;14import org.springframework.test.context.TestPropertySource;15import org.testng.annotations.Test;16import static com.consol.citrus.actions.CreateVariablesAction.Builder.createVariable;17import static com.consol.citrus.actions.ExecutePLSQLAction.Builder.executePLSQL;18import static com.consol.citrus.actions.ExecuteSQLQueryAction.Builder.executeSQLQuery;19import static com.consol.citrus.actions.ExecuteSQLUpdateAction.Builder.executeSQLUpdate;20import static com.consol.citrus.actions.SendMessageAction.Builder.withMessage;21import static com.consol.citrus.actions.StopTimeAction.Builder.stopTime;22import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimer;23import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimerAction;24import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimerActionBuilder;25import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimerBuilder;26import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimerBuilderAction;27import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimerBuilderActionBuilder;28import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimerBuilderBuilder;29import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimerBuilderBuilderAction;30import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimerBuilderBuilderActionBuilder;31import static com.consol.citrus.actions.StopTimeAction.Builder.stopTimerBuilderBuilderBuilder;32import static com.consol.citrus.actions.StopTimeAction.Builder.stop
startSftpMockServer
Using AI Code Generation
1public void testSftpMockServer() {2 SftpClient sftpClient = new SftpClient();3 sftpClient.setHost("localhost");4 sftpClient.setPort(2222);5 sftpClient.setUser("scott");6 sftpClient.setPassword("tiger");7 sftpClient.connect();8 sftpClient.sendCommand("mkdir /home/scott/citrus");9 sftpClient.sendCommand("put /home/scott/citrus-test.txt /home/scott/citrus/citrus-test.txt");10 sftpClient.sendCommand("get /home/scott/citrus/citrus-test.txt /home/scott/citrus-test.txt");11 sftpClient.sendCommand("rm /home/scott/citrus/citrus-test.txt");12 sftpClient.sendCommand("rm /home/scott/citrus");13 sftpClient.disconnect();14}
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!!