Skip to content

Commit d822e02

Browse files
sharonluongSharon Luong
andauthored
BXC-4310 add datastream check and fix tests (#1629)
Co-authored-by: Sharon Luong <[email protected]>
1 parent 738bb6e commit d822e02

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

web-services-app/src/main/java/edu/unc/lib/boxc/web/services/processing/DownloadImageService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class DownloadImageService {
3535
*/
3636
public ResponseEntity<InputStreamResource> streamImage(ContentObjectRecord contentObjectRecord, String size)
3737
throws IOException {
38+
if (contentObjectRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId()) == null) {
39+
return ResponseEntity.notFound().build();
40+
}
3841

3942
String pidString = contentObjectRecord.getPid().getId();
4043
String url = buildURL(pidString, size);

web-services-app/src/test/java/edu/unc/lib/boxc/web/services/rest/DownloadImageControllerIT.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import edu.unc.lib.boxc.auth.api.exceptions.AccessRestrictionException;
88
import edu.unc.lib.boxc.auth.api.services.AccessControlService;
99
import edu.unc.lib.boxc.auth.fcrepo.models.AccessGroupSetImpl;
10+
import edu.unc.lib.boxc.model.api.DatastreamType;
1011
import edu.unc.lib.boxc.model.api.ids.PID;
1112
import edu.unc.lib.boxc.search.api.models.Datastream;
1213
import edu.unc.lib.boxc.search.api.requests.SimpleIdRequest;
@@ -66,10 +67,14 @@ public void testGetImageAtFullSize() throws Exception {
6667
var formattedPid = idToPath(pidString, 4, 2) + pidString + ".jp2";
6768
var filename = "bunny.jpg";
6869
ContentObjectSolrRecord contentObjectSolrRecord = mock(ContentObjectSolrRecord.class);
69-
Datastream datastream = mock(Datastream.class);
70+
Datastream originalDatastream = mock(Datastream.class);
71+
Datastream jp2Datastream = mock(Datastream.class);
72+
7073
when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(contentObjectSolrRecord);
71-
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(datastream);
72-
when(datastream.getFilename()).thenReturn(filename);
74+
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.ORIGINAL_FILE.getId())).thenReturn(originalDatastream);
75+
when(originalDatastream.getFilename()).thenReturn(filename);
76+
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId())).thenReturn(jp2Datastream);
77+
7378
when(contentObjectSolrRecord.getPid()).thenReturn(pid);
7479

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

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

104110
when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(contentObjectSolrRecord);
105-
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(datastream);
106-
when(datastream.getExtent()).thenReturn("1200x1200");
107-
when(datastream.getFilename()).thenReturn(filename);
111+
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(originalDatastream);
112+
when(originalDatastream.getExtent()).thenReturn("1200x1200");
113+
when(originalDatastream.getFilename()).thenReturn(filename);
114+
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId())).thenReturn(jp2Datastream);
108115
when(contentObjectSolrRecord.getPid()).thenReturn(pid);
109116

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

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

135143
when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(contentObjectSolrRecord);
136-
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(datastream);
137-
when(datastream.getExtent()).thenReturn("1200x1200");
138-
when(datastream.getFilename()).thenReturn(filename);
144+
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(originalDatastream);
145+
when(originalDatastream.getExtent()).thenReturn("1200x1200");
146+
when(originalDatastream.getFilename()).thenReturn(filename);
147+
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId())).thenReturn(jp2Datastream);
139148
when(contentObjectSolrRecord.getPid()).thenReturn(pid);
140149

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

176-
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(datastream);
177-
when(datastream.getExtent()).thenReturn("1200x1200");
186+
when(contentObjectSolrRecord.getDatastreamObject("original_file")).thenReturn(originalDatastream);
187+
when(originalDatastream.getExtent()).thenReturn("1200x1200");
188+
when(contentObjectSolrRecord.getDatastreamObject(DatastreamType.JP2_ACCESS_COPY.getId())).thenReturn(jp2Datastream);
189+
178190

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

273+
@Test
274+
public void testGetImageNoJP2() throws Exception {
275+
PID filePid = makePid();
276+
ContentObjectSolrRecord contentObjectSolrRecord = mock(ContentObjectSolrRecord.class);
277+
when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(contentObjectSolrRecord);
278+
279+
mvc.perform(get("/downloadImage/" + filePid.getId() + "/full"))
280+
.andExpect(status().is4xxClientError())
281+
.andReturn();
282+
}
283+
261284
private void assertCorrectImageReturned(MockHttpServletResponse response) throws IOException {
262285
assertEquals("image/jpeg", response.getContentType());
263286

0 commit comments

Comments
 (0)