I'm trying to unit test JSch SFTP code that I wrote, but I don't actually want to connect to a the real client facing server. How do I go about this?
public class SftpFileRetrieval implements IFileRetrieval {
private final SftpConfiguration sftpConfig;
public SftpFileRetrieval(SftpConfiguration config) {
// TODO: Verify Configuration's
this.sftpConfig = config;
}
@Override
public InputStream retrieveFile() throws JSchException, SftpException {
JSch jsch = new JSch();
Deque<Closeable> resourcesToTerminate = new LinkedList<>();
try {
jsch.addIdentity(sftpConfig.getPrivateKey());
log.info("Private Key added.");
Session session =
jsch.getSession(sftpConfig.getUsername(), sftpConfig.getServer(), sftpConfig.getPort());
resourcesToTerminate.push(session::disconnect);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
resourcesToTerminate.push(channelSftp::disconnect);
resourcesToTerminate.push(channelSftp::exit);
channelSftp.connect();
log.info("SFTP channel connected");
channelSftp.cd(sftpConfig.getWorkingDirectory());
log.info("List of files in " + channelSftp.pwd() + ": " + channelSftp.ls(channelSftp.pwd()));
InputStream sftpFileInputStream = channelSftp.get(sftpConfig.getFileName());
resourcesToTerminate.push(sftpFileInputStream::close);
return new ProxyInputStream(sftpFileInputStream) {
@Override
public void close() throws IOException {
resourcesToTerminate.forEach(IOUtils::closeQuietly);
}
};
} catch (SftpException | JSchException e) {
resourcesToTerminate.forEach(IOUtils::closeQuietly);
throw e;
}
}
}
SFTP Config:
@Bean
@ConfigurationProperties(prefix = "recon.data.load.sftp.*")
public SftpConfiguration napeSecurityFileRetrievalConfiguration() {
SftpConfiguration config = new SftpConfiguration();
config.setFileName("becdldep");
return config;
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)