Search in sources :

Example 1 with VotingAnswer

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

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

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

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

Example 5 with VotingAnswer

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

the class VotingSubjectEnricherTest method addAnswerToChoice.

private void addAnswerToChoice(VotingChoice votingChoice, VotingQuestion votingQuestion, Vote vote, int alternativeOrdinal) {
    VotingAnswer votingAnswer = new VotingAnswer();
    votingAnswer.setAlternativeOrdinal(alternativeOrdinal);
    votingAnswer.setVote(vote);
    votingAnswer.setVotingChoice(votingChoice);
    if (vote.getVotingAnswers() != null) {
        vote.getVotingAnswers().add(votingAnswer);
    } else {
        vote.setVotingAnswers(new HashSet<>(Collections.singleton(votingAnswer)));
    }
    if (votingChoice.getVotingAnswers() != null) {
        votingChoice.getVotingAnswers().add(votingAnswer);
    } else {
        votingChoice.setVotingAnswers(new HashSet<>(Collections.singleton(votingAnswer)));
    }
    if (votingQuestion.getVotingChoices() != null) {
        votingQuestion.getVotingChoices().add(votingChoice);
    } else {
        votingQuestion.setVotingChoices(new HashSet<>(Collections.singleton(votingChoice)));
    }
}
Also used : VotingAnswer(com.faforever.api.data.domain.VotingAnswer)

Aggregations

VotingAnswer (com.faforever.api.data.domain.VotingAnswer)7 VotingChoice (com.faforever.api.data.domain.VotingChoice)6 VotingSubject (com.faforever.api.data.domain.VotingSubject)6 Player (com.faforever.api.data.domain.Player)5 Vote (com.faforever.api.data.domain.Vote)5 VotingQuestion (com.faforever.api.data.domain.VotingQuestion)5 Test (org.junit.Test)4 ApiException (com.faforever.api.error.ApiException)3 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 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 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Strings (com.google.common.base.Strings)1