feat(tests-util): Add comprehensive test coverage for FileService and MetadataChangeDetector (#1658)

This commit is contained in:
Balázs Szücs
2025-11-28 09:35:14 -07:00
committed by GitHub
parent 900cd0a884
commit 71f02c3238
5 changed files with 1688 additions and 54 deletions
@@ -17,7 +17,7 @@ public enum ApiError {
DEFAULT_EMAIL_RECIPIENT_NOT_FOUND(HttpStatus.NOT_FOUND, " Default Email recipient not found"),
UNSUPPORTED_BOOK_TYPE(HttpStatus.BAD_REQUEST, "Unsupported book type for viewer settings"),
INVALID_VIEWER_SETTING(HttpStatus.BAD_REQUEST, "Invalid viewer setting for the book"),
FILE_READ_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Error reading files from path"),
FILE_READ_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Error reading files from path: %s"),
IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "Image not found or not readable"),
INVALID_FILE_FORMAT(HttpStatus.BAD_REQUEST, "Invalid file format, only pdf and epub are supported"),
LIBRARY_NOT_FOUND(HttpStatus.NOT_FOUND, "Library not found with ID: %d"),
@@ -60,7 +60,7 @@ public class BackgroundUploadService {
String extension = getFileExtension(originalFilename);
String filename = "1." + extension;
BufferedImage originalImage = FileService.downloadImageFromUrl(imageUrl);
BufferedImage originalImage = fileService.downloadImageFromUrl(imageUrl);
deleteExistingBackgroundFiles(userId);
fileService.saveBackgroundImage(originalImage, filename, userId);
@@ -37,6 +37,7 @@ import java.util.stream.Stream;
public class FileService {
private final AppProperties appProperties;
private final RestTemplate restTemplate;
// @formatter:off
private static final String IMAGES_DIR = "images";
@@ -114,7 +115,11 @@ public class FileService {
throw new IllegalArgumentException("Uploaded file is empty");
}
String contentType = file.getContentType();
if (!(JPEG_MIME_TYPE.equalsIgnoreCase(contentType) || PNG_MIME_TYPE.equalsIgnoreCase(contentType))) {
if (contentType == null) {
throw new IllegalArgumentException("Content type is required");
}
String lowerType = contentType.toLowerCase();
if (!lowerType.startsWith(JPEG_MIME_TYPE) && !lowerType.startsWith(PNG_MIME_TYPE)) {
throw new IllegalArgumentException("Only JPEG and PNG files are allowed");
}
if (file.getSize() > MAX_FILE_SIZE_BYTES) {
@@ -146,7 +151,7 @@ public class FileService {
log.info("Image saved successfully to: {}", filePath);
}
public static BufferedImage downloadImageFromUrl(String imageUrl) throws IOException {
public BufferedImage downloadImageFromUrl(String imageUrl) throws IOException {
try {
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.USER_AGENT, "BookLore/1.0 (Metadata Fetcher)");
@@ -154,7 +159,7 @@ public class FileService {
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<byte[]> response = new RestTemplate().exchange(
ResponseEntity<byte[]> response = restTemplate.exchange(
imageUrl,
HttpMethod.GET,
entity,
@@ -175,7 +180,7 @@ public class FileService {
}
} catch (Exception e) {
log.error("Failed to download image from URL: {} - {}", imageUrl, e.getMessage());
throw new IOException("Failed to download image from URL: " + imageUrl, e);
throw new IOException("Failed to download image from URL: " + imageUrl + " - " + e.getMessage(), e);
}
}