Search in sources :

Example 6 with NotificationTemplate

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

the class NotificationSettingsManagerTest method testCreateAndUpdate.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testCreateAndUpdate() {
    NotificationTemplate template = createTemplate(1L, "template");
    NotificationSettings settings = createSettings(NotificationType.LONG_RUNNING, template.getId(), 1L, 1L);
    notificationSettingsManager.createOrUpdate(settings);
    NotificationSettings loaded = notificationSettingsManager.load(NotificationType.LONG_RUNNING);
    Assert.assertNotNull(loaded);
    Assert.assertEquals(NotificationType.LONG_RUNNING, loaded.getType());
    Assert.assertEquals(1L, loaded.getThreshold().longValue());
    Assert.assertEquals(settings.isKeepInformedOwner(), loaded.isKeepInformedOwner());
    settings.setThreshold(2L);
    notificationSettingsManager.createOrUpdate(settings);
    loaded = notificationSettingsManager.load(NotificationType.LONG_RUNNING);
    Assert.assertNotNull(loaded);
    Assert.assertEquals(2L, loaded.getThreshold().longValue());
}
Also used : NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with NotificationTemplate

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

the class NotificationManagerTest method createTemplate.

private NotificationTemplate createTemplate(Long id, String name) {
    NotificationTemplate template = new NotificationTemplate(id);
    template.setName(name);
    template.setBody("//");
    template.setSubject("//");
    notificationTemplateDao.createNotificationTemplate(template);
    return template;
}
Also used : NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate)

Example 8 with NotificationTemplate

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

the class SMTPNotificationManagerTest method testEmailSending.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testEmailSending() {
    PipelineUser user = new PipelineUser();
    user.setUserName(USER_NAME);
    user.setAdmin(true);
    user.setAttributes(Collections.singletonMap(EMAIL_KEY, EMAIL));
    userRepository.save(user);
    NotificationMessage message = new NotificationMessage();
    NotificationTemplate template = new NotificationTemplate();
    template.setSubject(MESSAGE_SUBJECT);
    template.setBody(MESSAGE_BODY);
    message.setTemplate(template);
    message.setTemplateParameters(Collections.emptyMap());
    message.setToUserId(user.getId());
    message.setCopyUserIds(Collections.singletonList(user.getId()));
    smtpNotificationManager.notifySubscribers(message);
    MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
    assertTrue(receivedMessages.length == 2);
    assertTrue(GreenMailUtil.getBody(receivedMessages[0]).contains(MESSAGE_BODY));
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) MimeMessage(javax.mail.internet.MimeMessage) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest) AbstractSpringTest(com.epam.pipeline.notifier.AbstractSpringTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with NotificationTemplate

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

the class SMTPNotificationManagerTest method testWontSendToOwnerIfNotConfigured.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testWontSendToOwnerIfNotConfigured() throws MessagingException {
    PipelineUser user = new PipelineUser();
    user.setUserName(USER_NAME);
    user.setAdmin(false);
    user.setAttributes(Collections.singletonMap(EMAIL_KEY, EMAIL));
    userRepository.save(user);
    NotificationMessage message = new NotificationMessage();
    NotificationTemplate template = new NotificationTemplate();
    template.setSubject(MESSAGE_SUBJECT);
    template.setBody(MESSAGE_BODY_WITH_PARAM);
    message.setTemplate(template);
    message.setTemplateParameters(Collections.singletonMap("name", USER_NAME));
    message.setToUserId(null);
    message.setCopyUserIds(Collections.singletonList(user.getId()));
    smtpNotificationManager.notifySubscribers(message);
    MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
    assertEquals(1, receivedMessages.length);
    assertNull(receivedMessages[0].getRecipients(Message.RecipientType.TO));
    assertEquals(1, receivedMessages[0].getRecipients(Message.RecipientType.CC).length);
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) MimeMessage(javax.mail.internet.MimeMessage) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest) AbstractSpringTest(com.epam.pipeline.notifier.AbstractSpringTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with NotificationTemplate

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

the class SMTPNotificationManager method buildEmail.

private Optional<Email> buildEmail(NotificationMessage message) throws EmailException {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(smtpServerHostName);
    email.setSmtpPort(smtpPort);
    email.setSSLOnConnect(sslOnConnect);
    email.setStartTLSEnabled(startTlsEnabled);
    // check that credentials are provided, otherwise try to proceed without authentication
    if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
        email.setAuthenticator(new DefaultAuthenticator(username, password));
    }
    email.setFrom(emailFrom);
    Optional<NotificationTemplate> template = Optional.ofNullable(message.getTemplate());
    String subject = template.map(NotificationTemplate::getSubject).orElse(message.getSubject());
    String body = template.map(NotificationTemplate::getBody).orElse(message.getBody());
    VelocityContext velocityContext = getVelocityContext(message);
    velocityContext.put("numberTool", new NumberTool());
    StringWriter subjectOut = new StringWriter();
    StringWriter bodyOut = new StringWriter();
    Velocity.evaluate(velocityContext, subjectOut, MESSAGE_TAG + message.hashCode(), subject);
    Velocity.evaluate(velocityContext, bodyOut, MESSAGE_TAG + message.hashCode(), body);
    email.setSubject(subjectOut.toString());
    email.setHtmlMsg(bodyOut.toString());
    if (message.getToUserId() != null) {
        PipelineUser targetUser = userRepository.findOne(message.getToUserId());
        email.addTo(targetUser.getEmail());
    } else {
        if (CollectionUtils.isEmpty(message.getCopyUserIds())) {
            LOGGER.info("Email with message {} won't be sent: no recipients found", message.getId());
            return Optional.empty();
        }
    }
    List<PipelineUser> keepInformedUsers = userRepository.findByIdIn(message.getCopyUserIds());
    for (PipelineUser user : keepInformedUsers) {
        String address = user.getEmail();
        if (address != null) {
            email.addCc(address);
        }
    }
    LOGGER.info("Email from message {} formed and will be send to: {}", message.getId(), email.getToAddresses().stream().map(InternetAddress::getAddress).collect(Collectors.toList()));
    return Optional.of(email);
}
Also used : NumberTool(org.apache.velocity.tools.generic.NumberTool) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) InternetAddress(javax.mail.internet.InternetAddress) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) HtmlEmail(org.apache.commons.mail.HtmlEmail) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate)

Aggregations

NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)24 Transactional (org.springframework.transaction.annotation.Transactional)17 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)13 NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)12 Test (org.junit.Test)12 NotificationSettings (com.epam.pipeline.entity.notification.NotificationSettings)8 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)7 AbstractSpringTest (com.epam.pipeline.notifier.AbstractSpringTest)5 ExtendedRole (com.epam.pipeline.entity.user.ExtendedRole)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 MimeMessage (javax.mail.internet.MimeMessage)3 Before (org.junit.Before)3 AbstractSecuredEntity (com.epam.pipeline.entity.AbstractSecuredEntity)2 Issue (com.epam.pipeline.entity.issue.Issue)2 IssueComment (com.epam.pipeline.entity.issue.IssueComment)2 NotificationType (com.epam.pipeline.entity.notification.NotificationSettings.NotificationType)2 DefaultRoles (com.epam.pipeline.entity.user.DefaultRoles)2 PipelineRunMapper (com.epam.pipeline.mapper.PipelineRunMapper)2 Arrays (java.util.Arrays)2