package com.twu.biblioteca;

import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class BibliotecaAppTest {
    private TestInputHandler input = new TestInputHandler();
    private TestOutputHandler output = new TestOutputHandler();
    private BibliotecaApp app;

    @Before
    public void initialize() {
        app = new BibliotecaApp(input, output, BibliotecaAppTester.initBookList());
    }

    @Test
    public void testBibliotecaStartup() {
        input.add(app.EXIT_CODE);
        app.run();

        assertTrue(output.hasMessage(app.WELCOME_MSG));
        assertTrue(output.hasMessage(app.MAIN_MENU_MSG));

        for (String s : app.MAIN_MENU_OPTIONS) {
            assertTrue(output.hasMessage(s));
        }
    }

    @Test
    public void testInvalidMenuOption() {
        input.add(Integer.toString(-1));
        input.add(app.EXIT_CODE);
        app.run();

        assertTrue(output.hasMessage(app.MENU_ERROR_MSG));
    }

    @Test
    public void testListBooks() {
        input.add(Integer.toString(app.LIST_BOOKS));
        input.add(app.EXIT_CODE);
        app.run();

        assertTrue(output.hasMessage(app.BOOK_HEADER));
        assertTrue(output.hasMessage(BibliotecaAppTester.BOOK_1.printString()));
        assertTrue(output.hasMessage(BibliotecaAppTester.BOOK_2.printString()));
        assertTrue(output.hasMessage(BibliotecaAppTester.BOOK_3.printString()));
        assertTrue(output.hasMessage(BibliotecaAppTester.BOOK_4.printString()));
    }

    @Test
    public void testMenuLoop() {
        input.add(Lists.newArrayList(Integer.toString(app.LIST_BOOKS), Integer.toString(-1), app.EXIT_CODE));
        app.run();

        assertTrue(output.hasMessage(app.BOOK_HEADER));
        assertTrue(output.hasMessage(BibliotecaAppTester.BOOK_1.printString()));
        assertTrue(output.hasMessage(BibliotecaAppTester.BOOK_2.printString()));
        assertTrue(output.hasMessage(BibliotecaAppTester.BOOK_3.printString()));
        assertTrue(output.hasMessage(BibliotecaAppTester.BOOK_4.printString()));
        assertTrue(output.hasMessage(app.MENU_ERROR_MSG));
    }

    @Test
    public void testCheckoutBookSuccess() {
        input.add(Lists.newArrayList(Integer.toString(app.CHECKOUT_BOOK), BibliotecaAppTester.BOOK_3.title, app.EXIT_CODE));
        app.run();

        assertTrue(output.hasMessage(String.format(app.CHECKOUT_SUCCESS_MSG, app.BOOK.toLowerCase())));
    }

    @Test
    public void testCheckoutBookFail() {
        input.add(Lists.newArrayList(Integer.toString(app.CHECKOUT_BOOK), "-1", app.EXIT_CODE));
        app.run();

        assertTrue(output.hasMessage(String.format(app.CHECKOUT_FAIL_MSG, app.BOOK.toLowerCase())));
    }

    @Test
    public void testReturnBookSuccess() {
        input.add(Lists.newArrayList(Integer.toString(app.CHECKOUT_BOOK), BibliotecaAppTester.BOOK_3.title));
        input.add(Lists.newArrayList(Integer.toString(app.RETURN_BOOK), BibliotecaAppTester.BOOK_3.title, app.EXIT_CODE));
        app.run();

        assertTrue(output.hasMessage(String.format(app.RETURN_SUCCESS_MSG, app.BOOK.toLowerCase())));
    }

    @Test
    public void testReturnBookFail() {
        input.add(Lists.newArrayList(Integer.toString(app.RETURN_BOOK), BibliotecaAppTester.BOOK_3.title, app.EXIT_CODE));
        app.run();

        assertTrue(output.hasMessage(String.format(app.RETURN_FAIL_MSG, app.BOOK.toLowerCase())));
    }

}



@@ -962,32 +962,51 @@
       input.add(
+Lists.newArrayList(
 Integer.toString
@@ -1009,37 +1009,18 @@
 ring(-1)
-);%0A        input.add(
+, 
 app.EXIT
@@ -1017,32 +1017,33 @@
 , app.EXIT_CODE)
+)
 ;%0A        app.ru
@@ -1176,16 +1176,35 @@
 put.add(
+Lists.newArrayList(
 Integer.
@@ -1227,37 +1227,18 @@
 T_BOOKS)
-);%0A        input.add(
+, 
 app.EXIT
@@ -1235,32 +1235,33 @@
 , app.EXIT_CODE)
+)
 ;%0A        app.ru
@@ -1557,32 +1557,32 @@
 rintString()));%0A
-
         assertTr
@@ -1645,32 +1645,496 @@
 ing()));%0A    %7D%0A%0A
+    @Test%0A    public void testListMovies() %7B%0A        input.add(Lists.newArrayList(Integer.toString(app.LIST_MOVIES), app.EXIT_CODE));%0A        app.run();%0A%0A        assertTrue(output.hasMessage(app.MOVIE_HEADER));%0A        assertTrue(output.hasMessage(BibliotecaAppTester.MOVIE_1.printString()));%0A        assertTrue(output.hasMessage(BibliotecaAppTester.MOVIE_2.printString()));%0A        assertTrue(output.hasMessage(BibliotecaAppTester.MOVIE_3.printString()));%0A    %7D%0A%0A
     @Test%0A    pu

Add test for listing movies