Search in sources :

Example 1 with AttachmentInputStream

use of org.ektorp.AttachmentInputStream in project sw360portal by sw360.

the class AttachmentStreamConnectorTest method testGetConcatenatedStream.

@Test
public void testGetConcatenatedStream() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.isOnlyRemote()).thenReturn(false);
    when(attachment.isSetPartsCount()).thenReturn(true);
    when(attachment.getPartsCount()).thenReturn("2");
    when(attachment.getFilename()).thenReturn("fil");
    String attachmentId = "id";
    when(attachment.getId()).thenReturn(attachmentId);
    AttachmentInputStream part1 = mock(AttachmentInputStream.class);
    when(connector.getAttachment(attachmentId, "fil_part1")).thenReturn(part1);
    AttachmentInputStream part2 = mock(AttachmentInputStream.class);
    when(connector.getAttachment(attachmentId, "fil_part2")).thenReturn(part2);
    when(part1.read()).thenReturn(1, -1);
    when(part2.read()).thenReturn(2, -1);
    InputStream attachmentStream = attachmentStreamConnector.getAttachmentStream(attachment, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentId))));
    verifyZeroInteractions(part2);
    assertThat(attachmentStream.read(), is(1));
    assertThat(attachmentStream.read(), is(2));
    verify(part1).close();
    assertThat(attachmentStream.read(), is(-1));
    verify(part2).close();
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) AttachmentInputStream(org.ektorp.AttachmentInputStream) InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) AttachmentInputStream(org.ektorp.AttachmentInputStream) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Test(org.junit.Test)

Example 2 with AttachmentInputStream

use of org.ektorp.AttachmentInputStream in project sw360portal by sw360.

the class AttachmentStreamConnectorTest method testGetConcatenatedStreamReadThrowsOnNonExistent.

@Test
public void testGetConcatenatedStreamReadThrowsOnNonExistent() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.isOnlyRemote()).thenReturn(false);
    when(attachment.isSetPartsCount()).thenReturn(true);
    when(attachment.getPartsCount()).thenReturn("2");
    when(attachment.getFilename()).thenReturn("fil");
    String attachmentId = "id";
    when(attachment.getId()).thenReturn(attachmentId);
    AttachmentInputStream part1 = mock(AttachmentInputStream.class);
    when(connector.getAttachment(attachmentId, "fil_part1")).thenReturn(part1);
    when(connector.getAttachment(attachmentId, "fil_part2")).thenThrow(new DocumentNotFoundException(""));
    when(part1.read()).thenReturn(1, -1);
    InputStream attachmentStream = attachmentStreamConnector.getAttachmentStream(attachment, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentId))));
    assertThat(attachmentStream.read(), is(1));
    try {
        assertThat(attachmentStream.read(), is(2));
        fail("expected Exception not thrown");
    } catch (IOException ignored) {
    }
    verify(part1).close();
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) DocumentNotFoundException(org.ektorp.DocumentNotFoundException) AttachmentInputStream(org.ektorp.AttachmentInputStream) InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) AttachmentInputStream(org.ektorp.AttachmentInputStream) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with AttachmentInputStream

use of org.ektorp.AttachmentInputStream in project sw360portal by sw360.

the class AttachmentStreamConnectorTest method testGetFullStream.

@Test
public void testGetFullStream() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.isOnlyRemote()).thenReturn(false);
    when(attachment.isSetPartsCount()).thenReturn(false);
    when(attachment.getFilename()).thenReturn("fil");
    String attachmentId = "id";
    when(attachment.getId()).thenReturn(attachmentId);
    AttachmentInputStream full = mock(AttachmentInputStream.class);
    when(connector.getAttachment(attachmentId, "fil")).thenReturn(full);
    when(full.read()).thenReturn(1, 2, -1);
    InputStream attachmentStream = attachmentStreamConnector.getAttachmentStream(attachment, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentId))));
    assertThat(attachmentStream.read(), is(1));
    assertThat(attachmentStream.read(), is(2));
    assertThat(attachmentStream.read(), is(-1));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) AttachmentInputStream(org.ektorp.AttachmentInputStream) InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) AttachmentInputStream(org.ektorp.AttachmentInputStream) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Test(org.junit.Test)

Example 4 with AttachmentInputStream

use of org.ektorp.AttachmentInputStream in project sw360portal by sw360.

the class AttachmentStreamConnector method addAttachmentTo.

private void addAttachmentTo(String attachmentContentId, String filename, InputStream stream) {
    String contentType = "application/octet-stream";
    AttachmentInputStream attachmentInputStream = new AttachmentInputStream(filename, stream, contentType);
    String revision = connector.getCurrentRevision(attachmentContentId);
    connector.createAttachment(attachmentContentId, revision, attachmentInputStream);
}
Also used : AttachmentInputStream(org.ektorp.AttachmentInputStream)

Aggregations

AttachmentInputStream (org.ektorp.AttachmentInputStream)4 InputStream (java.io.InputStream)3 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)3 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)3 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)3 Test (org.junit.Test)3 IOException (java.io.IOException)1 DocumentNotFoundException (org.ektorp.DocumentNotFoundException)1