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.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class SftpWithPrivateKeyReaderTest {

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

    @Test
    public void testXbib() throws Exception {
        Map<String, String> env = new HashMap<>();
        env.put("username", "joerg");
        URI uri = URI.create("sftp://alkmene");
        try (SshClient sshClient = ClientBuilder.builder()
                .build()) {
            sshClient.setNioWorkers(1);
            sshClient.start();
            SftpFileSystem fileSystem = new SftpFileSystemProvider(sshClient).newFileSystem(uri, env);
            assertTrue(Files.exists(fileSystem.getDefaultDir()));
            try (Stream<Path> stream = Files.list(fileSystem.getDefaultDir())) {
                stream.forEach(p -> logger.log(Level.INFO, "p = " + p));
            }
            fileSystem.close();
            sshClient.stop();
        }
    }
}
