package ru.matevosyan.start;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.util.NoSuchElementException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

/**
 * Class ConsoleInputTest created for testing.
 * Created on 18.01.2017.
 * @since 1.0
 * @version 1.0
 * @author Matevosyan Vardan
 */
public class ValidateInputTest {

    /**
     * whenMenuPointIsIllegalThanThrowException for testing validate input.
     */

    @Test
    public void whenMenuPointIsIllegalThanThrowException() {
        String answer = "4n";
        int[] range = {1, 2, 3};
        boolean invalid = true;

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

        Input validate = new ConsoleInput();
            try {
                validate.ask("Enter menu point", range);
            } catch (MenuOutException moe) {
                invalid = true;
            } catch (NumberFormatException nfe) {
                invalid = false;
            }

        assertThat(invalid, is(false));

    }

    /**
     * whenMenuPointIsOutOfRangeThanThrowException for testing validate input.
     */
    @Test
    public void whenMenuPointIsOutOfRangeThanThrowException() {
        String answer = "4";
        int[] range = {1, 2, 3};
        boolean invalid = false;
        String question = "Enter menu point";

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

        ConsoleInput validate = new ConsoleInput();
            try {
                validate.ask(question, range);
            } catch (MenuOutException moe) {
                invalid = true;
            }

        assertThat(invalid, is(true));
    }

    /**
     * whenMenuPointIsOKThanReturnMenuPoint for testing validate input.
     */
    @Test
    public void whenMenuPointIsOKThanReturnTrue() {
        String answer = "3";
        int[] range = {1, 2, 3};
        boolean invalid = true;

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

        Input validate = new ValidateInput();
            try {
                validate.ask("Enter menu point", range);
            } catch (NumberFormatException nfe) {
                invalid = false;
            }

        assertThat(invalid, is(true));
    }

    /**
     * whenMenuPointIsOutOfRangeAfterThatIsInTheRangeThanReturnTrue for testing validate input.
     */

    @Test
    public void whenMenuPointIsOutOfRangeAfterThatIsInTheRangeThanReturnTrue() {
        String answer = "4";
        String answer2 = "3";

        int[] range = {1, 2, 3};
        boolean invalid = true;
        String question = "Enter menu point";

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

        ByteArrayInputStream in2 = new ByteArrayInputStream(answer2.getBytes());
        System.setIn(in2);

        Input validate = new ValidateInput();
        try {
            validate.ask(question, range);
        } catch (MenuOutException moe) {
            invalid = false;
        }

        assertThat(invalid, is(true));
    }

    /**
     * whenItemsIdIsOKThanReturnTrue for testing validate input.
     */
    @Test
    public void whenItemsIdIsOKThanReturnTrue() {
        String answer = "3";
        String[] range = {"1", "2", "3"};
        boolean invalid = true;

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

        Input validate = new ValidateInput();
        try {
            validate.ask("Enter id", range);
        } catch (NumberFormatException nfe) {
            invalid = false;
        }

        assertThat(invalid, is(true));
    }

    /**
     * whenItemsIdIsNotOKThanReturnTrue for testing validate input.
     */
    @Test
    public void whenItemsIdIsNotOKThanReturnTrue() {
        String answer = "4";
        String[] range = {"1", "2", "3"};
        boolean invalid = true;

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

        Input validate = new ValidateInput();
        try {
            validate.ask("Enter id", range);
        } catch (NoSuchElementException nfe) {
            invalid = false;
        }

        assertThat(invalid, is(false));
    }
}

@@ -101,38 +101,25 @@
 va.util.
-NoSuchElementException
+ArrayList
 ;%0A%0Aimpor
@@ -582,39 +582,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      
@@ -1386,33 +1386,121 @@
     
-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
     
@@ -2127,33 +2127,121 @@
     
-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
     
@@ -2916,33 +2916,121 @@
     
-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
     
@@ -3553,1189 +3553,6 @@
  %7D%0A%0A
-    /**%0A     * whenItemsIdIsOKThanReturnTrue for testing validate input.%0A     */%0A    @Test%0A    public void whenItemsIdIsOKThanReturnTrue() %7B%0A        String answer = %223%22;%0A        String%5B%5D range = %7B%221%22, %222%22, %223%22%7D;%0A        boolean invalid = true;%0A%0A        ByteArrayInputStream in = new ByteArrayInputStream(answer.getBytes());%0A        System.setIn(in);%0A%0A        Input validate = new ValidateInput();%0A        try %7B%0A            validate.ask(%22Enter id%22, range);%0A        %7D catch (NumberFormatException nfe) %7B%0A            invalid = false;%0A        %7D%0A%0A        assertThat(invalid, is(true));%0A    %7D%0A%0A    /**%0A     * whenItemsIdIsNotOKThanReturnTrue for testing validate input.%0A     */%0A    @Test%0A    public void whenItemsIdIsNotOKThanReturnTrue() %7B%0A        String answer = %224%22;%0A        String%5B%5D range = %7B%221%22, %222%22, %223%22%7D;%0A        boolean invalid = true;%0A%0A        ByteArrayInputStream in = new ByteArrayInputStream(answer.getBytes());%0A        System.setIn(in);%0A%0A        Input validate = new ValidateInput();%0A        try %7B%0A            validate.ask(%22Enter id%22, range);%0A        %7D catch (NoSuchElementException nfe) %7B%0A            invalid = false;%0A        %7D%0A%0A        assertThat(invalid, is(false));%0A    %7D%0A
 %7D%0A

delete two test methods and change all array to arrayList