Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maddingo committed Dec 14, 2023
1 parent 2fdbc37 commit de09626
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 102 deletions.
12 changes: 12 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
<groupId>no.maddin.niofs</groupId>
<artifactId>nio-fs-sftp</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>no.maddin.niofs</groupId>
<artifactId>nio-fs-test-util</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
67 changes: 67 additions & 0 deletions integration-tests/src/test/java/FilesListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import no.maddin.niofs.testutil.BasicTestContainer;
import no.maddin.niofs.testutil.FileUtils;
import no.maddin.niofs.testutil.SftpgoContainer;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

/**
* We prepare a file structure and use the various providers list the files.
*/
@Testcontainers
public class FilesListTest {

private static final String TESTDATA_RESOURCE = "/sftpgo-data";
private static final File localDataFileRoot = FileUtils.classpathFile(FilesListTest.class, TESTDATA_RESOURCE);

public static Stream<Arguments> data() {
// anonymous class
return Stream.of(
Arguments.of(
"sftp",
(Supplier<BasicTestContainer>) () -> new SftpgoContainer(TESTDATA_RESOURCE)
),
Arguments.of(
"webdav",
(Supplier<BasicTestContainer>) () -> new SftpgoContainer(TESTDATA_RESOURCE)
)
);
}

@ParameterizedTest(name = "{index} {0}")
@MethodSource("data")
public void listFiles(String protocol, Supplier<BasicTestContainer> containerSupplier) throws Exception {

String dataSubDir = UUID.randomUUID().toString();
List<String> createdFiles = FileUtils.createFilesInDir(localDataFileRoot, dataSubDir, 10);
try (BasicTestContainer container = containerSupplier.get()) {
container.start();
URI uri = container.getBaseUri(protocol);
Path path = Paths.get(uri.resolve(dataSubDir));
try (Stream<Path> paths = Files.list(path)) {
List<String> foundFiles = paths
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toList());

assertThat(foundFiles, equalTo(createdFiles));
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Keep this directory. It is used by sftpgo to store the host keys.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
37 changes: 19 additions & 18 deletions sftp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,26 @@
</developers>

<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>2.9.2</version>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-mina</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-scp</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
Expand All @@ -68,6 +57,18 @@
<artifactId>junit</artifactId>
</exclusion-->
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>17.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
</project>
3 changes: 0 additions & 3 deletions sftp/src/main/java/no/maddin/niofs/sftp/SFTPHost.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package no.maddin.niofs.sftp;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -49,7 +47,6 @@ private static UserInfo userInfo(String userInfo) {
return new UserInfo(un, pw);
}

@NotNull
static URI getServerUri(URI uri, boolean requireEmptyPath) throws URISyntaxException {
String host = uri.getHost();
if (host == null) {
Expand Down
37 changes: 18 additions & 19 deletions sftp/src/main/java/no/maddin/niofs/sftp/SFTPPath.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package no.maddin.niofs.sftp;

import org.jetbrains.annotations.NotNull;
import jakarta.validation.constraints.NotNull;

import java.io.File;
import java.io.IOException;
Expand All @@ -10,7 +10,6 @@
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchEvent.Modifier;
import java.util.*;
import java.util.stream.Collectors;

/**
* A Path implementation for SFTP.
Expand Down Expand Up @@ -99,12 +98,12 @@ public int getNameCount() {
}

@Override
public Path getName(int index) {
public @NotNull Path getName(int index) {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public Path subpath(int beginIndex, int endIndex) {
public @NotNull Path subpath(int beginIndex, int endIndex) {
return new SFTPPath(beginIndex == 0 ? host : null, combineParts(0, endIndex));
}

Expand All @@ -126,7 +125,7 @@ public boolean startsWith(@NotNull String other) {
}

@Override
public boolean endsWith(Path other) {
public boolean endsWith(@NotNull Path other) {
throw new UnsupportedOperationException("Not Implemented");
}

Expand All @@ -139,37 +138,37 @@ public boolean endsWith(@NotNull String other) {
* SFTPPAths are normalized at creation time. This just returns itself.
*/
@Override
public Path normalize() {
public @NotNull Path normalize() {
return this;
}

@Override
public Path resolve(Path other) {
public @NotNull Path resolve(@NotNull Path other) {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public Path resolve(String other) {
public @NotNull Path resolve(@NotNull String other) {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public Path resolveSibling(Path other) {
public @NotNull Path resolveSibling(@NotNull Path other) {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public Path resolveSibling(String other) {
public @NotNull Path resolveSibling(@NotNull String other) {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public Path relativize(Path other) {
public @NotNull Path relativize(@NotNull Path other) {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public URI toUri() {
public @NotNull URI toUri() {

try {
String userInfo = null;
Expand All @@ -188,37 +187,37 @@ public URI toUri() {
}

@Override
public Path toAbsolutePath() {
public @NotNull Path toAbsolutePath() {
return normalize();
}

@Override
public Path toRealPath(LinkOption... options) throws IOException {
public @NotNull Path toRealPath(LinkOption @NotNull ... options) throws IOException {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public File toFile() {
public @NotNull File toFile() {
throw new UnsupportedOperationException();
}

@Override
public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException {
public @NotNull WatchKey register(@NotNull WatchService watcher, Kind<?> @NotNull [] events, Modifier... modifiers) throws IOException {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public WatchKey register(WatchService watcher, Kind<?>... events) throws IOException {
public @NotNull WatchKey register(@NotNull WatchService watcher, Kind<?> @NotNull ... events) throws IOException {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public Iterator<Path> iterator() {
public @NotNull Iterator<Path> iterator() {
throw new UnsupportedOperationException("Not Implemented");
}

@Override
public int compareTo(Path other) {
public int compareTo(@NotNull Path other) {
throw new UnsupportedOperationException("Not Implemented");
}

Expand Down
27 changes: 27 additions & 0 deletions sftp/src/test/java/no/maddin/niofs/sftp/PathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ static Stream<Arguments> invalidPathData() {
);
}

public static Stream<Arguments> namesData() {
return Stream.of(
Arguments.of(
URI.create("sftp://localhost/test/text.txt"),
new Matcher[] {
hasProperty("pathString", equalTo("test")),
hasProperty("pathString", equalTo("text.txt"))
}
)
);
}

@ParameterizedTest
@MethodSource("validPathData")
public void validPath(URI uri, Matcher<Path> expectedResult) throws IOException {
Expand Down Expand Up @@ -125,6 +137,21 @@ void invalidPaths(URI uri, Matcher<Exception> expectedException) {
}
}

// TODO: relative paths are not supported
// @SafeVarargs
// @ParameterizedTest
// @MethodSource("namesData")
// final void getNameTest(URI uri, Matcher<Path>... name0Matcher) throws IOException {
// try (FileSystem fs = FileSystems.newFileSystem(serverUri(uri), Collections.emptyMap())) {
// assertThat(fs, hasProperty("open", equalTo(true)));
// Path path = Paths.get(uri);
// for (int i = 0; i < name0Matcher.length; i++) {
// assertThat(path.getName(i), is(name0Matcher[i]));
// }
// }
// }
//

@NotNull
private static TypeSafeDiagnosingMatcher<Path> pathStartsWith(String startsWith) {
return new TypeSafeDiagnosingMatcher<Path>() {
Expand Down
14 changes: 0 additions & 14 deletions sftp/src/test/java/no/maddin/niofs/sftp/SshdContainer.java

This file was deleted.

5 changes: 0 additions & 5 deletions sftp/src/test/resources/entrypoint.d/setpasswd.sh

This file was deleted.

1 change: 1 addition & 0 deletions sftp/src/test/resources/sftpgo-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Keep this directory. It is used by sftpgo to store the host keys.
1 change: 1 addition & 0 deletions sftp/src/test/resources/testdata/testfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
5 changes: 5 additions & 0 deletions test-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@
<artifactId>junit-jupiter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package no.maddin.niofs.testutil;

import org.testcontainers.lifecycle.Startable;

import java.net.URI;

public interface BasicTestContainer extends Startable {
URI getBaseUri(String protocol);
}
Loading

0 comments on commit de09626

Please sign in to comment.