Enterprise Integration Zone is brought to you in partnership with:

Enterprise Architect in HCL Technologies a $7Billion IT services organization. My role is to work as a Technology Partner for large enterprise customers providing them low cost opensource solutions around Java, Spring and vFabric stack. I am also working on various projects involving, Cloud base solution, Mobile application and Business Analytics around Spring and vFabric space. Over 23 yrs, I have build repository of technologies and tools I liked and used extensively in my day to day work. In this blog, I am putting all these best practices and tools so that it will help the people who visit my website. Krishna is a DZone MVB and is not an employee of DZone and has posted 64 posts at DZone. You can read more from them at their website. View Full User Profile

Spring Integration Mock SftpServer Example

12.14.2012
| 5882 views |
  • submit to reddit

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.




Published at DZone with permission of Krishna Prasad, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)