package cz.muni.fi.pa165.soccermanager.repository;

import cz.muni.fi.pa165.soccermanager.PersistenceApplication;
import cz.muni.fi.pa165.soccermanager.entity.League;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.testng.annotations.Test;

import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.Transactional;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

import static org.testng.AssertJUnit.assertEquals;

/**
 * @author Vitalii Bortsov (uco: 514699)
 */
@Transactional
@ContextConfiguration(classes = PersistenceApplication.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
public class LeagueRepositoryTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private LeagueRepository leagueRepository;

    @PersistenceUnit
    private EntityManagerFactory emf;

    @Rollback
    @Test
    public void createTest() {
        final var l1 = new League();
        l1.setName("Moon League");
        l1.setEndDate(LocalDate.now());
        l1.setStartDate(LocalDate.now());
        leagueRepository.save(l1);

        final var res = leagueRepository.findAll();
        assertEquals(res, List.of(l1));
    }

    @Rollback
    @Test
    public void removeTest() {
        final var l1 = new League();
        l1.setName("Moon League");
        l1.setEndDate(LocalDate.now());
        l1.setStartDate(LocalDate.now());
        leagueRepository.save(l1);

        final var res = leagueRepository.findAll();
        assertEquals(res, List.of(l1));

        leagueRepository.delete(res.get(0));

        final var res2 = leagueRepository.findAll();
        assertEquals(res2, Collections.emptyList());
    }

    @Rollback
    @Test
    public void findAllTest() {

        assertEquals(Collections.emptyList(), leagueRepository.findAll());

        final var l1 = new League();
        l1.setName("Moon League");
        l1.setEndDate(LocalDate.now());
        l1.setStartDate(LocalDate.now());
        leagueRepository.save(l1);

        assertEquals(List.of(l1), leagueRepository.findAll());

        final var l2 = new League();
        l2.setName("Mars League");
        l2.setEndDate(LocalDate.now());
        l2.setStartDate(LocalDate.now());
        leagueRepository.save(l2);

        assertEquals(List.of(l1, l2), leagueRepository.findAll());

        final var l3 = new League();
        l3.setName("Earth League");
        l3.setEndDate(LocalDate.now());
        l3.setStartDate(LocalDate.now());
        leagueRepository.save(l3);

        assertEquals(List.of(l1, l2, l3), leagueRepository.findAll());
    }
}
