package ru.matevosyan.start;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotEquals;


/**
 * Class ConsoleInputTest created for testing.
 * Created on 18.12.2016.
 * @since 1.0
 * @version 1.0
 * @author Matevosyan Vardan
 */

public class ConsoleInputTest {

    /**
     * whenAskForReturnThanReturnScanIn test method for test ConsoleInput class.
     * when we expected right value
     */

    @Test
    public void whenAskForReturnThanReturnScanIn() {

        String testText = "Hello";
        ByteArrayInputStream in = new ByteArrayInputStream(testText.getBytes());
        System.setIn(in);

        ConsoleInput input = new ConsoleInput();
        assertEquals(input.ask("Question"), testText);

    }

    /**
     * whenAskForWrongReturnThanExpectedNotEqualsTwoValues test method for test ConsoleInput class.
     * when we expected wrong value
     */

    @Test
    public void whenAskForWrongReturnThanExpectedNotEqualsTwoValues() {

        String testTextWrong = "World";
        ByteArrayInputStream inSecond = new ByteArrayInputStream(testTextWrong.getBytes());
        System.setIn(inSecond);

        ConsoleInput input = new ConsoleInput();
        assertNotEquals(input.ask("Question"), "Hello");

    }

    /**
     * whenSetQuestionThanGetOutputStreamValue test method for test ConsoleInput class.
     * when we expected output value
     */

    @Test
    public void whenSetQuestionThanGetOutputStreamValue() {

        String testTextWrong = System.getProperty("line.separator");
        ByteArrayInputStream inSecond = new ByteArrayInputStream(testTextWrong.getBytes());
        System.setIn(inSecond);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));

        ConsoleInput consoleInput = new ConsoleInput();
        consoleInput.ask("Question");

        assertEquals(out.toString(), "Question");

    }

    /**
     * Created method whenEnterMenuPointCheckIfItEqualsWithValueInRange for test ConsoleInput class.
     * when we enter menu point value
     */

    @Test
    public void whenEnterMenuPointCheckIfItEqualsWithValueInRange() {

        int menuPoint = 1;
        int[] range = {1, 2, 3};

        ByteArrayInputStream in = new ByteArrayInputStream(String.valueOf(menuPoint).getBytes());
        System.setIn(in);

        ConsoleInput consoleInput = new ConsoleInput();
        int key = consoleInput.ask("Enter menu point", range);

        assertThat(key, is(1));
    }

    /**
     * Created method whenEnterWrongMenuPointCheckIfItEqualsWithValueInRangeIfItDoesThanThrowException for test ConsoleInput class.
     * when we enter menu point value out of range
     */

    @Test
    public void whenEnterWrongMenuPointCheckIfItEqualsWithValueInRangeIfItDoesThanThrowMOException() {

        int menuPoint = 4;
        int[] range = {1, 2, 3};
        boolean isOkOrNot = false;

        ByteArrayInputStream in = new ByteArrayInputStream(String.valueOf(menuPoint).getBytes());
        System.setIn(in);

        ConsoleInput consoleInput = new ConsoleInput();
        try {
            consoleInput.ask("Enter menu point", range);
        } catch (MenuOutException moe) {
            isOkOrNot = true;
        }

        assertThat(isOkOrNot, is(true));

    }

    /**
     * Created method whenEnterWrongIdCheckIfItEqualsWithValueInRangeIfItDoesThanThrowNFException for test ConsoleInput class.
     * when we enter menu point value out of range
     */

    @Test
    public void whenEnterWrongIdCheckIfItEqualsWithValueInRangeIfItDoesThanThrowNFException() {

        String menuPoint = "111";
        String[] range = {"11", "22", "33"};
        boolean isOkOrNot = false;

        ByteArrayInputStream in = new ByteArrayInputStream(menuPoint.getBytes());
        System.setIn(in);

        ConsoleInput consoleInput = new ConsoleInput();
        try {
            consoleInput.ask("Enter id", range);
        } catch (NumberFormatException nfe) {
            isOkOrNot = true;
        }

        assertThat(isOkOrNot, is(true));

    }

    /**
     * Created method whenEnterTrueIdCheckIfItEqualsWithValueInRangeIfItDoesThanReturnThisId for test ConsoleInput class.
     * when we enter menu point value out of range
     */

    @Test
    public void whenEnterTrueIdCheckIfItEqualsWithValueInRangeIfItDoesThanReturnThisId() {

        String menuPoint = "11";
        String[] range = {"11", "22", "33"};
        boolean isOkOrNot = false;
        int i = 0;
        ByteArrayInputStream in = new ByteArrayInputStream(menuPoint.getBytes());
        System.setIn(in);

        ConsoleInput consoleInput = new ConsoleInput();
        try {
            i = consoleInput.ask("Enter id", range);
        } catch (NumberFormatException nfe) {
            isOkOrNot = true;
        }

        assertThat(i, is(Integer.parseInt(menuPoint)));
        assertThat(isOkOrNot, is(false));

    }
}

@@ -149,16 +149,44 @@
 tStream;
+%0Aimport java.util.ArrayList;
 %0A%0Aimport
@@ -2459,39 +2459,126 @@
         
-int%5B%5D range = %7B1, 2, 3%7D
+ArrayList%3CInteger%3E range = new ArrayList%3C%3E();%0A        range.add(1);%0A        range.add(2);%0A        range.add(3)
 ;%0A%0A     
@@ -3209,31 +3209,118 @@
     
-int%5B%5D range = %7B1, 2, 3%7D
+ArrayList%3CInteger%3E range = new ArrayList%3C%3E();%0A        range.add(1);%0A        range.add(2);%0A        range.add(3)
 ;%0A  
@@ -3740,1635 +3740,6 @@
  %7D%0A%0A
-    /**%0A     * Created method whenEnterWrongIdCheckIfItEqualsWithValueInRangeIfItDoesThanThrowNFException for test ConsoleInput class.%0A     * when we enter menu point value out of range%0A     */%0A%0A    @Test%0A    public void whenEnterWrongIdCheckIfItEqualsWithValueInRangeIfItDoesThanThrowNFException() %7B%0A%0A        String menuPoint = %22111%22;%0A        String%5B%5D range = %7B%2211%22, %2222%22, %2233%22%7D;%0A        boolean isOkOrNot = false;%0A%0A        ByteArrayInputStream in = new ByteArrayInputStream(menuPoint.getBytes());%0A        System.setIn(in);%0A%0A        ConsoleInput consoleInput = new ConsoleInput();%0A        try %7B%0A            consoleInput.ask(%22Enter id%22, range);%0A        %7D catch (NumberFormatException nfe) %7B%0A            isOkOrNot = true;%0A        %7D%0A%0A        assertThat(isOkOrNot, is(true));%0A%0A    %7D%0A%0A    /**%0A     * Created method whenEnterTrueIdCheckIfItEqualsWithValueInRangeIfItDoesThanReturnThisId for test ConsoleInput class.%0A     * when we enter menu point value out of range%0A     */%0A%0A    @Test%0A    public void whenEnterTrueIdCheckIfItEqualsWithValueInRangeIfItDoesThanReturnThisId() %7B%0A%0A        String menuPoint = %2211%22;%0A        String%5B%5D range = %7B%2211%22, %2222%22, %2233%22%7D;%0A        boolean isOkOrNot = false;%0A        int i = 0;%0A        ByteArrayInputStream in = new ByteArrayInputStream(menuPoint.getBytes());%0A        System.setIn(in);%0A%0A        ConsoleInput consoleInput = new ConsoleInput();%0A        try %7B%0A            i = consoleInput.ask(%22Enter id%22, range);%0A        %7D catch (NumberFormatException nfe) %7B%0A            isOkOrNot = true;%0A        %7D%0A%0A        assertThat(i, is(Integer.parseInt(menuPoint)));%0A        assertThat(isOkOrNot, is(false));%0A%0A    %7D%0A
 %7D%0A

delete two test method and change all array to arrayList