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

时间:2017-11-01 16:51:37

标签: java integration-testing citrus-framework

我正在尝试接收上传到(柑橘)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());
    }
}

1 个答案:

答案 0 :(得分:0)

我设法测试文件内容,添加ChannelEndpoint,从ftp工作目录中读取文件。然后我可以测试文件内容:

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

有关详细信息,我上传了完整示例here