use of com.faforever.api.data.domain.VotingQuestion 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);
}
use of com.faforever.api.data.domain.VotingQuestion 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);
}
use of com.faforever.api.data.domain.VotingQuestion 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);
}
use of com.faforever.api.data.domain.VotingQuestion 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)));
}
use of com.faforever.api.data.domain.VotingQuestion 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));
}
Aggregations