Mocking SFTP, FTP, FTPS, Local File System Server in Java [closed]
Asked Answered
M

3

13

I need to test FTP/FTPS/SFTP/Local File System protocols in Java.

I need a mock server which can be used in any of these methods.

I could find a MockFTPServer. According to my understanding, it can be used only for simple FTP protocol and not for FTPS/SFTP/Local File System.

Can anybody suggest if there is any mock implementation available for a server which supports FTP/FTPS/SFTP/Local File System in Java?

Thanks,

Vijay Bhore

Mckellar answered 26/2, 2013 at 18:30 Comment(3)
There is a similar post here - #6038123. I wonder if this is what your are looking for.Autophyte
Perhaps Apache Camel? It has support for FTP/SFTP/etc... and it has many built-in testing features using mock services...Rue
Have you find an answer for this question ? It might be difficult to achieve this goal regarding the complexity of your client. At work, I wrote an abstract API with all the different implementations in order to access those different types of server in the same way, regardless the server (with some specificities due to our activities). And we had to run the tests for each protocol on real and specifics servers. It would have been nice to develop a test suite for all those by implementing a strategy pattern, for exemple, and systematically run the tests with Maven.Morez
S
11

There is Fake SFTP server rule. It is a rule/library for JUnit 4 that runs an SFTP server during test. It provides convenience method that help you to put files onto the server and get files from it.

Full Disclosure: I'm the author of Fake SFTP server rule.

Sammie answered 27/3, 2018 at 19:10 Comment(2)
Any support for TLS (for ftps)?Disbelieve
Hello @Stefan Birkner is there any chance to put it to work with Spock Framework ? If not, did you know an equivalent ? thx a lotGuthrun
M
4

The questions is a bit older, but I post my answer anyway as it might help someone else as well.

I´ve written an article on how create a mock sftp server using Testcontainers and the atmoz/sftp Docker image, which is adaptable to your other requirements as well.

The full example can be seen here

You would define your TestContainer with SFTP like that

 private static final GenericContainer sftp = new GenericContainer(
            new ImageFromDockerfile()
                    .withDockerfileFromBuilder(builder ->
                            builder
                                    .from("atmoz/sftp:latest")
                                    .run("mkdir -p /home/" + USER + "/upload; chmod -R 007 /home/" + USER)
                                    .build()))
            //.withFileSystemBind(sftpHomeDirectory.getAbsolutePath(), "/home/" + USER + REMOTE_PATH, BindMode.READ_WRITE) //uncomment to mount host directory - not required / recommended
            .withExposedPorts(PORT)
            .withCommand(USER + ":" + PASSWORD + ":1001:::upload");

When you need an FTPS server or any other protocoll, you can pick another image like this for example and adapt the container configuration. https://hub.docker.com/r/bozorgiyan/ftps-server

As for the Local File System, I´m not sure if there is anything special required. With JUnit 5 you can easily create a temporary directory like so

@TempDir
File mockFileSystemDirectory;

And you can create a util class that rewrites your paths to that directory like so:

public static File convertToFakeFileSystem(File yourFile, File fakeFileSystem) {
    return new File(fakeFileSystem.getAbsolutePath() + yourFile
            .getAbsolutePath()
            .replaceAll("C://", "/"));
}
Madson answered 30/4, 2021 at 10:50 Comment(2)
Thanks to the answer. Can I simulate SFTP with RSA key?Crosspollinate
Hey mate I followed your tutorial for testing with this and using JUnit5 and Mockito, but now im stuck at the point when I need to write to the container or sftp server, it is always giving me the error no such file or directory or when thats not the case it says permission denied, please check it out maybe you can spot what is wrong #72695112Sherborne
R
0

Yes, the best option to mock a SFTP server, is to use TestContainers, still I would like to add some comments to the previous answer.

Now we can use the image name atmoz/sftp. Also can use withCopyFileToContainer method which is very similar to the docker cp command.

// to initialize sftp server container
GenericContainer<?> sftpContainer = new GenericContainer<>(DockerImageName.parse("atmoz/sftp"))
        .withExposedPorts(22)
        .withEnv("SFTP_USERS", TEST_SFTP_USERNAME + ":" + TEST_SFTP_PASSWORD)
        .withCopyFileToContainer(
                MountableFile.forHostPath("src/test/resources/sftpFiles"),
                "/home/" + TEST_SFTP_USERNAME + "/SHOP/"
        );
sftpContainer.start();

NOTE: When uploading files using withCopyFileToContainer, we should give the container path by appending /home/<username_we_log_in_while_SSH>, as in the given example. Otherwise following exceptions may occur, when we SSH the testcontatiner to read the files.

net.schmizz.sshj.sftp.SFTPException: No such file

2 No such file

That is because, when we SSH the testcontainers it search in the path /home/<username_we_log_in_while_SSH>

We can verify if the files have been correctly uploaded in the given location, using the same testcontainer with execInContainer method to execute commands inside the testcontainer.

sftpContainer.execInContainer("ls","home/<username_we_log_in_while_SSH>").getStdout();
Rath answered 9/3, 2023 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.