package ru.matevosyan.server;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
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 BotClientTest to test BotClient class.
 * Created on 25.03.2017.
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class BotServerTest {

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

    /**
     * Method whenClientAskThenServerAnswer was created for check correct output and expected value.
     * @param input passing variable that server must read.
     * @param expected value that will be check with input variable.
     * @throws IOException to handle invoking  input and output exception.
     */

    private void whenClientAskThenServerAnswer(String input, String expected) throws IOException {
        Socket socket = mock(Socket.class);

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

        when(socket.getInputStream()).thenReturn(in);
        when(socket.getOutputStream()).thenReturn(out);
        BotServer botServer = new BotServer(socket);

        botServer.startServerWork();

        assertThat(out.toString(), is(expected));

    }

    /**
     * whenClientAskForExitThenServerAnswerEmpty was created to test server responding value to client.
     * invoking {@link BotServerTest#whenClientAskThenServerAnswer(String, String)} and passing client input value.
     * and expected value.
     * @throws IOException to handle invoking  input and output exception.
     */

    @Test
    public void whenClientAskForExitThenServerAnswerEmpty() throws IOException {

        this.whenClientAskThenServerAnswer("Bye", "");

    }

    /**
     * whenClientAskForExitThenServerAnswerEmpty was created to test server responding value to client.
     * invoking {@link BotServerTest#whenClientAskThenServerAnswer(String, String)} and passing client input value.
     * and expected value.
     * @throws IOException to handle invoking  input and output exception.
     */

    @Test
    public void whenClientAskForBlaBlaThenServerAnswerDoNotUnderstand() throws IOException {

        this.whenClientAskThenServerAnswer(String.format("sdfsdsdfsdfsdf%sBye", LN),
                "What do you want. I don't understand you!" + EMPTYSTRING);

    }

    /**
     * whenClientAskForExitThenServerAnswerEmpty was created to test server responding value to client.
     * invoking {@link BotServerTest#whenClientAskThenServerAnswer(String, String)} and passing client input value.
     * and expected value.
     * @throws IOException to handle invoking  input and output exception.
     */

    @Test
    public void whenClientAskForBlaBlaTwoTimesThenServerAnswerDoNotUnderstandTwoTimes() throws IOException {

        this.whenClientAskThenServerAnswer(String.format("sdfsdsdfsdfsdf%sBsdssdsdsdsdy%sBye", LN, LN),
                "What do you want. I don't understand you!" + EMPTYSTRING
                        + "What do you want. I don't understand you!" + EMPTYSTRING);

    }

}
@@ -151,16 +151,44 @@
 eption;%0A
+import java.io.PrintStream;%0A
 import j
@@ -3405,9 +3405,1714 @@
 %0A    %7D%0A%0A
+    /**%0A     * whenClientAskForHelloMyFriendTwoTimesThenServerAnswerDoNotUnderstandTwoTimes.%0A     * was created to test server responding value to client.%0A     * invoking %7B@link BotServerTest#whenClientAskThenServerAnswer(String, String)%7D and passing client input value.%0A     * and expected value.%0A     * @throws IOException to handle invoking  input and output exception.%0A     */%0A%0A    @Test%0A    public void whenClientAskForHelloMyFriendTwoTimesThenServerAnswerDoNotUnderstandTwoTimes() throws IOException %7B%0A%0A        this.whenClientAskThenServerAnswer(String.format(%22Hello%25sBsdssdsdsdsdy%25sBye%22, LN, LN),%0A                %22Hello my friend%22 + EMPTYSTRING%0A        + %22What do you want. I don't understand you!%22 + EMPTYSTRING);%0A%0A    %7D%0A%0A    /**%0A     * whenClientAskCheckOutput check output when client answering to server and print it.%0A     * @throws IOException to handle invoking  input and output exception.%0A     */%0A%0A    @Test%0A    public void whenClientAskCheckOutput() throws IOException %7B%0A        Socket socket = mock(Socket.class);%0A%0A        ByteArrayOutputStream outType = new ByteArrayOutputStream();%0A        System.setOut(new PrintStream(outType));%0A%0A        ByteArrayOutputStream out = new ByteArrayOutputStream();%0A        ByteArrayInputStream in = new ByteArrayInputStream(String.format(%22Hello%25sBlaBla%25sBye%22, LN, LN).getBytes());%0A%0A        when(socket.getInputStream()).thenReturn(in);%0A        when(socket.getOutputStream()).thenReturn(out);%0A        BotServer botServer = new BotServer(socket);%0A%0A        botServer.startServerWork();%0A%0A        assertThat(outType.toString(), is(String.format(%22Client ~ Hello%25s%22%0A                + %22Client ~ BlaBla%25s%22%0A                + %22Client ~ Bye%25s%22, LN, LN, LN)));%0A    %7D%0A
 %7D

add whenClientAskCheckOutput test method