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());
}
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;
}
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));
}
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);
}
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);
}
Aggregations