All Projects → stefanbirkner → fake-sftp-server-rule

stefanbirkner / fake-sftp-server-rule

Licence: MIT License
A JUnit rule that runs an in-memory SFTP server.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to fake-sftp-server-rule

Testfx
MSTest V2 framework and adapter
Stars: ✭ 391 (+1050%)
Mutual labels:  test, unittest
Testify
A unit testing framework written in bash for bash scripts
Stars: ✭ 45 (+32.35%)
Mutual labels:  test, unittest
Sftpgo
Fully featured and highly configurable SFTP server with optional HTTP, FTP/S and WebDAV support - S3, Google Cloud Storage, Azure Blob
Stars: ✭ 3,534 (+10294.12%)
Mutual labels:  sftp, sftp-server
Pytest Spec
Library pytest-spec is a pytest plugin to display test execution output like a SPECIFICATION.
Stars: ✭ 65 (+91.18%)
Mutual labels:  test, unittest
terraform-aws-sftp
This terraform module is used to create sftp on AWS for S3.
Stars: ✭ 20 (-41.18%)
Mutual labels:  sftp, sftp-server
local-data-api
Data API for local, you can write unittest for AWS Aurora Serverless's Data API
Stars: ✭ 99 (+191.18%)
Mutual labels:  test, unittest
Capture Stream
Capture stream output.
Stars: ✭ 10 (-70.59%)
Mutual labels:  test, unittest
Javascript Testing Best Practices
📗🌐 🚢 Comprehensive and exhaustive JavaScript & Node.js testing best practices (August 2021)
Stars: ✭ 13,976 (+41005.88%)
Mutual labels:  test, unittest
Expekt
BDD assertion library for Kotlin
Stars: ✭ 163 (+379.41%)
Mutual labels:  test, unittest
unittest expander
A library that provides flexible and easy-to-use tools to parameterize Python unit tests, especially those based on unittest.TestCase.
Stars: ✭ 12 (-64.71%)
Mutual labels:  test, unittest
Wasmite
Now WebAssembly has proper testing, unit-testing and debugging 🤗
Stars: ✭ 20 (-41.18%)
Mutual labels:  test, unittest
Robolectric-Instrumentation
Emulation of Android Instrumentation Framework API for Robolectric
Stars: ✭ 27 (-20.59%)
Mutual labels:  test
ttv
A command line tool for splitting files into test, train, and validation sets.
Stars: ✭ 38 (+11.76%)
Mutual labels:  test
reducer-tester
Utilities for testing redux reducers
Stars: ✭ 19 (-44.12%)
Mutual labels:  test
osgi-test
Testing support for OSGi. Includes JUnit 4 and JUnit 5 support and AssertJ support.
Stars: ✭ 22 (-35.29%)
Mutual labels:  test
angular-webpack-skeleton
This project is deprecated. Please refer to https://github.com/Ks89/angular-cli-skeleton
Stars: ✭ 16 (-52.94%)
Mutual labels:  test
pquery
pquery is an open-source (GPLv2 licensed) multi-threaded test program, written in C++, created to stress test the MySQL server (in any flavor), either randomly or sequentially, for QA purposes.
Stars: ✭ 48 (+41.18%)
Mutual labels:  test
CodeSpecJS
UI Automation Testing without writing a single line of code
Stars: ✭ 16 (-52.94%)
Mutual labels:  test
dakait
A tool to download files from your FTP/SFTP servers in an organized way.
Stars: ✭ 35 (+2.94%)
Mutual labels:  sftp
walrus
🎉 Cli development framework.
Stars: ✭ 17 (-50%)
Mutual labels:  test

Fake SFTP Server Rule

Build Status

Fake SFTP Server Rule is a JUnit rule that runs an in-memory SFTP server while your tests are running. It uses the SFTP server of the Apache SSHD project.

Fake SFTP Server Rule is published under the MIT license. It requires at least Java 8. Please open an issue if you want to use it with an older version of Java.

I want to thank my former team SAM at ThoughtWorks for using this library and @crizzis, @OArtyomov and @TheSentinel454 for their feature requests.

There is an alternative to Fake SFTP Server Rule that is independent of the test framework. Its name is Fake SFTP Server Lambda.

Installation

Fake SFTP Server Rule is available from Maven Central.

<dependency>
  <groupId>com.github.stefanbirkner</groupId>
  <artifactId>fake-sftp-server-rule</artifactId>
  <version>2.0.1</version>
</dependency>

If you upgrade from a version < 2.x to the newest version please read the last section of this readme.

Usage

The Fake SFTP Server Rule is used by adding it to your test class.

import com.github.stefanbirkner.fakesftpserver.rule.FakeSftpServerRule;

public class TestClass {
  @Rule
  public final FakeSftpServerRule sftpServer = new FakeSftpServerRule();

  ...
}

This rule starts a server before your test and stops it afterwards.

By default the SFTP server listens on an auto-allocated port. During the test this port can be obtained by sftpServer.getPort(). It can be changed by calling setPort(int). If you do this from within a test then the server gets restarted. The time-consuming restart can be avoided by setting the port immediately after creating the rule.

public class TestClass {
  @Rule
  public final FakeSftpServerRule sftpServer = new FakeSftpServerRule()
      .setPort(1234);

  ...
}

You can interact with the SFTP server by using the SFTP protocol with password authentication. By default the server accepts every pair of username and password, but you can restrict it to specific pairs.

public class TestClass {
  @Rule
  public final FakeSftpServerRule sftpServer = new FakeSftpServerRule()
      .addUser("username", "password");

  ...
}

It is also possible to do this during the test using the same method.

Testing code that reads files

If you test code that reads files from an SFTP server then you need a server that provides these files. Fake SFTP Server Rule provides a shortcut for uploading files to the server.

@Test
public void testTextFile() {
  sftpServer.putFile("/directory/file.txt", "content of file", UTF_8);
  //code that downloads the file
}

@Test
public void testBinaryFile() {
  byte[] content = createContent();
  sftpServer.putFile("/directory/file.bin", content);
  //code that downloads the file
}

Test data that is provided as an input stream can be uploaded directly from that input stream. This is very handy if your test data is available as a resource.

@Test
public void testFileFromInputStream() {
  InputStream is = getClass().getResourceAsStream("data.bin");
  sftpServer.putFile("/directory/file.bin", is);
  //code that downloads the file
}

If you need an empty directory then you can use the method createDirectory(String).

@Test
public void testDirectory() {
  sftpServer.createDirectory("/a/directory");
  //code that reads from or writes to that directory
}

You may create multiple directories at once with createDirectories(String...).

@Test
public void testDirectories() {
  sftpServer.createDirectories(
    "/a/directory",
    "/another/directory"
  );
  //code that reads from or writes to that directories
}

Testing code that writes files

If you test code that writes files to an SFTP server then you need to verify the upload. Fake SFTP Server Rule provides a shortcut for getting the file's content from the server.

@Test
public void testTextFile() {
  //code that uploads the file
  String fileContent = sftpServer.getFileContent("/directory/file.txt", UTF_8);
  ...
}

@Test
public void testBinaryFile() {
  //code that uploads the file
  byte[] fileContent = sftpServer.getFileContent("/directory/file.bin");
  ...
}

Testing existence of files

If you want to check whether a file hast been created or deleted then you can verify that it exists or not.

@Test
public void testFile() {
  //code that uploads or deletes the file
  boolean exists = sftpServer.existsFile("/directory/file.txt");
  ...
}

The method returns true iff the file exists and it is not a directory.

Delete all files

If you want to reuse the SFTP server then you can delete all files and directories on the SFTP server. (This is rarely necessary because the rule itself takes care that every test starts and ends with a clean SFTP server.)

sftpServer.deleteAllFilesAndDirectories()

Contributing

You have three options if you have a feature request, found a bug or simply have a question about Fake SFTP Server Rule.

Development Guide

Fake SFTP Server Rule is build with Maven. If you want to contribute code then

  • Please write a test for your change.
  • Ensure that you didn't break the build by running mvn verify -Dgpg.skip.
  • Fork the repo and create a pull request. (See Understanding the GitHub Flow)

The basic coding style is described in the EditorConfig file .editorconfig.

Fake SFTP Server Rule supports Travis CI for continuous integration. Your pull request will be automatically build by Travis CI.

Release Guide

  • Select a new version according to the Semantic Versioning 2.0.0 Standard.
  • Set the new version in pom.xml and in the Installation section of this readme.
  • Commit the modified pom.xml and README.md.
  • Run mvn clean deploy with JDK 8.
  • Add a tag for the release: git tag fake-sftp-server-rule-X.X.X

Upgrading from 0.x.y or 1.x.y to version >= 2

In older versions the SFTP server listened to port 23454 by default. From version 2 on it selects an arbitrary free port by default. If your tests fail after an upgrade you may consider to restore the old behaviour by immediately setting the old port after creating the rule.

@Rule
public final FakeSftpServerRule sftpServer = new FakeSftpServerRule()
    .setPort(23454);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].