package de.thpeetz.kontor.bookshelf.services;

import de.thpeetz.kontor.bookshelf.TestConstants;
import de.thpeetz.kontor.bookshelf.data.Author;
import de.thpeetz.kontor.bookshelf.data.Book;
import de.thpeetz.kontor.bookshelf.data.BookshelfPublisher;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class BookshelfServiceTest {

    @Autowired
    private BookshelfService bookshelfService;

    @Test
    @Order(1)
    void testFindAllPublishers() {
        List<BookshelfPublisher> publishers = bookshelfService.findAllPublishers(null);
        assertEquals(TestConstants.PUBLISHER_COUNT, publishers.size());
    }

    @Test
    @Order(2)
    void testSavePublisher() {
        BookshelfPublisher publisher = new BookshelfPublisher();
        publisher.setName(TestConstants.PUBLISHER_NAME);
        bookshelfService.savePublisher(publisher);
        assertEquals(TestConstants.PUBLISHER_COUNT + 1, bookshelfService.findAllPublishers(null).size());
    }

    @Test
    @Order(3)
    void testFindPublisherByName() {
        assertEquals(TestConstants.PUBLISHER_COUNT + 1, bookshelfService.findAllPublishers(null).size());
        BookshelfPublisher publisher = bookshelfService.findPublisherByName(TestConstants.PUBLISHER_NAME);
        assertNotNull(publisher);
    }

    @Test
    @Order(4)
    void testDeletePublisher() {
        List<BookshelfPublisher> publishers = bookshelfService.findAllPublishers(TestConstants.PUBLISHER_NAME);
        assertEquals(1, publishers.size());
        bookshelfService.deletePublisher(publishers.get(0));
        assertEquals(TestConstants.PUBLISHER_COUNT, bookshelfService.findAllPublishers(null).size());
    }

    @Test
    @Order(5)
    void testFindAllAuthors() {
        assertEquals(TestConstants.AUTHOR_COUNT, bookshelfService.findAllAuthors(null).size());
    }

    @Test
    @Order(6)
    void testSaveAuthor() {
        Author author = new Author();
        author.setFirstName(TestConstants.AUTHOR_FIRSTNAME);
        author.setLastName(TestConstants.AUTHOR_LASTNAME);
        bookshelfService.saveAuthor(author);
        assertEquals(TestConstants.AUTHOR_COUNT+1, bookshelfService.findAllAuthors(null).size());
    }

    @Test
    @Order(7)
    void testDeleteAuthor() {
        List<Author> authors = bookshelfService.findAllAuthors(TestConstants.AUTHOR_FIRSTNAME);
        assertEquals(1, authors.size());
        bookshelfService.deleteAuthor(authors.get(0));
        assertEquals(TestConstants.AUTHOR_COUNT, bookshelfService.findAllAuthors(null).size());
    }

    @Test
    @Order(8)
    void findAllBooks() {
        assertEquals(TestConstants.BOOK_COUNT, bookshelfService.findAllBooks(null).size());
    }

    @Test
    @Order(9)
    void saveBook() {
        assertEquals(TestConstants.BOOK_COUNT, bookshelfService.findAllBooks(null).size());
    }

    @Test
    @Order(10)
    void deleteBook() {
        List<Book> books = bookshelfService.findAllBooks(null);
        assertEquals(0, books.size());
    }
}
