package de.thpeetz.kontor.tysc.data;

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

import java.util.List;

import de.thpeetz.kontor.tysc.repository.TeamRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.TransactionSystemException;

import de.thpeetz.kontor.tysc.TestConstants;

@SpringBootTest
class TeamTest {

    @Autowired
    private TeamRepository teamRepository;

    @Test
    void checkInitialDataLoad() {
        List<Team> teams = teamRepository.findAll();
        assertEquals(TestConstants.TEAM_COUNT, teams.size());
    }

    @Test
    void throwExceptionWhenTeamSavedWithEmptyName() {
        Team team = new Team();
        assertThrows(TransactionSystemException.class, () -> {
            teamRepository.save(team);
        });
    }

    @Test
    void throwExceptionWhenTeamSavedWithoutSport() {
        Team team = new Team();
        team.setName(TestConstants.TEAM_NAME);
        assertThrows(TransactionSystemException.class, () -> {
            teamRepository.save(team);
        });
    }

    @Test
    void throwExceptionWhenTeamSavedWithExistingName() {
        Team existingTeam = teamRepository.findAll().get(11);
        Team team = new Team();
        team.setName(existingTeam.getName());
        team.setSport(existingTeam.getSport());
        assertThrows(TransactionSystemException.class, () -> {
            teamRepository.save(team);
        });
    }
}
