Search in sources :

Example 6 with IssueComment

use of com.epam.pipeline.entity.issue.IssueComment in project cloud-pipeline by epam.

the class NotificationManagerTest method testNotifyIssueComment.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testNotifyIssueComment() {
    Pipeline pipeline = createPipeline(testOwner);
    Issue issue = createIssue(testUser2, pipeline);
    issueDao.createIssue(issue);
    IssueComment comment = new IssueComment();
    comment.setIssueId(issue.getId());
    comment.setText("Notify @TestUser1");
    comment.setAuthor(testUser2.getUserName());
    notificationManager.notifyIssueComment(comment, issue, comment.getText());
    List<NotificationMessage> messages = monitoringNotificationDao.loadAllNotifications();
    Assert.assertEquals(1, messages.size());
    NotificationMessage message = messages.get(0);
    Assert.assertEquals(testUser2.getId(), message.getToUserId());
    Assert.assertEquals(issueCommentTemplate.getId(), message.getTemplate().getId());
    Assert.assertTrue(message.getCopyUserIds().stream().anyMatch(id -> id.equals(testUser1.getId())));
    Assert.assertTrue(message.getCopyUserIds().stream().anyMatch(id -> id.equals(testOwner.getId())));
    updateKeepInformedOwner(issueCommentSettings, false);
    notificationManager.notifyIssueComment(comment, issue, comment.getText());
    messages = monitoringNotificationDao.loadAllNotifications();
    Assert.assertEquals(2, messages.size());
    Assert.assertNull(messages.get(1).getToUserId());
}
Also used : Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) PodMonitor(com.epam.pipeline.manager.cluster.PodMonitor) Autowired(org.springframework.beans.factory.annotation.Autowired) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) MockitoAnnotations(org.mockito.MockitoAnnotations) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) Map(java.util.Map) IssueComment(com.epam.pipeline.entity.issue.IssueComment) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) MockBean(org.springframework.boot.test.mock.mockito.MockBean) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) DateUtils(com.epam.pipeline.entity.utils.DateUtils) AbstractSecuredEntity(com.epam.pipeline.entity.AbstractSecuredEntity) NotificationTemplateDao(com.epam.pipeline.dao.notification.NotificationTemplateDao) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) List(java.util.List) MonitoringNotificationDao(com.epam.pipeline.dao.notification.MonitoringNotificationDao) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) UserDao(com.epam.pipeline.dao.user.UserDao) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) EntityVO(com.epam.pipeline.controller.vo.EntityVO) Mockito.mock(org.mockito.Mockito.mock) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) NotificationSettingsDao(com.epam.pipeline.dao.notification.NotificationSettingsDao) Mock(org.mockito.Mock) PipelineRunManager(com.epam.pipeline.manager.pipeline.PipelineRunManager) Duration(org.joda.time.Duration) TestApplication(com.epam.pipeline.app.TestApplication) PipelineDao(com.epam.pipeline.dao.pipeline.PipelineDao) ArgumentCaptor(org.mockito.ArgumentCaptor) Propagation(org.springframework.transaction.annotation.Propagation) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) KubernetesManager(com.epam.pipeline.manager.cluster.KubernetesManager) KubernetesTestUtils(com.epam.pipeline.util.KubernetesTestUtils) NotificationType(com.epam.pipeline.entity.notification.NotificationSettings.NotificationType) Before(org.junit.Before) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) CustomAssertions.assertThrows(com.epam.pipeline.util.CustomAssertions.assertThrows) DateTime(org.joda.time.DateTime) TaskStatus(com.epam.pipeline.entity.pipeline.TaskStatus) Matchers(org.hamcrest.Matchers) Pod(io.fabric8.kubernetes.api.model.Pod) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) NotificationMessageVO(com.epam.pipeline.controller.vo.notification.NotificationMessageVO) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) Mockito.verify(org.mockito.Mockito.verify) PodList(io.fabric8.kubernetes.api.model.PodList) ContextConfiguration(org.springframework.test.context.ContextConfiguration) IssueDao(com.epam.pipeline.dao.issue.IssueDao) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Assert(org.junit.Assert) Issue(com.epam.pipeline.entity.issue.Issue) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Issue(com.epam.pipeline.entity.issue.Issue) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) IssueComment(com.epam.pipeline.entity.issue.IssueComment) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with IssueComment

use of com.epam.pipeline.entity.issue.IssueComment in project cloud-pipeline by epam.

the class IssueLoaderTest method shouldLoadIssueTest.

@Test
void shouldLoadIssueTest() throws EntityNotFoundException {
    Attachment attachment = new Attachment();
    attachment.setPath(TEST_PATH);
    attachment.setOwner(USER_NAME);
    IssueComment comment = IssueComment.builder().author(USER_NAME).text(TEST_DESCRIPTION).build();
    EntityVO entity = new EntityVO(1L, AclClass.TOOL);
    Issue expectedIssue = Issue.builder().id(1L).name(TEST_NAME).text(TEST_DESCRIPTION).status(IssueStatus.OPEN).labels(Collections.singletonList(TEST_LABEL)).attachments(Collections.singletonList(attachment)).comments(Collections.singletonList(comment)).entity(entity).author(TEST_NAME).build();
    IssueLoader issueLoader = new IssueLoader(apiClient);
    when(apiClient.loadIssue(anyLong())).thenReturn(expectedIssue);
    Optional<EntityContainer<Issue>> container = issueLoader.loadEntity(1L);
    EntityContainer<Issue> issueEntityContainer = container.orElseThrow(AssertionError::new);
    Issue actualIssue = issueEntityContainer.getEntity();
    assertNotNull(actualIssue);
    verifyIssue(expectedIssue, actualIssue);
    verifyPipelineUser(issueEntityContainer.getOwner());
    verifyPermissions(PERMISSIONS_CONTAINER_WITH_OWNER, issueEntityContainer.getPermissions());
    verifyMetadata(EXPECTED_METADATA, new ArrayList<>(issueEntityContainer.getMetadata().values()));
}
Also used : EntityVO(com.epam.pipeline.vo.EntityVO) LoaderVerificationUtils.verifyIssue(com.epam.pipeline.elasticsearchagent.LoaderVerificationUtils.verifyIssue) Issue(com.epam.pipeline.entity.issue.Issue) IssueComment(com.epam.pipeline.entity.issue.IssueComment) EntityContainer(com.epam.pipeline.elasticsearchagent.model.EntityContainer) Attachment(com.epam.pipeline.entity.issue.Attachment) Test(org.junit.jupiter.api.Test)

Example 8 with IssueComment

use of com.epam.pipeline.entity.issue.IssueComment in project cloud-pipeline by epam.

the class IssueManager method loadComment.

/**
 * Loads comment specified by ID. If comment doesn't exist an error will be thrown. If {@code issueId} and
 * loaded {@link IssueComment#id} are not equal an error will be occurred.
 * @param issueId issue's ID
 * @param commentId comment's ID
 * @return existing {@link IssueComment}
 */
public IssueComment loadComment(Long issueId, Long commentId) {
    IssueComment comment = commentDao.loadComment(commentId).orElseThrow(getCommentNotFoundException(commentId));
    comment.setAttachments(attachmentDao.loadAttachmentsByCommentIds(Collections.singletonList(commentId)).get(commentId));
    validateIssueId(issueId, comment);
    return comment;
}
Also used : IssueComment(com.epam.pipeline.entity.issue.IssueComment)

Example 9 with IssueComment

use of com.epam.pipeline.entity.issue.IssueComment in project cloud-pipeline by epam.

the class IssueManager method loadIssue.

/**
 * Loads an {@link Issue} specified by ID. If issue doesn't exist exception will be thrown.
 * @param id issue's ID
 * @return issue with comments that belong to it
 */
public Issue loadIssue(Long id) {
    Issue issue = issueDao.loadIssue(id).orElseThrow(getIssueNotFoundException(id));
    issue.setComments(commentDao.loadCommentsForIssue(id));
    issue.setAttachments(attachmentDao.loadAttachmentsByIssueId(id));
    Map<Long, List<Attachment>> attachmentMap = attachmentDao.loadAttachmentsByCommentIds(issue.getComments().stream().map(IssueComment::getId).collect(Collectors.toList()));
    issue.getComments().forEach(c -> c.setAttachments(attachmentMap.get(c.getId())));
    return issue;
}
Also used : Issue(com.epam.pipeline.entity.issue.Issue) IssueComment(com.epam.pipeline.entity.issue.IssueComment) List(java.util.List)

Example 10 with IssueComment

use of com.epam.pipeline.entity.issue.IssueComment 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

IssueComment (com.epam.pipeline.entity.issue.IssueComment)20 Issue (com.epam.pipeline.entity.issue.Issue)14 Transactional (org.springframework.transaction.annotation.Transactional)12 Test (org.junit.Test)9 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)6 IssueCommentVO (com.epam.pipeline.controller.vo.IssueCommentVO)6 List (java.util.List)6 Attachment (com.epam.pipeline.entity.issue.Attachment)5 EntityVO (com.epam.pipeline.controller.vo.EntityVO)4 Map (java.util.Map)4 AbstractSecuredEntity (com.epam.pipeline.entity.AbstractSecuredEntity)3 AclClass (com.epam.pipeline.entity.security.acl.AclClass)3 Collections (java.util.Collections)3 IssueVO (com.epam.pipeline.controller.vo.IssueVO)2 AttachmentDao (com.epam.pipeline.dao.issue.AttachmentDao)2 IssueDao (com.epam.pipeline.dao.issue.IssueDao)2 IssueStatus (com.epam.pipeline.entity.issue.IssueStatus)2 NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)2 NotificationSettings (com.epam.pipeline.entity.notification.NotificationSettings)2 NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)2