ResultsService { public GetRegistrationResultsPayload getResultsFor(Registration registration) { GetRegistrationResultsPayload payload = new GetRegistrationResultsPayload(); payload.setRegistration(registration); List<Run> runs = runEntityService.getAllWithRegistration(registration); int maxRunsPerRegistration = registration.getEvent().getMaxRunsPerRegistration(); List<ScoredRun> scoredRuns = new ArrayList<>(runs.size()); for (Run run : runs) { if (scoredRuns.size() >= maxRunsPerRegistration) { break; } ScoredRun scoredRun = runScoringInteractor.score(run); if (scoredRun != null) { scoredRuns.add(scoredRun); } } payload.setScoredRuns(scoredRuns); payload.setScore(scoredRuns.stream() .min(Comparator.comparing(ScoredRun::getRawTimeScored)) .orElse(null)); return payload; } @Inject  ResultsService(
            RunEntityService runEntityService,
            RunScoringInteractor runScoringInteractor
    ); GetRegistrationResultsPayload getResultsFor(Registration registration);  }
@Test public void itShouldGetResultsWithoutScoreForRegistrationWithNoRuns() { GetRegistrationResultsPayload actual = resultsService.getResultsFor(registration); assertThat(actual).extracting( GetRegistrationResultsPayload::getRegistration, GetRegistrationResultsPayload::getScore ).containsExactly(registration, null); verifyZeroInteractions(runScoringInteractor); }
@Test public void itShouldGetResultsWithScoreForRegistrationWithCompletedRuns() { ScoredRun expectedScoredRun = DomainEntityTestUtils.fullScoredRun(); allRunsWithRegistration.addAll(Arrays.asList(mock(Run.class), mock(Run.class))); expectedScoredRun.setRawTimeScored(TestConstants.SCORED_RUN_RAW_TIME_SCORED.subtract(BigDecimal.ONE)); expectedScoredRun.setHandicapTimeScored(TestConstants.SCORED_RUN_HANDICAP_TIME_SCORED.subtract(BigDecimal.ONE)); ScoredRun unexpectedScoredRun = DomainEntityTestUtils.fullScoredRun(); unexpectedScoredRun.setRawTimeScored(TestConstants.SCORED_RUN_RAW_TIME_SCORED.add(BigDecimal.ONE)); unexpectedScoredRun.setHandicapTimeScored(TestConstants.SCORED_RUN_HANDICAP_TIME_SCORED.add(BigDecimal.ONE)); List<ScoredRun> expectedScoredRuns = Arrays.asList(unexpectedScoredRun, expectedScoredRun); when(runScoringInteractor.score(any(Run.class))).thenReturn(unexpectedScoredRun, expectedScoredRun); GetRegistrationResultsPayload actual = resultsService.getResultsFor(registration); assertThat(actual.getScore()).isEqualTo(expectedScoredRun); assertThat(actual.getScoredRuns()).isEqualTo(expectedScoredRuns); assertThat(actual.getRegistration()).isSameAs(registration); }
@Test public void whenGetResultsForRegistrationItShouldNotScoreRunsBeyondMaxPerRegistration() { when(event.getMaxRunsPerRegistration()).thenReturn(1); allRunsWithRegistration.addAll(Arrays.asList(mock(Run.class), mock(Run.class))); ScoredRun expectedScoredRun = mock(ScoredRun.class); when(runScoringInteractor.score(any())).thenReturn(expectedScoredRun); GetRegistrationResultsPayload actual = resultsService.getResultsFor(registration); assertThat(actual.getScoredRuns()).hasSize(1); assertThat(actual.getScore()).isSameAs(expectedScoredRun); verify(runScoringInteractor, Mockito.times(1)).score(any(Run.class)); }