Skip to content

Commit

Permalink
feat: add DownloadAttachment endpoint to Attachments API (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
vfisyuk-smartling authored Oct 2, 2024
1 parent 588ba17 commit d3255ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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));
}
}

0 comments on commit d3255ed

Please sign in to comment.