use of com.faforever.api.data.domain.VotingQuestion 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.VotingQuestion 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.VotingQuestion 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);
}
use of com.faforever.api.data.domain.VotingQuestion in project faf-java-api by FAForever.
the class VotingElideTest method testRevealWinnerOnEndedSubjectWorks.
@Test
@WithUserDetails(AUTH_MODERATOR)
public void testRevealWinnerOnEndedSubjectWorks() throws Exception {
mockMvc.perform(patch("/data/votingSubject/2").header(HttpHeaders.CONTENT_TYPE, DataController.JSON_API_MEDIA_TYPE).content(PATCH_VOTING_SUBJECT_REVEAL_ID_2)).andExpect(status().isNoContent());
VotingQuestion question = votingQuestionRepository.getOne(2);
List<VotingChoice> winners = question.getWinners();
assertThat(winners, hasSize(1));
assertThat(winners.get(0).getId(), is(3));
}
use of com.faforever.api.data.domain.VotingQuestion in project faf-java-api by FAForever.
the class VotingSubjectEnricher method getWinners.
private List<VotingChoice> getWinners(VotingQuestion votingQuestion) {
if (!votingQuestion.isAlternativeQuestion()) {
OptionalInt max = votingQuestion.getVotingChoices().stream().mapToInt(value -> value.getVotingAnswers().size()).max();
if (max.isPresent()) {
return votingQuestion.getVotingChoices().stream().filter(votingChoice -> votingChoice.getVotingAnswers().size() == max.getAsInt()).collect(toList());
}
return Collections.emptyList();
}
// All the answers sorted by their choice, but only those that are the 1st choice
Map<VotingChoice, List<VotingAnswer>> votersByChoice = votingQuestion.getVotingChoices().stream().collect(Collectors.toMap(Function.identity(), choice -> new ArrayList<>(choice.getVotingAnswers().stream().filter(votingAnswer -> votingAnswer.getAlternativeOrdinal() == 0).collect(toList()))));
while (votersByChoice.size() > 1) {
OptionalInt min = votersByChoice.values().stream().mapToInt(List::size).min();
List<VotingChoice> candidatesToEliminate = votersByChoice.entrySet().stream().filter(votingChoiceListEntry -> votingChoiceListEntry.getValue().size() == min.getAsInt()).map(Entry::getKey).collect(toList());
if (candidatesToEliminate.size() == votersByChoice.size()) {
// We got a problem here, we would eliminate all the candidates if we went on normally
return candidatesToEliminate;
}
candidatesToEliminate.forEach(candidate -> {
List<VotingAnswer> votingAnswersForCandidate = votersByChoice.get(candidate);
// Lets distribute the answers of the candidate that is eliminated
votingAnswersForCandidate.forEach(votingAnswer -> {
int newAlternativeOrdinal = votingAnswer.getAlternativeOrdinal() + 1;
votingAnswer.getVote().getVotingAnswers().stream().filter(votingAnswer1 -> votingAnswer1.getVotingChoice().getVotingQuestion().equals(votingAnswer.getVotingChoice().getVotingQuestion()) && votingAnswer1.getAlternativeOrdinal() == newAlternativeOrdinal).findFirst().ifPresent(votingAnswer1 -> {
VotingChoice votingChoice1 = votingAnswer1.getVotingChoice();
votersByChoice.get(votingChoice1).add(votingAnswer1);
});
});
votersByChoice.remove(candidate);
});
}
Optional<Entry<VotingChoice, List<VotingAnswer>>> first = votersByChoice.entrySet().stream().findFirst();
return first.map(votingChoiceListEntry -> Collections.singletonList(votingChoiceListEntry.getKey())).orElse(Collections.emptyList());
}
Aggregations