package de.thpeetz.kontor.comics.repository;

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

import java.util.List;

import de.thpeetz.kontor.comics.data.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import de.thpeetz.kontor.comics.TestConstants;
import de.thpeetz.kontor.comics.services.ComicService;

@SpringBootTest
class TradePaperbackRepositoryTest {

    @Autowired
    private TradePaperbackRepository tradePaperbackRepository;

    @Autowired
    private ComicService comicService;

    @Test
    void testSearch() {
        List<TradePaperback> tradePaperbacks = tradePaperbackRepository.search("Dragon");
        assertNotNull(tradePaperbacks);
        assertEquals(1, tradePaperbacks.size());
    }

    @Test
    void testFindByComic() {
        Comic comic = TestConstants.getComicWithTradePaperbacks(comicService);
        List<TradePaperback> tradePaperbacks = tradePaperbackRepository.findByComic(comic);
        assertNotNull(tradePaperbacks);
        assertEquals(TestConstants.SOJOURN_TPB_COUNT, tradePaperbacks.size());
    }

    @Test
    void testFindByNameAndComic() {
        Comic comic = TestConstants.getComicWithTradePaperbacks(comicService);
        List<TradePaperback> tradePaperbacks = tradePaperbackRepository
                .findByNameAndComic(TestConstants.SOJOURN_TPB_NAME, comic);
        assertNotNull(tradePaperbacks);
        assertTrue(tradePaperbacks.size() > 0);
        assertEquals(TestConstants.SOJOURN_TPB_COMIC_TITLE, tradePaperbacks.get(0).getComic().getTitle());
    }

    @Test
    void testFindByFields() {
        Comic comic = TestConstants.getComicWithTradePaperbacks(comicService);
        TradePaperback tradePaperback = tradePaperbackRepository.findByFields(TestConstants.SOJOURN_TPB_NAME,
                comic, TestConstants.SOJOURN_TPB_START, TestConstants.SOJOURN_TPB_END);
        assertNotNull(tradePaperback);
        assertEquals(TestConstants.SOJOURN_TPB_COMIC_TITLE, tradePaperback.getComic().getTitle());
        assertEquals(TestConstants.SOJOURN_TPB_END, tradePaperback.getIssueEnd());
    }
}
