package de.thpeetz.kontor.comics.data;

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

import java.util.List;

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.dao.DataIntegrityViolationException;
import org.springframework.transaction.TransactionSystemException;

import de.thpeetz.kontor.comics.TestConstants;

@SpringBootTest
class ArtistTest {

    @Autowired
    private ArtistRepository artistRepository;

    @Test
    void checkInitialDataLoad() {
        List<Artist> artists = artistRepository.findAll();
        assertEquals(TestConstants.ARTIST_COUNT, artists.size());
    }

    @Test
    void throwExceptionWhenArtistSavedWithEmptyName() {
        Artist artist1 = new Artist();
        assertThrows(TransactionSystemException.class, () -> {
            artistRepository.save(artist1);
        });
    }

    @Test
    void saveArtistWithIdenticalName() {
        String artistName = "Lastname, Firstname";
        Artist artist1 = new Artist();
        artist1.setName(artistName);
        artistRepository.save(artist1);
        Artist artist2 = new Artist();
        artist2.setName(artistName);
        assertThrows(DataIntegrityViolationException.class, () -> {
            artistRepository.save(artist2);
        });
        artistRepository.delete(artist1);
    }
}
