RandomRunnerSelectionStrategy implements RunnerSelectionStrategy { @Override public RemoteRunner select(List<RemoteRunner> remoteRunners) { if (remoteRunners.size() == 1) { return remoteRunners.get(0); } return remoteRunners.get(random.nextInt(remoteRunners.size())); }  @Override RemoteRunner select(List<RemoteRunner> remoteRunners);  }
@Test public void shouldSelectFromOneElementInList() { RandomRunnerSelectionStrategy strategy = new RandomRunnerSelectionStrategy(); RemoteRunner actual = strategy.select(Lists.newArrayList(runner1)); Assert.assertEquals(actual, runner1); }
@Test public void shouldSelectFromTwoElementInList() { RandomRunnerSelectionStrategy strategy = new RandomRunnerSelectionStrategy(); List<RemoteRunner> remoteRunners = Lists.newArrayList(runner1, runner2); RemoteRunner actual = strategy.select(remoteRunners); Assert.assertTrue(remoteRunners.contains(actual)); }
@Test public void shouldSelectFromThree() { RandomRunnerSelectionStrategy strategy = new RandomRunnerSelectionStrategy(); List<RemoteRunner> remoteRunners = Lists.newArrayList(runner1, runner2, runner3); int runner1Select = 0; int runner2Select = 0; int runner3Select = 0; for (int i = 0; i < 1000; i++) { RemoteRunner actual = strategy.select(remoteRunners); if (actual == runner1) { runner1Select++; } else if (actual == runner2) { runner2Select++; } else if (actual == runner3) { runner3Select++; } } Assert.assertTrue(runner1Select > 250); Assert.assertTrue(runner2Select > 250); Assert.assertTrue(runner3Select > 250); Assert.assertEquals(runner1Select + runner2Select + runner3Select, 1000); }