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