RunEntityService extends AbstractEntityService<
        Run,
        RunAddPayload,
        RunGateway> { @Override public Run add(RunAddPayload addPayload) throws AddEntityException { Run lastInSequenceForEvent = gateway.findLastInSequenceForEvent(addPayload.getEvent()); addPayload.setSequence( 1 + (lastInSequenceForEvent != null ? lastInSequenceForEvent.getSequence() : 0) ); return gateway.add(addPayload); } @Inject  RunEntityService(RunGateway gateway, EventEntityService eventEntityService); @Override Run add(RunAddPayload addPayload); Run getByEventIdAndRunId(String eventId, String runId); List<Run> getAllWithEventId(String eventId); RunTimeAddedPayload addTimeToFirstRunInSequenceWithoutRawTime(RunAddTimePayload addTimePayload); List<Run> getAllWithRegistration(Registration registration);  }
@Test public void whenAddRunWithEventNoPriorRunsItShouldSetSequence() throws AddEntityException { when(gateway.findLastInSequenceForEvent(addPayloadEvent)).thenReturn(null); when(gateway.add(addPayload)).thenReturn(addedRun); Run actual = service.add(addPayload); verify(addPayload).setSequence(1); verify(gateway).add(addPayload); assertThat(actual).isSameAs(addedRun); }
@Test public void whenAddRunWithEventManyPriorRunsItShouldSetSequence() throws AddEntityException { Run lastInSequenceForEvent = mock(Run.class); when(gateway.findLastInSequenceForEvent(addPayloadEvent)).thenReturn(lastInSequenceForEvent); when(lastInSequenceForEvent.getSequence()).thenReturn(600); when(gateway.add(addPayload)).thenReturn(addedRun); Run actual = service.add(addPayload); verify(addPayload).setSequence(601); verify(gateway).add(addPayload); assertThat(actual).isSameAs(addedRun); }