diff options
author | Joss Armstrong <joss.armstrong@ericsson.com> | 2018-12-10 16:28:51 +0000 |
---|---|---|
committer | J Armstrong <joss.armstrong@ericsson.com> | 2018-12-11 22:00:19 +0000 |
commit | 9e63c1996f8433c147a95b09e25970e16af15db0 (patch) | |
tree | d31de8eff653de2214c593ecf36dc1f179bbc4c7 /appc-config/appc-config-adaptor | |
parent | 0190a9cfa67b3470d6cbdab2277f1b68a53d84bb (diff) |
Further fixes for APPC-1270
Fixed >100 sonar issues
Increased line coverage from 26% to 87%
Improved testing
Removal of redundant code in class under test
Issue-ID: APPC-1270
Change-Id: I63f53f03be815bb951d650df2f77999e5629ad75
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
Diffstat (limited to 'appc-config/appc-config-adaptor')
2 files changed, 714 insertions, 259 deletions
diff --git a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java index 28f00069a..d4deb1fa5 100644 --- a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java +++ b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications Copyright (C) 2018 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,8 +25,6 @@ package org.onap.appc.ccadaptor; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelShell; @@ -54,10 +54,7 @@ import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.StringTokenizer; -import java.util.concurrent.TimeUnit; -import javax.annotation.Nonnull; import org.apache.commons.lang.StringUtils; -import org.onap.appc.i18n.Msg; public class SshJcraftWrapper { @@ -71,7 +68,6 @@ public class SshJcraftWrapper { private String RouterName = null; private int BUFFER_SIZE = 512000; char[] charBuffer = new char[BUFFER_SIZE]; - // private int BUFFER_SIZE = 4000000; private DataInputStream dis = null; private BufferedReader reader = null; private BufferedWriter out = null; @@ -93,21 +89,20 @@ public class SshJcraftWrapper { private Runtime runtime = Runtime.getRuntime(); private DebugLog dbLog = new DebugLog(); - public void SshJcraftWrapper() { + public SshJcraftWrapper() { String fn = "SshJcraftWrapper.SshJcraftWrapper"; - debugLog.printRTAriDebug(fn, "SshJcraftWrapper has been instantated"); + DebugLog.printRTAriDebug(fn, "SshJcraftWrapper has been instantated"); routerLogFileName = "/tmp/" + host; - this.host = host; } public void connect(String hostname, String username, String password, String prompt, int timeOut) throws IOException { String fn = "SshJcraftWrapper.connect"; - jsch = new JSch(); - debugLog.printRTAriDebug(fn, + jsch = getJSch(); + DebugLog.printRTAriDebug(fn, "Attempting to connect to " + hostname + " username=" + username + " prompt='" + prompt + "' timeOut=" + timeOut); - debugLog.printRTAriDebug(fn, "Trace A"); + DebugLog.printRTAriDebug(fn, "Trace A"); RouterName = hostname; hostName = hostname; userName = username; @@ -126,16 +121,15 @@ public class SshJcraftWrapper { dis = new DataInputStream(inputStream); reader = new BufferedReader(new InputStreamReader(dis), BUFFER_SIZE); channel.connect(); - debugLog.printRTAriDebug(fn, "Successfully connected."); - debugLog.printRTAriDebug(fn, "Flushing input buffer"); + DebugLog.printRTAriDebug(fn, "Successfully connected."); + DebugLog.printRTAriDebug(fn, "Flushing input buffer"); try { receiveUntil(prompt, 3000, "No cmd was sent, just waiting"); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); - // dbLog.storeData("ErrorMsg= Exception trying to connect to "+hostname +" "+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); throw new IOException(e.toString()); } } @@ -144,7 +138,7 @@ public class SshJcraftWrapper { public void connect(String hostname, String username, String password, String prompt, int timeOut, int portNum) throws IOException { String fn = "SshJcraftWrapper.connect"; - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, ":Attempting to connect to " + hostname + " username=" + username + " prompt='" + prompt + "' timeOut=" + timeOut + " portNum=" + portNum); RouterName = hostname; @@ -152,14 +146,14 @@ public class SshJcraftWrapper { userName = username; passWord = password; RouterName = hostname; - jsch = new JSch(); + jsch = getJSch(); try { session = jsch.getSession(username, hostname, portNum); UserInfo ui = new MyUserInfo(); session.setPassword(password); session.setUserInfo(ui); session.setConfig("StrictHostKeyChecking", "no"); - debugLog.printRTAriDebug(fn, ":StrictHostKeyChecking set to 'no'"); + DebugLog.printRTAriDebug(fn, ":StrictHostKeyChecking set to 'no'"); session.connect(timeOut); session.setServerAliveCountMax( @@ -170,8 +164,8 @@ public class SshJcraftWrapper { dis = new DataInputStream(inputStream); reader = new BufferedReader(new InputStreamReader(dis), BUFFER_SIZE); channel.connect(); - debugLog.printRTAriDebug(fn, ":Successfully connected."); - debugLog.printRTAriDebug(fn, ":Flushing input buffer"); + DebugLog.printRTAriDebug(fn, ":Successfully connected."); + DebugLog.printRTAriDebug(fn, ":Flushing input buffer"); try { if (prompt.equals("]]>]]>")) { receiveUntil("]]>]]>", 10000, "No cmd was sent, just waiting"); @@ -179,13 +173,12 @@ public class SshJcraftWrapper { receiveUntil(":~#", 5000, "No cmd was sent, just waiting"); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception::: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception::: Nothing to flush out."); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, ":Caught an Exception. e=" + e); + DebugLog.printRTAriDebug(fn, ":Caught an Exception. e=" + e); dbLog.outputStackTrace(e); - // dbLog.storeData("ErrorMsg= Exception trying to connect to "+hostname +" "+e); throw new IOException(e.toString()); } } @@ -198,7 +191,7 @@ public class SshJcraftWrapper { boolean cliPromptCmd = false; StringBuffer sb2 = new StringBuffer(); StringBuffer sbReceive = new StringBuffer(); - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "delimeters='" + delimeters + "' timeout=" + timeout + " cmdThatWasSent='" + cmdThatWasSent + "'"); appendToFile(debugLogFileName, fn + " delimeters='" + delimeters + "' timeout=" + timeout + " cmdThatWasSent='" + cmdThatWasSent + "'\n"); @@ -212,20 +205,20 @@ public class SshJcraftWrapper { session.setTimeout(timeout); // This is the socket timeout value. while (!match) { if (new Date().getTime() > deadline) { - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "Throwing a TimedOutException: time in routine has exceed our deadline: RouterName:" + RouterName + " CmdThatWasSent=" + CmdThatWasSent); throw new TimedOutException("Timeout: time in routine has exceed our deadline"); } try { - Thread.sleep(500); + delay(500); } catch (java.lang.InterruptedException ee) { - boolean ignore = true; + Thread.currentThread().interrupt(); } int len = reader.read(charBuffer, 0, BUFFER_SIZE); appendToFile(debugLogFileName, fn + " After reader.read cmd: len=" + len + "\n"); if (len <= 0) { - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "Reader read " + len + " bytes. Looks like we timed out, router=" + RouterName); throw new TimedOutException("Received a SocketTimeoutException router=" + RouterName); } @@ -235,7 +228,7 @@ public class SshJcraftWrapper { // This is a IOS XR sw config file. We will write it to the disk. timeout = timeout * 2; deadline = new Date().getTime() + timeout; - debugLog.printRTAriDebug(fn, "IOS XR upload for software config: timeout=" + timeout); + DebugLog.printRTAriDebug(fn, "IOS XR upload for software config: timeout=" + timeout); StringTokenizer st = new StringTokenizer(cmdThatWasSent); st.nextToken(); routerFileName = st.nextToken(); @@ -243,10 +236,9 @@ public class SshJcraftWrapper { out = new BufferedWriter(fileWriter); routerLogFileName = "/tmp/" + RouterName; _tmpFile = new File(routerLogFileName); - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "Will write the swConfigFile to disk, routerFileName=" + routerFileName); } - int c; out.write(charBuffer, 0, len); out.flush(); appendToFile(debugLogFileName, fn + " Wrote " + len + " bytes to the disk\n"); @@ -286,31 +278,14 @@ public class SshJcraftWrapper { break; } } - } else { - debugLog.printRTAriDebug(fn, "cliPromptCmd, Trace 2"); - sb2.setLength(0); - for (int i = 0; i < len; i++) { - sbReceive.append((char) charBuffer[i]); - sb2.append((char) charBuffer[i]); - } - appendToRouterFile("/tmp/" + RouterName, sb2); - if (listener != null) { - listener.receivedString(sb2.toString()); - } - debugLog.printRTAriDebug(fn, "sb2='" + sb2.toString() + "' delimeters='" + delimeters + "'"); - if (sb2.toString().indexOf("\nariPrompt>") != -1) { - debugLog.printRTAriDebug(fn, "Found our prompt"); - match = true; - break; - } } } } catch (JSchException e) { - debugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e.toString()); + DebugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e.toString()); dbLog.outputStackTrace(e); throw new TimedOutException(e.toString()); } catch (IOException ee) { - debugLog.printRTAriDebug(fn, "Caught an IOException: ee=" + ee.toString()); + DebugLog.printRTAriDebug(fn, "Caught an IOException: ee=" + ee.toString()); dbLog.outputStackTrace(ee); throw new TimedOutException(ee.toString()); } finally { @@ -319,11 +294,11 @@ public class SshJcraftWrapper { fileWriter.close(); } } catch(IOException ex) { - debugLog.printRTAriDebug(fn, "Failed to close fileWriter output stream: ex=" + ex); + DebugLog.printRTAriDebug(fn, "Failed to close fileWriter output stream: ex=" + ex); } } String result = stripOffCmdFromRouterResponse(sbReceive.toString()); - debugLog.printRTAriDebug(fn, "Leaving method successfully"); + DebugLog.printRTAriDebug(fn, "Leaving method successfully"); return result; } @@ -372,7 +347,7 @@ public class SshJcraftWrapper { appendToFile(debugLogFileName, fn + " Looking for an delimeter of:'" + delimeter + "'\n"); appendToFile(debugLogFileName, fn + " receivedString='" + receivedString); if (aggregatedReceivedString.indexOf(delimeter) != -1) { - debugLog.printRTAriDebug(fn, "Found our delimeter, which was: '" + delimeter + "'"); + DebugLog.printRTAriDebug(fn, "Found our delimeter, which was: '" + delimeter + "'"); aggregatedReceivedString = ""; return (true); } @@ -400,7 +375,6 @@ public class SshJcraftWrapper { e.printStackTrace(); } } else { - // DebugLog.printAriDebug(fnName, "TRACE 1: ******************************"); // When looking at the end of the charBuffer, don't include any linefeeds or spaces. We only want to make the smallest string possible. for (x = len - 1; x >= 0; x--) { c = charBuffer[x]; @@ -447,7 +421,7 @@ public class SshJcraftWrapper { public void closeConnection() { String fn = "SshJcraftWrapper.closeConnection"; - debugLog.printRTAriDebug(fn, "Executing the closeConnection...."); + DebugLog.printRTAriDebug(fn, "Executing the closeConnection...."); inputStream = null; outputStream = null; dis = null; @@ -459,8 +433,7 @@ public class SshJcraftWrapper { public void send(String cmd) throws IOException { String fn = "SshJcraftWrapper.send"; OutputStream out = channel.getOutputStream(); - DataOutputStream dos = new DataOutputStream(out); - + DataOutputStream dos = getDataOutputStream(out); if ((cmd.charAt(cmd.length() - 1) != '\n') && (cmd.charAt(cmd.length() - 1) != '\r')) { cmd += "\n"; } @@ -471,8 +444,7 @@ public class SshJcraftWrapper { int ncharsSent = 0; appendToFile(debugLogFileName, fn + ": Sending: '" + cmd); - // debugLog.printRTAriDebug (fn, "cmd = "+cmd); - debugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 + DebugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 try { if (length > 600000) { int timeout = 9000; @@ -480,30 +452,30 @@ public class SshJcraftWrapper { String Cmd = cmd.substring(i, Math.min(length, i + nchars)); ncharsSent = Cmd.length(); ncharsTotalSent = ncharsTotalSent + Cmd.length(); - debugLog.printRTAriDebug(fn, "i=" + i + " Sending Cmd: ncharsSent=" + ncharsSent); + DebugLog.printRTAriDebug(fn, "i=" + i + " Sending Cmd: ncharsSent=" + ncharsSent); dos.writeBytes(Cmd); dos.flush(); try { - debugLog.printRTAriDebug(fn, ":::i=" + i + " length=" + length); + DebugLog.printRTAriDebug(fn, ":::i=" + i + " length=" + length); if (ncharsSent < length) { receiveUntilBufferFlush(ncharsSent, timeout, "buffer flush i=" + i); } else { - debugLog.printRTAriDebug(fn, "i=" + i + " No Waiting this time...."); + DebugLog.printRTAriDebug(fn, "i=" + i + " No Waiting this time...."); dos.flush(); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); } } } else { - debugLog.printRTAriDebug(fn, "Before executing the dos.writeBytes"); + DebugLog.printRTAriDebug(fn, "Before executing the dos.writeBytes"); dos.writeBytes(cmd); } dos.flush(); - debugLog.printRTAriDebug(fn, "Leaving method"); + DebugLog.printRTAriDebug(fn, "Leaving method"); appendToFile(debugLogFileName, fn + ": Leaving method\n"); } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); dbLog.outputStackTrace(e); throw new IOException(e.toString()); } @@ -513,13 +485,13 @@ public class SshJcraftWrapper { public void sendChar(int v) throws IOException { String fn = "SshJcraftWrapper.sendChar"; OutputStream out = channel.getOutputStream(); - DataOutputStream dos = new DataOutputStream(out); + DataOutputStream dos = getDataOutputStream(out); try { - debugLog.printRTAriDebug(fn, "Sending: '" + v + "'"); + DebugLog.printRTAriDebug(fn, "Sending: '" + v + "'"); dos.writeChar(v); dos.flush(); } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); throw new IOException(e.toString()); } } @@ -527,12 +499,12 @@ public class SshJcraftWrapper { public void send(byte[] b, int off, int len) throws IOException { String fn = "SshJcraftWrapper.send:byte[]"; OutputStream out = channel.getOutputStream(); - DataOutputStream dos = new DataOutputStream(out); + DataOutputStream dos = getDataOutputStream(out); try { dos.write(b, off, len); dos.flush(); } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); throw new IOException(e.toString()); } } @@ -584,15 +556,13 @@ public class SshJcraftWrapper { File tmpFile = new File(fileName); if (tmpFile.exists()) { try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) { - // out.write(dataToWrite); - // out.write(getTheDate() +": " +Thread.currentThread().getName() +": "+dataToWrite); out.write(getTheDate() + ": " + tId + ": " + dataToWrite); } } } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception: e=" + e); } } @@ -609,9 +579,9 @@ public class SshJcraftWrapper { } } } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException: e=" + e); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception: e=" + e); } } @@ -619,7 +589,6 @@ public class SshJcraftWrapper { public String getTheDate() { Calendar cal = Calendar.getInstance(); java.util.Date today = cal.getTime(); - DateFormat df1 = DateFormat.getDateInstance(); DateFormat df3 = new SimpleDateFormat("MM/dd/yyyy H:mm:ss "); return (df3.format(today)); } @@ -627,16 +596,14 @@ public class SshJcraftWrapper { public void appendToRouterFile(String fileName, StringBuffer dataToWrite) { String fnName = "SshJcraftWrapper.appendToRouterFile"; - debugLog.printRTAriDebug(fnName, "Entered.... "); + DebugLog.printRTAriDebug(fnName, "Entered.... "); try { // First check to see if a file 'fileName' exist, if it does // write to it. If it does not exist, don't write to it. File tmpFile = new File(fileName); { - // if ((tmpFile.exists()) && (tmpFile.setWritable(true, true))) if (tmpFile.exists()) { try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) { - // out.write("<!-- "+getTheDate() +": " +tId +" -->\n"); out.write(dataToWrite.toString()); } } @@ -648,16 +615,12 @@ public class SshJcraftWrapper { } public void appendToRouterFile(String fileName, int len) { - String fnName = "SshJcraftWrapper.appendToFile"; - // debugLog.printRTAriDebug (fnName, "Entered.... len="+len); try { // First check to see if a file 'fileName' exist, if it does // write to it. If it does not exist, don't write to it. File tmpFile = new File(fileName); - // if ((tmpFile.exists()) && (tmpFile.setWritable(true, true))) if (tmpFile.exists()) { try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) { - // out.write("<!-- "+getTheDate() +": " +tId +" -->\n"); out.write(charBuffer, 0, len); } } @@ -683,18 +646,14 @@ public class SshJcraftWrapper { } public String stripOffCmdFromRouterResponse(String routerResponse) { - String fn = "SshJcraftWrapper.stripOffCmdFromRouterResponse"; - // appendToFile(debugLogFileName, fn+": routerResponse='"+routerResponse +"'\n"); - // The session of SSH will echo the command sent to the router, in the router's response. // Since all our commands are terminated by a '\n', strip off the first line - // of the response from the router. This first line contains the orginal command. + // of the response from the router. This first line contains the original command. StringTokenizer rr = new StringTokenizer(routerResponse, "\n"); StringBuffer sb = new StringBuffer(); int numTokens = rr.countTokens(); - // debugLog.printRTAriDebug (fn, "Number of lines in the response from the router is:" +numTokens); if (numTokens > 1) { rr.nextToken(); //Skip the first line. while (rr.hasMoreTokens()) { @@ -707,7 +666,7 @@ public class SshJcraftWrapper { public void setRouterCommandType(String type) { String fn = "SshJcraftWrapper.setRouterCommandType"; this.routerCmdType = type; - debugLog.printRTAriDebug(fn, "Setting routerCmdType to a value of '" + type + "'"); + DebugLog.printRTAriDebug(fn, "Setting routerCmdType to a value of '" + type + "'"); } public String getLastFewLinesOfFile(File file, int linesToRead) throws IOException { @@ -725,7 +684,6 @@ public class SshJcraftWrapper { builder.append(c); if (c == '\n') { builder = builder.reverse(); - // System.out.println(builder.toString()); tail = builder.toString() + tail; lines++; builder.setLength(0); @@ -736,7 +694,7 @@ public class SshJcraftWrapper { } } if (!jcraftReadSwConfigFileFromDisk()) { - debugLog.printRTAriDebug(fn, "tail='" + tail + "'"); + DebugLog.printRTAriDebug(fn, "tail='" + tail + "'"); } appendToFile(debugLogFileName, "tail='" + tail + "'\n"); return tail; @@ -767,12 +725,9 @@ public class SshJcraftWrapper { public void receiveUntilBufferFlush(int ncharsSent, int timeout, String message) throws TimedOutException, IOException { String fn = "SshJcraftWrapper.receiveUntilBufferFlush"; - StringBuffer sb2 = new StringBuffer(); - StringBuffer sbReceive = new StringBuffer(); - debugLog.printRTAriDebug(fn, "ncharsSent=" + ncharsSent + " timeout=" + timeout + " " + message); + DebugLog.printRTAriDebug(fn, "ncharsSent=" + ncharsSent + " timeout=" + timeout + " " + message); int ncharsTotalReceived = 0; int ncharsRead = 0; - boolean flag = false; charactersFromBufferFlush.setLength(0); long deadline = new Date().getTime() + timeout; @@ -781,10 +736,9 @@ public class SshJcraftWrapper { session.setTimeout(timeout); // This is the socket timeout value. while (true) { if (new Date().getTime() > deadline) { - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "Throwing a TimedOutException: time in routine has exceed our deadline: ncharsSent=" + ncharsSent + " ncharsTotalReceived=" + ncharsTotalReceived); - flag = true; throw new TimedOutException("Timeout: time in routine has exceed our deadline"); } ncharsRead = reader.read(charBuffer, 0, BUFFER_SIZE); @@ -793,9 +747,8 @@ public class SshJcraftWrapper { } appendToRouterFile("/tmp/" + RouterName, ncharsRead); ncharsTotalReceived = ncharsTotalReceived + ncharsRead; - // debugLog.printRTAriDebug (fn, "::ncharsSent="+ncharsSent+" ncharsTotalReceived="+ncharsTotalReceived +" ncharsRead="+ncharsRead); if (ncharsTotalReceived >= ncharsSent) { - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "Received the correct number of characters, ncharsSent=" + ncharsSent + " ncharsTotalReceived=" + ncharsTotalReceived); logMemoryUsage(); @@ -803,8 +756,8 @@ public class SshJcraftWrapper { } } } catch (JSchException e) { - debugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e); - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e); + DebugLog.printRTAriDebug(fn, "ncharsSent=" + ncharsSent + " ncharsTotalReceived=" + ncharsTotalReceived + " ncharsRead=" + ncharsRead); throw new TimedOutException(e.toString()); @@ -824,26 +777,8 @@ public class SshJcraftWrapper { } public void sftpPut(String sourcePath, String destDirectory) throws IOException { - String fn = "SshJcraftWrapper.sftp"; - try { - Session sftpSession = jsch.getSession(userName, hostName, 22); - UserInfo ui = new MyUserInfo(); - sftpSession.setPassword(passWord); - sftpSession.setUserInfo(ui); - sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); - ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); - sftp.connect(); - debugLog.printRTAriDebug(fn, "Sending " + sourcePath + " --> " + destDirectory); - sftp.put(sourcePath, destDirectory, ChannelSftp.OVERWRITE); - debugLog.printRTAriDebug(fn, "Sent successfully"); - sftpSession.disconnect(); - } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); - // dbLog.storeData("ErrorMsg= sftp threw an Exception. error is:"+e); - throw new IOException(e.toString()); - } + // delegate to duplicate method + put(sourcePath, destDirectory); } @@ -855,18 +790,17 @@ public class SshJcraftWrapper { sftpSession.setPassword(passWord); sftpSession.setUserInfo(ui); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); InputStream is = new ByteArrayInputStream(stringOfData.getBytes()); - debugLog.printRTAriDebug(fn, "Sending stringOfData --> " + fullPathDest); + DebugLog.printRTAriDebug(fn, "Sending stringOfData --> " + fullPathDest); sftp.put(is, fullPathDest, ChannelSftp.OVERWRITE); - debugLog.printRTAriDebug(fn, "Sent successfully"); + DebugLog.printRTAriDebug(fn, "Sent successfully"); sftpSession.disconnect(); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); - // dbLog.storeData("ErrorMsg= sftp threw an Exception. error is:"+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } } @@ -879,20 +813,18 @@ public class SshJcraftWrapper { sftpSession.setPassword(passWord); sftpSession.setUserInfo(ui); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); InputStream in = null; in = sftp.get(fullFilePathName); String sftpFileString = readInputStreamAsString(in); - debugLog.printRTAriDebug(fn, "Retreived successfully"); - // debugLog.printRTAriDebug (fn, "sftpFileString="+sftpFileString); + DebugLog.printRTAriDebug(fn, "Retreived successfully"); sftpSession.disconnect(); return (sftpFileString); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); - // dbLog.storeData("ErrorMsg= sftp threw an Exception. error is:"+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } } @@ -935,11 +867,11 @@ public class SshJcraftWrapper { String subsystem) throws IOException { String fn = "SshJcraftWrapper.connect"; - debugLog.printRTAriDebug(fn, + DebugLog.printRTAriDebug(fn, ":::Attempting to connect to " + hostname + " username=" + username + " prompt='" + prompt + "' timeOut=" + timeOut + " portNum=" + portNum + " subsystem=" + subsystem); RouterName = hostname; - jsch = new JSch(); + jsch = getJSch(); try { session = jsch.getSession(username, hostname, portNum); UserInfo ui = new MyUserInfo(); @@ -951,32 +883,31 @@ public class SshJcraftWrapper { 0); // If this is not set to '0', then socket timeout on all reads will not work!!!! channel = session.openChannel("subsystem"); ((ChannelSubsystem) channel).setSubsystem(subsystem); - // ((ChannelSubsystem)channel).setPtyType("vt102"); ((ChannelSubsystem) channel).setPty(true); inputStream = channel.getInputStream(); dis = new DataInputStream(inputStream); reader = new BufferedReader(new InputStreamReader(dis), BUFFER_SIZE); channel.connect(); - debugLog.printRTAriDebug(fn, "Successfully connected."); - debugLog.printRTAriDebug(fn, "Five second sleep...."); + DebugLog.printRTAriDebug(fn, "Successfully connected."); + DebugLog.printRTAriDebug(fn, "Five second sleep...."); try { - Thread.sleep(5000); + delay(5000); } catch (java.lang.InterruptedException ee) { - boolean ignore = true; + Thread.currentThread().interrupt(); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); throw new IOException(e.toString()); } } public void connect(String hostName, String username, String password, int portNumber) throws IOException { String fn = "SshJcraftWrapper.connect"; - jsch = new JSch(); - debugLog.printRTAriDebug(fn, + jsch = getJSch(); + DebugLog.printRTAriDebug(fn, "::Attempting to connect to " + hostName + " username=" + username + " portNumber=" + portNumber); - debugLog.printRTAriDebug(fn, "Trace C"); + DebugLog.printRTAriDebug(fn, "Trace C"); RouterName = hostName; this.hostName = hostName; userName = username; @@ -985,7 +916,6 @@ public class SshJcraftWrapper { java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session = jsch.getSession(username, hostName, 22); - // session = jsch.getSession(username, hostName, portNumber); UserInfo ui = new MyUserInfo(); session.setConfig(config); session.setPassword(password); @@ -999,17 +929,16 @@ public class SshJcraftWrapper { dis = new DataInputStream(inputStream); reader = new BufferedReader(new InputStreamReader(dis), BUFFER_SIZE); channel.connect(); - debugLog.printRTAriDebug(fn, "::Successfully connected."); - debugLog.printRTAriDebug(fn, "::Flushing input buffer"); + DebugLog.printRTAriDebug(fn, "::Successfully connected."); + DebugLog.printRTAriDebug(fn, "::Flushing input buffer"); try { receiveUntil(":~#", 9000, "No cmd was sent, just waiting, but we can stop on a '~#'"); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception::: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception::: Nothing to flush out."); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); - // dbLog.storeData("ErrorMsg= Exception trying to connect to "+hostName +" "+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception. e=" + e); throw new IOException(e.toString()); } } @@ -1023,17 +952,16 @@ public class SshJcraftWrapper { sftpSession.setPassword(passWord); sftpSession.setUserInfo(ui); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); - debugLog.printRTAriDebug(fn, "Sending " + sourcePath + " --> " + destDirectory); + DebugLog.printRTAriDebug(fn, "Sending " + sourcePath + " --> " + destDirectory); sftp.put(sourcePath, destDirectory, ChannelSftp.OVERWRITE); - debugLog.printRTAriDebug(fn, "Sent successfully"); + DebugLog.printRTAriDebug(fn, "Sent successfully"); sftpSession.disconnect(); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); - // dbLog.storeData("ErrorMsg= sftp threw an Exception. error is:"+e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } } @@ -1043,8 +971,8 @@ public class SshJcraftWrapper { String fn = "SshJcraftWrapper.put"; Session sftpSession = null; try { - debugLog.printRTAriDebug(fn, "userName=" + userName + " hostName=" + hostName); - jsch = new JSch(); + DebugLog.printRTAriDebug(fn, "userName=" + userName + " hostName=" + hostName); + jsch = getJSch(); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); sftpSession = jsch.getSession(userName, hostName, 22); @@ -1053,29 +981,28 @@ public class SshJcraftWrapper { sftpSession.setUserInfo(ui); sftpSession.setConfig(config); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); String oldFiles = fullPathDest + "*"; - debugLog.printRTAriDebug(fn, "Deleting old files --> " + oldFiles); + DebugLog.printRTAriDebug(fn, "Deleting old files --> " + oldFiles); try { sftp.rm(oldFiles); - debugLog.printRTAriDebug(fn, "Sending stringOfData --> " + fullPathDest); + DebugLog.printRTAriDebug(fn, "Sending stringOfData --> " + fullPathDest); } catch (SftpException sft) { String exp = "No such file"; if (sft.getMessage() != null && sft.getMessage().contains(exp)) { - debugLog.printRTAriDebug(fn, "No files found -- Continue"); + DebugLog.printRTAriDebug(fn, "No files found -- Continue"); } else { - debugLog.printRTAriDebug(fn, "Exception while sftp.rm " + sft.getMessage()); - sft.printStackTrace(); + DebugLog.printRTAriDebug(fn, "Exception while sftp.rm " + sft.getMessage()); throw sft; } } sftp.put(is, fullPathDest, ChannelSftp.OVERWRITE); - debugLog.printRTAriDebug(fn, "Sent successfully"); + DebugLog.printRTAriDebug(fn, "Sent successfully"); } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } finally { if(sftpSession != null) { @@ -1089,8 +1016,8 @@ public class SshJcraftWrapper { String fn = "SshJcraftWrapper.get"; Session sftpSession = null; try { - debugLog.printRTAriDebug(fn, "userName=" + userName + " hostName=" + hostName); - jsch = new JSch(); + DebugLog.printRTAriDebug(fn, "userName=" + userName + " hostName=" + hostName); + jsch = getJSch(); sftpSession = jsch.getSession(userName, hostName, 22); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); @@ -1099,16 +1026,16 @@ public class SshJcraftWrapper { sftpSession.setUserInfo(ui); sftpSession.setConfig(config); sftpSession.connect(30 * 1000); - debugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); + DebugLog.printRTAriDebug(fn, "Opening up an sftp channel...."); ChannelSftp sftp = (ChannelSftp) sftpSession.openChannel("sftp"); - debugLog.printRTAriDebug(fn, "Connecting...."); + DebugLog.printRTAriDebug(fn, "Connecting...."); sftp.connect(); InputStream in = sftp.get(fullFilePathName); String sftpFileString = readInputStreamAsString(in); - debugLog.printRTAriDebug(fn, "Retreived successfully"); + DebugLog.printRTAriDebug(fn, "Retreived successfully"); return sftpFileString; } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an Exception, e=" + e); throw new IOException(e.toString()); } finally { if(sftpSession != null) { @@ -1120,7 +1047,7 @@ public class SshJcraftWrapper { public String send(String cmd, String delimiter) throws IOException { String fn = "SshJcraftWrapper.send"; OutputStream out = channel.getOutputStream(); - DataOutputStream dos = new DataOutputStream(out); + DataOutputStream dos = getDataOutputStream(out); if ((cmd.charAt(cmd.length() - 1) != '\n') && (cmd.charAt(cmd.length() - 1) != '\r')) { cmd += "\n"; @@ -1131,8 +1058,7 @@ public class SshJcraftWrapper { int ncharsTotalSent = 0; int ncharsSent = 0; - debugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 - debugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 + DebugLog.printRTAriDebug(fn, "Length of cmd is:" + length); // 2,937,706 try { if (length > 600000) { int timeout = 9000; @@ -1140,35 +1066,45 @@ public class SshJcraftWrapper { String Cmd = cmd.substring(i, Math.min(length, i + nchars)); ncharsSent = Cmd.length(); ncharsTotalSent = ncharsTotalSent + Cmd.length(); - debugLog.printRTAriDebug(fn, "i=" + i + " Sending Cmd: ncharsSent=" + ncharsSent); + DebugLog.printRTAriDebug(fn, "i=" + i + " Sending Cmd: ncharsSent=" + ncharsSent); dos.writeBytes(Cmd); dos.flush(); try { - debugLog.printRTAriDebug(fn, ":::i=" + i + " length=" + length); + DebugLog.printRTAriDebug(fn, ":::i=" + i + " length=" + length); if (ncharsSent < length) { receiveUntilBufferFlush(ncharsSent, timeout, "buffer flush i=" + i); } else { - debugLog.printRTAriDebug(fn, "i=" + i + " No Waiting this time...."); + DebugLog.printRTAriDebug(fn, "i=" + i + " No Waiting this time...."); dos.flush(); } } catch (Exception e) { - debugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); + DebugLog.printRTAriDebug(fn, "Caught an Exception: Nothing to flush out."); } } } else { - debugLog.printRTAriDebug(fn, "Before executing the dos.writeBytes"); + DebugLog.printRTAriDebug(fn, "Before executing the dos.writeBytes"); dos.writeBytes(cmd); } dos.flush(); // Now lets get the response. String response = receiveUntil(delimiter, 300000, cmd); - debugLog.printRTAriDebug(fn, "Leaving method"); + DebugLog.printRTAriDebug(fn, "Leaving method"); return (response); } catch (IOException e) { - debugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); + DebugLog.printRTAriDebug(fn, "Caught an IOException. e=" + e); throw new IOException(e.toString()); } } + protected JSch getJSch() { + return new JSch(); + } + protected DataOutputStream getDataOutputStream(OutputStream out) { + return new DataOutputStream(out); + } + + protected void delay(int milliseconds) throws InterruptedException { + Thread.sleep(5000); + } } diff --git a/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/SshJcraftWrapperTest.java b/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/SshJcraftWrapperTest.java index 7bf6b77d3..70de9e63a 100644 --- a/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/SshJcraftWrapperTest.java +++ b/appc-config/appc-config-adaptor/provider/src/test/java/org/onap/appc/ccadaptor/SshJcraftWrapperTest.java @@ -7,6 +7,8 @@ * Copyright (C) 2017 Amdocs * ============================================================================= * Modifications Copyright (C) 2018 IBM. + * ================================================================================ + * Modifications Copyright (C) 2018 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,28 +24,14 @@ * * ============LICENSE_END========================================================= */ - + package org.onap.appc.ccadaptor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.fail; -import static org.mockito.BDDMockito.given; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.inOrder; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - +import com.google.common.base.Charsets; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelShell; import com.jcraft.jsch.ChannelSubsystem; @@ -51,82 +39,619 @@ import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; -import com.jcraft.jsch.UserInfo; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.DataOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.URL; - -import org.apache.commons.lang.StringUtils; +import org.apache.commons.io.IOUtils; import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; -import org.mockito.InOrder; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.Mockito; +import org.mockito.internal.util.reflection.Whitebox; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.onap.appc.ccadaptor.SshJcraftWrapper.MyUserInfo; -import org.apache.commons.io.IOUtils; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; -@RunWith(MockitoJUnitRunner.class) +@RunWith(PowerMockRunner.class) +@PrepareForTest(DataOutputStream.class) public class SshJcraftWrapperTest { + + private SshJcraftWrapper wrapper; + private File debugFile; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Before + public void setupForTests() throws IOException, InterruptedException { + wrapper = Mockito.spy(new SshJcraftWrapper()); + Mockito.doNothing().when(wrapper).delay(Mockito.anyInt()); + debugFile = new File("src/test/resources/sshJcraftWrapperDebug"); + File debugFile2 = new File("src/test/resources/sshJcraftWrapperDEBUG"); + File configFile = new File("src/test/resources/jcraftReadSwConfigFileFromDisk"); + File proxyRouterLogFile = new File("src/test/resources/proxyRouterLogFile"); + debugFile.getParentFile().mkdirs(); + debugFile.createNewFile(); + debugFile2.createNewFile(); + configFile.createNewFile(); + proxyRouterLogFile.createNewFile(); + Whitebox.setInternalState(wrapper, "debugLogFileName", + "src/test/resources/sshJcraftWrapperDebug"); + Whitebox.setInternalState(wrapper, "extraDebugFile", debugFile2); + Whitebox.setInternalState(wrapper, "jcraftReadSwConfigFileFromDisk", configFile); + } + @Test - public void TestCheckIfReceivedStringMatchesDelimeter(){ - SshJcraftWrapper wrapper = new SshJcraftWrapper(); - wrapper.getTheDate(); - boolean result = wrapper.checkIfReceivedStringMatchesDelimeter("#", "test#", "test#"); - Assert.assertEquals(true, result); + public void testConnect() throws IOException, JSchException, InterruptedException { + JSch mockJSch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + InputStream stubInputStream = IOUtils.toInputStream("hello\n]]>]]>", Charsets.UTF_8); + Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell"); + Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000); + Mockito.verify(wrapper, Mockito.times(9)).appendToFile(Mockito.anyString(), + Mockito.anyString()); } @Test - public void testRemoveWhiteSpaceAndNewLineCharactersAroundString(){ - SshJcraftWrapper wrapper = new SshJcraftWrapper(); - String nameSpace = wrapper.removeWhiteSpaceAndNewLineCharactersAroundString("namespace "); - Assert.assertEquals("namespace", nameSpace); + public void testConnectExceptionFlow() throws IOException, JSchException { + JSch mockJSch = Mockito.mock(JSch.class); + Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("com.jcraft.jsch.JSchException"); + wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000); } @Test - public void testStripOffCmdFromRouterResponse(){ - SshJcraftWrapper wrapper = new SshJcraftWrapper(); - String result = wrapper.stripOffCmdFromRouterResponse("test\nsuccess"); - Assert.assertEquals("success\n", result); + public void testConnectExceptionFlow2() throws IOException, JSchException { + JSch mockJSch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell"); + Mockito.doThrow(new IOException()).when(wrapper).receiveUntil("]]>]]>", 3000, + "No cmd was sent, just waiting"); + Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000); + Mockito.verify(wrapper, Mockito.times(1)).receiveUntil("]]>]]>", 3000, + "No cmd was sent, just waiting"); } - //@Test - public void testGetLastFewLinesOfFile() throws FileNotFoundException, IOException{ - SshJcraftWrapper wrapper = new SshJcraftWrapper(); - URL path = SshJcraftWrapperTest.class.getResource("Test"); - File file = new File(path.getFile()); - String value = wrapper.getLastFewLinesOfFile(file,1); - Assert.assertEquals("\nTest data 3", value); + @Test + public void testConnectWithPortNumber() throws IOException, JSchException { + JSch mockJSch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + InputStream stubInputStream = IOUtils.toInputStream("hello\n:~#", Charsets.UTF_8); + Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell"); + Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + wrapper.connect("testHost", "testUser", "testPswd", ":~#", 1000, 22); + Mockito.verify(wrapper, Mockito.times(9)).appendToFile(Mockito.anyString(), + Mockito.anyString()); } - @Test(expected=Exception.class) - public void testSetRouterCommandType() throws IOException{ - SshJcraftWrapper wrapper = new SshJcraftWrapper(); - wrapper.setRouterCommandType("test"); - wrapper.receiveUntil("test", 2, "test"); + @Test + public void testConnectWithPortNumberSuccessFlow2() + throws IOException, JSchException, InterruptedException { + JSch mockJSch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + InputStream stubInputStream = IOUtils.toInputStream("hello\n]]>]]>", Charsets.UTF_8); + Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell"); + Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000, 22); + Mockito.verify(wrapper, Mockito.times(9)).appendToFile(Mockito.anyString(), + Mockito.anyString()); + } + + @Test + public void testConnectWithPortNumberExceptionFlow() throws IOException, JSchException { + JSch mockJSch = Mockito.mock(JSch.class); + Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("com.jcraft.jsch.JSchException"); + wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000, 22); + } + + @Test + public void testConnectWithPortNumberExceptionFlow2() throws IOException, JSchException { + JSch mockJSch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + InputStream stubInputStream = IOUtils.toInputStream("", Charsets.UTF_8); + Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell"); + Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000, 22); + Mockito.verify(wrapper, Mockito.times(1)).receiveUntil("]]>]]>", 10000, + "No cmd was sent, just waiting"); + } + + @Test + public void testReceiveUntilTimeout() throws TimedOutException, IOException, JSchException { + Session mockSession = Mockito.mock(Session.class); + Whitebox.setInternalState(wrapper, "session", mockSession); + // The sleep is required to make sure that the system clock has incremented by + // (at least) 1 millisecond between passing in our timeout value and setting a deadline + // and checking to see if the deadline has been missed + Mockito.doAnswer(new Answer<String>() { + @Override + public String answer(InvocationOnMock invocation) throws InterruptedException { + Thread.sleep(1); + return null; + } + }).when(mockSession).setTimeout(0); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("Timeout: time in routine has exceed our deadline"); + wrapper.receiveUntil("", 0, ""); + } + + @Test + public void testReceiveUntilReaderTimeout() + throws TimedOutException, IOException, JSchException { + Session mockSession = Mockito.mock(Session.class); + Whitebox.setInternalState(wrapper, "session", mockSession); + BufferedReader mockReader = Mockito.mock(BufferedReader.class); + Whitebox.setInternalState(wrapper, "reader", mockReader); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("Received a SocketTimeoutException router="); + wrapper.receiveUntil("", 3000, ""); } @Test - public void testValues() throws IOException{ + public void testReceiveUntilIosXr() throws TimedOutException, IOException, JSchException { + Session mockSession = Mockito.mock(Session.class); + Whitebox.setInternalState(wrapper, "session", mockSession); + BufferedReader mockReader = Mockito.mock(BufferedReader.class); + Mockito.doReturn(3).when(mockReader).read(Mockito.anyObject(), Mockito.anyInt(), + Mockito.anyInt()); + Whitebox.setInternalState(wrapper, "reader", mockReader); + Mockito.doReturn(false).when(wrapper).jcraftReadSwConfigFileFromDisk(); + Mockito.doReturn("\nXML>").when(wrapper).getLastFewLinesOfFile(Mockito.anyObject(), + Mockito.anyInt()); + + assertNull(wrapper.receiveUntil("]]>]]>", 3000, "IOS_XR_uploadedSwConfigCmd\nOTHER\nXML>")); + } + + @Test + public void testReceiveStringDelimiters() { + assertEquals(false, wrapper.checkIfReceivedStringMatchesDelimeter("#$", "", "")); + } + + @Test + public void testReceiveStringDelimitersShowConfig() { + assertEquals(true, wrapper.checkIfReceivedStringMatchesDelimeter("]]>]]>", "]]>]]>\n #", + "show config")); + } + + @Test + public void testReceiveStringDelimitersTwoArg() throws IOException { + SshJcraftWrapper localWrapper = Mockito.spy(new SshJcraftWrapper()); + Mockito.doReturn(true).when(localWrapper).jcraftReadSwConfigFileFromDisk(); + Mockito.doThrow(new IOException()).when(localWrapper) + .getLastFewLinesOfFile(Mockito.anyObject(), Mockito.anyInt()); + Whitebox.setInternalState(localWrapper, "routerFileName", "DUMMY_FILE_NAME"); + assertEquals(false, localWrapper.checkIfReceivedStringMatchesDelimeter(3, "]]>]]>\n #")); + } + + @Test + public void testCloseConnection() { + Session mockSession = Mockito.mock(Session.class); + Whitebox.setInternalState(wrapper, "session", mockSession); + wrapper.closeConnection(); + Mockito.verify(mockSession, Mockito.times(1)).disconnect(); + } + + @Test + public void testSend() throws IOException { + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class); + Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream(); + DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream)); + PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject()); + Whitebox.setInternalState(wrapper, "channel", mockChannel); + wrapper.send("TEST COMMAND\n"); + Mockito.verify(wrapper, Mockito.times(2)).appendToFile(Mockito.anyString(), + Mockito.anyString()); + } + + @Test + public void testSendExceptionFlow() throws IOException { + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class); + Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream(); + DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream)); + PowerMockito.doThrow(new IOException()).when(mockDos).flush(); + PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject()); + Whitebox.setInternalState(wrapper, "channel", mockChannel); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("java.io.IOException"); + wrapper.send("TEST COMMAND"); + } + + @Test + public void testSendChar() throws IOException { + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class); + Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream(); + DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream)); + PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject()); + Whitebox.setInternalState(wrapper, "channel", mockChannel); + wrapper.sendChar(74); + Mockito.verify(mockDos).flush(); + } + + @Test + public void testSendCharExceptionFlow() throws IOException { + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class); + Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream(); + DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream)); + PowerMockito.doThrow(new IOException()).when(mockDos).flush(); + PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject()); + Whitebox.setInternalState(wrapper, "channel", mockChannel); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("java.io.IOException"); + wrapper.sendChar(65); + } + + @Test + public void testSendByteArrayExceptionFlow() throws IOException { + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class); + Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream(); + DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream)); + PowerMockito.doThrow(new IOException()).when(mockDos).flush(); + PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject()); + Whitebox.setInternalState(wrapper, "channel", mockChannel); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("java.io.IOException"); + byte[] byteArray = new byte[] {65, 74}; + wrapper.send(byteArray, 0, 2); + } + + @Test + public void testGetLastFewLinesOfFile() throws FileNotFoundException, IOException { + File file = new File("src/test/resources/TEST_FILE.txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(file.getPath())); + writer.write("line1\nline2"); + writer.flush(); + writer.close(); + assertEquals("\nline2", wrapper.getLastFewLinesOfFile(file, 2)); + } + + @Test + public void testReceiveUntilBufferFlush() throws TimedOutException, IOException { + Session mockSession = Mockito.mock(Session.class); + Whitebox.setInternalState(wrapper, "session", mockSession); + BufferedReader mockReader = Mockito.mock(BufferedReader.class); + Mockito.doReturn(12).when(mockReader).read(Mockito.anyObject(), Mockito.anyInt(), + Mockito.anyInt()); + Whitebox.setInternalState(wrapper, "reader", mockReader); + + wrapper.receiveUntilBufferFlush(12, 100, "TEST_MESSAGE"); + Mockito.verify(wrapper, Mockito.times(2)).logMemoryUsage(); + } + + @Test + public void testReceiveUntilBufferFlushTimeout() + throws TimedOutException, IOException, JSchException { + Session mockSession = Mockito.mock(Session.class); + // The sleep is required to make sure that the system clock has incremented by + // (at least) 1 millisecond between passing in our timeout value and setting a deadline + // and checking to see if the deadline has been missed + Mockito.doAnswer(new Answer<String>() { + @Override + public String answer(InvocationOnMock invocation) throws InterruptedException { + Thread.sleep(1); + return null; + } + }).when(mockSession).setTimeout(0); + Whitebox.setInternalState(wrapper, "session", mockSession); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("Timeout: time in routine has exceed our deadline"); + wrapper.receiveUntilBufferFlush(10, 0, "TEST_MESSAGE"); + } + + @Test + public void testReceiveUntilBufferFlushJSchException() + throws TimedOutException, IOException, JSchException { + Session mockSession = Mockito.mock(Session.class); + Mockito.doThrow(new JSchException()).when(mockSession).setTimeout(0); + Whitebox.setInternalState(wrapper, "session", mockSession); + expectedEx.expect(TimedOutException.class); + expectedEx.expectMessage("com.jcraft.jsch.JSchException"); + wrapper.receiveUntilBufferFlush(10, 0, "TEST_MESSAGE"); + } + + @Test + public void testSftpPutSourceToDest() throws JSchException, IOException { + Whitebox.setInternalState(wrapper, "hostName", "testHost"); + Whitebox.setInternalState(wrapper, "userName", "testUser"); + Whitebox.setInternalState(wrapper, "passWord", "testPwd"); + ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class); + JSch mockJsch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22); + Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString()); + Mockito.doNothing().when(mockSession).connect(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp"); + Whitebox.setInternalState(wrapper, "jsch", mockJsch); + wrapper.sftpPut("DUMMY_SRC_PATH", "DUMMY_DEST_DIRECTORY"); + Mockito.verify(mockSession).disconnect(); + } + + @Test + public void testSftpPutSourceToDestExceptionFlow() throws JSchException, IOException { + JSch mockJsch = Mockito.mock(JSch.class); + Mockito.doThrow(new JSchException()).when(mockJsch).getSession(null, null, 22); + Whitebox.setInternalState(wrapper, "jsch", mockJsch); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("com.jcraft.jsch.JSchException"); + wrapper.sftpPut("DUMMY_SRC_PATH", "DUMMY_DEST_DIRECTORY"); + } + + @Test + public void testSftpPutStringToDest() throws JSchException, IOException { + Whitebox.setInternalState(wrapper, "hostName", "testHost"); + Whitebox.setInternalState(wrapper, "userName", "testUser"); + Whitebox.setInternalState(wrapper, "passWord", "testPwd"); + ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class); + JSch mockJsch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22); + Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString()); + Mockito.doNothing().when(mockSession).connect(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp"); + Whitebox.setInternalState(wrapper, "jsch", mockJsch); + wrapper.SftpPut("DUMMY_STRING", "DUMMY_DEST_DIRECTORY"); + Mockito.verify(mockSession).disconnect(); + } + + @Test + public void testSftpPutStringToDestExceptionFlow() throws JSchException, IOException { + JSch mockJsch = Mockito.mock(JSch.class); + Mockito.doThrow(new JSchException()).when(mockJsch).getSession(null, null, 22); + Whitebox.setInternalState(wrapper, "jsch", mockJsch); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("com.jcraft.jsch.JSchException"); + wrapper.SftpPut("DUMMY_STRING", "DUMMY_DEST_DIRECTORY"); + } + + @Test + public void testSftpGet() throws JSchException, IOException, SftpException { + File file = new File("src/test/resources/TEST_FILE.txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(file.getPath())); + writer.write("line1\nline2"); + writer.flush(); + Whitebox.setInternalState(wrapper, "hostName", "testHost"); + Whitebox.setInternalState(wrapper, "userName", "testUser"); + Whitebox.setInternalState(wrapper, "passWord", "testPwd"); + ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class); + JSch mockJsch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22); + Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString()); + Mockito.doNothing().when(mockSession).connect(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp"); + Mockito.doReturn(new FileInputStream(file)).when(mockChannel) + .get("src/test/resources/TEST_FILE.txt"); + Whitebox.setInternalState(wrapper, "jsch", mockJsch);; + assertEquals("line1\nline2", wrapper.sftpGet("src/test/resources/TEST_FILE.txt")); + } + + @Test + public void testSftpGetExceptionFlow() throws JSchException, IOException { + JSch mockJsch = Mockito.mock(JSch.class); + Mockito.doThrow(new JSchException()).when(mockJsch).getSession(null, null, 22); + Whitebox.setInternalState(wrapper, "jsch", mockJsch); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("com.jcraft.jsch.JSchException"); + wrapper.sftpGet("DUMMY_FILE_PATH"); + } + + @Test + public void testConnectWithSubsystem() throws IOException, JSchException, InterruptedException { + JSch mockJSch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + ChannelSubsystem mockChannel = Mockito.mock(ChannelSubsystem.class); + InputStream stubInputStream = IOUtils.toInputStream("hello\n:~#", Charsets.UTF_8); + Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("subsystem"); + Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + wrapper.connect("testHost", "testUser", "testPswd", ":~#", 1000, 22, "testSubsystem"); + Mockito.verify(mockChannel).connect(); + } + + @Test + public void testConnectWithSubsystemExceptionFlow() throws IOException, JSchException { + JSch mockJSch = Mockito.mock(JSch.class); + Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("com.jcraft.jsch.JSchException"); + wrapper.connect("testHost", "testUser", "testPswd", "]]>]]>", 1000, 22, "testSubsystem"); + } + + @Test + public void testConnectShellFourParameters() throws IOException, JSchException { + JSch mockJSch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + InputStream stubInputStream = IOUtils.toInputStream("hello\n]]>]]>", Charsets.UTF_8); + Mockito.doReturn(stubInputStream).when(mockChannel).getInputStream(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("shell"); + Mockito.doReturn(mockSession).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(null).when(wrapper).receiveUntil(":~#", 9000, + "No cmd was sent, just waiting, but we can stop on a '~#'"); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + wrapper.connect("testHost", "testUser", "testPswd", 22); + Mockito.verify(mockChannel).connect(); + } + + @Test + public void testConnectShellFourParametersExceptionFlow() throws IOException, JSchException { + JSch mockJSch = Mockito.mock(JSch.class); + Mockito.doThrow(new JSchException()).when(mockJSch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJSch).when(wrapper).getJSch(); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("com.jcraft.jsch.JSchException"); + wrapper.connect("testHost", "testUser", "testPswd", 22); + } + + @Test + public void testPutInputStreamToDest() throws JSchException, IOException { + ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class); + JSch mockJsch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22); + Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString()); + Mockito.doNothing().when(mockSession).connect(30 * 1000); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp"); + Mockito.doReturn(mockJsch).when(wrapper).getJSch(); + InputStream inputStream = Mockito.mock(InputStream.class); + wrapper.put(inputStream, "DUMMY_DEST_PATH/", "testHost", "testUser", "testPswd"); + Mockito.verify(mockSession).disconnect(); + } + + @Test + public void testPutInputStreamToDestExceptionFlow() + throws JSchException, IOException, SftpException { + ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class); + Mockito.doThrow(new SftpException(0, null)).when(mockChannel).rm("DUMMY_DEST_PATH/*"); + JSch mockJsch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22); + Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString()); + Mockito.doNothing().when(mockSession).connect(30 * 1000); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp"); + Mockito.doReturn(mockJsch).when(wrapper).getJSch(); + InputStream inputStream = Mockito.mock(InputStream.class); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("0: null"); + wrapper.put(inputStream, "DUMMY_DEST_PATH/", "testHost", "testUser", "testPswd"); + } + + @Test + public void testPutInputStreamToDestExceptionFlow2() + throws JSchException, IOException, SftpException { + ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class); + Mockito.doThrow(new SftpException(0, "No such file")).when(mockChannel) + .rm("DUMMY_DEST_PATH/*"); + JSch mockJsch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22); + Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString()); + Mockito.doNothing().when(mockSession).connect(30 * 1000); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp"); + Mockito.doReturn(mockJsch).when(wrapper).getJSch(); + InputStream inputStream = Mockito.mock(InputStream.class); + wrapper.put(inputStream, "DUMMY_DEST_PATH/", "testHost", "testUser", "testPswd"); + Mockito.verify(mockSession).disconnect(); + } + + @Test + public void testGet() throws JSchException, IOException, SftpException { + File file = new File("src/test/resources/TEST_FILE.txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(file.getPath())); + writer.write("line1\nline2"); + writer.flush(); + Whitebox.setInternalState(wrapper, "hostName", "testHost"); + Whitebox.setInternalState(wrapper, "userName", "testUser"); + Whitebox.setInternalState(wrapper, "passWord", "testPwd"); + ChannelSftp mockChannel = Mockito.mock(ChannelSftp.class); + JSch mockJsch = Mockito.mock(JSch.class); + Session mockSession = Mockito.mock(Session.class); + Mockito.doReturn(mockSession).when(mockJsch).getSession("testUser", "testHost", 22); + Mockito.doNothing().when(mockSession).setPassword(Mockito.anyString()); + Mockito.doNothing().when(mockSession).connect(); + Mockito.doReturn(mockChannel).when(mockSession).openChannel("sftp"); + Mockito.doReturn(new FileInputStream(file)).when(mockChannel) + .get("src/test/resources/TEST_FILE.txt"); + Mockito.doReturn(mockJsch).when(wrapper).getJSch(); + assertEquals("line1\nline2", + wrapper.get("src/test/resources/TEST_FILE.txt", "testHost", "testUser", "testPwd")); + } + + @Test + public void testGetExceptionFlow() throws JSchException, IOException { + JSch mockJsch = Mockito.mock(JSch.class); + Mockito.doThrow(new JSchException()).when(mockJsch).getSession("testUser", "testHost", 22); + Mockito.doReturn(mockJsch).when(wrapper).getJSch(); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("com.jcraft.jsch.JSchException"); + wrapper.get("src/test/resources/TEST_FILE.txt", "testHost", "testUser", "testPwd"); + } + + @Test + public void testSendWithDelimiter() throws IOException { + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class); + Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream(); + DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream)); + PowerMockito.doReturn("TEST RESPONSE").when(wrapper).receiveUntil("#$", 300000, + "TEST COMMAND\n"); + PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject()); + Whitebox.setInternalState(wrapper, "channel", mockChannel); + assertEquals("TEST RESPONSE", wrapper.send("TEST COMMAND\n", "#$")); + } + + @Test + public void testSendWithDelimiterExceptionFlow() throws IOException { + ChannelShell mockChannel = Mockito.mock(ChannelShell.class); + OutputStream mockOutputStream = Mockito.mock(BufferedOutputStream.class); + Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream(); + DataOutputStream mockDos = PowerMockito.spy(new DataOutputStream(mockOutputStream)); + PowerMockito.doThrow(new IOException()).when(mockDos).flush(); + PowerMockito.doReturn(mockDos).when(wrapper).getDataOutputStream(Mockito.anyObject()); + Whitebox.setInternalState(wrapper, "channel", mockChannel); + expectedEx.expect(IOException.class); + expectedEx.expectMessage("java.io.IOException"); + wrapper.send("TEST COMMAND", "]]>]]>"); + } + + @Test + public void testValues() throws IOException { SshJcraftWrapper wrapper = new SshJcraftWrapper(); wrapper.setEquipNameCode("testcode"); wrapper.setRouterCommandType("testcommand"); - String equipName =wrapper.getEquipNameCode(); - wrapper.getHostName(); - wrapper.getPassWord(); - wrapper.getRouterName(); - wrapper.getUserName(); - wrapper.getTheDate(); + String equipName = wrapper.getEquipNameCode(); + assertNull(wrapper.getHostName()); + assertNull(wrapper.getPassWord()); + assertNull(wrapper.getRouterName()); + assertNull(wrapper.getUserName()); + assertTrue( + wrapper.getTheDate().indexOf('/') > -1 && wrapper.getTheDate().indexOf(':') > -1); Assert.assertEquals("testcode", equipName); } - @Test(expected=Exception.class) - public void testSetRouterCommandType2() throws IOException{ + @Test(expected = Exception.class) + public void testSetRouterCommandType2() throws IOException { SshJcraftWrapper wrapper = new SshJcraftWrapper(); wrapper.appendToRouterFile("test", 2); StringBuffer buffer = new StringBuffer(); @@ -135,27 +660,21 @@ public class SshJcraftWrapperTest { wrapper.receiveUntilBufferFlush(3, 4, "test"); } - @Test(expected=Exception.class) - public void testSetRouterCommandType3() throws IOException{ + @Test(expected = Exception.class) + public void testSetRouterCommandType3() throws IOException { SshJcraftWrapper wrapper = new SshJcraftWrapper(); wrapper.checkIfReceivedStringMatchesDelimeter(3, "test"); } - - @Test(expected=IOException.class) - public void testConnect() throws IOException{ - SshJcraftWrapper wrapper = new SshJcraftWrapper(); - wrapper.connect("testHost", "testUser", "testPswd", "3000", 1000); - } - + @Test public void testMyUserInfoGetPassword() { - MyUserInfo myUserInfo=new MyUserInfo(); + MyUserInfo myUserInfo = new MyUserInfo(); assertNull(myUserInfo.getPassword()); } - + @Test public void testMyUserInfoPromptYesNo() { - MyUserInfo myUserInfo=new MyUserInfo(); + MyUserInfo myUserInfo = new MyUserInfo(); assertFalse(myUserInfo.promptYesNo("")); } } |