Search in sources :

Example 16 with NotificationMessage

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

the class NotificationManager method notifyIssue.

/**
 * Notify users that a new issue was created for a given entity. Will notify mentioned users as well.
 *
 * @param issue an issue to notify about
 * @param entity an entity for wich issue was created
 */
@Transactional(propagation = Propagation.REQUIRED)
public void notifyIssue(Issue issue, AbstractSecuredEntity entity, String htmlText) {
    NotificationSettings newIssueSettings = notificationSettingsManager.load(NotificationType.NEW_ISSUE);
    if (newIssueSettings == null || !newIssueSettings.isEnabled()) {
        LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_NOTIFICATION_TEMPLATE_NOT_CONFIGURED, "new issue"));
        return;
    }
    NotificationMessage message = new NotificationMessage();
    message.setTemplate(new NotificationTemplate(newIssueSettings.getTemplateId()));
    message.setCopyUserIds(getMentionedUsers(issue.getText()));
    Issue copyWithHtml = issue.toBuilder().text(htmlText).build();
    message.setTemplateParameters(jsonMapper.convertValue(copyWithHtml, new TypeReference<Map<String, Object>>() {
    }));
    if (newIssueSettings.isKeepInformedOwner()) {
        PipelineUser owner = userManager.loadUserByName(entity.getOwner());
        message.setToUserId(owner.getId());
    }
    monitoringNotificationDao.createMonitoringNotification(message);
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) Issue(com.epam.pipeline.entity.issue.Issue) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with NotificationMessage

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

the class NotificationManager method notifyRunStatusChanged.

@Transactional(propagation = Propagation.REQUIRED)
public void notifyRunStatusChanged(PipelineRun pipelineRun) {
    NotificationSettings runStatusSettings = notificationSettingsManager.load(NotificationType.PIPELINE_RUN_STATUS);
    if (runStatusSettings == null || !runStatusSettings.isEnabled()) {
        LOGGER.info("No template configured for pipeline run status changes notifications or it was disabled!");
        return;
    }
    NotificationMessage message = new NotificationMessage();
    message.setTemplate(new NotificationTemplate(runStatusSettings.getTemplateId()));
    message.setTemplateParameters(PipelineRunMapper.map(pipelineRun, null));
    List<Long> ccUserIds = getKeepInformedUserIds(runStatusSettings);
    if (runStatusSettings.isKeepInformedAdmins()) {
        ExtendedRole extendedRole = roleManager.loadRoleWithUsers(DefaultRoles.ROLE_ADMIN.getId());
        ccUserIds.addAll(extendedRole.getUsers().stream().map(PipelineUser::getId).collect(Collectors.toList()));
    }
    message.setCopyUserIds(ccUserIds);
    if (runStatusSettings.isKeepInformedOwner()) {
        PipelineUser pipelineOwner = userManager.loadUserByName(pipelineRun.getOwner());
        message.setToUserId(pipelineOwner.getId());
    }
    monitoringNotificationDao.createMonitoringNotification(message);
}
Also used : ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with NotificationMessage

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

the class MonitoringNotificationDaoTest method testCreateMonitoringNotifications.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testCreateMonitoringNotifications() {
    NotificationTemplate notificationTemplate = notificationTemplateDao.createNotificationTemplate(template);
    PipelineUser pipelineUser = userDao.loadUserByName(TEST_USER1);
    List<NotificationMessage> messages = IntStream.range(0, 5).mapToObj(i -> {
        NotificationMessage notificationMessage = new NotificationMessage();
        notificationMessage.setTemplate(notificationTemplate);
        notificationMessage.setToUserId(pipelineUser.getId());
        notificationMessage.setCopyUserIds(Collections.singletonList(pipelineUser.getId()));
        notificationMessage.setTemplateParameters(Collections.singletonMap("test", i));
        return notificationMessage;
    }).collect(Collectors.toList());
    monitoringNotificationDao.createMonitoringNotifications(messages);
    List<NotificationMessage> loaded = monitoringNotificationDao.loadAllNotifications();
    assertTrue(messages.size() <= loaded.size());
    assertTrue(loaded.stream().allMatch(l -> messages.stream().anyMatch(m -> m.getTemplateParameters().equals(l.getTemplateParameters()))));
}
Also used : IntStream(java.util.stream.IntStream) PipelineRunMapper(com.epam.pipeline.mapper.PipelineRunMapper) Arrays(java.util.Arrays) PipelineRunDao(com.epam.pipeline.dao.pipeline.PipelineRunDao) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) PipelineDao(com.epam.pipeline.dao.pipeline.PipelineDao) Propagation(org.springframework.transaction.annotation.Propagation) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) Before(org.junit.Before) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Collectors(java.util.stream.Collectors) com.epam.pipeline.entity.pipeline(com.epam.pipeline.entity.pipeline) Objects(java.util.Objects) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) UserDao(com.epam.pipeline.dao.user.UserDao) Collections(java.util.Collections) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Transactional(org.springframework.transaction.annotation.Transactional) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with NotificationMessage

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

the class MonitoringNotificationDaoTest method testLoadMonitoringNotification.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testLoadMonitoringNotification() {
    PipelineRun run1 = createTestPipelineRun();
    List<PipelineRun> pipelineRuns = pipelineRunDao.loadPipelineRuns(Collections.singletonList(run1.getId()));
    NotificationTemplate notificationTemplate = notificationTemplateDao.createNotificationTemplate(template);
    notificationMessage = new NotificationMessage();
    notificationMessage.setTemplate(notificationTemplate);
    PipelineUser pipelineUser = userDao.loadUserByName(TEST_USER1);
    notificationMessage.setToUserId(pipelineUser.getId());
    notificationMessage.setCopyUserIds(Collections.singletonList(pipelineUser.getId()));
    notificationMessage.setTemplateParameters(PipelineRunMapper.map(pipelineRuns.get(0), TEST_THRESHOLD));
    monitoringNotificationDao.createMonitoringNotification(notificationMessage);
    NotificationMessage actualMessage = monitoringNotificationDao.loadMonitoringNotification(notificationMessage.getId());
    assertEquals(notificationMessage.getId(), actualMessage.getId());
    assertEquals(notificationMessage.getTemplate().getId(), actualMessage.getTemplate().getId());
    assertEquals(user.getId(), notificationMessage.getToUserId());
    assertFalse(notificationMessage.getCopyUserIds().isEmpty());
    notificationMessage.getTemplateParameters().values().removeIf(Objects::isNull);
    notificationMessage.getTemplateParameters().forEach((k, v) -> {
        assertTrue(actualMessage.getTemplateParameters().containsKey(k));
        assertEquals(v.toString(), actualMessage.getTemplateParameters().get(k).toString());
    });
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) Objects(java.util.Objects) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 20 with NotificationMessage

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

the class MonitoringNotificationDaoTest method testCreateAndLoadCustomMonitoringNotifications.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testCreateAndLoadCustomMonitoringNotifications() {
    PipelineUser pipelineUser = userDao.loadUserByName(TEST_USER1);
    NotificationMessage message = new NotificationMessage();
    message.setBody(BODY_STRING);
    message.setSubject(SUBJECT_STRING);
    message.setToUserId(pipelineUser.getId());
    message.setCopyUserIds(Collections.emptyList());
    message.setTemplateParameters(Collections.singletonMap(TEMPLATE_PARAMETER, TEMPLATE_PARAMETER_VALUE));
    monitoringNotificationDao.createMonitoringNotification(message);
    NotificationMessage singleLoadedMessage = monitoringNotificationDao.loadMonitoringNotification(message.getId());
    assertEquals(singleLoadedMessage.getBody(), BODY_STRING);
    assertEquals(singleLoadedMessage.getSubject(), SUBJECT_STRING);
    assertEquals(singleLoadedMessage.getToUserId(), pipelineUser.getId());
    assertEquals(singleLoadedMessage.getCopyUserIds(), Collections.emptyList());
    assertNull(singleLoadedMessage.getTemplate());
    NotificationMessage batchLoadedMessage = monitoringNotificationDao.loadAllNotifications().stream().filter(m -> m.getId().equals(message.getId())).findFirst().get();
    assertEquals(singleLoadedMessage, batchLoadedMessage);
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test) 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