Search in sources :

Example 11 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class SMTPNotificationManagerTest method testEmailSendingWithParamsWithoutTemplate.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testEmailSendingWithParamsWithoutTemplate() {
    PipelineUser user = new PipelineUser();
    user.setUserName(USER_NAME);
    user.setAttributes(Collections.singletonMap(EMAIL_KEY, EMAIL));
    userRepository.save(user);
    NotificationMessage message = new NotificationMessage();
    message.setSubject(MESSAGE_SUBJECT);
    message.setBody(MESSAGE_BODY_WITH_PARAM);
    message.setTemplateParameters(Collections.singletonMap("name", USER_NAME));
    message.setToUserId(user.getId());
    message.setCopyUserIds(Collections.singletonList(user.getId()));
    smtpNotificationManager.notifySubscribers(message);
    MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
    assertTrue(receivedMessages.length == 2);
    String filledMessage = PARSED_MESSAGE_BODY_WITH_PARAM.replace("$templateParameters.get(\"name\")", USER_NAME);
    System.out.println(GreenMailUtil.getBody(receivedMessages[0]));
    System.out.println(filledMessage);
    assertTrue(GreenMailUtil.getBody(receivedMessages[0]).contains(filledMessage));
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest) AbstractSpringTest(com.epam.pipeline.notifier.AbstractSpringTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class NotificationRepositoryTest method deleteByIdTest.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void deleteByIdTest() {
    NotificationMessage message = new NotificationMessage();
    NotificationTemplate template = new NotificationTemplate();
    template.setSubject(SUBJECT);
    template.setBody(BODY_WITHOUT_PARAM);
    templateRepository.save(template);
    message.setTemplate(template);
    message.setToUserId(0L);
    message.setCopyUserIds(Collections.singletonList(0L));
    notificationRepository.save(message);
    Long idToDelete = message.getId();
    notificationRepository.deleteById(idToDelete);
    Assert.assertNull(notificationRepository.findOne(idToDelete));
}
Also used : NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.notifier.AbstractSpringTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class NotificationRepositoryTest method loadTest.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void loadTest() {
    NotificationMessage message = new NotificationMessage();
    NotificationTemplate template = new NotificationTemplate();
    template.setSubject(SUBJECT);
    template.setBody(BODY_WITH_PARAM);
    templateRepository.save(template);
    message.setTemplate(template);
    message.setTemplateParameters(Collections.singletonMap(PARAM, Integer.toHexString(this.hashCode())));
    message.setToUserId(0L);
    message.setCopyUserIds(Collections.singletonList(0L));
    notificationRepository.save(message);
    List<NotificationMessage> messages = notificationRepository.loadNotification(new PageRequest(0, 5));
    Assert.assertTrue(messages.size() == 1);
    NotificationMessage loaded = messages.get(0);
    Assert.assertEquals(Integer.toHexString(this.hashCode()), loaded.getTemplateParameters().get(PARAM));
    Assert.assertEquals(BODY_WITH_PARAM, loaded.getTemplate().getBody());
    Assert.assertEquals(SUBJECT, loaded.getTemplate().getSubject());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.notifier.AbstractSpringTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class NotificationManager method notifyIssueComment.

@Transactional(propagation = Propagation.REQUIRED)
public void notifyIssueComment(IssueComment comment, Issue issue, String htmlText) {
    NotificationSettings newIssueCommentSettings = notificationSettingsManager.load(NotificationType.NEW_ISSUE_COMMENT);
    if (newIssueCommentSettings == null || !newIssueCommentSettings.isEnabled()) {
        LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_NOTIFICATION_TEMPLATE_NOT_CONFIGURED, "new issue"));
        return;
    }
    NotificationMessage message = new NotificationMessage();
    message.setTemplate(new NotificationTemplate(newIssueCommentSettings.getTemplateId()));
    AbstractSecuredEntity entity = entityManager.load(issue.getEntity().getEntityClass(), issue.getEntity().getEntityId());
    List<PipelineUser> referencedUsers = userManager.loadUsersByNames(Arrays.asList(entity.getOwner(), issue.getAuthor()));
    List<Long> ccUserIds = getMentionedUsers(comment.getText());
    referencedUsers.stream().filter(u -> u.getUserName().equals(entity.getOwner())).findFirst().ifPresent(owner -> ccUserIds.add(owner.getId()));
    message.setCopyUserIds(ccUserIds);
    if (newIssueCommentSettings.isKeepInformedOwner()) {
        PipelineUser author = referencedUsers.stream().filter(u -> u.getUserName().equals(issue.getAuthor())).findFirst().orElseThrow(() -> new IllegalArgumentException("No issue author was found"));
        message.setToUserId(author.getId());
    }
    IssueComment copyWithHtml = comment.toBuilder().text(htmlText).build();
    Map<String, Object> commentParams = jsonMapper.convertValue(copyWithHtml, new TypeReference<Map<String, Object>>() {
    });
    commentParams.put("issue", jsonMapper.convertValue(issue, new TypeReference<Map<String, Object>>() {
    }));
    message.setTemplateParameters(commentParams);
    monitoringNotificationDao.createMonitoringNotification(message);
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) AbstractSecuredEntity(com.epam.pipeline.entity.AbstractSecuredEntity) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) IssueComment(com.epam.pipeline.entity.issue.IssueComment) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with NotificationMessage

use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.

the class NotificationManager method createNotification.

/**
 * Creates a custom notification.
 *
 * @param messageVO Notification message request that contains at least subject, body and toUser fields.
 * @return Created notification.
 */
@Transactional(propagation = Propagation.REQUIRED)
public NotificationMessage createNotification(final NotificationMessageVO messageVO) {
    Assert.notNull(messageVO.getSubject(), messageHelper.getMessage(MessageConstants.ERROR_NOTIFICATION_SUBJECT_NOT_SPECIFIED));
    Assert.notNull(messageVO.getBody(), messageHelper.getMessage(MessageConstants.ERROR_NOTIFICATION_BODY_NOT_SPECIFIED));
    Assert.notNull(messageVO.getToUser(), messageHelper.getMessage(MessageConstants.ERROR_NOTIFICATION_RECEIVER_NOT_SPECIFIED));
    final NotificationMessage message = toMessage(messageVO);
    monitoringNotificationDao.createMonitoringNotification(message);
    return message;
}
Also used : NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)26 Transactional (org.springframework.transaction.annotation.Transactional)25 Test (org.junit.Test)18 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)15 NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)14 AbstractManagerTest (com.epam.pipeline.manager.AbstractManagerTest)8 NotificationSettings (com.epam.pipeline.entity.notification.NotificationSettings)7 PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)6 AbstractSpringTest (com.epam.pipeline.notifier.AbstractSpringTest)6 NotificationMessageVO (com.epam.pipeline.controller.vo.notification.NotificationMessageVO)5 ExtendedRole (com.epam.pipeline.entity.user.ExtendedRole)5 Collections (java.util.Collections)5 AbstractSecuredEntity (com.epam.pipeline.entity.AbstractSecuredEntity)4 Issue (com.epam.pipeline.entity.issue.Issue)4 IssueComment (com.epam.pipeline.entity.issue.IssueComment)4 DefaultRoles (com.epam.pipeline.entity.user.DefaultRoles)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)4 Arrays (java.util.Arrays)4 List (java.util.List)4 Map (java.util.Map)4