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

import cz.muni.fi.pa165.soccermanager.dto.LeagueDto;
import cz.muni.fi.pa165.soccermanager.entity.League;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDate;
import java.util.List;

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

/**
 * @author Petr Adamec (485584)
 */
@ExtendWith(MockitoExtension.class)
public class LeagueMapperTest {
    private final LeagueMapper leagueMapper = new LeagueMapperImpl();

    @ParameterizedTest
    @NullSource
    public void mapNullTest(League league) {
        assertNull(leagueMapper.map(league));
    }

    @ParameterizedTest
    @NullSource
    public void mapNullTest(List<League> leagues) {
        assertNull(leagueMapper.map(leagues));
    }

    @Test
    public void mapSourceHappy() {
        final var league = new League();
        league.setEndDate(LocalDate.now());
        league.setStartDate(LocalDate.now());
        league.setName("Moon league");
        final LeagueDto res = leagueMapper.map(league);
        assertEquals(res.getName(), league.getName());
        assertEquals(res.getStartDate(), league.getStartDate());
        assertEquals(res.getEndDate(), league.getEndDate());
    }

    @Test
    public void mapSourceListHappy() {
        final var league = new League();
        league.setEndDate(LocalDate.now());
        league.setStartDate(LocalDate.now());
        league.setName("Moon league");

        final List<League> list = List.of(league);
        final var resList = leagueMapper.map(list);
        assertEquals(1, resList.size());
        final LeagueDto res = resList.get(0);
        assertEquals(res.getName(), league.getName());
        assertEquals(res.getStartDate(), league.getStartDate());
        assertEquals(res.getEndDate(), league.getEndDate());
    }

    @Test
    public void updateMapTest() {
        final var league = new League();
        league.setEndDate(LocalDate.now());
        league.setStartDate(LocalDate.now());
        league.setName("Moon league");

        final var dto = new LeagueDto();
        dto.setEndDate(LocalDate.EPOCH);
        dto.setStartDate(LocalDate.EPOCH);
        dto.setName("Sun league");

        leagueMapper.mapOnUpdate(league, dto);
        assertEquals(dto.getName(), league.getName());
        assertEquals(dto.getStartDate(), league.getStartDate());
        assertEquals(dto.getEndDate(), league.getEndDate());
    }
}

