Search in sources :

Example 6 with Attachment

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

the class AttachmentFileManagerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    attachmentFileManager = new AttachmentFileManager(dataStorageManager, preferenceManager, attachmentManager, messageHelper, authManager);
    Preference systemDataStorage = SystemPreferences.DATA_STORAGE_SYSTEM_DATA_STORAGE_NAME.toPreference();
    systemDataStorage.setName(TEST_SYSTEM_DATA_STORAGE);
    when(preferenceManager.getPreference(SystemPreferences.DATA_STORAGE_SYSTEM_DATA_STORAGE_NAME)).thenReturn(TEST_SYSTEM_DATA_STORAGE);
    when(dataStorageManager.loadByNameOrId(TEST_SYSTEM_DATA_STORAGE)).thenReturn(testSystemDataStorage);
    when(dataStorageManager.createDataStorageFile(Mockito.eq(1L), Mockito.anyString(), Mockito.anyString(), Mockito.any(InputStream.class))).then((Answer<DataStorageFile>) invocation -> {
        String path = invocation.getArgumentAt(1, String.class);
        String name = invocation.getArgumentAt(2, String.class);
        DataStorageFile file = new DataStorageFile();
        file.setPath(path + "/" + name);
        return file;
    });
    when(attachmentManager.load(Mockito.anyLong())).thenAnswer(invocation -> {
        Attachment attachment = new Attachment();
        attachment.setId(invocation.getArgumentAt(0, Long.class));
        attachment.setName(TEST_ATTACHMENT_NAME);
        attachment.setPath(TEST_ATTACHMENT_PATH);
        return attachment;
    });
    DataStorageStreamingContent content = new DataStorageStreamingContent(new ByteArrayInputStream(new byte[] { 1 }), TEST_ATTACHMENT_NAME);
    when(dataStorageManager.getStreamingContent(testSystemDataStorage.getId(), TEST_ATTACHMENT_PATH, null)).thenReturn(content);
    when(authManager.getAuthorizedUser()).thenReturn(TEST_USER);
}
Also used : PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) Mock(org.mockito.Mock) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Attachment(com.epam.pipeline.entity.issue.Attachment) Mockito.verify(org.mockito.Mockito.verify) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) Mockito(org.mockito.Mockito) MockitoAnnotations(org.mockito.MockitoAnnotations) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) ByteArrayInputStream(java.io.ByteArrayInputStream) MessageHelper(com.epam.pipeline.common.MessageHelper) DataStorageStreamingContent(com.epam.pipeline.entity.datastorage.DataStorageStreamingContent) Matchers.eq(org.mockito.Matchers.eq) Preference(com.epam.pipeline.entity.preference.Preference) S3bucketDataStorage(com.epam.pipeline.entity.datastorage.aws.S3bucketDataStorage) AuthManager(com.epam.pipeline.manager.security.AuthManager) Assert(org.junit.Assert) DataStorageManager(com.epam.pipeline.manager.datastorage.DataStorageManager) InputStream(java.io.InputStream) Before(org.junit.Before) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) Preference(com.epam.pipeline.entity.preference.Preference) DataStorageStreamingContent(com.epam.pipeline.entity.datastorage.DataStorageStreamingContent) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(com.epam.pipeline.entity.issue.Attachment) Before(org.junit.Before)

Example 7 with Attachment

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

the class AttachmentFileManager method deleteAttachment.

/**
 * Deletes a single attachment by it's ID
 * @param attachmentId ID of attachment to delete
 */
@Transactional(propagation = Propagation.REQUIRED)
public void deleteAttachment(long attachmentId) {
    Attachment attachment = attachmentManager.load(attachmentId);
    if (!authManager.isAdmin() && !Objects.equals(attachment.getOwner(), authManager.getAuthorizedUser())) {
        throw new AccessDeniedException("Only deletion of user's own attachments is allowed");
    }
    deleteAttachments(Collections.singletonList(attachment));
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) Attachment(com.epam.pipeline.entity.issue.Attachment) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with Attachment

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

the class AttachmentFileManager method downloadAttachment.

public DataStorageStreamingContent downloadAttachment(long attachmentId) {
    String systemDataStorageName = preferenceManager.getPreference(SystemPreferences.DATA_STORAGE_SYSTEM_DATA_STORAGE_NAME);
    Assert.notNull(systemDataStorageName, messageHelper.getMessage(MessageConstants.ERROR_ATTACHMENT_SYSTEM_DATA_STORAGE_NOT_CONFIGURED));
    AbstractDataStorage attachmentStorage = dataStorageManager.loadByNameOrId(systemDataStorageName);
    Attachment attachment = attachmentManager.load(attachmentId);
    DataStorageStreamingContent content = dataStorageManager.getStreamingContent(attachmentStorage.getId(), attachment.getPath(), null);
    return new DataStorageStreamingContent(content.getContent(), attachment.getName());
}
Also used : AbstractDataStorage(com.epam.pipeline.entity.datastorage.AbstractDataStorage) DataStorageStreamingContent(com.epam.pipeline.entity.datastorage.DataStorageStreamingContent) Attachment(com.epam.pipeline.entity.issue.Attachment)

Example 9 with Attachment

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

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

the class AttachmentFileManager method deleteAttachments.

/**
 * Deletes attachments. Deletes records from DB transactionally and files from Data Storage in async mode.
 * @param attachments attachments to delete
 */
public void deleteAttachments(List<Attachment> attachments) {
    String systemDataStorageName = preferenceManager.getPreference(SystemPreferences.DATA_STORAGE_SYSTEM_DATA_STORAGE_NAME);
    if (StringUtils.isBlank(systemDataStorageName)) {
        LOGGER.debug(messageHelper.getMessage(MessageConstants.ERROR_ATTACHMENT_SYSTEM_DATA_STORAGE_NOT_CONFIGURED));
        return;
    }
    AbstractDataStorage attachmentStorage = dataStorageManager.loadByNameOrId(systemDataStorageName);
    CompletableFuture.runAsync(() -> {
        List<UpdateDataStorageItemVO> itemsToDelete = attachments.stream().map(a -> {
            UpdateDataStorageItemVO updateVO = new UpdateDataStorageItemVO();
            updateVO.setPath(a.getPath());
            return updateVO;
        }).collect(Collectors.toList());
        try {
            dataStorageManager.deleteDataStorageItems(attachmentStorage.getId(), itemsToDelete, attachmentStorage.isVersioningEnabled());
        } catch (Exception e) {
            LOGGER.error("Error while deleting attachments:", e);
        }
    });
    attachmentManager.deleteAttachments(attachments);
}
Also used : MessageConstants(com.epam.pipeline.common.MessageConstants) LoggerFactory(org.slf4j.LoggerFactory) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) StringUtils(org.apache.commons.lang3.StringUtils) MessageHelper(com.epam.pipeline.common.MessageHelper) Propagation(org.springframework.transaction.annotation.Propagation) Service(org.springframework.stereotype.Service) DateUtils(com.epam.pipeline.entity.utils.DateUtils) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) Logger(org.slf4j.Logger) AbstractDataStorage(com.epam.pipeline.entity.datastorage.AbstractDataStorage) Attachment(com.epam.pipeline.entity.issue.Attachment) AccessDeniedException(org.springframework.security.access.AccessDeniedException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) Objects(java.util.Objects) List(java.util.List) DataStorageStreamingContent(com.epam.pipeline.entity.datastorage.DataStorageStreamingContent) AuthManager(com.epam.pipeline.manager.security.AuthManager) UpdateDataStorageItemVO(com.epam.pipeline.controller.vo.data.storage.UpdateDataStorageItemVO) DataStorageManager(com.epam.pipeline.manager.datastorage.DataStorageManager) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) AbstractDataStorage(com.epam.pipeline.entity.datastorage.AbstractDataStorage) UpdateDataStorageItemVO(com.epam.pipeline.controller.vo.data.storage.UpdateDataStorageItemVO) AccessDeniedException(org.springframework.security.access.AccessDeniedException)

Aggregations

Attachment (com.epam.pipeline.entity.issue.Attachment)17 Transactional (org.springframework.transaction.annotation.Transactional)9 Issue (com.epam.pipeline.entity.issue.Issue)7 IssueComment (com.epam.pipeline.entity.issue.IssueComment)7 Date (java.util.Date)7 Test (org.junit.Test)7 EntityVO (com.epam.pipeline.controller.vo.EntityVO)6 AuthManager (com.epam.pipeline.manager.security.AuthManager)6 List (java.util.List)6 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)5 AclClass (com.epam.pipeline.entity.security.acl.AclClass)5 Collections (java.util.Collections)5 Collectors (java.util.stream.Collectors)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 Propagation (org.springframework.transaction.annotation.Propagation)5 IssueCommentVO (com.epam.pipeline.controller.vo.IssueCommentVO)4 IssueVO (com.epam.pipeline.controller.vo.IssueVO)4 AttachmentDao (com.epam.pipeline.dao.issue.AttachmentDao)4 IssueStatus (com.epam.pipeline.entity.issue.IssueStatus)4 Folder (com.epam.pipeline.entity.pipeline.Folder)4