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

import de.thpeetz.kontor.comics.TestConstants;

@SpringBootTest
class PublisherTest {

    @Autowired
    private PublisherRepository publisherRepository;

    @Test
    void checkInitialDataLoad() {
        assertEquals(TestConstants.PUBLISHER_COUNT, publisherRepository.findAll().size());
    }

    @Test
    void throwExceptionWhenPublisherSavedWithEmptyName() {
        Publisher publisher = new Publisher();
        assertThrows(TransactionSystemException.class, () -> {
            publisherRepository.save(publisher);
        });
    }

    @Test
    void savePublisherWithIdenticalName() {
        Publisher publisher1 = new Publisher();
        publisher1.setName(TestConstants.PUBLISHER_NAME);
        publisherRepository.save(publisher1);
        Publisher publisher2 = new Publisher();
        publisher2.setName(TestConstants.PUBLISHER_NAME);
        assertThrows(DataIntegrityViolationException.class, () -> {
            publisherRepository.save(publisher2);
        });
        publisherRepository.delete(publisher1);
        assertEquals(TestConstants.PUBLISHER_COUNT, publisherRepository.findAll().size());
    }
}
