SshUtils implements InitializingBean { protected Session authenticateSshSessionWithPublicKey(Connection sshConnection) throws IOException { sshConnection.connect(null, CONNECT_TIMEOUT, CONNECT_TIMEOUT); if (!sshConnection.authenticateWithPublicKey("root", certificate, null)) { throw new CloudRuntimeException(String.format("Unable to authenticate to (%s)", sshConnection.getHostname())); } return sshConnection.openSession(); }  void executeCommandOnHostViaSsh(String hostIp, String command); void sendFileToHost(File localfilePath, String remoteFileName, String remotePath, String address); @Override void afterPropertiesSet();  }
@Test public void authenticateSshSessionWithPublicKeyTest() throws IOException { Session sess = Mockito.mock(Session.class); Connection sshConnection = setupAuthenticateSshSessionWithPublicKeyTest(sess, true); Session result = spy.authenticateSshSessionWithPublicKey(sshConnection); Assert.assertEquals(sess, result); }
@Test(expected = CloudRuntimeException.class) public void authenticateSshSessionWithPublicKeyTestExpectCloudRuntimeException() throws IOException { Session sess = Mockito.mock(Session.class); Connection sshConnection = setupAuthenticateSshSessionWithPublicKeyTest(sess, false); spy.authenticateSshSessionWithPublicKey(sshConnection); }