### Question:
PropertyBasedSshSessionFactory extends JschConfigSessionFactory { @Override protected Session createSession(Host hc, String user, String host, int port, FS fs) throws JSchException { if (sshKeysByHostname.containsKey(host)) { SshUri sshUriProperties = sshKeysByHostname.get(host); jSch.addIdentity(host, sshUriProperties.getPrivateKey().getBytes(), null, null); if (sshUriProperties.getKnownHostsFile() != null) { jSch.setKnownHosts(sshUriProperties.getKnownHostsFile()); } if (sshUriProperties.getHostKey() != null) { HostKey hostkey = new HostKey(host, Base64.decode(sshUriProperties.getHostKey())); jSch.getHostKeyRepository().add(hostkey, null); } return jSch.getSession(user, host, port); } throw new JSchException("no keys configured for hostname " + host); } PropertyBasedSshSessionFactory(Map<String, SshUri> sshKeysByHostname, JSch jSch);   }### Answer:
@Test public void privateKeyIsUsed() throws Exception { SshUri sshKey = new SshUriProperties.SshUriPropertiesBuilder() .uri("git@gitlab.example.local:someorg/somerepo.git") .privateKey(PRIVATE_KEY) .build(); setupSessionFactory(sshKey); factory.createSession(hc, null, SshUriPropertyProcessor.getHostname(sshKey.getUri()), 22, null); verify(jSch).addIdentity("gitlab.example.local", PRIVATE_KEY.getBytes(), null, null); }
@Test public void hostKeyIsUsed() throws Exception { SshUri sshKey = new SshUriProperties.SshUriPropertiesBuilder() .uri("git@gitlab.example.local:someorg/somerepo.git") .hostKey(HOST_KEY) .privateKey(PRIVATE_KEY) .build(); setupSessionFactory(sshKey); factory.createSession(hc, null, SshUriPropertyProcessor.getHostname(sshKey.getUri()), 22, null); ArgumentCaptor<HostKey> captor = ArgumentCaptor.forClass(HostKey.class); verify(hostKeyRepository).add(captor.capture(), isNull(UserInfo.class)); HostKey hostKey = captor.getValue(); Assert.assertEquals("gitlab.example.local", hostKey.getHost()); Assert.assertEquals(HOST_KEY, hostKey.getKey()); }
@Test public void customKnownHostsFileIsUsed() throws Exception { SshUri sshKey = new SshUriProperties.SshUriPropertiesBuilder() .uri("git@gitlab.example.local:someorg/somerepo.git") .privateKey(PRIVATE_KEY) .knownHostsFile("/ssh/known_hosts") .build(); setupSessionFactory(sshKey); factory.createSession(hc, null, SshUriPropertyProcessor.getHostname(sshKey.getUri()), 22, null); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(jSch).setKnownHosts(captor.capture()); Assert.assertEquals("/ssh/known_hosts", captor.getValue()); }