Search in sources :

Example 6 with Vote

use of com.faforever.api.data.domain.Vote in project faf-java-api by FAForever.

the class VotingSubjectEnricherTest method testQuestionEnhancing.

@Test
public void testQuestionEnhancing() {
    VotingQuestion votingQuestion = new VotingQuestion();
    votingQuestion.setAlternativeQuestion(true);
    votingQuestion.setQuestionKey("abc");
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setEndOfVoteTime(OffsetDateTime.MIN);
    votingSubject.setRevealWinner(true);
    votingQuestion.setVotingSubject(votingSubject);
    Vote vote1 = new Vote();
    Player player1 = new Player();
    vote1.setPlayer(player1);
    Vote vote2 = new Vote();
    Player player2 = new Player();
    vote2.setPlayer(player2);
    Vote vote3 = new Vote();
    Player player3 = new Player();
    vote1.setPlayer(player3);
    Vote vote4 = new Vote();
    Player player4 = new Player();
    vote1.setPlayer(player4);
    Vote vote5 = new Vote();
    Player player5 = new Player();
    vote1.setPlayer(player5);
    VotingChoice votingChoice = new VotingChoice();
    votingChoice.setId(1);
    votingChoice.setVotingQuestion(votingQuestion);
    addAnswerToChoice(votingChoice, votingQuestion, vote1, 0);
    addAnswerToChoice(votingChoice, votingQuestion, vote2, 0);
    VotingChoice votingChoice2 = new VotingChoice();
    votingChoice2.setId(2);
    votingChoice2.setVotingQuestion(votingQuestion);
    addAnswerToChoice(votingChoice2, votingQuestion, vote3, 0);
    addAnswerToChoice(votingChoice2, votingQuestion, vote4, 0);
    addAnswerToChoice(votingChoice2, votingQuestion, vote5, 1);
    VotingChoice votingChoice3 = new VotingChoice();
    votingChoice3.setId(3);
    votingChoice3.setVotingQuestion(votingQuestion);
    addAnswerToChoice(votingChoice2, votingQuestion, vote5, 0);
    instance.calculateWinners(votingQuestion);
    assertThat(votingQuestion.getWinners(), hasItem(votingChoice2));
}
Also used : Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingQuestion(com.faforever.api.data.domain.VotingQuestion) VotingChoice(com.faforever.api.data.domain.VotingChoice) Test(org.junit.Test)

Example 7 with Vote

use of com.faforever.api.data.domain.Vote in project faf-java-api by FAForever.

the class VotingService method ableToVote.

private List<Error> ableToVote(Player player, int votingSubjectId) {
    VotingSubject subject = votingSubjectRepository.findById(votingSubjectId).orElseThrow(() -> new ApiException(new Error(ErrorCode.VOTING_SUBJECT_DOES_NOT_EXIST, votingSubjectId)));
    List<Error> errors = new ArrayList<>();
    Optional<Vote> byPlayerAndVotingSubject = voteRepository.findByPlayerAndVotingSubjectId(player, votingSubjectId);
    if (byPlayerAndVotingSubject.isPresent()) {
        errors.add(new Error(ErrorCode.VOTED_TWICE));
    }
    int gamesPlayed = gamePlayerStatsRepository.countByPlayerAndGameValidity(player, Validity.VALID);
    if (subject.getBeginOfVoteTime().isAfter(OffsetDateTime.now())) {
        errors.add(new Error(ErrorCode.VOTE_DID_NOT_START_YET, subject.getBeginOfVoteTime()));
    }
    if (subject.getEndOfVoteTime().isBefore(OffsetDateTime.now())) {
        errors.add(new Error(ErrorCode.VOTE_ALREADY_ENDED, subject.getEndOfVoteTime()));
    }
    if (gamesPlayed < subject.getMinGamesToVote()) {
        errors.add(new Error(ErrorCode.NOT_ENOUGH_GAMES, gamesPlayed, subject.getMinGamesToVote()));
    }
    return errors;
}
Also used : Vote(com.faforever.api.data.domain.Vote) ArrayList(java.util.ArrayList) Error(com.faforever.api.error.Error) VotingSubject(com.faforever.api.data.domain.VotingSubject) ApiException(com.faforever.api.error.ApiException)

Example 8 with Vote

use of com.faforever.api.data.domain.Vote in project faf-java-api by FAForever.

the class VotingServiceTest method notSaveVoteOnTooManyAnswers.

@Test
public void notSaveVoteOnTooManyAnswers() {
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    votingSubject.setBeginOfVoteTime(OffsetDateTime.now());
    votingSubject.setEndOfVoteTime(OffsetDateTime.MAX);
    VotingQuestion votingQuestion = new VotingQuestion();
    votingQuestion.setAlternativeQuestion(true);
    votingSubject.setVotingQuestions(Collections.singleton(votingQuestion));
    votingQuestion.setMaxAnswers(1);
    Vote vote = new Vote();
    VotingAnswer votingAnswer = new VotingAnswer();
    VotingChoice votingChoice = new VotingChoice();
    votingChoice.setId(1);
    votingChoice.setVotingQuestion(votingQuestion);
    votingAnswer.setVotingChoice(votingChoice);
    VotingAnswer votingAnswer2 = new VotingAnswer();
    VotingChoice votingChoice2 = new VotingChoice();
    votingChoice2.setId(2);
    votingChoice2.setVotingQuestion(votingQuestion);
    votingAnswer2.setVotingChoice(votingChoice2);
    vote.setVotingAnswers(new HashSet<>(Arrays.asList(votingAnswer, votingAnswer2)));
    vote.setVotingSubject(votingSubject);
    Player player = new Player();
    when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.empty());
    when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject));
    when(votingChoiceRepository.findById(votingChoice.getId())).thenReturn(Optional.of(votingChoice));
    when(votingChoiceRepository.findById(votingChoice2.getId())).thenReturn(Optional.of(votingChoice2));
    try {
        instance.saveVote(vote, player);
    } catch (ApiException e) {
        assertTrue(Arrays.stream(e.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.TOO_MANY_ANSWERS)));
    }
    verify(voteRepository, never()).save(vote);
}
Also used : Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) VotingAnswer(com.faforever.api.data.domain.VotingAnswer) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingQuestion(com.faforever.api.data.domain.VotingQuestion) VotingChoice(com.faforever.api.data.domain.VotingChoice) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Example 9 with Vote

use of com.faforever.api.data.domain.Vote in project faf-java-api by FAForever.

the class VotingServiceTest method saveVoteSuccessful.

@Test
public void saveVoteSuccessful() {
    Vote vote = new Vote();
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    votingSubject.setBeginOfVoteTime(OffsetDateTime.now());
    votingSubject.setEndOfVoteTime(OffsetDateTime.MAX);
    VotingQuestion votingQuestion = new VotingQuestion();
    votingQuestion.setAlternativeQuestion(false);
    votingQuestion.setMaxAnswers(1);
    votingSubject.setVotingQuestions(Collections.singleton(votingQuestion));
    vote.setVotingSubject(votingSubject);
    Player player = new Player();
    when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.empty());
    when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject));
    instance.saveVote(vote, player);
    verify(voteRepository).save(vote);
}
Also used : Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingQuestion(com.faforever.api.data.domain.VotingQuestion) Test(org.junit.Test)

Example 10 with Vote

use of com.faforever.api.data.domain.Vote in project faf-java-api by FAForever.

the class VotingServiceTest method notSaveVoteIfAlternativeOrdinalWrong.

@Test
public void notSaveVoteIfAlternativeOrdinalWrong() {
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    votingSubject.setBeginOfVoteTime(OffsetDateTime.now());
    votingSubject.setEndOfVoteTime(OffsetDateTime.MAX);
    VotingQuestion votingQuestion = new VotingQuestion();
    votingQuestion.setAlternativeQuestion(true);
    votingSubject.setVotingQuestions(Collections.singleton(votingQuestion));
    votingQuestion.setMaxAnswers(2);
    Vote vote = new Vote();
    VotingAnswer votingAnswer = new VotingAnswer();
    VotingChoice votingChoice = new VotingChoice();
    votingChoice.setId(1);
    votingChoice.setVotingQuestion(votingQuestion);
    votingAnswer.setVotingChoice(votingChoice);
    votingAnswer.setAlternativeOrdinal(1);
    VotingAnswer votingAnswer2 = new VotingAnswer();
    VotingChoice votingChoice2 = new VotingChoice();
    votingChoice2.setId(2);
    votingChoice2.setVotingQuestion(votingQuestion);
    votingAnswer2.setVotingChoice(votingChoice2);
    votingAnswer2.setAlternativeOrdinal(1);
    vote.setVotingAnswers(new HashSet<>(Arrays.asList(votingAnswer, votingAnswer2)));
    vote.setVotingSubject(votingSubject);
    Player player = new Player();
    when(voteRepository.findByPlayerAndVotingSubjectId(player, votingSubject.getId())).thenReturn(Optional.empty());
    when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject));
    when(votingChoiceRepository.findById(votingChoice.getId())).thenReturn(Optional.of(votingChoice));
    when(votingChoiceRepository.findById(votingChoice2.getId())).thenReturn(Optional.of(votingChoice2));
    try {
        instance.saveVote(vote, player);
    } catch (ApiException e) {
        assertTrue(Arrays.stream(e.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.MALFORMATTED_ALTERNATIVE_ORDINALS)));
    }
    verify(voteRepository, never()).save(vote);
}
Also used : Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) VotingAnswer(com.faforever.api.data.domain.VotingAnswer) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingQuestion(com.faforever.api.data.domain.VotingQuestion) VotingChoice(com.faforever.api.data.domain.VotingChoice) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Aggregations

Vote (com.faforever.api.data.domain.Vote)11 VotingSubject (com.faforever.api.data.domain.VotingSubject)11 Player (com.faforever.api.data.domain.Player)10 Test (org.junit.Test)9 VotingQuestion (com.faforever.api.data.domain.VotingQuestion)8 VotingChoice (com.faforever.api.data.domain.VotingChoice)7 VotingAnswer (com.faforever.api.data.domain.VotingAnswer)5 ApiException (com.faforever.api.error.ApiException)5 Error (com.faforever.api.error.Error)2 ArrayList (java.util.ArrayList)2 Validity (com.faforever.api.data.domain.Validity)1 ErrorCode (com.faforever.api.error.ErrorCode)1 GamePlayerStatsRepository (com.faforever.api.game.GamePlayerStatsRepository)1 OffsetDateTime (java.time.OffsetDateTime)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Transactional (javax.transaction.Transactional)1