package de.thpeetz.kontor.comics.repository;

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

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 VolumeRepositoryTest {

    @Autowired
    private VolumeRepository volumeRepository;

    @Autowired
    private ComicRepository comicRepository;

    @Autowired
    private ComicService comicService;

    @Test
    void testFindByComic() {
        Comic comic = TestConstants.getComicWithoutReferences(comicService);
        assertEquals(0, comic.getVolumes().size());
        Volume volume = new Volume();
        volume.setName(TestConstants.VOLUME_NAME);
        volume.setComic(comic);
        Volume savedInstance = volumeRepository.save(volume);
        assertNotNull(savedInstance);

        List<Volume> found = volumeRepository.findByComic(comic);
        assertEquals(1, found.size());

        comicService.deleteVolume(found.get(0));
        assertEquals(0, comic.getVolumes().size());
        assertEquals(TestConstants.VOLUME_COUNT, volumeRepository.count());
    }

    @Test
    void testFindByName() {
        Comic comic = TestConstants.getComicWithoutReferences(comicService);
        assertEquals(0, comic.getVolumes().size());
        Volume volume = new Volume();
        volume.setName(TestConstants.VOLUME_NAME);
        volume.setComic(comic);
        Volume savedInstance = volumeRepository.save(volume);
        assertNotNull(savedInstance);

        List<Volume> found = volumeRepository.findByName(TestConstants.VOLUME_NAME);
        assertEquals(1, found.size());

        comicService.deleteVolume(found.get(0));
        assertEquals(0, comic.getVolumes().size());
        assertEquals(TestConstants.VOLUME_COUNT, volumeRepository.count());
    }
}
