package de.thpeetz.kontor.tysc.data;

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

import java.util.List;

import de.thpeetz.kontor.tysc.repository.SportRepository;
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.tysc.TestConstants;

@SpringBootTest
class SportTest {

    @Autowired
    private SportRepository sportRepository;

    @Test
    void checkInitialDataLoad() {
        List<Sport> sports = sportRepository.findAll();
        assertEquals(TestConstants.SPORT_COUNT, sports.size());
    }

    @Test
    void exceptionThrownWhenSavingSportWithEmptyName() {
        Sport sport = new Sport();
        assertThrows(TransactionSystemException.class, () -> {
            sportRepository.save(sport);
        });
    }

    @Test
    void exceptionThrownWhenSavingSportWithExistingName() {
        Sport existingSport = sportRepository.findAll().get(0);
        Sport sport = new Sport();
        sport.setName(existingSport.getName());
        assertThrows(DataIntegrityViolationException.class, () -> {
            sportRepository.save(sport);
        });
    }
}
