JUnits for receiveUntil method
[appc.git] / appc-config / appc-config-adaptor / provider / src / test / java / org / onap / appc / ccadaptor / SshJcraftWrapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.ccadaptor;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.fail;
32 import static org.mockito.BDDMockito.given;
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Matchers.anyInt;
35 import static org.mockito.Matchers.anyString;
36 import static org.mockito.Mockito.doThrow;
37 import static org.mockito.Mockito.inOrder;
38 import static org.mockito.Mockito.times;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.verifyNoMoreInteractions;
41
42 import com.jcraft.jsch.ChannelShell;
43 import com.jcraft.jsch.ChannelSubsystem;
44 import com.jcraft.jsch.JSch;
45 import com.jcraft.jsch.JSchException;
46 import com.jcraft.jsch.Session;
47 import com.jcraft.jsch.UserInfo;
48 import java.io.File;
49 import java.io.FileNotFoundException;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.net.URL;
53
54 import org.junit.Assert;
55 import org.junit.Before;
56 import org.junit.Ignore;
57 import org.junit.Test;
58 import org.junit.runner.RunWith;
59 import org.mockito.InOrder;
60 import org.mockito.Mock;
61 import org.mockito.runners.MockitoJUnitRunner;
62 import org.apache.commons.io.IOUtils;
63
64 @RunWith(MockitoJUnitRunner.class)
65 public class SshJcraftWrapperTest {
66
67     private static final String USER = "username";
68     private static final String PASS = "pass";
69     private static final String HOST = "hostname";
70     private static final String SUBSYSTEM = "netconf";
71     private static final String PROMPT = "]]>]]>";
72     private static final String SEARCH_STR = "</rpc-reply>";
73     private static final int READ_TIMEOUT = 180_000;
74     private static final int PORT_NUM = 23;
75     private static final int SESSION_TIMEOUT = 30_000;
76     private static final int READ_INTERVAL_MS = 1;
77     private static final int READ_BUFFER_SIZE = 10;
78
79     private SshJcraftWrapper cut;
80     @Mock
81     private JSch jSchMock;
82     @Mock
83     private Session session;
84     @Mock
85     private ChannelShell channelShell;
86     @Mock
87     private ChannelSubsystem channelSubsystem;
88     @Mock
89     private InputStream channelIs;
90
91     @Before
92     public void setUpTest() throws Exception {
93         InputStream is = IOUtils.toInputStream("test input stream:~#", "UTF-8");
94         given(channelShell.getInputStream()).willReturn(is);
95         given(channelSubsystem.getInputStream()).willReturn(is);
96         given(session.openChannel(SshJcraftWrapper.CHANNEL_SHELL_TYPE)).willReturn(channelShell);
97         given(session.openChannel(SshJcraftWrapper.CHANNEL_SUBSYSTEM_TYPE)).willReturn(channelSubsystem);
98         given(jSchMock.getSession(anyString(), anyString(), anyInt())).willReturn(session);
99         cut = new SshJcraftWrapper(jSchMock, READ_INTERVAL_MS, READ_BUFFER_SIZE);
100     }
101
102     @Ignore
103     @Test
104     public void TestCheckIfReceivedStringMatchesDelimeter(){
105         SshJcraftWrapper wrapper = new SshJcraftWrapper();
106         wrapper.getTheDate();
107         boolean result = wrapper.checkIfReceivedStringMatchesDelimeter("#", "test#", "test#");
108         Assert.assertEquals(true, result);
109     }
110
111     @Ignore
112     @Test
113     public void testRemoveWhiteSpaceAndNewLineCharactersAroundString(){
114         SshJcraftWrapper wrapper = new SshJcraftWrapper();
115         String nameSpace = wrapper.removeWhiteSpaceAndNewLineCharactersAroundString("namespace ");
116         Assert.assertEquals("namespace", nameSpace);
117     }
118
119     @Ignore
120     @Test
121     public void testStripOffCmdFromRouterResponse(){
122         SshJcraftWrapper wrapper = new SshJcraftWrapper();
123         String result = wrapper.stripOffCmdFromRouterResponse("test\nsuccess");
124         Assert.assertEquals("success\n", result);            
125     }
126     
127     //@Test
128     public void testGetLastFewLinesOfFile() throws FileNotFoundException, IOException{
129         SshJcraftWrapper wrapper = new SshJcraftWrapper();
130         URL path = SshJcraftWrapperTest.class.getResource("Test");
131         File file = new File(path.getFile());        
132         String value = wrapper.getLastFewLinesOfFile(file,1);
133         Assert.assertEquals("\nTest data 3", value);
134     }
135
136     @Ignore
137     @Test(expected=Exception.class)
138     public void testSetRouterCommandType() throws IOException{
139         SshJcraftWrapper wrapper = new SshJcraftWrapper();
140         wrapper.setRouterCommandType("test");    
141         wrapper.receiveUntil("test", 2, "test");
142     }
143
144     @Ignore
145     @Test
146     public void testValues() throws IOException{
147         SshJcraftWrapper wrapper = new SshJcraftWrapper();
148         wrapper.setEquipNameCode("testcode");
149         wrapper.setRouterCommandType("testcommand");
150         String equipName = wrapper.getEquipNameCode();
151         wrapper.getHostName();
152         wrapper.getPassWord();
153         wrapper.getRouterName();
154         wrapper.getUserName();
155         wrapper.getTheDate();
156         Assert.assertEquals("testcode", equipName);
157     }
158
159     @Ignore
160     @Test(expected=Exception.class)
161     public void testSetRouterCommandType2() throws IOException{
162         SshJcraftWrapper wrapper = new SshJcraftWrapper();
163         wrapper.appendToRouterFile("test", 2);
164         StringBuilder sb = new StringBuilder();
165         sb.append("test");
166         wrapper.appendToRouterFile("Test.txt", sb);
167         wrapper.receiveUntilBufferFlush(3, 4, "test");        
168     }
169
170     @Ignore
171     @Test(expected=Exception.class)
172     public void testSetRouterCommandType3() throws IOException{
173         SshJcraftWrapper wrapper = new SshJcraftWrapper();
174         wrapper.checkIfReceivedStringMatchesDelimeter(3, "test");
175     }
176
177     //real jUnits
178     @Test(expected = IOException.class)
179     public void connect_shouldThrowIOException_whenJSchFails() throws Exception {
180         //given
181         given(jSchMock.getSession(anyString(), anyString(), anyInt())).willThrow(new JSchException());
182
183         //when
184         cut.connect(HOST, USER, PASS);
185
186         //then
187         fail("IOException should be thrown");
188     }
189
190     @Test
191     public void connect_shouldSetVariables() throws Exception {
192         //when
193         cut.connect(HOST, USER, PASS);
194
195         //then
196         assertEquals(HOST, cut.getHostName());
197         assertEquals(HOST, cut.getRouterName());
198         assertEquals(USER, cut.getUserName());
199         assertEquals(PASS, cut.getPassWord());
200     }
201
202     @Test
203     public void connect_shouldSetUpSessionWithProperInvocationOrder() throws Exception {
204         //given
205         InOrder inOrder =  inOrder(session, channelShell);
206
207         //when
208         cut.connect(HOST, USER, PASS);
209
210         //then
211         verifySessionConfigurationOrderForChannelShellOpenning(
212             inOrder, USER, HOST, PASS, SshJcraftWrapper.DEFAULT_PORT, SESSION_TIMEOUT);
213     }
214
215     @Test
216     public void connect_shouldFinishSuccessfully_whenExceptionThrownDuringReceivingPhase() throws Exception {
217         //given
218         doThrow(new JSchException()).when(session).setTimeout(anyInt());
219
220         //when
221         cut.connect(HOST, USER, PASS);
222
223         //then
224         verify(session).setTimeout(anyInt());
225     }
226
227     @Test(expected = IOException.class)
228     public void connect_withSubsystem_shouldThrowIOException_whenJSchFails() throws Exception {
229         //given
230         given(jSchMock.getSession(anyString(), anyString(), anyInt())).willThrow(new JSchException());
231
232         //when
233         cut.connect(HOST, USER, PASS, SESSION_TIMEOUT, PORT_NUM, SUBSYSTEM);
234
235         //then
236         fail("IOException should be thrown");
237     }
238
239     @Test
240     public void connect_withSubsystem_shouldSetRouterName() throws Exception {
241         //when
242         cut.connect(HOST, USER, PASS, SESSION_TIMEOUT, PORT_NUM, SUBSYSTEM);
243
244         //then
245         assertEquals(HOST, cut.getRouterName());
246     }
247
248     @Test
249     public void connect_withSubsystem_shouldSetUpSessionWithProperInvocationOrder() throws Exception {
250         //given
251         InOrder inOrder =  inOrder(session, channelSubsystem);
252
253         //when
254         cut.connect(HOST, USER, PASS, SESSION_TIMEOUT, PORT_NUM, SUBSYSTEM);
255
256         //then
257         verify(jSchMock).getSession(USER, HOST, PORT_NUM);
258         inOrder.verify(session).setPassword(PASS);
259         inOrder.verify(session).setUserInfo(any(UserInfo.class));
260         inOrder.verify(session).setConfig(SshJcraftWrapper.STRICT_HOST_CHECK_KEY, SshJcraftWrapper.STRICT_HOST_CHECK_VALUE);
261         inOrder.verify(session).connect(SESSION_TIMEOUT);
262         inOrder.verify(session).setServerAliveCountMax(0);
263         inOrder.verify(session).openChannel(SshJcraftWrapper.CHANNEL_SUBSYSTEM_TYPE);
264         inOrder.verify(channelSubsystem).getInputStream();
265         inOrder.verify(channelSubsystem).connect(anyInt());
266         inOrder.verifyNoMoreInteractions();
267         verifyNoMoreInteractions(jSchMock);
268     }
269
270     @Test(expected = IOException.class)
271     public void connect_withPrompt_shouldThrowIOException_whenJSchFails() throws Exception {
272         //given
273         given(jSchMock.getSession(anyString(), anyString(), anyInt())).willThrow(new JSchException());
274
275         //when
276         cut.connect(HOST, USER, PASS, PROMPT, SESSION_TIMEOUT);
277
278         //then
279         fail("IOException should be thrown");
280     }
281
282     @Test
283     public void connect_withPrompt_shouldSetVariables() throws Exception {
284         //when
285         cut.connect(HOST, USER, PASS, PROMPT, SESSION_TIMEOUT);
286
287         //then
288         assertEquals(HOST, cut.getHostName());
289         assertEquals(HOST, cut.getRouterName());
290         assertEquals(USER, cut.getUserName());
291         assertEquals(PASS, cut.getPassWord());
292     }
293
294     @Test
295     public void connect_withPrompt_shouldFinishSuccessfully_whenExceptionThrownDuringReceivingPhase() throws Exception {
296         //given
297         doThrow(new JSchException()).when(session).setTimeout(anyInt());
298
299         //when
300         cut.connect(HOST, USER, PASS, PROMPT, SESSION_TIMEOUT);
301
302         //then
303         verify(session).setTimeout(anyInt());
304     }
305
306     @Test
307     public void connect_withPrompt_shouldSetUpSessionWithProperInvocationOrder() throws Exception {
308         //given
309         InOrder inOrder =  inOrder(session, channelShell);
310
311         //when
312         cut.connect(HOST, USER, PASS, PROMPT, SESSION_TIMEOUT);
313
314         //then
315         verifySessionConfigurationOrderForChannelShellOpenning(
316             inOrder, USER, HOST, PASS, SshJcraftWrapper.DEFAULT_PORT, SESSION_TIMEOUT);
317     }
318
319     @Test(expected = IOException.class)
320     public void connect_withPort_shouldThrowIOException_whenJSchFails() throws Exception {
321         //given
322         given(jSchMock.getSession(anyString(), anyString(), anyInt())).willThrow(new JSchException());
323
324         //when
325         cut.connect(HOST, USER, PASS, PROMPT, SESSION_TIMEOUT, PORT_NUM);
326
327         //then
328         fail("IOException should be thrown");
329     }
330
331     @Test
332     public void connect_withPort_shouldSetVariables() throws Exception {
333         //when
334         cut.connect(HOST, USER, PASS, PROMPT, SESSION_TIMEOUT, PORT_NUM);
335
336         //then
337         assertEquals(HOST, cut.getHostName());
338         assertEquals(HOST, cut.getRouterName());
339         assertEquals(USER, cut.getUserName());
340         assertEquals(PASS, cut.getPassWord());
341     }
342
343     @Test
344     public void connect_withPort_shouldFinishSuccessfully_whenExceptionThrownDuringReceivingPhase() throws Exception {
345         //given
346         doThrow(new JSchException()).when(session).setTimeout(anyInt());
347
348         //when
349         cut.connect(HOST, USER, PASS, PROMPT, SESSION_TIMEOUT, PORT_NUM);
350
351         //then
352         verify(session).setTimeout(anyInt());
353     }
354
355     @Test
356     public void connect_withPort_shouldSetUpSessionWithProperInvocationOrder() throws Exception {
357         //given
358         InOrder inOrder =  inOrder(session, channelShell);
359
360         //when
361         cut.connect(HOST, USER, PASS, PROMPT, SESSION_TIMEOUT, PORT_NUM);
362
363         //then
364         verifySessionConfigurationOrderForChannelShellOpenning(inOrder, USER, HOST, PASS, PORT_NUM, SESSION_TIMEOUT);
365     }
366
367     private void verifySessionConfigurationOrderForChannelShellOpenning(InOrder inOrder, String user, String host, String pass, int port, int sessionTimeout) throws Exception {
368         verify(jSchMock).getSession(user, host, port);
369         inOrder.verify(session).setPassword(pass);
370         inOrder.verify(session).setUserInfo(any(UserInfo.class));
371         inOrder.verify(session).setConfig(SshJcraftWrapper.STRICT_HOST_CHECK_KEY, SshJcraftWrapper.STRICT_HOST_CHECK_VALUE);
372         inOrder.verify(session).connect(sessionTimeout);
373         inOrder.verify(session).setServerAliveCountMax(0);
374         inOrder.verify(session).openChannel(SshJcraftWrapper.CHANNEL_SHELL_TYPE);
375         inOrder.verify(channelShell).getInputStream();
376         inOrder.verify(channelShell).connect();
377         inOrder.verify(session).setTimeout(anyInt());
378         inOrder.verifyNoMoreInteractions();
379         verifyNoMoreInteractions(jSchMock);
380     }
381
382     @Test
383     public void closeConnection_shouldCloseReaderChannelAndSession_inAGivenOrder() throws Exception {
384         //given
385         provideConnectedSubsystemInstance();
386         InOrder inOrder = inOrder(channelIs, channelSubsystem, session);
387
388         //when
389         cut.closeConnection();
390
391         //then
392         inOrder.verify(channelIs).close();
393         inOrder.verify(channelSubsystem).disconnect();
394         inOrder.verify(session).disconnect();
395         inOrder.verifyNoMoreInteractions();
396     }
397
398     @Test
399     public void closeConnection_shouldCloseChannelAndSession_whenClosingReaderFails() throws Exception {
400         //given
401         doThrow(new IOException("failed to close reader")).when(channelIs).close();
402         provideConnectedSubsystemInstance();
403
404         //when
405         cut.closeConnection();
406
407         //then
408         verify(channelIs).close();
409         verify(channelSubsystem).disconnect();
410         verify(session).disconnect();
411     }
412
413     @Test
414     public void closeConnection_shouldBeIdempotent_whenRunOnNewInstance() throws Exception {
415         //given
416         assertFalse(cut.isConnected());
417
418         //when
419         cut.closeConnection();
420
421         //then
422         assertFalse(cut.isConnected());
423     }
424
425     @Test
426     public void closeConnection_shouldBeIdempotent_whenRunTwiceOnConnectedInstance() throws Exception {
427         //given
428         provideConnectedSubsystemInstance();
429
430         //when
431         cut.closeConnection();
432         cut.closeConnection();
433
434         //then
435         assertFalse(cut.isConnected());
436     }
437
438     @Test
439     public void closeConnection_shouldCloseResourcesOnce_whenRunTwiceOnConnectedInstance() throws Exception {
440         //given
441         provideConnectedSubsystemInstance();
442
443         //when
444         cut.closeConnection();
445         cut.closeConnection();
446
447         //then
448         verify(channelIs, times(1)).close();
449         verify(channelSubsystem, times(1)).disconnect();
450         verify(session, times(1)).disconnect();
451     }
452
453     private void provideConnectedSubsystemInstance() throws Exception {
454         given(channelSubsystem.getInputStream()).willReturn(channelIs);
455         cut.connect(HOST, USER, PASS, SESSION_TIMEOUT, PORT_NUM, SUBSYSTEM);
456         assertTrue(cut.isConnected());
457     }
458
459     //receiveUntil tests begin
460     @Test(expected = IllegalStateException.class)
461     public void receiveUntil_shouldThrowIllegalStateException_whenInstanceIsNotConnected() throws Exception {
462         //given
463         assertFalse(cut.isConnected());
464
465         //when
466         cut.receiveUntil(SEARCH_STR, READ_TIMEOUT, "");
467
468         //then
469         fail("IllegalStateException should be thrown");
470     }
471
472     @Test(expected = IllegalStateException.class)
473     public void receiveUntil_shouldThrowIllegalStateException_whenJschReaderStreamIsNotAvailable() throws Exception {
474         //given
475         provideConnectedSubsystemInstance();
476         given(channelIs.available()).willReturn(0);
477
478         //when
479         cut.receiveUntil(SEARCH_STR, READ_TIMEOUT, "");
480
481         //then
482         fail("IllegalStateException should be thrown");
483     }
484
485     @Test(expected = TimedOutException.class)
486     public void receiveUntil_shouldThrowTimedOutException_whenSessionFails() throws Exception {
487         //given
488         given(channelSubsystem.getInputStream()).willReturn(IOUtils.toInputStream("test input stream:~#", "UTF-8"));
489         cut.connect(HOST, USER, PASS, SESSION_TIMEOUT, PORT_NUM, SUBSYSTEM);
490         assertTrue(cut.isConnected());
491         doThrow(new JSchException("Session is not available")).when(session).setTimeout(anyInt());
492
493         //when
494         cut.receiveUntil(SEARCH_STR, READ_TIMEOUT, "");
495
496         //then
497         fail("TimedOutException should be thrown");
498     }
499
500     @Test(expected = TimedOutException.class)
501     public void receiveUntil_shouldThrowTimedOutException_whenReadFails() throws Exception {
502         //given
503         provideConnectedSubsystemInstance();
504         given(channelIs.available()).willReturn(1);
505         given(channelIs.read(any(), anyInt(), anyInt())).willThrow(new IOException("Could not read stream"));
506
507         //when
508         cut.receiveUntil(SEARCH_STR, READ_TIMEOUT, "");
509
510         //then
511         fail("TimedOutException should be thrown");
512     }
513
514     @Test(expected = TimedOutException.class)
515     public void receiveUntil_shouldThrowException_whenTimeoutIsReached() throws Exception {
516         //given
517         String streamContent = "test input stream:~#";
518         provideConnectedSubsystemInstanceWithStreamContent(streamContent);
519
520         //when
521         cut.receiveUntil(SEARCH_STR, -1000, " Some fake command\n");
522
523         //then
524         fail("TimedOutException should be thrown");
525     }
526
527     @Test(expected = TimedOutException.class)
528     public void receiveUntil_shouldThrowException_whenReachedEndOfStream_andCouldNotReadMoreBytes() throws Exception {
529         //given
530         provideConnectedSubsystemInstance();
531         given(channelIs.available()).willReturn(1);
532         given(channelIs.read(any(), anyInt(), anyInt())).willReturn(-1);
533
534         //when
535         cut.receiveUntil(SEARCH_STR, READ_TIMEOUT, "");
536
537         //then
538         fail("TimedOutException should be thrown");
539     }
540
541     @Test
542     public void receiveUntil_shouldReadUnderlyingStream_andStripOffFirstLine() throws Exception {
543         //given
544         String command = "Command"+SshJcraftWrapper.EOL;
545         String reply = "Reply"+SshJcraftWrapper.EOL;
546         String streamContent = command+reply+PROMPT;
547         provideConnectedSubsystemInstanceWithStreamContent(streamContent);
548
549         //when
550         String result = cut.receiveUntil(PROMPT, SESSION_TIMEOUT, command);
551
552         //then
553         assertEquals(reply+PROMPT, result);
554     }
555
556     @Test
557     public void receiveUntil_shouldReadUnderlyingStream_andReturnWholeReadString() throws Exception {
558         //given
559         String streamContent = "Command and Reply in just one line"+PROMPT;
560         provideConnectedSubsystemInstanceWithStreamContent(streamContent);
561
562         //when
563         String result = cut.receiveUntil(PROMPT, SESSION_TIMEOUT, streamContent);
564
565         //then
566         assertEquals(streamContent, result);
567     }
568
569     @Test
570     public void receiveUntil_shouldCutOffSpecialCharactersFromStream() throws Exception {
571         //given
572         char special1 = Character.UNASSIGNED;
573         char special2 = Character.ENCLOSING_MARK;
574         char special3 = Character.LINE_SEPARATOR;
575         char special4 = Character.MODIFIER_SYMBOL;
576         StringBuilder sb = new StringBuilder("Command");
577         sb.append(special1).append("With").append(special2).append("Special")
578             .append(special3).append("Characters").append(special4).append("Set").append(PROMPT);
579
580         provideConnectedSubsystemInstanceWithStreamContent(sb.toString());
581
582         //when
583         String result = cut.receiveUntil(PROMPT, SESSION_TIMEOUT, "");
584
585         //then
586         assertEquals("CommandWithSpecialCharactersSet"+PROMPT, result);
587     }
588
589     @Test
590     public void receiveUntil_shouldReadUnderlyingStream_untilCLIDelimiterFound_whenProperDelimiterSet() throws Exception {
591         //given
592         String cliDelimiter = "#$";
593         String delimiters = PROMPT+SshJcraftWrapper.DELIMITERS_SEPARATOR+cliDelimiter;
594         String streamContent = "Command for CLI invocation #";
595         provideConnectedSubsystemInstanceWithStreamContent(streamContent);
596
597         //when
598         String result = cut.receiveUntil(delimiters, SESSION_TIMEOUT, streamContent);
599
600         //then
601         assertEquals(streamContent, result);
602     }
603
604     @Test
605     public void receiveUntil_shouldReadUnderlyingStream_untilCLIDelimiterFound_whenCLICommandSet() throws Exception {
606         //given
607         String streamContent = "Command for CLI invocation #";
608         provideConnectedSubsystemInstanceWithStreamContent(streamContent);
609         cut.setRouterCommandType("CLI");
610
611         //when
612         String result = cut.receiveUntil("", SESSION_TIMEOUT, streamContent);
613
614         //then
615         assertEquals(streamContent, result);
616     }
617
618     @Test
619     public void receiveUntil_shouldReadUnderlyingStream_untilCLIDelimiterFound_forShowConfigCommand() throws Exception {
620         //given
621         String streamContent = "show config\nconfig content#";
622         provideConnectedSubsystemInstanceWithStreamContent(streamContent);
623
624         //when
625         String result = cut.receiveUntil("#", SESSION_TIMEOUT, streamContent);
626
627         //then
628         assertEquals("config content#", result);
629     }
630
631     @Test
632     public void receiveUntil_shouldWriteOutputToRouterFile_whenReadingIOSXRswConfigFile_confirmFromFile() throws Exception {
633         receiveUntil_shouldWriteOutputToRouterFile_whenReadingIOSXRswConfigFile();
634     }
635
636     @Test
637     public void receiveUntil_shouldWriteOutputToRouterFile_whenReadingIOSXRswConfigFile_confirmFromBuffer() throws Exception {
638         //given
639         int biggerBufferSize = 32;
640         cut = new SshJcraftWrapper(jSchMock, READ_INTERVAL_MS, biggerBufferSize);
641
642         receiveUntil_shouldWriteOutputToRouterFile_whenReadingIOSXRswConfigFile();
643     }
644
645     private void receiveUntil_shouldWriteOutputToRouterFile_whenReadingIOSXRswConfigFile() throws Exception {
646         //given
647         String routerName = "router";
648         String command = "RP/0/RP0/CPU0: "+routerName+" #IOS_XR_uploadedSwConfigCmd";
649         String configFileEnding = "\nXML>";
650         String streamContent = "Config file\ncontent"+configFileEnding;
651         provideConnectedSubsystemInstanceWithStreamContent(streamContent);
652
653         //when
654         String result = cut.receiveUntil("", SESSION_TIMEOUT, command);
655
656         //then
657         assertNull(result); //TO-DO: it would be better to return empty string in this situation
658         assertFileExist(routerName);
659
660         //after
661         teardownFile(routerName);
662     }
663
664     private void provideConnectedSubsystemInstanceWithStreamContent( String streamContent) throws Exception {
665         given(channelSubsystem.getInputStream()).willReturn(IOUtils.toInputStream(streamContent, "UTF-8"));
666         cut.connect(HOST, USER, PASS, SESSION_TIMEOUT, PORT_NUM, SUBSYSTEM);
667         assertTrue(cut.isConnected());
668     }
669
670     private void teardownFile(String routerName) {
671         File file = new File(routerName);
672         if(file.exists() && file.isFile()) {
673             file.delete();
674         }
675     }
676
677     private void assertFileExist(String fileName) {
678         File file = new File(fileName);
679         assertTrue(file.exists());
680         assertTrue(file.isFile());
681     }
682
683 }