Spring Integration Mock SftpServer Example

in continuation of my earlier blog spring integration fakeftpserver example in this example i will show how to test spring integration flow using mock sftpserver. there a are few good writeups on the net including the stackoverflow writeup, using apache mina as a mock/in memory sftp server for unit testing. the code for this blog is @ spring integration flow to test ftp/sftp server .

to run the junit test, run “mvn test” and understand the test flow.

again talking about the same spring integration flows as mentioned in my earlier blog, i will write a test for a sftp server,

spring integration mock sftpserver example

spring integration mock sftpserver example

the maven dependency for this is as below,

<dependency>
<groupid>org.apache.sshd</groupid>
<artifactid>sshd-core</artifactid>
<version>0.5.0</version>
</dependency>
<dependency>
<groupid>org.slf4j</groupid>
<artifactid>slf4j-api</artifactid>
<version>1.6.1</version>
</dependency>
<dependency>
<groupid>com.jcraft</groupid>
<artifactid>jsch</artifactid>
<version>0.1.49</version>
</dependency>
<dependency>
<groupid>org.slf4j</groupid>
<artifactid>slf4j-log4j12</artifactid>
<version>1.7.2</version>
</dependency>

the startup and teardown of the junit is as below,

@before
public void beforetestsetup() throws exception {
sshd = sshserver.setupdefaultserver();
sshd.setport(22999);

sshd.setkeypairprovider(new simplegeneratorhostkeyprovider("hostkey.ser"));
sshd.setpasswordauthenticator(new passwordauthenticator() {

public boolean authenticate(string username, string password, serversession session) {
// todo auto-generated method stub
return true;
}
});

commandfactory mycommandfactory = new commandfactory() {

public command createcommand(string command) {
system.out.println("command: " + command);
return null;
}
};
sshd.setcommandfactory(new scpcommandfactory(mycommandfactory));

list<namedfactory<command>> namedfactorylist = new arraylist<namedfactory<command>>(); namedfactorylist.add(new sftpsubsystem.factory()); sshd.setsubsystemfactories(namedfactorylist); sshd.start(); }

@after

public void teardown() throws exception { sshd.stop(); }

the junit test is as below,

public void testputandgetfile() throws exception {
jsch jsch = new jsch();

hashtable config = new hashtable();
config.put("stricthostkeychecking", "no");
jsch.setconfig(config);

session session = jsch.getsession("remote-username", "localhost", 22999);
session.setpassword("remote-password");

session.connect();

channel channel = session.openchannel("sftp");
channel.connect();

channelsftp sftpchannel = (channelsftp) channel;

final string testfilecontents = "some file contents";

string uploadedfilename = "uploadfile";
sftpchannel.put(new bytearrayinputstream(testfilecontents.getbytes()), uploadedfilename);

string downloadedfilename = "downloadfile";
sftpchannel.get(uploadedfilename, downloadedfilename);

file downloadedfile = new file(downloadedfilename);
asserttrue(downloadedfile.exists());

string filedata = getfilecontents(downloadedfile);

assertequals(testfilecontents, filedata);

if (sftpchannel.isconnected()) {
sftpchannel.exit();
logger.debug("disconnected channel");
}

if (session.isconnected()) {
session.disconnect();
logger.debug("disconnected session");
}

}

i hope this blog helped you.




 

 

 

 

Top