+ @Test
+ public void send_withReceive_shouldWriteCommandToChannelOutputStream_andReturnReceivedCommand() throws Exception {
+ //given
+ String command = "sdc";
+ String delimiter = ":";
+ InOrder inOrder = inOrder(channelOs);
+ provideConnectedSubsystemInstanceWithStreamContent(command+delimiter);
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+
+ //when
+ String result = cut.send(command, delimiter);
+
+ //then
+ verifySdcCommandSent(inOrder);
+ assertEquals(command+delimiter, result);
+ }
+
+ @Test
+ public void send_shouldWriteCommandToChannelOutputStream() throws Exception {
+ //given
+ String command = "sdc";
+ InOrder inOrder = inOrder(channelOs);
+ provideConnectedSubsystemInstance();
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+
+ //when
+ cut.send(command);
+
+ //then
+ verifySdcCommandSent(inOrder);
+ }
+
+ private void verifySdcCommandSent(InOrder inOrder) throws IOException {
+ inOrder.verify(channelOs).write('s');
+ inOrder.verify(channelOs).write('d');
+ inOrder.verify(channelOs).write('c');
+ inOrder.verify(channelOs, atLeastOnce()).flush();
+ inOrder.verify(channelOs, atLeastOnce()).close();
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ @Test
+ public void send_withReceive_shouldWriteCommandInChunksToChannelOutputStream_andReturnReceivedCommand() throws Exception {
+ //given
+ cut = new SshJcraftWrapper(jSchMock, READ_INTERVAL_MS, 1);
+ cut.setCharsChunkSize(1);
+ String command = "sdc";
+ String delimiter = ":";
+ int timeout = 9000;
+ provideConnectedSubsystemInstanceWithStreamContent(command+delimiter+SshJcraftWrapper.EOL);
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+ InOrder inOrder = inOrder(channelOs, session);
+
+ //when
+ String result = cut.send(command, delimiter);
+
+ //then
+ verifySdcCommandSentInChunk(inOrder, timeout);
+ assertEquals(command+delimiter, result);
+ }
+
+ @Test
+ public void send_shouldWriteCommandInChunksToChannelOutputStream() throws Exception {
+ //given
+ cut = new SshJcraftWrapper(jSchMock, READ_INTERVAL_MS, 1);
+ cut.setCharsChunkSize(1);
+ String command = "sdc";
+ int timeout = 9000;
+ provideConnectedSubsystemInstanceWithStreamContent(command+SshJcraftWrapper.EOL);
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+ InOrder inOrder = inOrder(channelOs, session);
+
+ //when
+ cut.send(command);
+
+ //then
+ verifySdcCommandSentInChunk(inOrder, timeout);
+ }
+
+ private void verifySdcCommandSentInChunk(InOrder inOrder, int timeout) throws Exception{
+ inOrder.verify(channelOs).write('s');
+ inOrder.verify(channelOs).flush();
+ inOrder.verify(session).setTimeout(timeout);
+ inOrder.verify(channelOs).write('d');
+ inOrder.verify(channelOs).flush();
+ inOrder.verify(session).setTimeout(timeout);
+ inOrder.verify(channelOs).write('c');
+ inOrder.verify(channelOs).flush();
+ inOrder.verify(session).setTimeout(timeout);
+ inOrder.verify(channelOs, atLeastOnce()).flush();
+ inOrder.verify(channelOs, atLeastOnce()).close();
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ @Test
+ public void send_withReceive_shouldReturnActualResult_whenTimeoutReached() throws Exception {
+ //given
+ cut = new SshJcraftWrapper(jSchMock, READ_INTERVAL_MS, 1);
+ cut.setCharsChunkSize(1);
+ cut.setSessionTimeoutMs(-1);
+ String command = "sdc";
+ String delimiter = ":";
+ provideConnectedSubsystemInstanceWithStreamContent(command+SshJcraftWrapper.EOL);
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+
+ //when
+ String result = cut.send(command, delimiter);
+
+ //then
+ assertEquals(StringUtils.EMPTY, result);
+ }
+
+ @Test
+ public void send_withReceive_shouldReturnActualResult_whenCouldNotSetSessionTimeout() throws Exception {
+ //given
+ cut = new SshJcraftWrapper(jSchMock, READ_INTERVAL_MS, 1);
+ cut.setCharsChunkSize(1);
+ String command = "sdc";
+ String delimiter = ":";
+ provideConnectedSubsystemInstanceWithStreamContent(command+SshJcraftWrapper.EOL);
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+ doThrow(new JSchException("failed to set session timeout")).when(session).setTimeout(anyInt());
+
+ //when
+ String result = cut.send(command, delimiter);
+
+ //then
+ assertEquals(StringUtils.EMPTY, result);
+ }
+
+ @Test
+ public void sendChar_shouldWriteCharacterToChannelOutputStream() throws Exception {
+ //given
+ int charNum = 100;
+ provideConnectedSubsystemInstance();
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+ InOrder inOrder = inOrder(channelOs);
+
+ //when
+ cut.sendChar(charNum);
+
+ //then
+ inOrder.verify(channelOs).write(charNum);
+ inOrder.verify(channelOs, atLeastOnce()).flush();
+ inOrder.verify(channelOs, atLeastOnce()).close();
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ @Test(expected = IOException.class)
+ public void sendChar_shouldRethrowIOException_whenOccurs() throws Exception {
+ //given
+ int charNum = 100;
+ provideConnectedSubsystemInstance();
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+ doThrow(new IOException()).when(channelOs).write(charNum);
+
+ //when
+ cut.sendChar(charNum);
+
+ //then
+ fail("IOException should be thrown");
+ }
+
+ @Test
+ public void send_withByteBuffer_shouldWriteBufferToChannelOutputStream() throws Exception {
+ //given
+ byte[] buffer = "Command".getBytes();
+ int offset = 5;
+ provideConnectedSubsystemInstance();
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+ InOrder inOrder = inOrder(channelOs);
+
+ //when
+ cut.send(buffer,offset,buffer.length);
+
+ //then
+ inOrder.verify(channelOs).write(buffer, offset, buffer.length);
+ inOrder.verify(channelOs, atLeastOnce()).flush();
+ inOrder.verify(channelOs, atLeastOnce()).close();
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ @Test(expected = IOException.class)
+ public void send_withByteBuffer_shouldRethrowIOException_whenOccurs() throws Exception {
+ //given
+ byte[] buffer = "Command".getBytes();
+ int offset = 5;
+ provideConnectedSubsystemInstance();
+ given(channelSubsystem.getOutputStream()).willReturn(channelOs);
+ doThrow(new IOException()).when(channelOs).write(buffer, offset, buffer.length);
+
+ //when
+ cut.send(buffer,offset,buffer.length);
+
+ //then
+ fail("IOException should be thrown");
+ }