package de.thpeetz.kontor.tysc.data;

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

import de.thpeetz.kontor.tysc.repository.FieldPositionRepository;
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 FieldPositionTest {

    @Autowired
    private FieldPositionRepository fieldPositionRepository;

    @Test
    void checkInitialDataLoad() {
        assertEquals(Integer.toUnsignedLong(TestConstants.POSITION_COUNT), fieldPositionRepository.count());
    }

    @Test
    void throwExceptionWhenPositionSavedWithEmptyName() {
        FieldPosition fieldPosition = new FieldPosition();
        assertThrows(TransactionSystemException.class, () -> {
            fieldPositionRepository.save(fieldPosition);
        });
        fieldPosition.setName("Position");
        assertThrows(TransactionSystemException.class, () -> {
            fieldPositionRepository.save(fieldPosition);
        });
    }

    @Test
    void throwExceptionWhenPositionSavedWithExistingName() {
        FieldPosition existingFieldPosition = fieldPositionRepository.findAll().get(11);
        FieldPosition fieldPosition = new FieldPosition();
        fieldPosition.setName(existingFieldPosition.getName());
        assertThrows(TransactionSystemException.class, () -> {
            fieldPositionRepository.save(fieldPosition);
        });
    }
}
