package de.thpeetz.kontor.tysc.data;

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

import java.util.List;

import de.thpeetz.kontor.tysc.repository.PlayerRepository;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
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
@TestMethodOrder(OrderAnnotation.class)
class PlayerTest {

    @Autowired
    private PlayerRepository playerRepository;

    @Test
    @Order(1)
    void checkInitialDataLoad() {
        List<Player> players = playerRepository.findAll();
        assertEquals(TestConstants.PLAYER_COUNT, players.size());
    }

    @Test
    @Order(2)
    void throwExceptionWhenPlayerSavedWithEmptyName() {
        Player player = new Player();
        assertThrows(TransactionSystemException.class, () -> {
            playerRepository.save(player);
        });
    }

    @Test
    @Order(3)
    void throwExceptionWhenPlayerSavedWithExistingName() {
        Player existingPlayer = playerRepository.findAll().get(10);
        assertNotNull(existingPlayer);
        Player player = new Player();
        player.setFirstName(existingPlayer.getFirstName());
        player.setLastName(existingPlayer.getLastName());
        assertThrows(DataIntegrityViolationException.class, () -> {
            playerRepository.save(player);
        });
    }
}
