Skip to content

Commit

Permalink
Fixed test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Oct 25, 2023
1 parent c1df418 commit 40b5a2e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
4 changes: 4 additions & 0 deletions camel-scp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-scp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-sftp</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions camel-scp/src/main/java/nl/vpro/camel/ScpEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

/**
* Represents a Scp endpoint.
*
* <p>
* This is completely inspired by the default camel-scp endpoint. It just uses the much more robust, and better tested 'scp' command line tool.
*
* <p>
* 'jsch' e.g. simply didn't support the server in our use case.
*
*/
Expand Down
24 changes: 21 additions & 3 deletions camel-scp/src/test/java/nl/vpro/camel/ScpComponentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.*;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Arrays;
import java.util.Set;
import java.util.function.UnaryOperator;

Expand All @@ -25,6 +26,7 @@
import org.apache.sshd.server.config.keys.AuthorizedKeysAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.sftp.server.*;
import org.junit.jupiter.api.*;

import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied;
Expand Down Expand Up @@ -52,7 +54,6 @@ public void setup() throws IOException {
scpRoot = Files.createTempDirectory("scp_tmp");
sshd = configureSshServer(scpRoot, scpEventListener);
sshd.start();

}

@AfterEach
Expand Down Expand Up @@ -176,6 +177,7 @@ public void configure() {
+ "?remotePath=/&port=" + port
+ "&remoteUser=" + user
+ "&connectTimeout=30000"
+ "&useUserKnownHostsFile=false"
)
)
.to("mock:result");
Expand Down Expand Up @@ -206,7 +208,7 @@ private void addRoutesBuilder(UnaryOperator<String> appendMore) throws Exception
* @return SshServer
*/
private SshServer configureSshServer(final Path scpRoot,
final ScpTransferEventListener scpTransferEventListener) {
final ScpEventListener scpTransferEventListener) {
final SshServer sshd = SshServer.setUpDefaultServer();
log.debug("Ssh-server filesystem-root is {}", scpRoot);
sshd.setHost("localhost");
Expand All @@ -216,6 +218,11 @@ private SshServer configureSshServer(final Path scpRoot,
ScpCommandFactory factory = new ScpCommandFactory.Builder().build();
factory.addEventListener(scpTransferEventListener);
sshd.setCommandFactory(factory);
SftpSubsystemFactory sftp = new SftpSubsystemFactory();
sftp.addSftpEventListener(scpTransferEventListener);

sshd.setSubsystemFactories(Arrays.asList(sftp));

// Pass key for test-purposes
sshd.setPublickeyAuthenticator(
new UserAuthorizedKeysAuthenticator(new File("src/test/resources/id_rsa.pub").toPath(), "test"));
Expand All @@ -227,17 +234,28 @@ private SshServer configureSshServer(final Path scpRoot,
* Get file and length from SCP'd files
*/
@Getter
private static class ScpEventListener implements ScpTransferEventListener {
private static class ScpEventListener implements ScpTransferEventListener, SftpEventListener {

private Path file;
private long length;

@Override
public void startFileEvent(
Session session, FileOperation op, Path file, long length, Set<PosixFilePermission> perms) {
log.info("Incoming file: {}", file);
this.file = file;
this.length = length;
}
@Override
public void written(
ServerSession session, String remoteHandle, FileHandle localHandle,
long offset, byte[] data, int dataOffset, int dataLen, Throwable thrown)
throws IOException {

log.info("{}", remoteHandle);
this.file = localHandle.getFile();
this.length = dataLen;
}
}

/**
Expand Down
16 changes: 11 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<newrelic.version>8.6.0</newrelic.version>
<log4j.version>2.20.0</log4j.version>
<vpro.shared.version>3.5.3</vpro.shared.version>
<apache.mina.sshd.version>2.10.0</apache.mina.sshd.version>
<apache.mina.sshd.version>2.11.0</apache.mina.sshd.version>
</properties>

<build>
Expand All @@ -90,7 +90,7 @@
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<version>3.6.0</version>
<configuration>
<doclint>none</doclint>
<additionalJOption>-quiet</additionalJOption>
Expand All @@ -100,7 +100,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<version>0.8.11</version>
<executions>
<execution>
<goals>
Expand Down Expand Up @@ -147,7 +147,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<version>0.8.11</version>
<configuration>
<append>true</append>
</configuration>
Expand Down Expand Up @@ -187,6 +187,12 @@
<artifactId>sshd-scp</artifactId>
<version>${apache.mina.sshd.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-sftp</artifactId>
<version>${apache.mina.sshd.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.newrelic.agent.java</groupId>
Expand Down Expand Up @@ -223,7 +229,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.5.0</version>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down

0 comments on commit 40b5a2e

Please sign in to comment.