test(thumbnails): lock corrupt-image + corrupt-pdf failure paths

Both cases already return FAILED via the existing catch-Exception blocks
in readSourceImage. Pinning the behavior with regression tests before
thumbnailAspect and pageCount computation is added, so a future
refactor that removes the safety net is caught at compile/test time.

Refs #305

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-04-23 14:18:04 +02:00
committed by marcel
parent 8ac996f6b2
commit 7fc517b787

View File

@@ -167,6 +167,38 @@ class ThumbnailServiceTest {
verify(documentRepository, never()).save(any());
}
@Test
void generate_returnsFailed_whenImageBytesAreCorrupt() throws IOException {
// Truncated JPEG header — ImageIO returns null rather than throwing.
// Without the corrupt-image guard this would later NPE inside the aspect /
// dimension computation in scaleToWidth.
Document doc = makeDoc("image/jpeg", "documents/corrupt.jpg");
byte[] truncated = new byte[]{(byte) 0xFF, (byte) 0xD8, (byte) 0xFF, (byte) 0xE0};
when(fileService.downloadFileStream(anyString()))
.thenReturn(new ByteArrayInputStream(truncated));
ThumbnailService.Outcome outcome = thumbnailService.generate(doc);
assertThat(outcome).isEqualTo(ThumbnailService.Outcome.FAILED);
verifyNoInteractions(s3Client);
verify(documentRepository, never()).save(any());
}
@Test
void generate_returnsFailed_whenPdfBytesAreCorrupt() throws IOException {
// "PDF" header but no body — PDFBox throws IOException while loading.
Document doc = makeDoc("application/pdf", "documents/corrupt.pdf");
byte[] fakePdf = "%PDF-1.4\n".getBytes();
when(fileService.downloadFileStream(anyString()))
.thenReturn(new ByteArrayInputStream(fakePdf));
ThumbnailService.Outcome outcome = thumbnailService.generate(doc);
assertThat(outcome).isEqualTo(ThumbnailService.Outcome.FAILED);
verifyNoInteractions(s3Client);
verify(documentRepository, never()).save(any());
}
@Test
void generate_returnsFailed_whenPersistThrows_butUploadSucceeded() throws IOException {
// Covers the "orphan thumbnail" edge case: S3 upload succeeded but the