package de.thpeetz.kontor.bookshelf.data;

import de.thpeetz.kontor.bookshelf.TestConstants;
import org.junit.jupiter.api.MethodOrderer;
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.*;

@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ArticleRepositoryTest {

    @Autowired
    ArticleRepository articleRepository;

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

    @Test
    @Order(2)
    void saveArticle() {
        Article article = new Article();
        article.setTitle(TestConstants.ARTICLE_TITLE);
        articleRepository.save(article);
        assertEquals(TestConstants.ARTICLE_COUNT+1, articleRepository.findAll().size());
    }

    @Test
    @Order(3)
    void search() {
        List<Article> articles = articleRepository.search(TestConstants.ARTICLE_TITLE.substring(2,5));
        assertEquals(1, articles.size());
        assertEquals(0, articleRepository.search(TestConstants.BOOK_TITLE).size());
    }

    @Test
    @Order(4)
    void findByTitle() {
        List<Article> articles = articleRepository.search(TestConstants.ARTICLE_TITLE);
        assertEquals(1, articles.size());
    }

    @Test
    @Order(5)
    void deleteArticle() {
        List<Article> articles = articleRepository.findAll();
        assertEquals(1, articles.size());
        assertEquals(0, articles.get(0).getAuthors().size());
        articleRepository.delete(articles.get(0));
        assertEquals(TestConstants.ARTICLE_COUNT, articleRepository.findAll().size());
    }
}