package de.thpeetz.kontor.bookshelf.data;

import de.thpeetz.kontor.bookshelf.TestConstants;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.transaction.TransactionSystemException;

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

@SpringBootTest
@Disabled
class BookAuthorTest {

    @Autowired
    BookRepository bookRepository;

    @Autowired
    AuthorRepository authorRepository;

    @Autowired
    BookAuthorRepository bookAuthorRepository;

    @Autowired
    BookshelfPublisherRepository bookshelfPublisherRepository;

    @Test
    void checkInitialLoad() {
        assertEquals(TestConstants.BOOKAUTHOR_COUNT, bookAuthorRepository.findAll().size());
    }

    @Test
    void exceptionThrownWhenSavingEmptyValues() {
        BookAuthor bookAuthor = new BookAuthor();
        assertThrows(TransactionSystemException.class, () -> {
            bookAuthorRepository.save(bookAuthor);
        });
    }

    @Test
    void exceptionThrownWhenSavingIdenticalEntry() {
        Author author = new Author();
        author.setFirstName(TestConstants.AUTHOR_FIRSTNAME);
        author.setLastName(TestConstants.AUTHOR_LASTNAME);
        Author savedAuthor = authorRepository.save(author);
        BookshelfPublisher publisher = new BookshelfPublisher();
        publisher.setName(TestConstants.PUBLISHER_NAME);
        BookshelfPublisher savedPublisher = bookshelfPublisherRepository.save(publisher);
        Book book = new Book();
        book.setTitle(TestConstants.BOOK_TITLE);
        book.setIsbn(TestConstants.BOOK_ISBN);
        book.setPublisher(publisher);
        Book savedBook = bookRepository.save(book);
        BookAuthor bookAuthor = new BookAuthor();
        bookAuthor.setBook(book);
        bookAuthor.setAuthor(author);
        BookAuthor savedBookAuthor = bookAuthorRepository.save(bookAuthor);
        BookAuthor bookAuthor1 = new BookAuthor();
        bookAuthor1.setBook(book);
        bookAuthor1.setAuthor(author);
        assertThrows(DataIntegrityViolationException.class, () -> {
            bookAuthorRepository.save(bookAuthor1);
        });
        savedAuthor.getBookAuthors().remove(savedBookAuthor);
        authorRepository.save(savedAuthor);
        savedBook.getAuthors().remove(savedBookAuthor);
        bookRepository.save(savedBook);
        savedPublisher.getBooks().remove(savedBook);
        bookshelfPublisherRepository.save(savedPublisher);
        authorRepository.delete(savedAuthor);
        assertEquals(TestConstants.AUTHOR_COUNT, authorRepository.findAll().size());
        bookRepository.delete(savedBook);
        assertEquals(TestConstants.ARTICLE_COUNT, bookRepository.findAll().size());
        bookshelfPublisherRepository.delete(savedPublisher);
        assertEquals(TestConstants.PUBLISHER_COUNT, bookshelfPublisherRepository.findAll().size());
        bookAuthorRepository.delete(savedBookAuthor);
        assertEquals(TestConstants.ARTICLEAUTHOR_COUNT, bookAuthorRepository.findAll().size());
    }
}
