MailDigestService { @Scheduled(cron = "0 0 22 * * ?") public void dailyDigest() { log.info("Starting Daily digest mail process "); Set<Domain> domains = domainRepository.getAllDomains(); String day = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_WEEK)); for (Domain d : domains) { log.info("Sending daily digest for domain {} and day {}", d, day); int pagination = 0; List<String> logins; do { logins = mailDigestRepository.getLoginsRegisteredToDigest( DigestType.DAILY_DIGEST, d.getName(), day, pagination); pagination = pagination + logins.size(); for (String login : logins) { try { handleDailyDigestPageForLogin(login); } catch (Exception e) { log.warn("An error has occured when generating daily digest for user " + login + ": " + e.getMessage()); StringWriter stack = new StringWriter(); PrintWriter pw = new PrintWriter(stack); e.printStackTrace(pw); log.debug("{}", stack.toString()); } } } while (logins.size() > 0); } }  @Scheduled(cron = "0 0 22 * * ?") void dailyDigest(); @Scheduled(cron = "0 0 01 ? * MON") void weeklyDigest();  }
@Test public void order_05_shouldGenerateDailyDigestWithManyMessages() { log.debug("In shouldGenerateDailyDigestWithManyMessages"); mockAuthenticationOnUserService(DAILY_DIGEST_USER); userService.updateDailyDigestRegistration(true); mockAuthenticationOnFriendshipService(DAILY_DIGEST_USER); friendshipService.followUser("userWhoPostForDigests"); mockAuthenticationOnTimelineServiceWithACurrentUser("userWhoPostForDigests@ippon.fr"); mockAuthenticationOnStatusUpdateServiceWithACurrentUser("userWhoPostForDigests@ippon.fr"); for (int i = 0; i < 20; i++) { String content2 = "voilà un message " + i + " qui devrait se retrouver dans le digest ! "; statusUpdateService.postStatus(content2, false, new ArrayList<String>()); } ArgumentCaptor<List> statuses = ArgumentCaptor.forClass(List.class); mailDigestService.dailyDigest(); verify(mailServiceMock).sendDailyDigestEmail(any(User.class), statuses.capture(), anyInt(), anyCollection()); assertThat(statuses.getValue().size() == 10, is(true)); }
@Test public void order_00_shouldNotGenerateDailyDigest() { log.debug("In shouldNotGenerateDailyDigest"); mailDigestService.dailyDigest(); verifyZeroInteractions(mailServiceMock); }
@Test public void order_01_shouldNotGenerateWeeklyDigest() { log.debug("In shouldNotGenerateWeeklyDigest"); mailDigestService.dailyDigest(); verifyZeroInteractions(mailServiceMock); }
@Test public void order_03_shouldGenerateDailyDigestNoMessage() { log.debug("In shouldGenerateDailyDigestNoMessage"); mockAuthenticationOnUserService(DAILY_DIGEST_USER); userService.updateDailyDigestRegistration(true); ArgumentCaptor<List> statuses = ArgumentCaptor.forClass(List.class); mailDigestService.dailyDigest(); verify(mailServiceMock).sendDailyDigestEmail(any(User.class), statuses.capture(), anyInt(), anyCollection()); assertThat(statuses.getValue().size() == 0, is(true)); }
@Test public void order_04_shouldGenerateDailyDigestOneMessage() { log.debug("In shouldGenerateDailyDigestOneMessage"); mockAuthenticationOnUserService(DAILY_DIGEST_USER); userService.updateDailyDigestRegistration(true); mockAuthenticationOnFriendshipService(DAILY_DIGEST_USER); friendshipService.followUser("userWhoPostForDigests"); mockAuthenticationOnTimelineServiceWithACurrentUser("userWhoPostForDigests@ippon.fr"); mockAuthenticationOnStatusUpdateServiceWithACurrentUser("userWhoPostForDigests@ippon.fr"); String content = "voilà un message qui devrait se retrouver dans le digest ! "; statusUpdateService.postStatus(content, false, new ArrayList<String>()); ArgumentCaptor<List> statuses = ArgumentCaptor.forClass(List.class); mailDigestService.dailyDigest(); verify(mailServiceMock).sendDailyDigestEmail(any(User.class), statuses.capture(), anyInt(), anyCollection()); assertThat(statuses.getValue().size() == 1, is(true)); }
@Test public void order_04_shouldGenerateDailyDigestWithTwoMessages() { log.debug("In shouldGenerateDailyDigestWithTwoMessages"); mockAuthenticationOnUserService(DAILY_DIGEST_USER); userService.updateDailyDigestRegistration(true); mockAuthenticationOnFriendshipService(DAILY_DIGEST_USER); friendshipService.followUser("userWhoPostForDigests"); mockAuthenticationOnTimelineServiceWithACurrentUser("userWhoPostForDigests@ippon.fr"); mockAuthenticationOnStatusUpdateServiceWithACurrentUser("userWhoPostForDigests@ippon.fr"); String content2 = "voilà un message 2 qui devrait se retrouver dans le digest ! "; statusUpdateService.postStatus(content2, false, new ArrayList<String>()); ArgumentCaptor<List> statuses = ArgumentCaptor.forClass(List.class); mailDigestService.dailyDigest(); verify(mailServiceMock).sendDailyDigestEmail(any(User.class), statuses.capture(), anyInt(), anyCollection()); assertThat(statuses.getValue().size() == 2, is(true)); }