UserService { @Scheduled(cron = "0 0 0 * * ?") public void removeOldPersistentTokens() { LocalDate now = new LocalDate(); persistentTokenRepository.findByTokenDateBefore(now.minusMonths(1)).stream().forEach(token -> { log.debug("Deleting token {}", token.getSeries()); User user = token.getUser(); user.getPersistentTokens().remove(token); persistentTokenRepository.delete(token); }); }  Optional<User> activateRegistration(final String key); Optional<User> completePasswordReset(final String newPassword, final String key); Optional<User> requestPasswordReset(final String mail); User createUserInformation(final String login, final String password, final String firstName, final String lastName, final String email, final String langKey); void updateUserInformation(final String firstName, final String lastName, final String email, final String langKey); void changePassword(final String password); @Transactional(readOnly = true) Optional<User> getUserWithAuthoritiesByLogin(final String login); @Transactional(readOnly = true) User getUserWithAuthorities(final Long id); @Transactional(readOnly = true) User getUserWithAuthorities(); @Scheduled(cron = "0 0 0 * * ?") void removeOldPersistentTokens(); @Scheduled(cron = "0 0 1 * * ?") void removeNotActivatedUsers();  }
@Test public void testRemoveOldPersistentTokens() { User admin = userRepository.findOneByLogin("admin").get(); int existingCount = persistentTokenRepository.findByUser(admin).size(); generateUserToken(admin, "1111-1111", new LocalDate()); LocalDate now = new LocalDate(); generateUserToken(admin, "2222-2222", now.minusDays(32)); assertThat(persistentTokenRepository.findByUser(admin)).hasSize(existingCount + 2); userService.removeOldPersistentTokens(); assertThat(persistentTokenRepository.findByUser(admin)).hasSize(existingCount + 1); }