FinancingRoundService implements ApplicationListener<ContextRefreshedEvent> { public FinancingRound stopFinancingRound(String financingRoundId) throws ResourceNotFoundException, InvalidRequestException { FinancingRoundEntity financingRoundEntity = financingRoundRepository.findOne(financingRoundId); if (financingRoundEntity == null) { throw new ResourceNotFoundException(); } if (financingRoundEntity.getEndDate().isBeforeNow()) { throw InvalidRequestException.financingRoundAlreadyStopped(); } financingRoundEntity.stopFinancingRound(); financingRoundRepository.save(financingRoundEntity); financingRoundEntity = financingRoundPostProcessor.postProcess(financingRoundEntity); return financingRound(financingRoundEntity); } @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 stopFinancingRound() throws Exception { final DateTime futureDate = fixedDate.plusDays(5000); when(financingRoundRepository.findOne(ROUND_ID)).thenReturn(financingRoundEntity(ROUND_ID, fixedDate.minusDays(100), futureDate)); FinancingRound res = financingRoundService.stopFinancingRound(ROUND_ID); ArgumentCaptor<FinancingRoundEntity> entityCaptor = ArgumentCaptor.forClass(FinancingRoundEntity.class); verify(financingRoundRepository, atLeastOnce()).save(entityCaptor.capture()); verify(financingRoundPostProcessor).postProcess(entityCaptor.getValue()); assertThat(entityCaptor.getValue().getEndDate(), not(futureDate)); assertThat(res.isActive(), is(false)); assertThat(res.getBudget(), is(ROUND_BUDGET)); }
@Test(expected = ResourceNotFoundException.class) public void stopFinancingRound_missingRoundThrowsResourceNotFoundException() throws Exception { String roundId = "1337"; when(financingRoundRepository.findOne(roundId)).thenReturn(null); financingRoundService.stopFinancingRound(roundId); }
@Test public void stopFinancingRound_alreadyStoppedThrowsInvalidRequestException() throws Exception { String roundId = "1337"; when(financingRoundRepository.findOne(anyString())).thenReturn(financingRoundEntity(null, fixedDate.minusDays(100), fixedDate.minusDays(50))); try { financingRoundService.stopFinancingRound(roundId); fail("Expected InvalidRequestException did not occur"); } catch (Exception e) { assertTrue("Expected an InvalidRequestException", e instanceof InvalidRequestException); assertThat(e.getMessage(), is(InvalidRequestException.financingRoundAlreadyStopped().getMessage())); verify(financingRoundRepository, never()).save(any(FinancingRoundEntity.class)); } }