package ru.job4j.chat;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertTrue;

/**
 * Created by Intellij IDEA.
 * User: Vitaly Zubov.
 * Email: Zubov.VP@yandex.ru.
 * Version: $Id$.
 * Date: 28.08.2019.
 */
public class ChatTest {
    private ByteArrayOutputStream out;
    private final PrintStream stdout = System.out;
    private File file = new File(String.format("%s%s", System.getProperty("user.dir"), "\\src\\main\\java\\ru\\job4j\\chat\\answers.txt"));
    private StringBuilder questions = new StringBuilder();

    @Before
    public void prepareOutputStream() {
        this.out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(this.out));
        this.questions.append("стоп\n");
        this.questions.append("Как дела?\n");
        this.questions.append("Как день прошёл?\n");
        this.questions.append("старт\n");
        this.questions.append("Что делаешь?\n");
        this.questions.append("закончить\n");
        String data = questions.toString();
        InputStream is = new ByteArrayInputStream(data.getBytes());
        System.setIn(is);
        new Chat(this.file).start();
    }

    @After
    public void backOutput() throws IOException {
        System.setOut(this.stdout);
        this.out.close();
        this.file.delete();
    }

    @Test
    public void checkAnsersOfUser() {
        List<String> result = new ArrayList<>();
        try (FileReader reader = new FileReader(this.file);
             BufferedReader bfr = new BufferedReader(reader)) {
            String line = bfr.readLine();
            while (line != null) {
                result.add(line.split(": ")[1]);
                line = bfr.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        assertTrue(result.contains("стоп"));
        assertTrue(result.contains("Как дела?"));
        assertTrue(result.contains("Что делаешь?"));
        assertTrue(result.contains("закончить"));
    }

    @Test
    public void checkAnswersOfBot() {
        List<String> result = new ArrayList<>();
        try (FileReader reader = new FileReader(this.file);
             BufferedReader bfr = new BufferedReader(reader)) {
            String line = bfr.readLine();
            while (line != null) {
                result.add(line);
                line = bfr.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        assertThat(result.size() - this.questions.toString().split("\n").length, is(2));
    }
}
@@ -453,98 +453,8 @@
 t %7B%0A
-    private ByteArrayOutputStream out;%0A    private final PrintStream stdout = System.out;%0A
     
@@ -585,24 +585,24 @@
 ers.txt%22));%0A
+
     private 
@@ -705,106 +705,8 @@
 ) %7B%0A
-        this.out = new ByteArrayOutputStream();%0A        System.setOut(new PrintStream(this.out));%0A
     
@@ -1204,90 +1204,9 @@
 t() 
-throws IOException %7B%0A        System.setOut(this.stdout);%0A        this.out.close();
+%7B
 %0A   

Correct Chat 2.