use of com.epam.pipeline.entity.user.PipelineUser 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.user.PipelineUser in project cloud-pipeline by epam.
the class SMTPNotificationManagerTest method testEmailSendingWithoutTemplate.
@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testEmailSendingWithoutTemplate() {
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);
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.user.PipelineUser 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));
}
use of com.epam.pipeline.entity.user.PipelineUser 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);
}
use of com.epam.pipeline.entity.user.PipelineUser 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);
}
Aggregations