package ru.job4j.start;

import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertNull;

import java.util.Date;
import ru.job4j.models.Item;
import ru.job4j.start.*;

/**.
* chapter_002
* It's class for testing behavior users
* 
* @author Anton Vasilyuk
* @version 1.0
* @since 0.1
*/

public class StubInputTest {

	/**.
	* @date date for getTime
	*/
	private Date date = new Date();

	/**.
	* Test for method ADD
	*/
	@Test
	public void whenUserAddItemThenTrackerHasNewItemWithSameName() {
		Tracker tracker = new Tracker();
		Input input = new StubInput(new String[] {"0", "Testing name", "desc", "6"});
		new StartUI(input, tracker).init();
		assertThat(tracker.findAll()[0].getName(), is("Testing name"));
	}

	/**.
	* Test for method FINDALL
	*/
	@Test
	public void whenUserFindAllItemThenTrackerHasShowAllItemWithSameName() {
		Tracker tracker = new Tracker();
		Item item = new Item("Ivan","desc", date.getTime());
		tracker.add(item);
		Input input = new StubInput(new String[] {"1", "6"});
		new StartUI(input, tracker).init();
		assertThat(tracker.findAll()[0].getName(), is("Ivan"));
	}

	/**.
	* Test for method EDIT
	*/
	@Test
	public void whenUserEditItemThenTrackerHasEditItemById() {
		Tracker tracker = new Tracker();
		Item item = new Item("Ivan","desc", date.getTime());
		tracker.add(item);
		item.setId("1");
		Input input = new StubInput(new String[] {"2", "1", "Egor", "desc", "6"});
		new StartUI(input, tracker).init();
		assertThat(tracker.findAll()[0].getName(), is("Egor"));
	}

	/**.
	* Test for method DELETE
	*/
	@Test
	public void whenUserDeleteItemThenTrackerHasDeleteItemById() {
		Tracker tracker = new Tracker();
		Item item = new Item("Ivan","desc", date.getTime());
		tracker.add(item);
		String itemId = item.getId();
		Input input = new StubInput(new String[] {"3", itemId, "6"});
		new StartUI(input, tracker).init();
		assertNull(tracker.findById(itemId));
	}

	/**.
	* Test for method FIND BY ID
	*/
	@Test
	public void whenUserFindByIdItemThenTrackerHasFindItemById() {
		Tracker tracker = new Tracker();
		Item item = new Item("Ivan","desc", date.getTime());
		tracker.add(item);
		item.setId("1");
		Input input = new StubInput(new String[] {"4", "1", "6"});
		new StartUI(input, tracker).init();
		assertThat(tracker.findAll()[0].getName(), is("Ivan"));
	}

	/**.
	* Test for method FIND BY NAME
	*/
	@Test
	public void whenUserFindByNameItemThenTrackerHasFindItemByName() {
		Tracker tracker = new Tracker();
		Item item = new Item("Zoiberg","Bingo", date.getTime());
		tracker.add(item);
		Input input = new StubInput(new String[] {"5", "Zoiberg", "6"});
		new StartUI(input, tracker).init();
		assertThat(tracker.findAll()[0].getDesc(), is("Bingo"));
	}
}
@@ -172,31 +172,180 @@
 ll;%0A
-%0Aimport java.util.Date;
+import static org.junit.Assert.assertTrue;%0A%0Aimport java.util.*;%0Aimport java.lang.*;%0Aimport java.io.*;%0Aimport java.nio.charset.StandardCharsets;%0Aimport java.nio.file.Paths;%0A
 %0Aimp
@@ -396,16 +396,17 @@
 art.*;%0A%0A
+%0A
 /**.%0A* c
@@ -616,16 +616,386 @@
 ate();%0A%0A
+%09/**.%0A%09* method for write print%0A%09* @param fileName name file%0A%09* @return string from file%0A%09*/%0A%09private static String readUsingScanner(String fileName) throws IOException %7B%0A        Scanner scanner = new Scanner(Paths.get(fileName), StandardCharsets.UTF_8.name());%0A        String data = scanner.useDelimiter(%22%5C%5CA%22).next();%0A        scanner.close();%0A        return data;%0A%09%7D%0A%0A
 %09/**.%0A%09*
@@ -2604,32 +2604,49 @@
 asFindItemById()
+ throws Exception
  %7B%0A%09%09Tracker tra
@@ -2690,36 +2690,37 @@
 ew Item(%22Ivan%22,%22
-desc
+bingo
 %22, date.getTime(
@@ -2744,35 +2744,16 @@
 (item);%0A
-%09%09item.setId(%221%22);%0A
 %09%09Input 
@@ -2797,111 +2797,240 @@
 4%22, 
-%221%22, %226%22%7D);%0A%09%09new StartUI(input, tracker).init();%0A%09%09assertThat(tracker.findAll()%5B0%5D.getName(), is(%22Ivan
+item.getId(), %226%22%7D);%0A%09%09FileOutputStream f = new FileOutputStream(%22file.txt%22);%0A%09%09System.setOut(new PrintStream(f));%0A%09%09new StartUI(input, tracker).init();%0A%09%09String fact = readUsingScanner(%22file.txt%22);%0A%09%09assertTrue(fact.contains(%22bingo
 %22));

add file for chapter_002_2.5.3