Search in sources :

Example 1 with IssueCommentVO

use of com.epam.pipeline.controller.vo.IssueCommentVO in project cloud-pipeline by epam.

the class IssueManager method updateComment.

/**
 * Updates comment specified by ID. If issue was closed or issue doesn't exist or comment doesn't exist an error
 * will be thrown. If text for comment is empty exception will be thrown.
 * @param issueId issue's ID
 * @param commentId comment's ID
 * @param commentVO comment's text
 * @return updated {@link IssueComment}
 */
@Transactional(propagation = Propagation.REQUIRED)
public IssueComment updateComment(Long issueId, Long commentId, IssueCommentVO commentVO) {
    IssueComment issueComment = loadComment(issueId, commentId);
    loadIssueAndCheckIfNotClosed(issueId);
    String text = commentVO.getText();
    ensureNotEmptyString(text, MessageConstants.ERROR_INVALID_COMMENT_TEXT);
    issueComment.setText(text);
    commentDao.updateComment(issueComment);
    // Find the attachments that were removed during update and delete them
    HashSet<Attachment> newAttachments = new HashSet<>(commentVO.getAttachments());
    List<Attachment> allAttachments = attachmentDao.loadAttachmentsByCommentId(commentId);
    List<Attachment> toDelete = allAttachments.stream().filter(a -> !newAttachments.contains(a)).collect(Collectors.toList());
    attachmentFileManager.deleteAttachments(toDelete);
    // New attachments might be added so we need to link them to this comment
    commentVO.getAttachments().forEach(a -> attachmentDao.updateAttachmentCommentId(a.getId(), commentId));
    return issueComment;
}
Also used : MessageConstants(com.epam.pipeline.common.MessageConstants) IssueVO(com.epam.pipeline.controller.vo.IssueVO) AttachmentDao(com.epam.pipeline.dao.issue.AttachmentDao) Autowired(org.springframework.beans.factory.annotation.Autowired) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) MessageHelper(com.epam.pipeline.common.MessageHelper) IssueCommentDao(com.epam.pipeline.dao.issue.IssueCommentDao) CollectionUtils(org.apache.commons.collections.CollectionUtils) Propagation(org.springframework.transaction.annotation.Propagation) Service(org.springframework.stereotype.Service) Map(java.util.Map) IssueComment(com.epam.pipeline.entity.issue.IssueComment) EntityManager(com.epam.pipeline.manager.EntityManager) IssueCommentVO(com.epam.pipeline.controller.vo.IssueCommentVO) AbstractSecuredEntity(com.epam.pipeline.entity.AbstractSecuredEntity) IssueStatus(com.epam.pipeline.entity.issue.IssueStatus) Attachment(com.epam.pipeline.entity.issue.Attachment) Collectors(java.util.stream.Collectors) PagedResult(com.epam.pipeline.controller.PagedResult) List(java.util.List) IssueMapper(com.epam.pipeline.mapper.IssueMapper) IssueDao(com.epam.pipeline.dao.issue.IssueDao) Optional(java.util.Optional) AclClass(com.epam.pipeline.entity.security.acl.AclClass) NotificationManager(com.epam.pipeline.manager.notification.NotificationManager) AuthManager(com.epam.pipeline.manager.security.AuthManager) Issue(com.epam.pipeline.entity.issue.Issue) Collections(java.util.Collections) EntityVO(com.epam.pipeline.controller.vo.EntityVO) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) IssueComment(com.epam.pipeline.entity.issue.IssueComment) Attachment(com.epam.pipeline.entity.issue.Attachment) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with IssueCommentVO

use of com.epam.pipeline.controller.vo.IssueCommentVO in project cloud-pipeline by epam.

the class IssueManagerTest method testLoadIssueWithComments.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void testLoadIssueWithComments() {
    Issue issue = registerIssue();
    Long issueId = issue.getId();
    IssueCommentVO commentVO1 = getCommentVO(COMMENT_TEXT);
    IssueCommentVO commentVO2 = getCommentVO(COMMENT_TEXT2);
    issueManager.createComment(issueId, commentVO1);
    issueManager.createComment(issueId, commentVO2);
    assertEquals(2, issueManager.loadIssue(issueId).getComments().size());
}
Also used : IssueCommentVO(com.epam.pipeline.controller.vo.IssueCommentVO) Issue(com.epam.pipeline.entity.issue.Issue) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with IssueCommentVO

use of com.epam.pipeline.controller.vo.IssueCommentVO in project cloud-pipeline by epam.

the class IssueManagerTest method createCommentWithAttachments.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void createCommentWithAttachments() {
    Issue issue = registerIssue();
    Long issueId = issue.getId();
    IssueCommentVO commentVO = getCommentVO(COMMENT_TEXT);
    commentVO.setAttachments(Collections.singletonList(testAttachment));
    IssueComment comment = issueManager.createComment(issueId, commentVO);
    IssueComment loaded = issueManager.loadComment(issueId, comment.getId());
    Issue loadedIssue = issueManager.loadIssue(issueId);
    assertEquals(1, loadedIssue.getComments().stream().mapToLong(c -> c.getAttachments().size()).sum());
    assertFalse(loaded.getAttachments().isEmpty());
    issueManager.deleteIssue(issueId);
    assertFalse(attachmentDao.load(testAttachment.getId()).isPresent());
}
Also used : IssueCommentVO(com.epam.pipeline.controller.vo.IssueCommentVO) Issue(com.epam.pipeline.entity.issue.Issue) IssueComment(com.epam.pipeline.entity.issue.IssueComment) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with IssueCommentVO

use of com.epam.pipeline.controller.vo.IssueCommentVO in project cloud-pipeline by epam.

the class IssueManagerTest method testDeleteIssueWithComment.

@Test(expected = IllegalArgumentException.class)
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void testDeleteIssueWithComment() {
    Issue issue = registerIssue();
    Long issueId = issue.getId();
    IssueCommentVO commentVO = getCommentVO(COMMENT_TEXT);
    issueManager.createComment(issueId, commentVO);
    issueManager.deleteIssue(issueId);
    issueManager.loadIssue(issueId);
}
Also used : IssueCommentVO(com.epam.pipeline.controller.vo.IssueCommentVO) Issue(com.epam.pipeline.entity.issue.Issue) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with IssueCommentVO

use of com.epam.pipeline.controller.vo.IssueCommentVO in project cloud-pipeline by epam.

the class IssueApiServiceTest method testUpdateCommentAccessDeniedForNotOwner.

@WithMockUser(username = CAN_READ_USER)
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
@Test(expected = AccessDeniedException.class)
public void testUpdateCommentAccessDeniedForNotOwner() {
    IssueCommentVO issueCommentVO = new IssueCommentVO();
    issueCommentVO.setText(COMMENT_TEXT);
    issueApiService.updateComment(createdIssue.getId(), createdIssueComment.getId(), issueCommentVO);
}
Also used : IssueCommentVO(com.epam.pipeline.controller.vo.IssueCommentVO) WithMockUser(org.springframework.security.test.context.support.WithMockUser) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

IssueCommentVO (com.epam.pipeline.controller.vo.IssueCommentVO)16 Transactional (org.springframework.transaction.annotation.Transactional)14 Test (org.junit.Test)13 Issue (com.epam.pipeline.entity.issue.Issue)11 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)9 IssueComment (com.epam.pipeline.entity.issue.IssueComment)6 AbstractManagerTest (com.epam.pipeline.manager.AbstractManagerTest)4 WithMockUser (org.springframework.security.test.context.support.WithMockUser)4 EntityVO (com.epam.pipeline.controller.vo.EntityVO)2 IssueVO (com.epam.pipeline.controller.vo.IssueVO)2 AttachmentDao (com.epam.pipeline.dao.issue.AttachmentDao)2 Attachment (com.epam.pipeline.entity.issue.Attachment)2 IssueStatus (com.epam.pipeline.entity.issue.IssueStatus)2 AclClass (com.epam.pipeline.entity.security.acl.AclClass)2 NotificationManager (com.epam.pipeline.manager.notification.NotificationManager)2 AuthManager (com.epam.pipeline.manager.security.AuthManager)2 IssueMapper (com.epam.pipeline.mapper.IssueMapper)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2