Search in sources :

Example 16 with PipelineUser

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);
}
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 17 with PipelineUser

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));
}
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 18 with PipelineUser

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));
}
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 19 with PipelineUser

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

Example 20 with PipelineUser

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

Aggregations

PipelineUser (com.epam.pipeline.entity.user.PipelineUser)44 Transactional (org.springframework.transaction.annotation.Transactional)23 Test (org.junit.Test)19 NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)13 NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)13 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)12 ArrayList (java.util.ArrayList)8 DefaultRoles (com.epam.pipeline.entity.user.DefaultRoles)7 Collections (java.util.Collections)7 List (java.util.List)7 Map (java.util.Map)7 Autowired (org.springframework.beans.factory.annotation.Autowired)7 Role (com.epam.pipeline.entity.user.Role)6 AbstractSpringTest (com.epam.pipeline.notifier.AbstractSpringTest)6 Arrays (java.util.Arrays)6 S3bucketDataStorage (com.epam.pipeline.entity.datastorage.aws.S3bucketDataStorage)5 PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)5 Collection (java.util.Collection)5 CollectionUtils (org.apache.commons.collections4.CollectionUtils)5 DataStorageDao (com.epam.pipeline.dao.datastorage.DataStorageDao)4