From 016febfd1aee29f42a34c2c90af00a88ce0c877a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20Szirbucz?= Date: Mon, 3 Jun 2019 08:28:00 +0200 Subject: [PATCH] Feature/rank candidates 257 (#325) * Add missing test cases to rank candidate #257 * Fix expected times of call calculateWinners. --- .../VoteResultComposerIgnoreTest.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/test/java/org/rulez/demokracia/pdengine/votecalculator/VoteResultComposerIgnoreTest.java diff --git a/src/test/java/org/rulez/demokracia/pdengine/votecalculator/VoteResultComposerIgnoreTest.java b/src/test/java/org/rulez/demokracia/pdengine/votecalculator/VoteResultComposerIgnoreTest.java new file mode 100644 index 00000000..02b85aaa --- /dev/null +++ b/src/test/java/org/rulez/demokracia/pdengine/votecalculator/VoteResultComposerIgnoreTest.java @@ -0,0 +1,83 @@ +package org.rulez.demokracia.pdengine.votecalculator; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; +import static org.rulez.demokracia.pdengine.testhelpers.BeatTableTestHelper.*; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.stubbing.OngoingStubbing; +import org.rulez.demokracia.pdengine.annotations.TestedBehaviour; +import org.rulez.demokracia.pdengine.annotations.TestedFeature; +import org.rulez.demokracia.pdengine.annotations.TestedOperation; +import org.rulez.demokracia.pdengine.beattable.BeatTable; + +@TestedFeature("Schulze method") +@TestedOperation("rank candidates") +@TestedBehaviour("calculates winners until all choices are ignored") +@RunWith(MockitoJUnitRunner.class) +public class VoteResultComposerIgnoreTest { + + @InjectMocks + private VoteResultComposerImpl voteResultComposer; + @Mock + private WinnerCalculatorService winnerCalculatorService; + private OngoingStubbing> whenCalculateWinnersCalled; + + @Test + public void + winner_calculation_ends_in_one_step_when_all_choices_ignored_at_once() { + verifyWinnerCalculatorRunnedNTimes( + createNewBeatTableWithComplexData(), + List.of(List.of(CHOICE1, CHOICE2, CHOICE3)), 1 + ); + } + + @Test + public void + winner_calculation_ends_in_n_steps_when_choices_ignored_one_by_one() { + verifyWinnerCalculatorRunnedNTimes( + createNewBeatTableWithComplexData(), + List.of(List.of(CHOICE1), List.of(CHOICE2), List.of(CHOICE3)), 3 + ); + } + + @Test + public void winner_calculation_ends_in_zero_step_when_beat_table_is_empty() { + verifyWinnerCalculatorRunnedNTimes(new BeatTable(), List.of(), 0); + } + + private void verifyWinnerCalculatorRunnedNTimes( + final BeatTable beatTable, + final List> returnedChoices, final int timesOfRun + ) { + prepareMocks(returnedChoices); + + voteResultComposer + .composeResult(beatTable); + + verify(winnerCalculatorService, times(timesOfRun)) + .calculateWinners(any(), any()); + } + + private void prepareMocks(final List> returnedChoices) { + whenCalculateWinnersCalled = + when(winnerCalculatorService.calculateWinners(any(), any())); + + returnedChoices.forEach( + choiceList -> whenCalculateWinnersCalled = + whenCalculateWinnersCalled.thenReturn(choiceList) + ); + + whenCalculateWinnersCalled.thenThrow( + new AssertionError( + "Computing vote should stop when all choices are ignored" + ) + ); + } +}