package pl.rozart.uvote.dao;


import org.junit.Test;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.transaction.annotation.Transactional;
import pl.rozart.uvote.model.Candidate;
import pl.rozart.uvote.model.Election;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;


@Transactional(readOnly = true)
public class VoteDAOTest extends BaseDAOTest{

    @Resource
    private VoteDAO voteDAO;

    @Resource
    private ElectionDAO electionDAO;

    @Test
    @Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:dao/voteCard/create_vote_cards.sql",
            config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED) )
    @Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:dao/voteCard/drop_vote_cards.sql",
            config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED) )
    public void testFindCandidatesByVotesHash(){
        Optional<Election> electionOptional = electionDAO.findById(1L);
        assertThat(electionOptional).isPresent();
        Election election = electionOptional.get();

        List<Candidate> candidates = voteDAO.findCandidatesByVotesHashAndElection("1234", election);
        assertThat(candidates).isNotEmpty().hasSize(4);
        assertThat(candidates.get(0)).isNotNull();
        assertThat(candidates.get(0).getId()).isEqualTo(1L);
        assertThat(candidates.get(1)).isNotNull();
        assertThat(candidates.get(1).getId()).isEqualTo(2L);
        assertThat(candidates.get(2)).isNotNull();
        assertThat(candidates.get(2).getId()).isEqualTo(3L);
        assertThat(candidates.get(3)).isNotNull();
        assertThat(candidates.get(3).getId()).isEqualTo(4L);
    }

}
