diff --git a/.gitignore b/.gitignore
index 8643541..b49b435 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
.flattened-pom.xml
target
+.idea
diff --git a/pom.xml b/pom.xml
index 2562113..a4d0213 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,7 +9,7 @@
fake-sftp-server-lambda
- 2.0.0
+ 2.1.0-SNAPSHOT
jar
Fake SFTP Server Lambda
diff --git a/src/main/java/com/github/stefanbirkner/fakesftpserver/lambda/FakeSftpServer.java b/src/main/java/com/github/stefanbirkner/fakesftpserver/lambda/FakeSftpServer.java
index 6bf01cc..b35f910 100644
--- a/src/main/java/com/github/stefanbirkner/fakesftpserver/lambda/FakeSftpServer.java
+++ b/src/main/java/com/github/stefanbirkner/fakesftpserver/lambda/FakeSftpServer.java
@@ -16,6 +16,7 @@
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;
import java.util.*;
+import java.util.stream.Stream;
import static com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder.newLinux;
import static java.nio.file.FileVisitResult.CONTINUE;
@@ -413,6 +414,21 @@ public boolean existsFile(
return exists(pathAsObject) && !isDirectory(pathAsObject);
}
+ /**
+ * List all files and directories in a given directory
+ * @param path the path to the directory
+ * @return a stream with of all files in the directory
+ * @throws IOException see {@link Files#list(Path)}
+ */
+ public Stream listFilesAndDirectories(
+ String path
+ ) throws IOException
+ {
+ verifyWithSftpServerIsNotFinished("list files");
+ Path pathAsObject = fileSystem.getPath(path);
+ return Files.list(pathAsObject);
+ }
+
/**
* Deletes all files and directories.
* @throws IOException if an I/O error is thrown while deleting the files
diff --git a/src/test/java/com/github/stefanbirkner/fakesftpserver/lambda/FakeSftpServerTest.java b/src/test/java/com/github/stefanbirkner/fakesftpserver/lambda/FakeSftpServerTest.java
index c93a37b..62c5243 100644
--- a/src/test/java/com/github/stefanbirkner/fakesftpserver/lambda/FakeSftpServerTest.java
+++ b/src/test/java/com/github/stefanbirkner/fakesftpserver/lambda/FakeSftpServerTest.java
@@ -11,11 +11,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
+import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.List;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
import static com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.withSftpServer;
import static com.github.stefanbirkner.fishbowl.Fishbowl.exceptionThrownBy;
@@ -603,6 +607,70 @@ public void existence_of_a_file_cannot_be_checked_outside_of_the_lambda(
}
}
+ public static class list_files_and_directories_check {
+ @Test
+ public void list_returns_all_existing_files_in_given_directory(
+ ) throws Exception {
+ withSftpServer(
+ server -> {
+ uploadFile(
+ server,
+ "/dummy_directory/dummy_file.bin",
+ DUMMY_CONTENT
+ );
+ uploadFile(
+ server,
+ "/dummy_directory/directory/dummy_file.bin",
+ DUMMY_CONTENT
+ );
+ Stream fileList = server.listFilesAndDirectories(
+ "/dummy_directory"
+ );
+ assertThat(fileList)
+ .hasSize(2)
+ .extracting(Path::toString)
+ .contains("/dummy_directory/dummy_file.bin", "/dummy_directory/directory");
+ }
+ );
+ }
+
+ @Test
+ public void list_on_not_existing_directory_will_fail_with_exception(
+ ) throws Exception {
+ withSftpServer(
+ server -> {
+ uploadFile(
+ server,
+ "/dummy_directory/dummy_file.bin",
+ DUMMY_CONTENT
+ );
+ assertThatThrownBy(() -> server.listFilesAndDirectories(
+ "/unknown_directory"
+ )).isInstanceOf(NoSuchFileException.class);
+ }
+ );
+ }
+
+ @Test
+ public void list_cannot_be_checked_outside_of_the_lambda(
+ ) throws Exception {
+ AtomicReference serverCapture
+ = new AtomicReference<>();
+ withSftpServer(
+ serverCapture::set
+ );
+ Throwable exception = exceptionThrownBy(
+ () -> serverCapture.get().listFilesAndDirectories("/dummy_directory")
+ );
+ assertThat(exception)
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
+ "Failed to list files because withSftpServer"
+ + " is already finished."
+ );
+ }
+ }
+
public static class server_shutdown {
@Test
public void after_a_successful_test_SFTP_server_is_shutdown(