package org.apache.sshd.fs.test;

import java.nio.file.Files;
import org.apache.sshd.client.ClientBuilder;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.fs.SftpFileSystem;
import org.apache.sshd.fs.SftpFileSystemProvider;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.xbib.net.security.PrivateKeyReader;

public class SFTPFileSystemTest {

    private static final Logger logger = Logger.getLogger(SFTPFileSystemTest.class.getName());

    @Test
    public void init() throws Exception {
        Map<String, String> env = new HashMap<>();
        env.put("username", "joerg");
        URI uri = URI.create("sftp://xbib.org");
        SshClient sshClient = ClientBuilder.builder().build();
        Path privateKey = Paths.get(System.getProperty("user.home") + "/.ssh/id_ed25519");
        PrivateKeyReader privateKeyReader = new PrivateKeyReader();
        KeyPair keyPair = privateKeyReader.generateFrom(Files.newInputStream(privateKey), null);
        sshClient.addPublicKeyIdentity(keyPair);
        sshClient.setNioWorkers(1);
        sshClient.start();
        SftpFileSystem fileSystem = new SftpFileSystemProvider(sshClient).newFileSystem(uri, env);
        fileSystem.close();
        // ...
        sshClient.stop();
    }
}
