DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Marry MDC With Spring Integration
  • When Memory Overflows: Too Many ApplicationContexts in Spring Integration Tests
  • Integrate Spring With Open AI
  • Introducing Stalactite ORM

Trending

  • From Indicators to Insights: Automating IOC Enrichment Using Python and Threat Feeds
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • What Is Plagiarism? How to Avoid It and Cite Sources
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Spring Integration Mock SftpServer Example

Spring Integration Mock SftpServer Example

In this example I will show how to test Spring Integration flow using Mock SftpServer.

By 
Krishna Prasad user avatar
Krishna Prasad
·
Dec. 14, 12 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
47.6K Views

Join the DZone community and get the full member experience.

Join For Free

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.




Spring Integration Integration Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • How to Marry MDC With Spring Integration
  • When Memory Overflows: Too Many ApplicationContexts in Spring Integration Tests
  • Integrate Spring With Open AI
  • Introducing Stalactite ORM

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook