As part of my testing, I have to connect to Unix server using putty and execute few unix jobs.
Feature file Steps for test scenario:
Scenario Outline: Job execution in Unix Given I logged into unix server When I navigate to job directory "<job_directory_path>" and submit the job "<job>" Then I validate job run status code And close the session Examples: [email protected]:jobs | | | | | snapshot of excelsheet jobs.xlsx |jobdirectorypath|jobname| |/com/home/joblist|QAWAS12| |/com/home/joblist|QAWAS13|
Step definition file
@Given("^I logged into unix server$") public void i_logged_into_unix_server() throws Throwable { try{ Unixcon ucon = new Unixcon.getInstance(); uc.con_to_unix(); System.out.println("connected"); }catch(Exception e){ e.printStackTrace(); } } @When("^I navigate to job directory \"([^\"]*)\" and submit the job \"([^\"]*)\"$") public void i_navigate_to_job_directory_something_and_submit_the_job_something(String jobdirectorypath, String jobname) throws Throwable { try{ Unixcon ucon = new Unixcon.getInstance(); uc.execute_job(jobdirectorypath, jobname); System.out.println("job executed:"+jobname); }catch(Exception e){ e.printStackTrace(); } } @Then("^I validate job run status code$") public void i_validate_job_run_status_code() throws Throwable { try{ Unixcon ucon = new Unixcon.getInstance(); String status_code = String.valueOf(ucon.ch.getExitStatus()); if(status_code == 0){ System.out.println("Passed"); write_to_excel("status",status_code,"Passed"); }else{ System.out.println("Failed"); write_to_excel("status",status_code,"Failed"); }catch(Exception e){ e.printStackTrace(); } } @And("^close the session$") public void close_the_session() throws Throwable { try{ Unixcon ucon = new Unixcon.getInstance(); ucon.close_session(); System.out.println("closed"); }catch(Exception e){ e.printStackTrace(); } }
Unixcon.JAVA
public class Unixcon{ public JSch jsch =null; public Session session = nulll; public Channel ch =null; String host = TestProperty.getProperty("HOSTNAME"); String username = TestProperty.getProperty("USERNAME"); String password = TestProperty.getProperty("PASSWORD"); int port = TestProperty.getProperty("PORT"); private static Unixcon ucon; public static Unixcon getInstatnce(){ if(ucon==null){ ucon = new Unixcon(); } return ucon; } private Unixcon(){ super(); } public void con_to_unix(){ try{ jsch = new JSch(); session = jsch.getSession(username,host,port); session.setpassword(password); Properties p = new Properties(); p.put("StrictHostKeyChecking", "no"); session.setconfig(p); session.connect(); }catch(Exception e) { e.printStackTrace(); } } public void execute_job(String jobdirectorypath, String jobname){ try{ ch = session.OpenChannel("exec"); ((ChannelExec) ch).setCommand("cd" + jobdirectorypath +" && ./" + jobname); ch.connect(); InputStrem in = ch.getInputStream(); BufferedReader r = new BufferedReader(new InputStreamReader(in)); String l = ""; while((l=r.readLine()) != null){ System.out.println(l); } }catch(Exception e){ e.printStachTrace(); } } public void close_session() { try{ if(session != null) { ch.disconnect(); session.disconnect(); }catch(Exception e){ e.printStachTrace(); } } }
Is my implementation approch correct? Please provide your suggestions for code improvement.