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