package de.thpeetz.kontor.bookshelf.data;

import de.thpeetz.kontor.bookshelf.TestConstants;
import org.junit.jupiter.api.Disabled;
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 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
@TestMethodOrder(OrderAnnotation.class)
@Disabled
class ArticleAuthorTest {

    @Autowired
    ArticleRepository articleRepository;

    @Autowired
    AuthorRepository authorRepository;

    @Autowired
    ArticleAuthorRepository articleAuthorRepository;

    @Test
    @Order(1)
    void checkInitialLoad() {
        assertEquals(TestConstants.ARTICLEAUTHOR_COUNT, articleAuthorRepository.findAll().size());
    }

    @Test
    @Order(2)
    void exceptionThrownWhenSavingEmptyValues() {
        ArticleAuthor articleAuthor = new ArticleAuthor();
        assertThrows(TransactionSystemException.class, () -> {
            articleAuthorRepository.save(articleAuthor);
        });
    }

    @Test
    @Order(3)
    void exceptionThrownWhenSavingIdenticalEntry() {
        Author author = new Author();
        author.setFirstName(TestConstants.AUTHOR_FIRSTNAME);
        author.setLastName(TestConstants.AUTHOR_LASTNAME);
        Author savedAuthor = authorRepository.save(author);
        Article article = new Article();
        article.setTitle(TestConstants.ARTICLE_TITLE);
        Article savedArticle = articleRepository.save(article);
        ArticleAuthor articleAuthor = new ArticleAuthor();
        articleAuthor.setArticle(article);
        articleAuthor.setAuthor(author);
        ArticleAuthor savedArticleAuthor = articleAuthorRepository.save(articleAuthor);
        ArticleAuthor articleAuthor1 = new ArticleAuthor();
        articleAuthor1.setArticle(article);
        articleAuthor1.setAuthor(author);
        assertThrows(DataIntegrityViolationException.class, () -> {
            articleAuthorRepository.save(articleAuthor1);
        });
        articleAuthorRepository.delete(savedArticleAuthor);
        assertEquals(TestConstants.ARTICLEAUTHOR_COUNT, articleAuthorRepository.findAll().size());
        savedAuthor.getArticleAuthors().remove(savedArticleAuthor);
        authorRepository.save(savedAuthor);
        authorRepository.delete(savedAuthor);
        assertEquals(TestConstants.AUTHOR_COUNT, authorRepository.findAll().size());
        savedArticle.getAuthors().remove(savedArticleAuthor);
        articleRepository.save(savedArticle);
        articleRepository.delete(savedArticle);
        assertEquals(TestConstants.ARTICLE_COUNT, articleRepository.findAll().size());
    }
}
