Skip to content

Commit

Permalink
BXC-4310 add datastream check and fix tests (#1629)
Browse files Browse the repository at this point in the history
Co-authored-by: Sharon Luong <snluong@email.lib.unc.edu>
  • Loading branch information
sharonluong and Sharon Luong authored Nov 30, 2023
1 parent 738bb6e commit d822e02
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class DownloadImageService {
*/
public ResponseEntity<InputStreamResource> streamImage(ContentObjectRecord contentObjectRecord, String size)
throws IOException {
if (contentObjectRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId()) == null) {
return ResponseEntity.notFound().build();
}

String pidString = contentObjectRecord.getPid().getId();
String url = buildURL(pidString, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.unc.lib.boxc.auth.api.exceptions.AccessRestrictionException;
import edu.unc.lib.boxc.auth.api.services.AccessControlService;
import edu.unc.lib.boxc.auth.fcrepo.models.AccessGroupSetImpl;
import edu.unc.lib.boxc.model.api.DatastreamType;
import edu.unc.lib.boxc.model.api.ids.PID;
import edu.unc.lib.boxc.search.api.models.Datastream;
import edu.unc.lib.boxc.search.api.requests.SimpleIdRequest;
Expand Down Expand Up @@ -66,10 +67,14 @@ public void testGetImageAtFullSize() throws Exception {
var formattedPid = idToPath(pidString, 4, 2) + pidString + ".jp2";
var filename = "bunny.jpg";
ContentObjectSolrRecord contentObjectSolrRecord = mock(ContentObjectSolrRecord.class);
Datastream datastream = mock(Datastream.class);
Datastream originalDatastream = mock(Datastream.class);
Datastream jp2Datastream = mock(Datastream.class);

when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(contentObjectSolrRecord);
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(datastream);
when(datastream.getFilename()).thenReturn(filename);
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.ORIGINAL_FILE.getId())).thenReturn(originalDatastream);
when(originalDatastream.getFilename()).thenReturn(filename);
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId())).thenReturn(jp2Datastream);

when(contentObjectSolrRecord.getPid()).thenReturn(pid);

stubFor(WireMock.get(urlMatching("/" + formattedPid + "/full/full/0/default.jpg"))
Expand All @@ -93,7 +98,8 @@ public void testGetImageAtPixelSizeSmallerThanFull() throws Exception {
var formattedPid = idToPath(pidString, 4, 2) + pidString + ".jp2";
var filename = "bunny.jpg";
ContentObjectSolrRecord contentObjectSolrRecord = mock(ContentObjectSolrRecord.class);
Datastream datastream = mock(Datastream.class);
Datastream originalDatastream = mock(Datastream.class);
Datastream jp2Datastream = mock(Datastream.class);

stubFor(WireMock.get(urlMatching("/" + formattedPid + "/full/!800,800/0/default.jpg"))
.willReturn(aResponse()
Expand All @@ -102,9 +108,10 @@ public void testGetImageAtPixelSizeSmallerThanFull() throws Exception {
.withHeader("Content-Type", "image/jpeg")));

when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(contentObjectSolrRecord);
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(datastream);
when(datastream.getExtent()).thenReturn("1200x1200");
when(datastream.getFilename()).thenReturn(filename);
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(originalDatastream);
when(originalDatastream.getExtent()).thenReturn("1200x1200");
when(originalDatastream.getFilename()).thenReturn(filename);
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId())).thenReturn(jp2Datastream);
when(contentObjectSolrRecord.getPid()).thenReturn(pid);

MvcResult result = mvc.perform(get("/downloadImage/" + pidString + "/800"))
Expand All @@ -124,7 +131,8 @@ public void testGetImageAtPixelSizeBiggerThanFull() throws Exception {
var formattedPid = idToPath(pidString, 4, 2) + pidString + ".jp2";
var filename = "bunny.jpg";
ContentObjectSolrRecord contentObjectSolrRecord = mock(ContentObjectSolrRecord.class);
Datastream datastream = mock(Datastream.class);
Datastream originalDatastream = mock(Datastream.class);
Datastream jp2Datastream = mock(Datastream.class);

stubFor(WireMock.get(urlMatching("/" + formattedPid + "/full/full/0/default.jpg"))
.willReturn(aResponse()
Expand All @@ -133,9 +141,10 @@ public void testGetImageAtPixelSizeBiggerThanFull() throws Exception {
.withHeader("Content-Type", "image/jpeg")));

when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(contentObjectSolrRecord);
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(datastream);
when(datastream.getExtent()).thenReturn("1200x1200");
when(datastream.getFilename()).thenReturn(filename);
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(originalDatastream);
when(originalDatastream.getExtent()).thenReturn("1200x1200");
when(originalDatastream.getFilename()).thenReturn(filename);
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId())).thenReturn(jp2Datastream);
when(contentObjectSolrRecord.getPid()).thenReturn(pid);

MvcResult result = mvc.perform(get("/downloadImage/" + pidString + "/2500"))
Expand Down Expand Up @@ -168,13 +177,16 @@ public void testFullSizeAccessImageNoFullSizePermissions() throws Exception {
public void testGetImageAtPixelSizeBiggerThanFullNoPermission() throws Exception {
PID filePid = makePid();
ContentObjectSolrRecord contentObjectSolrRecord = mock(ContentObjectSolrRecord.class);
Datastream datastream = mock(Datastream.class);
Datastream originalDatastream = mock(Datastream.class);
Datastream jp2Datastream = mock(Datastream.class);
when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(contentObjectSolrRecord);
doThrow(new AccessRestrictionException()).when(accessControlService)
.assertHasAccess(anyString(), eq(filePid), any(AccessGroupSetImpl.class), eq(viewOriginal));

when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(datastream);
when(datastream.getExtent()).thenReturn("1200x1200");
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(originalDatastream);
when(originalDatastream.getExtent()).thenReturn("1200x1200");
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId())).thenReturn(jp2Datastream);


MvcResult result = mvc.perform(get("/downloadImage/" + filePid.getId() + "/2500"))
.andExpect(status().isForbidden())
Expand Down Expand Up @@ -258,6 +270,17 @@ public void testGetAccessImageNoOriginalFile() throws Exception {
assertEquals(message, DownloadImageService.INVALID_SIZE_MESSAGE);
}

@Test
public void testGetImageNoJP2() throws Exception {
PID filePid = makePid();
ContentObjectSolrRecord contentObjectSolrRecord = mock(ContentObjectSolrRecord.class);
when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(contentObjectSolrRecord);

mvc.perform(get("/downloadImage/" + filePid.getId() + "/full"))
.andExpect(status().is4xxClientError())
.andReturn();
}

private void assertCorrectImageReturned(MockHttpServletResponse response) throws IOException {
assertEquals("image/jpeg", response.getContentType());

Expand Down

0 comments on commit d822e02

Please sign in to comment.