Search in sources :

Example 1 with VotingChoice

use of com.faforever.api.data.domain.VotingChoice 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 VotingChoice

use of com.faforever.api.data.domain.VotingChoice 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 3 with VotingChoice

use of com.faforever.api.data.domain.VotingChoice 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)

Example 4 with VotingChoice

use of com.faforever.api.data.domain.VotingChoice 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 5 with VotingChoice

use of com.faforever.api.data.domain.VotingChoice 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)

Aggregations

VotingChoice (com.faforever.api.data.domain.VotingChoice)10 VotingSubject (com.faforever.api.data.domain.VotingSubject)9 VotingQuestion (com.faforever.api.data.domain.VotingQuestion)8 Player (com.faforever.api.data.domain.Player)7 Vote (com.faforever.api.data.domain.Vote)7 Test (org.junit.Test)7 VotingAnswer (com.faforever.api.data.domain.VotingAnswer)6 ApiException (com.faforever.api.error.ApiException)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 OffsetDateTime (java.time.OffsetDateTime)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 AbstractIntegrationTest (com.faforever.api.AbstractIntegrationTest)1 Validity (com.faforever.api.data.domain.Validity)1 Error (com.faforever.api.error.Error)1 ErrorCode (com.faforever.api.error.ErrorCode)1 GamePlayerStatsRepository (com.faforever.api.game.GamePlayerStatsRepository)1