Search in sources :

Example 1 with Vote

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

the class VotingServiceTest method saveVoteInvalidChoiceId.

@Test
public void saveVoteInvalidChoiceId() {
    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(0);
    vote.setVotingAnswers(new HashSet<>(Collections.singletonList(votingAnswer)));
    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));
    expectedException.expect(ApiExceptionWithCode.apiExceptionWithCode(ErrorCode.VOTING_CHOICE_DOES_NOT_EXIST));
    instance.saveVote(vote, player);
    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) Test(org.junit.Test)

Example 2 with Vote

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

the class VotingServiceTest method notSaveVoteIfUserVotedAlready.

@Test
public void notSaveVoteIfUserVotedAlready() {
    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.of(new Vote()));
    when(votingSubjectRepository.findById(votingSubject.getId())).thenReturn(Optional.of(votingSubject));
    try {
        instance.saveVote(vote, player);
    } catch (ApiException e) {
        assertTrue(Arrays.stream(e.getErrors()).anyMatch(error -> error.getErrorCode().equals(ErrorCode.VOTED_TWICE)));
    }
    verify(voteRepository, never()).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) ApiException(com.faforever.api.error.ApiException) Test(org.junit.Test)

Example 3 with Vote

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

the class VotingServiceTest method saveVoteInvalidVoteId.

@Test
public void saveVoteInvalidVoteId() {
    Vote vote = new Vote();
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    vote.setVotingSubject(votingSubject);
    expectedException.expect(ApiExceptionWithCode.apiExceptionWithCode(ErrorCode.VOTING_SUBJECT_DOES_NOT_EXIST));
    instance.saveVote(vote, new Player());
    verify(voteRepository, never()).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) Test(org.junit.Test)

Example 4 with Vote

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

the class VotingServiceTest method saveVoteIfAlternativeOrdinalCorrect.

@Test
public void saveVoteIfAlternativeOrdinalCorrect() {
    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(0);
    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));
    instance.saveVote(vote, player);
    verify(voteRepository).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) Test(org.junit.Test)

Example 5 with Vote

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

the class VotingSubjectEnricherTest method testQuestionEnhancingDraw.

@Test
public void testQuestionEnhancingDraw() {
    VotingQuestion votingQuestion = new VotingQuestion();
    votingQuestion.setId(1);
    votingQuestion.setAlternativeQuestion(true);
    votingQuestion.setQuestionKey("abc");
    VotingSubject votingSubject = new VotingSubject();
    votingSubject.setId(1);
    votingSubject.setEndOfVoteTime(OffsetDateTime.MIN);
    votingSubject.setRevealWinner(true);
    votingQuestion.setVotingSubject(votingSubject);
    Vote vote1 = (Vote) new Vote().setId(1);
    Player player1 = (Player) new Player().setId(1);
    vote1.setPlayer(player1);
    Vote vote2 = (Vote) new Vote().setId(2);
    Player player2 = (Player) new Player().setId(2);
    vote2.setPlayer(player2);
    Vote vote3 = (Vote) new Vote().setId(3);
    Player player3 = (Player) new Player().setId(3);
    vote3.setPlayer(player3);
    Vote vote4 = (Vote) new Vote().setId(4);
    Player player4 = (Player) new Player().setId(4);
    vote4.setPlayer(player4);
    Vote vote5 = (Vote) new Vote().setId(5);
    Player player5 = (Player) new Player().setId(5);
    vote5.setPlayer(player5);
    Vote vote6 = (Vote) new Vote().setId(6);
    Player player6 = (Player) new Player().setId(6);
    vote6.setPlayer(player6);
    VotingChoice votingChoice = new VotingChoice();
    votingChoice.setId(1);
    votingChoice.setVotingQuestion(votingQuestion);
    addAnswerToChoice(votingChoice, votingQuestion, vote1, 0);
    addAnswerToChoice(votingChoice, votingQuestion, vote2, 0);
    addAnswerToChoice(votingChoice, votingQuestion, vote6, 0);
    VotingChoice votingChoice2 = new VotingChoice();
    votingChoice2.setId(2);
    votingChoice2.setVotingQuestion(votingQuestion);
    addAnswerToChoice(votingChoice2, votingQuestion, vote4, 0);
    addAnswerToChoice(votingChoice2, votingQuestion, vote3, 0);
    addAnswerToChoice(votingChoice2, votingQuestion, vote5, 1);
    VotingChoice votingChoice3 = new VotingChoice();
    votingChoice3.setId(3);
    votingChoice3.setVotingQuestion(votingQuestion);
    addAnswerToChoice(votingChoice3, votingQuestion, vote5, 0);
    instance.calculateWinners(votingQuestion);
    assertThat(votingQuestion.getWinners(), Matchers.allOf(hasItem(votingChoice2), hasItem(votingChoice)));
}
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)

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