FinancingRoundService implements ApplicationListener<ContextRefreshedEvent> { void schedulePostProcessing(FinancingRoundEntity financingRound) { Assert.hasText(financingRound.getId()); final String financingRoundId = financingRound.getId(); crowdScheduler.schedule(() -> { FinancingRoundEntity entity2Process = financingRoundRepository.findOne(financingRoundId); if (entity2Process == null) { log.warn("The FinancingRoundEntity having ID {}, scheduled for post processing doesn't exist (anymore).", financingRoundId); return; } financingRoundPostProcessor.postProcess(entity2Process); }, financingRound.getEndDate().toDate()); log.info("|-- Scheduled post processing of financing round targeted at: {} for financing round {}.", financingRound.getEndDate().toDate(), financingRound); } @Autowired  FinancingRoundService(UserRepository userRepository,
                                 FinancingRoundRepository financingRoundRepository,
                                 ProjectRepository projectRepository,
                                 FinancingRoundPostProcessor financingRoundPostProcessor,
                                 TaskScheduler crowdScheduler,
                                 PledgeRepository pledgeRepository); @Override void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent); List<FinancingRound> allFinancingRounds(); FinancingRound currentlyActiveRound(); FinancingRound mostRecentRound(); FinancingRoundEntity mostRecentRoundEntity(); FinancingRound startNewFinancingRound(FinancingRound creationCommand); FinancingRound stopFinancingRound(String financingRoundId);  }
@Test public void schedulePostProcessing() throws Exception { final FinancingRoundEntity round = financingRoundEntities.get(0); prepareSynchronizedCrowdScheduler(); when(financingRoundRepository.findOne(round.getId())).thenReturn(round); financingRoundService.schedulePostProcessing(round); verify(financingRoundRepository).findOne(eq(round.getId())); verify(financingRoundPostProcessor).postProcess(round); }
@Test public void schedulePostProcessing_doesNothinOnNotExistingEntity() throws Exception { final FinancingRoundEntity round = financingRoundEntities.get(0); prepareSynchronizedCrowdScheduler(); when(financingRoundRepository.findOne(round.getId())).thenReturn(null); financingRoundService.schedulePostProcessing(round); verify(financingRoundRepository).findOne(eq(round.getId())); verify(financingRoundPostProcessor, never()).postProcess(round); }