package com.thoughtworks.biblioteca;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;

import static org.junit.Assert.assertEquals;

public class DescisionMakerTest {



    @Test
    public void shouldDisplayLibraryWhenOptionIsOne() {
        ByteArrayInputStream inputStream = new ByteArrayInputStream("1".getBytes());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        System.setIn(inputStream);
        System.setOut(printStream);
        Scanner scanner = new Scanner(inputStream);
        ArrayList<Book> books = new ArrayList<Book>();
        books.add(new Book("Harry Potter", "JK Rowling", 1993));
        Library library = new Library(books);
        DecisionMaker decisionMaker = new DecisionMaker(library,scanner);
        decisionMaker.start();
        assertEquals(String.format("%-34s %-34s %-34d\n","Harry Potter","JK Rowling",1993), outputStream.toString());
    }

    @Test
    public void shouldSuccesfullyCheckoutBookWhenBookIsAvailableAndOptionIsThree() {
        ByteArrayInputStream inputStream = new ByteArrayInputStream("3\nHarry Potter\n".getBytes());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        System.setIn(inputStream);
        System.setOut(printStream);
        Scanner scanner = new Scanner(inputStream);
        ArrayList<Book> books = new ArrayList<Book>();
        books.add(new Book("Harry Potter", "JK Rowling", 1993));
        books.add(new Book("Da Vinci Code", "Dan Brown", 2007));
        Library library = new Library(books);
        DecisionMaker decisionMaker = new DecisionMaker(library,scanner);
        decisionMaker.start();
        assertEquals("Thank you! Enjoy the book\n", outputStream.toString());
    }

    @Test
    public void shouldReturnBookNotValidMessageWhenBookIsNotAvailableAndOptionIsThree() {
        ByteArrayInputStream inputStream = new ByteArrayInputStream("3\nHarry\n".getBytes());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        System.setIn(inputStream);
        System.setOut(printStream);
        Scanner scanner = new Scanner(inputStream);
        ArrayList<Book> books = new ArrayList<Book>();
        books.add(new Book("Harry Potter", "JK Rowling", 1993));
        books.add(new Book("Da Vinci Code", "Dan Brown", 2007));
        Library library = new Library(books);
        DecisionMaker decisionMaker = new DecisionMaker(library,scanner);
        decisionMaker.start();
        assertEquals("That book is not available\n", outputStream.toString());
    }
}

@@ -2860,11 +2860,964 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void shouldReturnMessageWhenValidBookIsReturnedAndOptionIsFour() %7B%0A        ByteArrayInputStream inputStream = new ByteArrayInputStream(%224%5CnHarry Potter%5Cn%22.getBytes());%0A        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();%0A        PrintStream printStream = new PrintStream(outputStream);%0A        System.setIn(inputStream);%0A        System.setOut(printStream);%0A        Scanner scanner = new Scanner(inputStream);%0A        ArrayList%3CBook%3E books = new ArrayList%3CBook%3E();%0A        books.add(new Book(%22Harry Potter%22, %22JK Rowling%22, 1993));%0A        books.add(new Book(%22Da Vinci Code%22, %22Dan Brown%22, 2007));%0A        Library library = new Library(books);%0A        library.checkOutBook(%22Harry Potter%22);%0A        DecisionMaker decisionMaker = new DecisionMaker(library,scanner);%0A        decisionMaker.start();%0A        assertEquals(%22Thank you! Enjoy the book%5CnThank you for returning the book.%5Cn%22, outputStream.toString());%0A    %7D
 %0A%7D%0A

should return valid book when option is 4