繁体   English   中英

如何测试上传到 FTP 的文件的内容?

[英]How can I test the content of a file uploaded to an FTP?

我正在尝试接收上传到(柑橘类)ftp 服务器的文件,并验证上传的文件内容是否符合预期。

我设法收到了STOR消息(我的代码如下),但我不知道如何测试内容是否等于某些文本(我正在处理 xml、json 和 csv 文件)。

你能帮助我吗? 谢谢。

@Test
public class SampleFtpTest extends TestNGCitrusTestDesigner {
    @Autowired
    private FtpServer ftpServer;

    @CitrusTest
    public void testFileContent() {
        Condition waitUntilFileIsUploaded = new Condition() {
            @Override
            public String getName () {
                return "Check files on FTP";
            }

            @Override
            public boolean isSatisfied (TestContext testContext){
                return new File("target/ftp/user/anonymous").listFiles().length != 0;
            }

            @Override
            public String getSuccessMessage (TestContext testContext){
                return "Files found in FTP!";
            }

            @Override
            public String getErrorMessage (TestContext testContext){
                return "No file was found in FTP";
            }
        };

        waitFor().condition(waitUntilFileIsUploaded).seconds(1200L).interval(5000L);

        // simulate integration uploading a file...
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(20000L);
                    Runtime.getRuntime().exec("curl -T /tmp/test.txt ftp://localhost:22222 --user anonymous:secret");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        // don't care about connection related messages
        receive(ftpServer);
        receive(ftpServer);
        receive(ftpServer);
        receive(ftpServer);
        receive(ftpServer);

        // i'd like to assert that the uploaded files content is as expected
        receive(ftpServer).header("citrus_ftp_command", FTPCmd.STOR.getCommand());
    }
}

我设法通过添加一个ChannelEndpoint来测试文件内容,该ChannelEndpoint从 ftp 工作目录读取文件。 然后我可以测试文件内容这样做:

receive(fileEndpoint)
            .header("file_originalFile", "@contains('/test.txt')@")
            .payload(new ClassPathResource("ar/com/jidev/citrus/samples/expected/test.txt"));

有关更多信息,我在此处上传了完整示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM