package de.thpeetz.kontor.tysc.data;

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

import java.util.List;

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

@SpringBootTest
class RoosterTest {

    @Autowired
    private RoosterRepository roosterRepository;

    @Test
    void checkInitialDataLoad() {
        List<Rooster> roosters = roosterRepository.findAll();
        assertEquals(TestConstants.ROOSTER_COUNT, roosters.size());
    }

    @Test
    void checkFetchingData() {
        Rooster rooster = roosterRepository.findAll().get(4);
        assertNotNull(rooster.getTeam());
        assertNotNull(rooster.getPlayer());
        assertNotNull(rooster.getPosition());
    }

    @Test
    void exceptionThrownWhenSavingDuplicateRooster() {
        List<Rooster> roosters = roosterRepository.findAll();
        Rooster existingRooster = roosters.get(5);
        Rooster rooster = new Rooster();
        rooster.setPlayer(existingRooster.getPlayer());
        rooster.setTeam(existingRooster.getTeam());
        rooster.setPosition(existingRooster.getPosition());
        rooster.setYear(existingRooster.getYear());
        assertThrows(DataIntegrityViolationException.class, () -> {
            roosterRepository.save(rooster);
        });
    }
}
