package de.thpeetz.kontor.tysc.data;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.List;

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

    @Autowired
    private VendorRepository vendorRepository;

    @Test
    void checkInitialDataLoad() {
        List<Vendor> vendors = vendorRepository.findAll();
        assertEquals(TestConstants.VENDOR_COUNT, vendors.size());
    }

    @Test
    void exceptionThrownWhenSavingVendortWithEmptyName() {
        Vendor vendor = new Vendor();
        assertThrows(TransactionSystemException.class, () -> {
            vendorRepository.save(vendor);
        });
    }

    @Test
    void exceptionThrownWhenSavingSportWithExistingName() {
        Vendor vendor = new Vendor();
        vendor.setName(TestConstants.TOPPS_NAME);
        assertThrows(DataIntegrityViolationException.class, () -> {
            vendorRepository.save(vendor);
        });
    }
}
