UserNotificationService { public void notifyCreatorAndAdminOnProjectModification(ProjectEntity project, UserEntity modifier) { final String projectLink = buildProjectLink(project.getId()); final Set<UserEntity> users2Notify = new HashSet<>(userRepository.findAllAdminUsers()); users2Notify.add(modifier); users2Notify.add(project.getCreator()); final List<SimpleMailMessage> mails = users2Notify.stream().map(recipient -> { StandardEvaluationContext context = new StandardEvaluationContext(); context.setVariable("link", projectLink); context.setVariable("recipientName", recipient.getFullName()); context.setVariable("modifierName", modifier.getFullName()); final String mailContent = projectModifiedEmailTemplate.getValue(context, String.class); return newMailMessage(recipient.getEmail(), SUBJECT_PROJECT_MODIFIED, mailContent); }).collect(Collectors.toList()); sendMails(mails); }  void sendActivationMail(UserEntity user); void sendPasswordRecoveryMail(UserEntity user); void notifyCreatorOnProjectStatusUpdate(ProjectEntity project); void notifyCreatorOnComment(CommentEntity comment); void notifyCreatorOnIdeaAccepted(IdeaEntity idea, String campaignTitle); void notifyCreatorOnIdeaRejected(IdeaEntity idea, String rejectionComment, String campaignTitle); void notifyCreatorAndAdminOnProjectModification(ProjectEntity project, UserEntity modifier); void notifyAdminOnProjectCreation(ProjectEntity project, String emailAddress); void notifyAdminOnIdeaCreation(IdeaEntity idea, String emailAddress, String campaignTitle); static final Integer COMMENT_EXCERPT_LENGTH; static final String FROM_ADDRESS; static final String PROJECT_LINK_PATTERN; static final String ACTIVATION_LINK_PATTERN; static final String PASSWORD_RECOVERY_LINK_PATTERN; static final String IDEA_CAMPAIGN_LINK_PATTERN; static final String IDEA_CAMPAIGN_LINK_ADMIN_PATTERN; static final String IDEA_CAMPAIGN_YOUR_IDEAS_LINK_PATTERN; static final String SUBJECT_ACTIVATION; static final String SUBJECT_PASSWORD_FORGOTTEN; static final String SUBJECT_IDEA_CREATED; static final String SUBJECT_IDEA_ACCEPTED; static final String SUBJECT_IDEA_REJECTED; static final String SUBJECT_PROJECT_MODIFIED; static final String SUBJECT_PROJECT_CREATED; static final String SUBJECT_PROJECT_PUBLISHED; static final String SUBJECT_PROJECT_REJECTED; static final String SUBJECT_PROJECT_DEFERRED; static final String SUBJECT_PROJECT_COMMENTED; }
@Test public void notifyCreatorAndAdminOnProjectModification() { final UserEntity creator = aProjectCreator(); final UserEntity modifier = aUser("test_id_modifier"); final UserEntity admin = aUser("test_id_admin_0", "admin.0@email.com", "Karl", "Ranseier"); final List<UserEntity> admins = Arrays.asList(admin); final ProjectEntity project = project("proj3ctId", ProjectStatus.PUBLISHED, creator, "My Super Project"); when(userRepository.findAllAdminUsers()).thenReturn(admins); userNotificationService.notifyCreatorAndAdminOnProjectModification(project, modifier); final List<SimpleMailMessage> capturedMessages = getMessagesFromMailSender(); assertThat(capturedMessages.size(), is(3)); assertCreatorModifierAndAdminNotifiedOfProjectEdit(creator, modifier, admin, capturedMessages); capturedMessages.stream().forEach(mail -> { assertThat(mail.getFrom(), is(UserNotificationService.FROM_ADDRESS)); assertThat(mail.getSubject(), is(UserNotificationService.SUBJECT_PROJECT_MODIFIED)); }); }
@Test public void notifyCreatorAndAdminOnProjectModification_NoOneGetsNotifiedTwiceBeeingAlsoAdmin() { final UserEntity creator = aProjectCreator(); final UserEntity modifier = aUser("test_id_modifier"); final UserEntity admin = aUser("test_id_admin_0", "admin.0@email.com", "Karl", "Ranseier"); final List<UserEntity> admins = Arrays.asList(admin, modifier, creator); final ProjectEntity project = project("proj3ctId", ProjectStatus.PUBLISHED, creator, "My Super Project"); when(userRepository.findAllAdminUsers()).thenReturn(admins); userNotificationService.notifyCreatorAndAdminOnProjectModification(project, modifier); final List<SimpleMailMessage> capturedMessages = getMessagesFromMailSender(); assertThat(capturedMessages.size(), is(3)); assertCreatorModifierAndAdminNotifiedOfProjectEdit(creator, modifier, admin, capturedMessages); capturedMessages.stream().forEach(mail -> { assertThat(mail.getFrom(), is(UserNotificationService.FROM_ADDRESS)); assertThat(mail.getSubject(), is(UserNotificationService.SUBJECT_PROJECT_MODIFIED)); }); }