+ cut.send(buffer, offset, buffer.length);
+
+ //then
+ fail(IO_EXCEPTION_MSG);
+ }
+
+ @Test
+ public void getSftpConnection_shouldSetupSessionWithProperInvocationOrder() throws Exception {
+ //given
+ SshJcraftWrapper instance = spy(cut);
+ given(instance.openSftpChannel(session)).willReturn(channelSftp);
+ InOrder inOrder = inOrder(session, channelSftp, instance);
+
+ //when
+ ChannelSftp result = instance.getSftpConnection(HOST, USER, PASS);
+
+ //then
+ verify(jSchMock).getSession(USER, HOST, SshJcraftWrapper.DEFAULT_PORT);
+ inOrder.verify(session).setPassword(PASS);
+ inOrder.verify(session).setUserInfo(any(UserInfo.class));
+ inOrder.verify(session)
+ .setConfig(SshJcraftWrapper.STRICT_HOST_CHECK_KEY, SshJcraftWrapper.STRICT_HOST_CHECK_VALUE);
+ inOrder.verify(session).connect(SESSION_TIMEOUT);
+ inOrder.verify(instance).openSftpChannel(session);
+ inOrder.verify(channelSftp).connect();
+ inOrder.verifyNoMoreInteractions();
+ assertEquals(channelSftp, result);
+ }
+
+ @Test
+ public void sftp_get_shouldReadFromChannelInputStream_andCloseConnection() throws Exception {
+ //given
+ String streamContent = "test input stream content";
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+ given(channelSftp.get(REMOTE_PATH)).willReturn(IOUtils.toInputStream(streamContent, "UTF-8"));
+
+ //when
+ String result = spyInstance.get(REMOTE_PATH, HOST, USER, PASS);
+
+ //then
+ assertEquals(streamContent, result);
+ verify(channelSftp).disconnect();
+ verify(session).disconnect();
+ }
+
+ @Test(expected = IOException.class)
+ public void sftp_get_shouldThrowIOException_whenJschFails() throws Exception {
+ //given
+ SshJcraftWrapper spyInstance = spy(cut);
+ doThrow(new JSchException()).when(spyInstance).openSftpChannel(session);
+
+ //when
+ spyInstance.get(REMOTE_PATH, HOST, USER, PASS);
+
+ //then
+ fail(IO_EXCEPTION_MSG);
+ }
+
+ @Test(expected = IOException.class)
+ public void sftp_get_shouldThrowIOException_whenSftpOperationFails() throws Exception {
+ //given
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+ doThrow(new SftpException(0, "sftp error")).when(channelSftp).get(REMOTE_PATH);
+
+ //when
+ spyInstance.get(REMOTE_PATH, HOST, USER, PASS);
+
+ //then
+ fail(IO_EXCEPTION_MSG);
+ }
+
+ @Test
+ public void sftp_get_shouldCloseSessionAndChannel_whenExceptionOccursOnConnectedInstance() throws Exception {
+ //given
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+ doThrow(new SftpException(0, "sftp error")).when(channelSftp).get(REMOTE_PATH);
+ boolean ioException = false;
+
+ //when
+ try {
+ spyInstance.get(REMOTE_PATH, HOST, USER, PASS);
+ } catch (IOException e) {
+ ioException = true;
+ }
+
+ //then
+ assertTrue(ioException);
+ verify(channelSftp).disconnect();
+ verify(session).disconnect();
+ }
+
+ @Test
+ public void sftp_get_shouldSkipClosingSessionAndChannel_whenExceptionOccursOnNotConnectedInstance() throws Exception {
+ //given
+ doThrow(new JSchException()).when(jSchMock).getSession(anyString(), anyString(), anyInt());
+ boolean ioException = false;
+
+ //when
+ try {
+ cut.get(REMOTE_PATH, HOST, USER, PASS);
+ } catch (IOException e) {
+ ioException = true;
+ }
+
+ //then
+ assertTrue(ioException);
+ verify(channelSftp, never()).disconnect();
+ verify(session, never()).disconnect();
+ }
+
+ @Test
+ public void sftp_put_withIs_shouldRemoveOldFilesFromDestinationPath_andPutNewData() throws Exception {
+ //given
+ InputStream is = IOUtils.toInputStream("test input stream content", "UTF-8");
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+ InOrder inOrder = inOrder(channelSftp, session);
+
+ //when
+ spyInstance.put(is, REMOTE_PATH, HOST, USER, PASS);
+
+ //then
+ inOrder.verify(channelSftp).rm(REMOTE_PATH + "*");
+ inOrder.verify(channelSftp).put(is, REMOTE_PATH, ChannelSftp.OVERWRITE);
+ inOrder.verify(channelSftp).disconnect();
+ inOrder.verify(session).disconnect();
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ @Test
+ public void sftp_put_withIs_shouldContinueInsertingNewData_whenFileNotFoundDuringRemoval() throws Exception {
+ //given
+ InputStream is = IOUtils.toInputStream("test input stream content", "UTF-8");
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+ doThrow(new SftpException(ChannelSftp.SSH_FX_NO_SUCH_FILE, "No such file/dir"))
+ .when(channelSftp).rm(REMOTE_PATH + "*");
+
+ //when
+ spyInstance.put(is, REMOTE_PATH, HOST, USER, PASS);
+
+ //then
+ verify(channelSftp).rm(REMOTE_PATH + "*");
+ verify(channelSftp).put(is, REMOTE_PATH, ChannelSftp.OVERWRITE);
+ }
+
+ @Test(expected = IOException.class)
+ public void sftp_put_withIs_shouldThrowIOException_whenSftpFails() throws Exception {
+ //given
+ InputStream is = IOUtils.toInputStream("test input stream content", "UTF-8");
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+ doThrow(new SftpException(ChannelSftp.SSH_FX_FAILURE, "general error"))
+ .when(channelSftp).rm(anyString());
+
+ //when
+ spyInstance.put(is, REMOTE_PATH, HOST, USER, PASS);
+
+ //then
+ fail(IO_EXCEPTION_MSG);
+ }
+
+ @Test(expected = IOException.class)
+ public void sftp_put_withIs_shouldThrowIOException_whenJschFails() throws Exception {
+ //given
+ InputStream is = IOUtils.toInputStream("test input stream content", "UTF-8");
+ SshJcraftWrapper spyInstance = spy(cut);
+ doThrow(new JSchException()).when(spyInstance).openSftpChannel(session);
+
+ //when
+ spyInstance.put(is, REMOTE_PATH, HOST, USER, PASS);
+
+ //then
+ fail(IO_EXCEPTION_MSG);
+ }
+
+ @Test
+ public void sftp_put_withSourcePath_shouldPutSrcPathToDestinationPath_andDisconnectAfterwards() throws Exception {
+ //given
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+
+ //when
+ spyInstance.put(SOURCE_PATH, REMOTE_PATH);
+
+ //then
+ verify(channelSftp).put(SOURCE_PATH, REMOTE_PATH, ChannelSftp.OVERWRITE);
+ verify(channelSftp).disconnect();
+ verify(session).disconnect();
+ }
+
+ @Test(expected = IOException.class)
+ public void sftp_put_withSourcePath_shouldThrowIOException_whenSftpFails() throws Exception {
+ //given
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+ doThrow(new SftpException(ChannelSftp.SSH_FX_FAILURE, "general error"))
+ .when(channelSftp).put(anyString(), anyString(), anyInt());
+
+ //when
+ spyInstance.put(SOURCE_PATH, REMOTE_PATH);
+
+ //then
+ fail(IO_EXCEPTION_MSG);
+ }
+
+ @Test(expected = IOException.class)
+ public void sftp_put_withSourcePath_shouldThrowIOException_whenJschFails() throws Exception {
+ //given
+ SshJcraftWrapper spyInstance = spy(cut);
+ doThrow(new JSchException()).when(spyInstance).openSftpChannel(session);
+
+ //when
+ spyInstance.put(SOURCE_PATH, REMOTE_PATH);
+
+ //then
+ fail(IO_EXCEPTION_MSG);
+ }
+
+ @Test
+ public void sftpPutStringData_shouldPutInputStreamToRemotePath() throws Exception {
+ //given
+ String inputData = "Test data";
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+
+ //when
+ spyInstance.sftpPutStringData(inputData, REMOTE_PATH);
+
+ //then
+ verify(channelSftp).put(any(InputStream.class), eq(REMOTE_PATH), eq(ChannelSftp.OVERWRITE));
+ verify(channelSftp).disconnect();
+ verify(session).disconnect();
+ }
+
+ @Test(expected = IOException.class)
+ public void sftpPutStringData_shouldThrowIOException_whenJschFails() throws Exception {
+ //given
+ String inputData = "Test data";
+ SshJcraftWrapper spyInstance = spy(cut);
+ doThrow(new JSchException()).when(spyInstance).openSftpChannel(session);
+
+ //when
+ spyInstance.sftpPutStringData(inputData, REMOTE_PATH);
+
+ //then
+ fail(IO_EXCEPTION_MSG);
+ }
+
+ @Test(expected = IOException.class)
+ public void sftpPutStringData_shouldThrowIOException_whenSftpFails() throws Exception {
+ //given
+ String inputData = "Test data";
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+ doThrow(new SftpException(ChannelSftp.SSH_FX_FAILURE, "general error"))
+ .when(channelSftp).put(any(InputStream.class), anyString(), anyInt());
+
+ //when
+ spyInstance.sftpPutStringData(inputData, REMOTE_PATH);
+
+ //then
+ fail(IO_EXCEPTION_MSG);
+ }
+
+ @Test
+ public void sftpGet_shouldReadFromChannelInputStream_withInstanceCredentials() throws Exception {
+ //given
+ String remoteStreamContent = "test input stream content";
+ SshJcraftWrapper spyInstance = spy(cut);
+ given(spyInstance.openSftpChannel(session)).willReturn(channelSftp);
+ given(channelSftp.get(REMOTE_PATH)).willReturn(IOUtils.toInputStream(remoteStreamContent, "UTF-8"));
+
+ //when
+ String result = spyInstance.sftpGet(REMOTE_PATH);