Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a "FTPS" Mock Server to unit test File Transfer in Java

I have a CreateFTPConnection class which create a FTPS connection. Using this connection, files are transferred. Here is the code of TransferFile class

public class TransferFile
{
   private CreateFTPConnection ftpConnection;
   private FTPSClient client;

public TransferFile(CreateFTPConnection ftpConnection) {
    this.ftpConnection = ftpConnection;
    this.client = ftpConnection.getClient();
}

public void transfer(Message<?> msg)
{
    InputStream inputStream = null;
    try
    {
        if(!client.isConnected()){
            ftpConnection.init();
            client = ftpConnection.getClient();
        }
        File file = (File) msg.getPayload();
        inputStream = new FileInputStream(file);
        client.storeFile(file.getName(), inputStream);
        client.sendNoOp();
    } catch (Exception e) {
        try
        {
            client.disconnect();
        }
        catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    finally
    {
        try {
            inputStream.close();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

I have to write jUnit Testcase for this class. For this, I have to create a FTPS Mock Server connection and have to use that connection to test the File Transfer. So can anyone plz give me any idea of how to make FTPS Mock Server and do the test case. I googled on this, but what I get is on FTP or SFTP, not FTPS. Please help me.

like image 773
Jyoti Ranjan Avatar asked Dec 26 '12 14:12

Jyoti Ranjan


1 Answers

You might find this useful MockFTPServer

The issue is that these mock servers don't implement the TLS portion from what I can see. You may need to do a little work to allow connections via TLS.

You should be able to search around and find some articles here on SO about dealing with certificates, (or in some cases, bypassing them) for the sake of your testing.

Here's another Article that goes through the steps of creating a basic FTP server Test.

Short of a full blown FTP server (Apache http w/ mod_ftp add on), there doesn't seem to be anything useful to do this.

like image 61
Mike Avatar answered Nov 15 '22 00:11

Mike