Skip to content

feat: add DownloadAttachment endpoint to Attachments API #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import java.io.InputStream;

import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA;
import static javax.ws.rs.core.MediaType.WILDCARD;

@Path("/attachments-api/v2")
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -23,4 +26,9 @@ public interface AttachmentsApi extends AutoCloseable
@Path("/accounts/{accountUid}/{type}/attachments")
@Consumes(MULTIPART_FORM_DATA)
AttachmentPTO uploadAttachment(@PathParam("accountUid") String accountUid, @PathParam("type") String type, @MultipartForm AttachmentUploadPTO attachmentUploadPTO);

@GET
@Path("/accounts/{accountUid}/{type}/attachments/{attachmentUid}")
@Produces(WILDCARD)
InputStream downloadAttachment(@PathParam("accountUid") String accountUid, @PathParam("type") String type, @PathParam("attachmentUid") String attachmentUid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.junit.After;
import org.junit.Before;
Expand All @@ -24,13 +25,17 @@
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class AttachmentsApiTest
Expand All @@ -45,9 +50,11 @@ public class AttachmentsApiTest
private static final String LINKS = IDENTITY_URL;
private static final String ENTITY_LINKS = LINKS + "/{" + ENTITY_UID_PARAMETER + "}";
private static final String ATTACHMENTS = IDENTITY_URL + "/attachments";
private static final String DOWNLOAD_ATTACHMENT = ATTACHMENTS + "/{" + ATTACHMENT_UID_PARAMETER + "}";
private static final String BEARER_TOKEN = UUID.randomUUID().toString();
private static final String ACCOUNT_UID = "account_uid";
private static final String ENTITY_UID = "entity_uid";
private static final String ATTACHMENT_UID = "attachment_uid";
private static final String ATTACHMENT_TYPE = AttachmentType.JOBS.name().toLowerCase();
private static final List<String> PATH_PLACEHOLDERS = asList(ACCOUNT_UID_PARAMETER, TYPE_PARAMETER, ENTITY_UID_PARAMETER, ATTACHMENT_UID_PARAMETER);

Expand Down Expand Up @@ -203,4 +210,21 @@ public void testUploadAttachment() throws Exception
assertEquals(expectedPTO.getCreatedDate(), response.getCreatedDate());
assertEquals(expectedPTO.getCreatedByUserUid(), response.getCreatedByUserUid());
}

@Test
public void testDownloadAttachment() throws IOException, InterruptedException {
// given
String attachmentContent = "Attachment content\nThe end.";
assignResponse(HttpStatus.SC_OK, "application/zip;charset=UTF-8", attachmentContent);

// when
InputStream response = attachmentsApi.downloadAttachment(ACCOUNT_UID, ATTACHMENT_TYPE, ATTACHMENT_UID);

// then

getRequestWithValidation(HttpMethod.GET, DOWNLOAD_ATTACHMENT, ACCOUNT_UID, ATTACHMENT_TYPE, ENTITY_UID, ATTACHMENT_UID);

assertNotNull(response);
assertEquals(attachmentContent, IOUtils.toString(response, StandardCharsets.UTF_8));
}
}
Loading