package de.thpeetz.kontor.comics.data;

import static org.junit.jupiter.api.Assertions.*;

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

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

@SpringBootTest
class StoryArcTest {

    @Autowired
    private StoryArcRepository storyArcRepository;

    @Autowired
    private ComicService comicService;

    @Test
    void checkInitialLoad() {
        assertEquals(TestConstants.STORYARC_COUNT, storyArcRepository.findAll().size());
    }

    @Test
    void exceptionThrownWhenSavingStoryArcWithEmptyName() {
        Comic comic = TestConstants.getComicWithoutReferences(comicService);
        assertNotNull(comic);
        StoryArc storyArc = new StoryArc();
        storyArc.setComic(comic);
        assertThrows(TransactionSystemException.class, () -> {
            storyArcRepository.save(storyArc);
        });
    }

    @Test
    void exceptionThrownWhenSavingStoryArcWithoutComic() {
        StoryArc storyArc = new StoryArc();
        storyArc.setName(TestConstants.STORYARC_NAME);
        assertThrows(TransactionSystemException.class, () -> {
            storyArcRepository.save(storyArc);
        });
    }

    @Test
    void saveStoryArc() {
        Comic comic = TestConstants.getComicWithoutReferences(comicService);
        assertEquals(0, comic.getStoryArcs().size());
        StoryArc storyArc = new StoryArc();
        storyArc.setName(TestConstants.STORYARC_NAME);
        storyArc.setComic(comic);
        StoryArc savedInstance = storyArcRepository.save(storyArc);
        assertNotNull(savedInstance);
        comicService.deleteStoryArc(savedInstance);
        assertEquals(0, comic.getStoryArcs().size());
    }
}
