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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Integrate Spring With Open AI
  • Introducing Stalactite ORM
  • Registering Spring Converters via Extending Its Interface
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS

Trending

  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • Docker Base Images Demystified: A Practical Guide
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  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.0K 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

  • Integrate Spring With Open AI
  • Introducing Stalactite ORM
  • Registering Spring Converters via Extending Its Interface
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!