SshUtil { public static Session getSessionObject( String userName, String password, String hostName, int port, Properties sessionConfig ) { Session session = null; if ( userName != null && !"".equals( userName ) && password != null && hostName != null && !"".equals( hostName ) ) { JSch jsch = new JSch(); try { session = jsch.getSession( userName, hostName, port ); session.setPassword( password ); if ( sessionConfig != null ) { session.setConfig( sessionConfig ); } } catch ( Exception e ) { logger.error( "Creating SSH session failed", e ); } return session; } return null; }  static Session getSessionObject( String userName, String password, String hostName, int port,
                                            Properties sessionConfig ); static String executeCommand( Session sessionObj, String command ); static void upload( Session session, InputStream localInputStream, String remoteFile ); static void download( Session session, OutputStream localOutputStream, String remoteFile ); static SftpATTRS lstat( Session session, String remoteFile );  }
@Test public void testGetSessionObjectWithAcceptableParameters() { logger.info( "getSessionObject" ); String username = "ADMIN"; String password = "ADMIN"; String hostname = "10.28.197.23"; int port = 0; Properties sessionConfig = new java.util.Properties(); sessionConfig.put( "StrictHostKeyChecking", "no" ); Session expResult = null; Session result = null; result = SshUtil.getSessionObject( username, password, hostname, port, sessionConfig ); boolean isResultNotNull = false; if ( result == null ) { isResultNotNull = false; } else { isResultNotNull = true; } logger.info( "[TS] : Expected result : Session Object should be NOT NULL , actual result : Session Object is NOT NULL =" + isResultNotNull ); assertNotNull( result ); }
@Test( expected = JSchException.class ) public void testGetSessionObjectWithInvalidPortNumber() throws JSchException { logger.info( "getSessionObject" ); String username = "ADMIN"; String password = "ADMIN"; String hostname = "45.89.97.09"; int port = 66636; Properties sessionConfig = new java.util.Properties(); sessionConfig.put( "StrictHostKeyChecking", "no" ); Session result = null; result = SshUtil.getSessionObject( username, password, hostname, port, sessionConfig ); boolean isResultNull = false; if ( result == null ) { isResultNull = true; } else { isResultNull = false; } logger.info( "[TS] : Expected result : Will throw an error when we will try to connect" ); result.connect( 1000 ); }
@Test( expected = Exception.class ) public void testGetSessionObjectWithNullUsername() throws JSchException { logger.info( "[TS] : testGetSessionObjectWithNullSessionObj" ); String username = null; String password = "ADMIN"; String hostname = "10.28.197.23"; int port = 65534; Properties sessionConfig = new java.util.Properties(); sessionConfig.put( "StrictHostKeyChecking", "no" ); Session result = null; result = SshUtil.getSessionObject( username, password, hostname, port, sessionConfig ); result.connect( 1000 ); boolean isResultNull = false; if ( result == null ) { isResultNull = true; } else { isResultNull = false; } logger.info( "[TS] : Expected result : Will throw an error when we will try to connect" ); result.connect( 1000 ); }