package ru.matevosyan.client;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Created by Admin on 26.03.2017.
 */
public class BotClientTest {

    private static final String LN = System.getProperty("line.separator");

    public void whenClientSendThenServerReturn(String input, String expected, String typeIn) throws IOException {
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream outType = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outType));

        ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream typeValue = new ByteArrayInputStream(typeIn.getBytes());

        System.setIn(typeValue);
        when(socket.getInputStream()).thenReturn(in);
        when(socket.getOutputStream()).thenReturn(out);


        BotClient botClient = new BotClient(socket);

        botClient.startClient();

        assertThat(outType.toString("UTF-8"), is(expected));
    }


    @Test
    public void whenSendHelloThenReturnHello() throws IOException {
        this.whenClientSendThenServerReturn("Hello my friend",
                String.format("Type something to send to Oracle%sSending this line to the server...%s" +
                                "Server ~ Hello my friend%sSending this line to the server...%sServer ~ null%s",
                        LN, LN, LN, LN, LN),
                String.format("Hello%sBye", LN));
    }
//    @Test
//    public void whenSendByThenReturnEmpty() throws IOException {
//        this.whenClientSendThenServerReturn("", "", "Bye");
//    }
}
@@ -155,36 +155,8 @@
 on;%0A
-import java.io.PrintStream;%0A
 impo
@@ -657,16 +657,19 @@
 class);%0A
+%0A//
         
@@ -721,32 +721,34 @@
 OutputStream();%0A
+//
         System.s
@@ -924,16 +924,17 @@
 ream();%0A
+%0A
         
@@ -1007,25 +1007,24 @@
 etBytes());%0A
-%0A
         Syst
@@ -1044,16 +1044,17 @@
 Value);%0A
+%0A
         
@@ -1156,17 +1156,16 @@
 (out);%0A%0A
-%0A
         
@@ -1205,25 +1205,24 @@
 nt(socket);%0A
-%0A
         botC
@@ -1266,32 +1266,31 @@
 hat(
-outType.toString(%22UTF-8%22
+socket.getOutputStream(
 ), i
@@ -1311,16 +1311,18 @@
     %7D%0A%0A%0A
+//
     @Tes
@@ -1323,16 +1323,18 @@
   @Test%0A
+//
     publ
@@ -1385,32 +1385,34 @@
 s IOException %7B%0A
+//
         this.whe
@@ -1458,16 +1458,18 @@
 riend%22,%0A
+//
         
@@ -1565,16 +1565,18 @@
 ..%25s%22 +%0A
+//
         
@@ -1680,16 +1680,18 @@
 ull%25s%22,%0A
+//
         
@@ -1727,16 +1727,18 @@
 N, LN),%0A
+//
         
@@ -1783,16 +1783,16 @@
 ));%0A
+//
     %7D%0A
-//
     
@@ -1789,34 +1789,32 @@
     %7D%0A    @Test%0A
-//
     public void 
@@ -1854,34 +1854,32 @@
 s IOException %7B%0A
-//
         this.whe
@@ -1911,25 +1911,165 @@
 rn(%22
-%22, %22%22, %22Bye%22);%0A//
+Hello my friend%22, %22Hello my friend%22, String.format(%22Hello%25sBye%22, LN));%0A    %7D%0A%0A    @Test%0A    public void whenSendHelloThenReturnHello() throws IOException %7B%0A%0A
     

change a little