Skip to content

Commit

Permalink
Fix Error on reveal voting subject
Browse files Browse the repository at this point in the history
Fixes #313
  • Loading branch information
Alexander von Trostorff committed May 8, 2019
1 parent 5d80893 commit 3ef74fc
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,7 @@ private List<VotingChoice> getWinners(VotingQuestion votingQuestion) {

//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);
});
moveOnToTheNextAnswer(votersByChoice, votingAnswer);
});

votersByChoice.remove(candidate);
Expand All @@ -120,4 +113,27 @@ private List<VotingChoice> getWinners(VotingQuestion votingQuestion) {

return first.map(votingChoiceListEntry -> Collections.singletonList(votingChoiceListEntry.getKey())).orElse(Collections.emptyList());
}

private void moveOnToTheNextAnswer(Map<VotingChoice, List<VotingAnswer>> votersByChoice, VotingAnswer votingAnswer) {
int newAlternativeOrdinal = votingAnswer.getAlternativeOrdinal() + 1;
votingAnswer.getVote().getVotingAnswers().stream()
.filter(votingAnswer1 -> votingAnswer1.getVotingChoice() != null
&& votingAnswer1.getVotingChoice().getVotingQuestion().equals(votingAnswer.getVotingChoice().getVotingQuestion())
&& votingAnswer1.getAlternativeOrdinal() == newAlternativeOrdinal)
.findFirst()
.ifPresent(newVotingAnswer -> {
VotingChoice votingChoiceToBeRedistributed = newVotingAnswer.getVotingChoice();
List<VotingAnswer> votingAnswersOfNewChoice = votersByChoice.get(votingChoiceToBeRedistributed);
if (votingAnswersOfNewChoice == null) {
/*
We eliminated two choices/candidates at once and this is the second one.
Apparently one votes voted the candidate we eliminate right now as the next answer.
So we need to skip this one and to straight to the next answer.
*/
moveOnToTheNextAnswer(votersByChoice, newVotingAnswer);
return;
}
votingAnswersOfNewChoice.add(newVotingAnswer);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashSet;

import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -154,6 +155,150 @@ public void testQuestionEnhancingDraw() {
assertThat(votingQuestion.getWinners(), Matchers.allOf(hasItem(votingChoice2), hasItem(votingChoice)));
}

@Test
public void testQuestionEnhancingDrawWithBlankOption() {
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);

Vote vote7 = (Vote) new Vote().setId(7);
Player player7 = (Player) new Player().setId(7);
vote6.setPlayer(player7);


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, 0);

VotingChoice votingChoice3 = new VotingChoice();
votingChoice3.setId(3);
votingChoice3.setVotingQuestion(votingQuestion);

addAnswerToChoice(votingChoice3, votingQuestion, vote7, 0);

addAnswerToChoice(null, votingQuestion, vote7, 1);

instance.calculateWinners(votingQuestion);

assertThat(votingQuestion.getWinners(), Matchers.allOf(hasItem(votingChoice2), hasItem(votingChoice)));
}

@Test
public void testQuestionEnhancingDrawWithTwoCandidatesGettingEliminatedAtTheSameTime() {
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);

Vote vote7 = (Vote) new Vote().setId(7);
Player player7 = (Player) new Player().setId(7);
vote6.setPlayer(player7);


VotingChoice votingChoice = new VotingChoice();
votingChoice.setId(1);
votingChoice.setVotingQuestion(votingQuestion);

addAnswerToChoice(votingChoice, votingQuestion, vote1, 0);
addAnswerToChoice(votingChoice, votingQuestion, vote2, 0);
addAnswerToChoice(votingChoice, votingQuestion, vote3, 1);


VotingChoice votingChoice2 = new VotingChoice();
votingChoice2.setId(2);
votingChoice2.setVotingQuestion(votingQuestion);

addAnswerToChoice(votingChoice2, votingQuestion, vote3, 0);
addAnswerToChoice(votingChoice2, votingQuestion, vote4, 0);
addAnswerToChoice(votingChoice2, votingQuestion, vote1, 1);


VotingChoice votingChoice3 = new VotingChoice();
votingChoice3.setId(3);
votingChoice3.setVotingQuestion(votingQuestion);

addAnswerToChoice(votingChoice3, votingQuestion, vote5, 0);
addAnswerToChoice(votingChoice3, votingQuestion, vote6, 0);
addAnswerToChoice(votingChoice3, votingQuestion, vote7, 0);


instance.calculateWinners(votingQuestion);

assertThat(votingQuestion.getWinners(), is(Collections.singletonList(votingChoice3)));
}

private void addAnswerToChoice(VotingChoice votingChoice, VotingQuestion votingQuestion, Vote vote, int alternativeOrdinal) {
VotingAnswer votingAnswer = new VotingAnswer();
votingAnswer.setAlternativeOrdinal(alternativeOrdinal);
Expand All @@ -166,16 +311,18 @@ private void addAnswerToChoice(VotingChoice votingChoice, VotingQuestion votingQ
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)));
if (votingChoice != null) {
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)));
}
}
}

Expand Down

0 comments on commit 3ef74fc

Please sign in to comment.