use of org.eclipse.sw360.datahandler.couchdb.AttachmentStreamConnector in project sw360portal by sw360.
the class AttachmentStreamConnectorTest method testTryingToDownloadIfNotAvailable.
@Test
public void testTryingToDownloadIfNotAvailable() throws Exception {
String id = "11";
String filename = "filename";
AttachmentContent attachment = mock(AttachmentContent.class);
when(attachment.isOnlyRemote()).thenReturn(true);
when(attachment.getId()).thenReturn(id);
when(attachment.getFilename()).thenReturn(filename);
InputStream downloadUrlStream = mock(InputStream.class);
InputStream returnedStream = mock(InputStream.class);
AttachmentContent rereadAttachment = mock(AttachmentContent.class);
when(rereadAttachment.getId()).thenReturn(id);
when(rereadAttachment.getFilename()).thenReturn(filename);
attachmentStreamConnector = spy(attachmentStreamConnector);
doReturn(returnedStream).when(attachmentStreamConnector).readAttachmentStream(rereadAttachment);
doNothing().when(attachmentStreamConnector).uploadAttachmentPart(attachment, 1, downloadUrlStream);
when(attachmentContentDownloader.download(eq(attachment), Matchers.any(Duration.class))).thenReturn(downloadUrlStream);
when(connector.get(AttachmentContent.class, id)).thenReturn(rereadAttachment);
doReturn(rereadAttachment).when(rereadAttachment).setOnlyRemote(anyBoolean());
assertThat(attachmentStreamConnector.getAttachmentStream(attachment, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(id)))), sameInstance(returnedStream));
verify(attachmentContentDownloader).download(eq(attachment), Matchers.any(Duration.class));
verify(attachmentStreamConnector).uploadAttachment(attachment, downloadUrlStream);
verify(attachmentStreamConnector).readAttachmentStream(rereadAttachment);
verify(rereadAttachment).setOnlyRemote(false);
verify(connector).update(rereadAttachment);
}
use of org.eclipse.sw360.datahandler.couchdb.AttachmentStreamConnector in project sw360portal by sw360.
the class AttachmentPortletUtils method uploadAttachmentPartFromRequest.
private boolean uploadAttachmentPartFromRequest(PortletRequest request, String fileUploadName) throws IOException, TException {
final UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);
final InputStream stream = uploadPortletRequest.getFileAsStream(fileUploadName);
final ResumableUpload resumableUpload = ResumableUpload.from(uploadPortletRequest);
AttachmentContent attachment = null;
if (resumableUpload.isValid()) {
final AttachmentStreamConnector attachmentStreamConnector = getConnector();
attachment = getAttachmentContent(resumableUpload, stream);
if (attachment != null) {
try {
attachmentStreamConnector.uploadAttachmentPart(attachment, resumableUpload.getChunkNumber(), stream);
} catch (TException e) {
log.error("Error saving attachment part", e);
return false;
}
}
}
return attachment != null;
}
Aggregations