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
Newsletter
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

  • Genkit Flows on Kubernetes, Routed by a Decision Model: senro + jev in Go
  • Member Spotlight: Shai Almong
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Dec. 14, 12 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
47.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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