Skip to content

Commit

Permalink
Merge b9f2567 into 06048cf
Browse files Browse the repository at this point in the history
  • Loading branch information
1-alex98 committed May 8, 2019
2 parents 06048cf + b9f2567 commit d5d581b
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 21 deletions.
26 changes: 25 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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(votingAnswerFilter -> votingAnswerFilter.getVotingChoice() != null
&& votingAnswerFilter.getVotingChoice().getVotingQuestion().equals(votingAnswer.getVotingChoice().getVotingQuestion())
&& votingAnswerFilter.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 voter voted the candidate we eliminated before as the next answer.
So we need to skip this answer and go straight to the next one.
*/
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 d5d581b

Please sign in to comment.