feat(metadata): add custom metadata write to file support for moods, tags, ratings, and external IDs in PDF, and CBZ (#2552)
* feat(metadata): add BookLore custom metadata support for moods, tags, ratings, and external IDs in EPUB, PDF, and CBZ - Introduce BookLoreSchema for XMP custom fields and BookLoreMetadata for namespace constants - Write and extract moods, tags, ratings, and external IDs (Goodreads, Amazon, Hardcover, Lubimyczytac, RanobeDB, Google) in EPUB, PDF, and CBZ metadata - Store BookLore custom fields in ComicInfo.xml notes and Web fields for CBZ - Add BookLore fields to XMP metadata in PDF, including moods and tags as separate fields - Update extractors and writers to handle BookLore fields and ensure separation of categories, moods, and tags - Add comprehensive tests for BookLore metadata extraction and writing Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * refactor(metadata): standardize hardcoverBookId as String and improve identifier handling - Change hardcoverBookId field and related methods to use String instead of Integer for consistency across extractors and writers - Update metadata copy helpers and tests to reflect new hardcoverBookId type - Improve identifier prefix handling in EPUB and PDF extractors to be case-insensitive and trim whitespace - Allow PDF keywords to be split by comma or semicolon - Rename TaskCreateRequest#getOptions to getOptionsAs for clarity and update usages - Adjust series number writing in EPUB metadata to preserve decimals when needed Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * refactor(metadata): standardize usage of tags over categories and improve BookLore field extraction Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): add rating and ratingLocked fields to BookMetadata and improve PDF metadata extraction - Add rating and ratingLocked fields to BookMetadata for enhanced rating support - Refine PDF keyword extraction to handle both comma and semicolon delimiters - Ensure ISBN fields are only set if cleaned values are non-blank - Use temp file only stream cache when loading PDFs for metadata writing Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): implement ComicInfo.xml writing via JAXB, ensure XSD compliance and add tests for CBX metadata persistence Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): enhance metadata extraction with additional identifiers and improve parsing logic Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): improve metadata handling by ensuring descriptions are prioritized and enhancing keyword management for PDF compatibility Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * refactor(metadata): simplify Pages class structure in ComicInfo.java Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): refine PDF metadata writing by isolating categories for legacy compatibility and removing moods and tags from keywords Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): enhance PDF metadata extraction and writing with support for additional fields and improved handling of series information Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): enhance PDF metadata extraction and writing with support for additional fields and improved handling of series information Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): implement secure XML parsing utility and enhance metadata extraction with improved GTIN validation and debug logging Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * refactor(metadata): update package structure to align with new organization naming conventions Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(epub-metadata): streamline cover image extraction logic and improve error handling Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): enhance PDF and comic metadata extraction with additional fields Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(metadata): migrate to tools.jackson for JSON processing in sidecar metadata handling Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(epub-metadata): streamline cover image extraction and enhance XML parsing security Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> --------- Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
@@ -1,21 +1,26 @@
|
||||
package org.booklore.mapper;
|
||||
|
||||
import org.booklore.model.dto.Book;
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookFileEntity;
|
||||
import org.booklore.model.entity.LibraryEntity;
|
||||
import org.booklore.model.entity.LibraryPathEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
class BookMapperTest {
|
||||
|
||||
private final BookMapper mapper = Mappers.getMapper(BookMapper.class);
|
||||
@Autowired
|
||||
private BookMapper mapper;
|
||||
|
||||
@Test
|
||||
void shouldMapExistingFieldsCorrectly() {
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.booklore.mapper;
|
||||
|
||||
import org.booklore.model.dto.BookMetadata;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class BookMetadataMapperTest {
|
||||
|
||||
@Mock
|
||||
private AuthorMapper authorMapper;
|
||||
|
||||
@Mock
|
||||
private CategoryMapper categoryMapper;
|
||||
|
||||
@Mock
|
||||
private MoodMapper moodMapper;
|
||||
|
||||
@Mock
|
||||
private TagMapper tagMapper;
|
||||
|
||||
@Mock
|
||||
private ComicMetadataMapper comicMetadataMapper;
|
||||
|
||||
private BookMetadataMapperImpl bookMetadataMapper;
|
||||
|
||||
@org.junit.jupiter.api.BeforeEach
|
||||
void setUp() {
|
||||
bookMetadataMapper = new BookMetadataMapperImpl();
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(bookMetadataMapper, "authorMapper", authorMapper);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(bookMetadataMapper, "categoryMapper", categoryMapper);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(bookMetadataMapper, "moodMapper", moodMapper);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(bookMetadataMapper, "tagMapper", tagMapper);
|
||||
org.springframework.test.util.ReflectionTestUtils.setField(bookMetadataMapper, "comicMetadataMapper", comicMetadataMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMapping() {
|
||||
BookMetadataEntity entity = new BookMetadataEntity();
|
||||
entity.setTitle("Test Title");
|
||||
entity.setHardcoverId("hc-id");
|
||||
entity.setHardcoverRating(4.5);
|
||||
entity.setGoodreadsId("gr-id");
|
||||
entity.setAuthors(new java.util.HashSet<>());
|
||||
entity.setCategories(new java.util.HashSet<>());
|
||||
entity.setMoods(new java.util.HashSet<>());
|
||||
entity.setTags(new java.util.HashSet<>());
|
||||
|
||||
BookMetadata dto = bookMetadataMapper.toBookMetadata(entity);
|
||||
|
||||
assertEquals("Test Title", dto.getTitle());
|
||||
assertEquals("hc-id", dto.getHardcoverId());
|
||||
assertEquals(4.5, dto.getHardcoverRating());
|
||||
assertEquals("gr-id", dto.getGoodreadsId());
|
||||
}
|
||||
}
|
||||
|
||||
+385
@@ -0,0 +1,385 @@
|
||||
package org.booklore.service.metadata;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.booklore.model.MetadataClearFlags;
|
||||
import org.booklore.model.dto.BookMetadata;
|
||||
import org.booklore.model.dto.settings.AppSettingKey;
|
||||
import org.booklore.model.dto.settings.MetadataPersistenceSettings;
|
||||
import org.booklore.model.entity.*;
|
||||
import org.booklore.model.enums.BookFileExtension;
|
||||
import org.booklore.model.enums.MetadataProvider;
|
||||
import org.booklore.repository.AppSettingsRepository;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.booklore.service.metadata.extractor.MetadataExtractorFactory;
|
||||
import org.booklore.service.metadata.writer.MetadataWriter;
|
||||
import org.booklore.service.metadata.writer.MetadataWriterFactory;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
@Transactional
|
||||
class EndToEndMetadataPersistenceTest {
|
||||
|
||||
@Autowired
|
||||
private MetadataWriterFactory metadataWriterFactory;
|
||||
|
||||
@Autowired
|
||||
private MetadataExtractorFactory metadataExtractorFactory;
|
||||
|
||||
@Autowired
|
||||
private AppSettingService appSettingService;
|
||||
|
||||
@Autowired
|
||||
private AppSettingsRepository appSettingsRepository;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private File pdfFile;
|
||||
private File epubFile;
|
||||
private File cbzFile;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
// Ensure clean state for settings using repository directly (transactional rollout will handle cleanup after test)
|
||||
appSettingsRepository.deleteAll();
|
||||
appSettingsRepository.flush();
|
||||
|
||||
MetadataPersistenceSettings settings = MetadataPersistenceSettings.builder()
|
||||
.saveToOriginalFile(MetadataPersistenceSettings.SaveToOriginalFile.builder()
|
||||
.epub(MetadataPersistenceSettings.FormatSettings.builder().enabled(true).maxFileSizeInMb(500).build())
|
||||
.pdf(MetadataPersistenceSettings.FormatSettings.builder().enabled(true).maxFileSizeInMb(500).build())
|
||||
.cbx(MetadataPersistenceSettings.FormatSettings.builder().enabled(true).maxFileSizeInMb(500).build())
|
||||
.build())
|
||||
.build();
|
||||
|
||||
AppSettingEntity entity = new AppSettingEntity();
|
||||
entity.setName(AppSettingKey.METADATA_PERSISTENCE_SETTINGS.getDbKey());
|
||||
entity.setVal(objectMapper.writeValueAsString(settings));
|
||||
appSettingsRepository.saveAndFlush(entity);
|
||||
|
||||
// Force reload settings cache
|
||||
ReflectionTestUtils.setField(appSettingService, "appSettings", null);
|
||||
|
||||
pdfFile = tempDir.resolve("test.pdf").toFile();
|
||||
createDummyPdf(pdfFile);
|
||||
|
||||
epubFile = tempDir.resolve("test.epub").toFile();
|
||||
createDummyEpub(epubFile);
|
||||
|
||||
cbzFile = tempDir.resolve("test.cbz").toFile();
|
||||
createDummyCbz(cbzFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPersistAndRetrieveCompleteMetadataForPdf() {
|
||||
verifyMetadataPersistence(pdfFile, BookFileExtension.PDF);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPersistAndRetrieveCompleteMetadataForEpub() {
|
||||
verifyMetadataPersistence(epubFile, BookFileExtension.EPUB);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPersistAndRetrieveCompleteMetadataForCbz() {
|
||||
verifyMetadataPersistence(cbzFile, BookFileExtension.CBZ);
|
||||
}
|
||||
|
||||
private void verifyMetadataPersistence(File file, BookFileExtension extension) {
|
||||
BookMetadata originalMetadata = createFullMetadata();
|
||||
BookMetadataEntity entity = toEntity(originalMetadata);
|
||||
|
||||
MetadataWriter writer = metadataWriterFactory.getWriter(extension.getType())
|
||||
.orElseThrow(() -> new RuntimeException("No writer found for " + extension));
|
||||
|
||||
writer.saveMetadataToFile(file, entity, originalMetadata.getThumbnailUrl(), new MetadataClearFlags());
|
||||
|
||||
assertThat(file).exists();
|
||||
|
||||
BookMetadata rescannedMetadata = metadataExtractorFactory.extractMetadata(extension, file);
|
||||
|
||||
assertMetadataEquality(originalMetadata, rescannedMetadata, extension);
|
||||
}
|
||||
|
||||
private BookMetadata createFullMetadata() {
|
||||
return BookMetadata.builder()
|
||||
.title("The Ultimate Test Book")
|
||||
.subtitle("A Comprehensive Guide to Metadata")
|
||||
.provider(MetadataProvider.Google)
|
||||
.authors(Set.of("Author One", "Author Two"))
|
||||
.publisher("Test Publisher")
|
||||
.publishedDate(LocalDate.of(2023, 10, 27))
|
||||
.description("This is a description used for testing metadata persistence.")
|
||||
.seriesName("The Test Series")
|
||||
.seriesNumber(1.5f)
|
||||
.seriesTotal(3)
|
||||
.isbn13("978-3-16-148410-0")
|
||||
.isbn10("3-16-148410-0")
|
||||
.asin("B00TESTASI")
|
||||
.pageCount(350)
|
||||
.language("en")
|
||||
.categories(Set.of("Fiction", "Testing", "Science"))
|
||||
.tags(Set.of("Best Seller", "Must Read"))
|
||||
.moods(Set.of("Happy", "Suspenseful"))
|
||||
|
||||
// Ratings
|
||||
.amazonRating(4.5)
|
||||
.goodreadsRating(4.2)
|
||||
.hardcoverRating(85.0)
|
||||
.lubimyczytacRating(7.5)
|
||||
.ranobedbRating(9.0)
|
||||
|
||||
// Identifiers
|
||||
.goodreadsId("12345")
|
||||
.comicvineId("4000-12345")
|
||||
.hardcoverId("test-book-slug")
|
||||
.hardcoverBookId("999")
|
||||
.googleId("google_id_123")
|
||||
.ranobedbId("ranobe-000")
|
||||
.lubimyczytacId("lub-123")
|
||||
.externalUrl("https://booklore.org")
|
||||
|
||||
// Ensure all strict locks are disabled to allow writing
|
||||
.titleLocked(false)
|
||||
.subtitleLocked(false)
|
||||
.publisherLocked(false)
|
||||
.publishedDateLocked(false)
|
||||
.descriptionLocked(false)
|
||||
.seriesNameLocked(false)
|
||||
.seriesNumberLocked(false)
|
||||
.seriesTotalLocked(false)
|
||||
.isbn13Locked(false)
|
||||
.isbn10Locked(false)
|
||||
.asinLocked(false)
|
||||
.goodreadsIdLocked(false)
|
||||
.comicvineIdLocked(false)
|
||||
.hardcoverIdLocked(false)
|
||||
.hardcoverBookIdLocked(false)
|
||||
.doubanIdLocked(false)
|
||||
.googleIdLocked(false)
|
||||
.languageLocked(false)
|
||||
.amazonRatingLocked(false)
|
||||
.amazonReviewCountLocked(false)
|
||||
.goodreadsRatingLocked(false)
|
||||
.goodreadsReviewCountLocked(false)
|
||||
.hardcoverRatingLocked(false)
|
||||
.hardcoverReviewCountLocked(false)
|
||||
.doubanRatingLocked(false)
|
||||
.doubanReviewCountLocked(false)
|
||||
.lubimyczytacIdLocked(false)
|
||||
.lubimyczytacRatingLocked(false)
|
||||
.ranobedbIdLocked(false)
|
||||
.ranobedbRatingLocked(false)
|
||||
.externalUrlLocked(false)
|
||||
.coverLocked(false)
|
||||
.authorsLocked(false)
|
||||
.categoriesLocked(false)
|
||||
.moodsLocked(false)
|
||||
.tagsLocked(false)
|
||||
.reviewsLocked(false)
|
||||
.build();
|
||||
}
|
||||
|
||||
// Manual mapping for test purposes
|
||||
private BookMetadataEntity toEntity(BookMetadata dto) {
|
||||
BookMetadataEntity entity = new BookMetadataEntity();
|
||||
entity.setTitle(dto.getTitle());
|
||||
entity.setSubtitle(dto.getSubtitle());
|
||||
entity.setPublisher(dto.getPublisher());
|
||||
entity.setPublishedDate(dto.getPublishedDate());
|
||||
entity.setDescription(dto.getDescription());
|
||||
entity.setSeriesName(dto.getSeriesName());
|
||||
entity.setSeriesNumber(dto.getSeriesNumber());
|
||||
entity.setSeriesTotal(dto.getSeriesTotal());
|
||||
entity.setIsbn13(dto.getIsbn13());
|
||||
entity.setIsbn10(dto.getIsbn10());
|
||||
entity.setAsin(dto.getAsin());
|
||||
entity.setPageCount(dto.getPageCount());
|
||||
entity.setLanguage(dto.getLanguage());
|
||||
entity.setGoodreadsId(dto.getGoodreadsId());
|
||||
entity.setComicvineId(dto.getComicvineId());
|
||||
entity.setHardcoverId(dto.getHardcoverId());
|
||||
entity.setGoogleId(dto.getGoogleId());
|
||||
entity.setRanobedbId(dto.getRanobedbId());
|
||||
entity.setLubimyczytacId(dto.getLubimyczytacId());
|
||||
|
||||
entity.setAmazonRating(dto.getAmazonRating());
|
||||
entity.setGoodreadsRating(dto.getGoodreadsRating());
|
||||
entity.setHardcoverRating(dto.getHardcoverRating());
|
||||
entity.setHardcoverBookId(dto.getHardcoverBookId());
|
||||
entity.setLubimyczytacRating(dto.getLubimyczytacRating());
|
||||
entity.setRanobedbRating(dto.getRanobedbRating());
|
||||
|
||||
if (dto.getAuthors() != null) {
|
||||
entity.setAuthors(dto.getAuthors().stream().map(name -> {
|
||||
var a = new AuthorEntity();
|
||||
a.setName(name);
|
||||
return a;
|
||||
}).collect(Collectors.toSet()));
|
||||
}
|
||||
if (dto.getCategories() != null) {
|
||||
entity.setCategories(dto.getCategories().stream().map(name -> {
|
||||
var c = new CategoryEntity();
|
||||
c.setName(name);
|
||||
return c;
|
||||
}).collect(Collectors.toSet()));
|
||||
}
|
||||
if (dto.getTags() != null) {
|
||||
entity.setTags(dto.getTags().stream().map(name -> {
|
||||
var t = new TagEntity();
|
||||
t.setName(name);
|
||||
return t;
|
||||
}).collect(Collectors.toSet()));
|
||||
}
|
||||
if (dto.getMoods() != null) {
|
||||
entity.setMoods(dto.getMoods().stream().map(name -> {
|
||||
var m = new MoodEntity();
|
||||
m.setName(name);
|
||||
return m;
|
||||
}).collect(Collectors.toSet()));
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
private void assertMetadataEquality(BookMetadata expected, BookMetadata actual, BookFileExtension extension) {
|
||||
// 1. Basic Fields
|
||||
assertThat(actual.getTitle()).as("Title").isEqualTo(expected.getTitle());
|
||||
assertThat(actual.getPublisher()).as("Publisher").isEqualTo(expected.getPublisher());
|
||||
assertThat(actual.getLanguage()).as("Language").isEqualTo(expected.getLanguage());
|
||||
assertThat(actual.getDescription()).as("Description").isEqualTo(expected.getDescription());
|
||||
|
||||
// 2. Series Info
|
||||
assertThat(actual.getSeriesName()).as("SeriesName").isEqualTo(expected.getSeriesName());
|
||||
assertThat(actual.getSeriesNumber()).as("SeriesNumber").isEqualTo(expected.getSeriesNumber());
|
||||
assertThat(actual.getSeriesTotal()).as("SeriesTotal").isEqualTo(expected.getSeriesTotal());
|
||||
|
||||
// 3. Collections
|
||||
assertThat(actual.getAuthors()).as("Authors").containsExactlyInAnyOrderElementsOf(expected.getAuthors());
|
||||
assertThat(actual.getCategories()).as("Categories").containsExactlyInAnyOrderElementsOf(expected.getCategories());
|
||||
|
||||
if (extension != BookFileExtension.CBZ) {
|
||||
assertThat(actual.getTags()).as("Tags").containsExactlyInAnyOrderElementsOf(expected.getTags());
|
||||
assertThat(actual.getMoods()).as("Moods").containsExactlyInAnyOrderElementsOf(expected.getMoods());
|
||||
} else {
|
||||
assertThat(actual.getTags()).as("Tags").containsExactlyInAnyOrderElementsOf(expected.getTags());
|
||||
assertThat(actual.getMoods()).as("Moods").containsExactlyInAnyOrderElementsOf(expected.getMoods());
|
||||
}
|
||||
|
||||
// 4. Identifiers & ISBN Normalization
|
||||
String expectedIsbn13 = expected.getIsbn13() != null ? expected.getIsbn13().replaceAll("[- ]", "") : null;
|
||||
String actualIsbn13 = actual.getIsbn13() != null ? actual.getIsbn13().replaceAll("[- ]", "") : null;
|
||||
assertThat(actualIsbn13).as("ISBN13").isEqualTo(expectedIsbn13);
|
||||
|
||||
String expectedIsbn10 = expected.getIsbn10() != null ? expected.getIsbn10().replaceAll("[- ]", "") : null;
|
||||
String actualIsbn10 = actual.getIsbn10() != null ? actual.getIsbn10().replaceAll("[- ]", "") : null;
|
||||
assertThat(actualIsbn10).as("ISBN10").isEqualTo(expectedIsbn10);
|
||||
|
||||
assertThat(actual.getAsin()).as("ASIN").isEqualTo(expected.getAsin());
|
||||
assertThat(actual.getGoodreadsId()).as("GoodreadsId").isEqualTo(expected.getGoodreadsId());
|
||||
assertThat(actual.getComicvineId()).as("ComicvineId").isEqualTo(expected.getComicvineId());
|
||||
assertThat(actual.getHardcoverId()).as("HardcoverId").isEqualTo(expected.getHardcoverId());
|
||||
assertThat(actual.getHardcoverBookId()).as("HardcoverBookId").isEqualTo(expected.getHardcoverBookId());
|
||||
assertThat(actual.getGoogleId()).as("GoogleId").isEqualTo(expected.getGoogleId());
|
||||
assertThat(actual.getRanobedbId()).as("RanobeDBId").isEqualTo(expected.getRanobedbId());
|
||||
assertThat(actual.getLubimyczytacId()).as("LubimyczytacId").isEqualTo(expected.getLubimyczytacId());
|
||||
|
||||
// 5. Ratings
|
||||
assertThat(actual.getAmazonRating()).as("AmazonRating").isEqualTo(expected.getAmazonRating());
|
||||
assertThat(actual.getGoodreadsRating()).as("GoodreadsRating").isEqualTo(expected.getGoodreadsRating());
|
||||
assertThat(actual.getHardcoverRating()).as("HardcoverRating").isEqualTo(expected.getHardcoverRating());
|
||||
assertThat(actual.getLubimyczytacRating()).as("LubimyczytacRating").isEqualTo(expected.getLubimyczytacRating());
|
||||
assertThat(actual.getRanobedbRating()).as("RanobeDBRating").isEqualTo(expected.getRanobedbRating());
|
||||
|
||||
// 6. Format Specific Limitations
|
||||
if (extension != BookFileExtension.PDF) {
|
||||
// PDF page count corresponds to physical pages, which is 1 in dummy file.
|
||||
assertThat(actual.getPageCount()).as("PageCount").isEqualTo(expected.getPageCount());
|
||||
|
||||
// PDF extractor reads CreationDate which might differ from PublishedDate
|
||||
assertThat(actual.getPublishedDate()).as("PublishedDate").isEqualTo(expected.getPublishedDate());
|
||||
} else {
|
||||
// For PDF, we expect 1 page because the dummy pdf only has 1 page
|
||||
assertThat(actual.getPageCount()).as("PdfPageCount").isEqualTo(1);
|
||||
}
|
||||
|
||||
// 7. Subtitle Check
|
||||
assertThat(actual.getSubtitle()).as("Subtitle").isEqualTo(expected.getSubtitle());
|
||||
}
|
||||
|
||||
private void createDummyPdf(File file) throws IOException {
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
doc.addPage(new PDPage());
|
||||
doc.save(file);
|
||||
}
|
||||
}
|
||||
|
||||
private void createDummyEpub(File file) throws IOException {
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file))) {
|
||||
// mimetype file must be first and uncompressed
|
||||
ZipEntry mimetype = new ZipEntry("mimetype");
|
||||
mimetype.setMethod(ZipEntry.STORED);
|
||||
mimetype.setSize(20);
|
||||
mimetype.setCompressedSize(20);
|
||||
mimetype.setCrc(0x2cab616f); // CRC for "application/epub+zip"
|
||||
zos.putNextEntry(mimetype);
|
||||
zos.write("application/epub+zip".getBytes());
|
||||
zos.closeEntry();
|
||||
|
||||
// META-INF/container.xml
|
||||
zos.putNextEntry(new ZipEntry("META-INF/container.xml"));
|
||||
String containerXml = "<?xml version=\"1.0\"?>\n" +
|
||||
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n" +
|
||||
" <rootfiles>\n" +
|
||||
" <rootfile full-path=\"content.opf\" media-type=\"application/oebps-package+xml\"/>\n" +
|
||||
" </rootfiles>\n" +
|
||||
"</container>";
|
||||
zos.write(containerXml.getBytes());
|
||||
zos.closeEntry();
|
||||
|
||||
// content.opf
|
||||
zos.putNextEntry(new ZipEntry("content.opf"));
|
||||
String opf = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||
"<package xmlns=\"http://www.idpf.org/2007/opf\" unique-identifier=\"BookId\" version=\"2.0\">\n" +
|
||||
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">\n" +
|
||||
" <dc:title>Dummy Title</dc:title>\n" +
|
||||
" </metadata>\n" +
|
||||
"</package>";
|
||||
zos.write(opf.getBytes());
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
|
||||
private void createDummyCbz(File file) throws IOException {
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file))) {
|
||||
ZipEntry entry = new ZipEntry("dummy.txt");
|
||||
zos.putNextEntry(entry);
|
||||
zos.write("dummy content".getBytes());
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
}
|
||||
+55
-2
@@ -56,7 +56,7 @@ class CbxMetadataExtractorTest {
|
||||
" <LanguageISO>en</LanguageISO>" +
|
||||
" <Writer>Alice</Writer>" +
|
||||
" <Penciller>Bob</Penciller>" +
|
||||
" <Tags>action;adventure</Tags>" +
|
||||
" <Genre>action;adventure</Genre>" +
|
||||
"</ComicInfo>";
|
||||
|
||||
File cbz = createCbz("with_meta.cbz", new LinkedHashMap<>() {{
|
||||
@@ -94,7 +94,7 @@ class CbxMetadataExtractorTest {
|
||||
" <LanguageISO>en</LanguageISO>" +
|
||||
" <Writer>Alice</Writer>" +
|
||||
" <Penciller>Bob</Penciller>" +
|
||||
" <Tags>action;adventure</Tags>" +
|
||||
" <Genre>action;adventure</Genre>" +
|
||||
"</ComicInfo>";
|
||||
|
||||
File cbz = createCbz("with_meta.cbz", new LinkedHashMap<>() {{
|
||||
@@ -281,6 +281,59 @@ class CbxMetadataExtractorTest {
|
||||
assertEquals(10f, md.getSeriesNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractMetadata_fromCbz_withBookloreNotes_populatesFields() throws Exception {
|
||||
String xml = "<ComicInfo>" +
|
||||
" <Title>BookLore Comic</Title>" +
|
||||
" <Notes>" +
|
||||
"Some random notes here.\n" +
|
||||
"[BookLore:Subtitle] The Subtitle\n" +
|
||||
"[BookLore:Moods] Dark, Mystery\n" +
|
||||
"[BookLore:Tags] Fiction, Thriller\n" +
|
||||
"[BookLore:ISBN13] 9781234567890\n" +
|
||||
"[BookLore:AmazonRating] 4.5\n" +
|
||||
"[BookLore:GoodreadsRating] 3.8\n" +
|
||||
" </Notes>" +
|
||||
"</ComicInfo>";
|
||||
|
||||
File cbz = createCbz("booklore_notes.cbz", new LinkedHashMap<>() {{
|
||||
put("ComicInfo.xml", xml.getBytes(StandardCharsets.UTF_8));
|
||||
}});
|
||||
|
||||
BookMetadata md = extractor.extractMetadata(cbz);
|
||||
|
||||
assertEquals("BookLore Comic", md.getTitle());
|
||||
assertEquals("The Subtitle", md.getSubtitle());
|
||||
assertEquals("Some random notes here.", md.getDescription()); // Notes extracted as description if summary missing
|
||||
assertEquals("9781234567890", md.getIsbn13());
|
||||
assertEquals(4.5, md.getAmazonRating());
|
||||
assertEquals(3.8, md.getGoodreadsRating());
|
||||
assertTrue(md.getMoods().contains("Dark"));
|
||||
assertTrue(md.getMoods().contains("Mystery"));
|
||||
assertTrue(md.getTags().contains("Fiction"));
|
||||
assertTrue(md.getTags().contains("Thriller"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractMetadata_fromCbz_withWebField_populatesIds() throws Exception {
|
||||
String xml = "<ComicInfo>" +
|
||||
" <Title>Web Links Comic</Title>" +
|
||||
" <Web>https://www.goodreads.com/book/show/12345, https://www.amazon.com/dp/B001234567, https://comicvine.gamespot.com/issue/9999, https://hardcover.app/books/hc-id</Web>" +
|
||||
"</ComicInfo>";
|
||||
|
||||
File cbz = createCbz("web_links.cbz", new LinkedHashMap<>() {{
|
||||
put("ComicInfo.xml", xml.getBytes(StandardCharsets.UTF_8));
|
||||
}});
|
||||
|
||||
BookMetadata md = extractor.extractMetadata(cbz);
|
||||
|
||||
assertEquals("Web Links Comic", md.getTitle());
|
||||
assertEquals("12345", md.getGoodreadsId());
|
||||
assertEquals("B001234567", md.getAsin());
|
||||
assertEquals("9999", md.getComicvineId());
|
||||
assertEquals("hc-id", md.getHardcoverId());
|
||||
}
|
||||
|
||||
// ---------- helpers ----------
|
||||
|
||||
private File createCbz(String name, Map<String, byte[]> entries) throws IOException {
|
||||
|
||||
+8
-13
@@ -5,13 +5,10 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -22,10 +19,8 @@ import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.Color;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class ComicInfoParsingIssuesTest {
|
||||
|
||||
@@ -160,10 +155,10 @@ class ComicInfoParsingIssuesTest {
|
||||
|
||||
assertTrue(metadata.getAuthors().contains("Frank Miller"));
|
||||
|
||||
assertTrue(metadata.getCategories().contains("Marvel"));
|
||||
assertTrue(metadata.getCategories().contains("Daredevil"));
|
||||
assertTrue(metadata.getTags().contains("Marvel"));
|
||||
assertTrue(metadata.getTags().contains("Daredevil"));
|
||||
assertTrue(metadata.getCategories().contains("Superhero"));
|
||||
assertTrue(metadata.getCategories().contains("Frank Miller"));
|
||||
assertTrue(metadata.getTags().contains("Frank Miller"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+63
@@ -641,6 +641,69 @@ class EpubMetadataExtractorTest {
|
||||
return createEpubWithOpf(opfContent, "test-booklore-series-" + System.nanoTime() + ".epub");
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("BookLore Metadata Tests")
|
||||
class BookLoreMetadataTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should extract BookLore custom properties from EPUB metadata")
|
||||
void extractMetadata_withBookloreProperties_returnsExtendedMetadata() throws IOException {
|
||||
File epubFile = createEpubWithBookloreMetadata();
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(epubFile);
|
||||
|
||||
assertAll(
|
||||
() -> assertNotNull(result),
|
||||
() -> assertEquals("A Subtitle", result.getSubtitle()),
|
||||
() -> assertEquals(10, result.getSeriesTotal()),
|
||||
() -> assertEquals(4.5, result.getAmazonRating()),
|
||||
() -> assertEquals(4.0, result.getGoodreadsRating()),
|
||||
() -> assertEquals(5.0, result.getHardcoverRating()),
|
||||
() -> assertEquals(3.5, result.getLubimyczytacRating()),
|
||||
() -> assertEquals(2.0, result.getRanobedbRating()),
|
||||
() -> assertEquals("B001", result.getAsin()),
|
||||
() -> assertEquals("1001", result.getGoodreadsId()),
|
||||
() -> assertEquals("2002", result.getComicvineId()),
|
||||
() -> assertEquals("3003", result.getHardcoverId()),
|
||||
() -> assertEquals("4004", result.getRanobedbId()),
|
||||
() -> assertEquals("5005", result.getGoogleId()),
|
||||
() -> assertEquals("6006", result.getLubimyczytacId()),
|
||||
() -> assertTrue(result.getMoods().contains("Dark")),
|
||||
() -> assertTrue(result.getMoods().contains("Mystery")),
|
||||
() -> assertTrue(result.getTags().contains("Fiction")),
|
||||
() -> assertTrue(result.getTags().contains("Thriller"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private File createEpubWithBookloreMetadata() throws IOException {
|
||||
String opfContent = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<package xmlns="http://www.idpf.org/2007/opf" xmlns:booklore="http://booklore.org/metadata" version="3.0">
|
||||
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
|
||||
<dc:title>BookLore Test</dc:title>
|
||||
<meta property="booklore:subtitle">A Subtitle</meta>
|
||||
<meta property="booklore:series_total">10</meta>
|
||||
<meta property="booklore:amazon_rating">4.5</meta>
|
||||
<meta property="booklore:goodreads_rating">4.0</meta>
|
||||
<meta property="booklore:hardcover_rating">5.0</meta>
|
||||
<meta property="booklore:lubimyczytac_rating">3.5</meta>
|
||||
<meta property="booklore:ranobedb_rating">2.0</meta>
|
||||
<meta property="booklore:asin">B001</meta>
|
||||
<meta property="booklore:goodreads_id">1001</meta>
|
||||
<meta property="booklore:comicvine_id">2002</meta>
|
||||
<meta property="booklore:hardcover_id">3003</meta>
|
||||
<meta property="booklore:ranobedb_id">4004</meta>
|
||||
<meta property="booklore:google_books_id">5005</meta>
|
||||
<meta property="booklore:lubimyczytac_id">6006</meta>
|
||||
<meta property="booklore:moods">Dark, Mystery</meta>
|
||||
<meta property="booklore:tags">Fiction, Thriller</meta>
|
||||
</metadata>
|
||||
</package>
|
||||
""";
|
||||
return createEpubWithOpf(opfContent, "test-booklore-" + System.nanoTime() + ".epub");
|
||||
}
|
||||
|
||||
private File createEpubWithIsbn(String isbn13, String isbn10) throws IOException {
|
||||
StringBuilder identifiers = new StringBuilder();
|
||||
if (isbn13 != null) {
|
||||
|
||||
+9
-10
@@ -71,9 +71,9 @@ class ExtractFromComicInfoXmlTest {
|
||||
// Verify categories
|
||||
assertTrue(metadata.getCategories().contains("Superhero"));
|
||||
assertTrue(metadata.getCategories().contains("Crime"));
|
||||
assertTrue(metadata.getCategories().contains("Batman"));
|
||||
assertTrue(metadata.getCategories().contains("DC"));
|
||||
assertTrue(metadata.getCategories().contains("Frank Miller"));
|
||||
assertTrue(metadata.getTags().contains("Batman"));
|
||||
assertTrue(metadata.getTags().contains("DC"));
|
||||
assertTrue(metadata.getTags().contains("Frank Miller"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -336,7 +336,6 @@ class ExtractFromComicInfoXmlTest {
|
||||
assertTrue(metadata.getAuthors().contains("Jane Smith"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExtractFromComicInfoXml_MultipleCategories() throws IOException {
|
||||
String xml = """
|
||||
<ComicInfo>
|
||||
@@ -353,9 +352,9 @@ class ExtractFromComicInfoXmlTest {
|
||||
assertTrue(metadata.getCategories().contains("Action"));
|
||||
assertTrue(metadata.getCategories().contains("Adventure"));
|
||||
assertTrue(metadata.getCategories().contains("Sci-Fi"));
|
||||
assertTrue(metadata.getCategories().contains("robots"));
|
||||
assertTrue(metadata.getCategories().contains("space"));
|
||||
assertTrue(metadata.getCategories().contains("future"));
|
||||
assertTrue(metadata.getTags().contains("robots"));
|
||||
assertTrue(metadata.getTags().contains("space"));
|
||||
assertTrue(metadata.getTags().contains("future"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -736,9 +735,9 @@ class ExtractFromComicInfoXmlTest {
|
||||
// Verify categories
|
||||
assertTrue(metadata.getCategories().contains("Superhero"));
|
||||
assertTrue(metadata.getCategories().contains("Crime"));
|
||||
assertTrue(metadata.getCategories().contains("Batman"));
|
||||
assertTrue(metadata.getCategories().contains("DC"));
|
||||
assertTrue(metadata.getCategories().contains("Frank Miller"));
|
||||
assertTrue(metadata.getTags().contains("Batman"));
|
||||
assertTrue(metadata.getTags().contains("DC"));
|
||||
assertTrue(metadata.getTags().contains("Frank Miller"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+171
@@ -0,0 +1,171 @@
|
||||
package org.booklore.service.metadata.writer;
|
||||
|
||||
import org.apache.xmpbox.XMPMetadata;
|
||||
import org.apache.xmpbox.type.TextType;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BookLoreSchemaTest {
|
||||
|
||||
private XMPMetadata xmpMetadata;
|
||||
private BookLoreSchema schema;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
xmpMetadata = XMPMetadata.createXMPMetadata();
|
||||
schema = new BookLoreSchema(xmpMetadata);
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ScalarTextPropertiesTests {
|
||||
@Test
|
||||
void setSubtitle_addsPropertyToSchema() {
|
||||
schema.setSubtitle("A Novel");
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.SUBTITLE);
|
||||
assertNotNull(property);
|
||||
assertEquals("A Novel", property.getStringValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void setSubtitle_whenBlank_doesNotAddProperty() {
|
||||
schema.setSubtitle(" ");
|
||||
|
||||
assertNull(schema.getProperty(BookLoreSchema.SUBTITLE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void setIsbn10_addsPropertyToSchema() {
|
||||
schema.setIsbn10("0123456789");
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.ISBN_10);
|
||||
assertNotNull(property);
|
||||
assertEquals("0123456789", property.getStringValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void setLubimyczytacId_addsPropertyToSchema() {
|
||||
schema.setLubimyczytacId("12345");
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.LUBIMYCZYTAC_ID);
|
||||
assertNotNull(property);
|
||||
assertEquals("12345", property.getStringValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void setHardcoverBookId_addsPropertyToSchema() {
|
||||
schema.setHardcoverBookId(98765);
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.HARDCOVER_BOOK_ID);
|
||||
assertNotNull(property);
|
||||
assertEquals("98765", property.getStringValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class RatingPropertiesTests {
|
||||
@Test
|
||||
void setRating_formatsWithLocaleUS() {
|
||||
schema.setRating(8.5);
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.RATING);
|
||||
assertNotNull(property);
|
||||
assertEquals("8.50", property.getStringValue());
|
||||
assertFalse(property.getStringValue().contains(","));
|
||||
}
|
||||
|
||||
@Test
|
||||
void setAmazonRating_formatsCorrectly() {
|
||||
schema.setAmazonRating(4.25);
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.AMAZON_RATING);
|
||||
assertNotNull(property);
|
||||
assertEquals("4.25", property.getStringValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void setGoodreadsRating_formatsCorrectly() {
|
||||
schema.setGoodreadsRating(3.99);
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.GOODREADS_RATING);
|
||||
assertNotNull(property);
|
||||
assertEquals("3.99", property.getStringValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void setRating_whenNull_doesNotAddProperty() {
|
||||
schema.setRating(null);
|
||||
|
||||
assertNull(schema.getProperty(BookLoreSchema.RATING));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class TextCollectionPropertiesTests {
|
||||
@Test
|
||||
void setMoods_createsDelimitedText() {
|
||||
schema.setMoods(Set.of("Dark", "Atmospheric", "Suspenseful"));
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.MOODS);
|
||||
assertNotNull(property);
|
||||
String value = property.getStringValue();
|
||||
assertTrue(value.contains("Dark"));
|
||||
assertTrue(value.contains("Atmospheric"));
|
||||
assertTrue(value.contains("Suspenseful"));
|
||||
assertTrue(value.contains("; "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void setMoods_whenEmpty_doesNotAddProperty() {
|
||||
schema.setMoods(Set.of());
|
||||
|
||||
assertNull(schema.getProperty(BookLoreSchema.MOODS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void setMoods_whenNull_doesNotAddProperty() {
|
||||
schema.setMoods(null);
|
||||
|
||||
assertNull(schema.getProperty(BookLoreSchema.MOODS));
|
||||
}
|
||||
|
||||
@Test
|
||||
void setTags_createsDelimitedText() {
|
||||
schema.setTags(Set.of("Fantasy", "Epic", "Magic"));
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.TAGS);
|
||||
assertNotNull(property);
|
||||
String value = property.getStringValue();
|
||||
assertTrue(value.contains("Fantasy"));
|
||||
assertTrue(value.contains("Epic"));
|
||||
assertTrue(value.contains("Magic"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void setTags_filtersBlankValues() {
|
||||
schema.setTags(Set.of("Fantasy", " ", "Epic"));
|
||||
|
||||
TextType property = (TextType) schema.getProperty(BookLoreSchema.TAGS);
|
||||
assertNotNull(property);
|
||||
assertFalse(property.getStringValue().contains(" "));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class NamespaceTests {
|
||||
@Test
|
||||
void namespace_isCorrect() {
|
||||
assertEquals("http://booklore.org/metadata/1.0/", BookLoreSchema.NAMESPACE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void prefix_isCorrect() {
|
||||
assertEquals("booklore", BookLoreSchema.PREFIX);
|
||||
}
|
||||
}
|
||||
}
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
package org.booklore.service.metadata.writer;
|
||||
|
||||
import org.booklore.model.MetadataClearFlags;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.dto.settings.MetadataPersistenceSettings;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.entity.MoodEntity;
|
||||
import org.booklore.model.entity.TagEntity;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class CbxComicInfoComplianceTest {
|
||||
|
||||
private CbxMetadataWriter writer;
|
||||
private Path tempDir;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
AppSettingService appSettingService = Mockito.mock(AppSettingService.class);
|
||||
AppSettings settings = new AppSettings();
|
||||
MetadataPersistenceSettings persistence = new MetadataPersistenceSettings();
|
||||
MetadataPersistenceSettings.SaveToOriginalFile save = new MetadataPersistenceSettings.SaveToOriginalFile();
|
||||
MetadataPersistenceSettings.FormatSettings cbx = new MetadataPersistenceSettings.FormatSettings();
|
||||
cbx.setEnabled(true);
|
||||
cbx.setMaxFileSizeInMb(100);
|
||||
save.setCbx(cbx);
|
||||
persistence.setSaveToOriginalFile(save);
|
||||
settings.setMetadataPersistenceSettings(persistence);
|
||||
Mockito.when(appSettingService.getAppSettings()).thenReturn(settings);
|
||||
|
||||
writer = new CbxMetadataWriter(appSettingService);
|
||||
tempDir = Files.createTempDirectory("compliance_test_");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() throws Exception {
|
||||
if (tempDir != null) {
|
||||
Files.walk(tempDir)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.forEach(p -> {
|
||||
try {
|
||||
Files.deleteIfExists(p);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testComicInfoCompliance_OrderingAndFormatting() throws Exception {
|
||||
File cbz = createDummyCbz("compliance.cbz");
|
||||
|
||||
BookMetadataEntity metadata = new BookMetadataEntity();
|
||||
metadata.setTitle("The Boys #70 - The Bloody Doors Off, Part 5");
|
||||
metadata.setSeriesName("The Boys");
|
||||
metadata.setSeriesNumber(70f);
|
||||
metadata.setSeriesTotal(14);
|
||||
metadata.setPublishedDate(LocalDate.of(2012, 9, 5));
|
||||
metadata.setPageCount(170);
|
||||
metadata.setDescription("<p><i>On his own and out of options, Hughie resorts to extreme measures...</i></p>");
|
||||
|
||||
Set<TagEntity> tags = new HashSet<>();
|
||||
TagEntity t1 = new TagEntity(); t1.setName("Superhero");
|
||||
tags.add(t1);
|
||||
metadata.setTags(tags);
|
||||
|
||||
Set<MoodEntity> moods = new HashSet<>();
|
||||
MoodEntity m1 = new MoodEntity(); m1.setName("Dark");
|
||||
moods.add(m1);
|
||||
metadata.setMoods(moods);
|
||||
|
||||
metadata.setHardcoverBookId("547027");
|
||||
metadata.setIsbn13("9781606903735");
|
||||
metadata.setIsbn10("160690373X");
|
||||
metadata.setHardcoverRating(4.0d);
|
||||
|
||||
writer.saveMetadataToFile(cbz, metadata, null, new MetadataClearFlags());
|
||||
|
||||
String xmlContent = readComicInfoFromCbz(cbz);
|
||||
System.out.println("Generated XML:\n" + xmlContent);
|
||||
|
||||
// 1. Verify Element Ordering (Crucial for XSD v2.0)
|
||||
// Title -> Series -> Number -> Count ... -> Summary -> Notes ... -> Web ...
|
||||
assertOrder(xmlContent, "Title", "Series");
|
||||
assertOrder(xmlContent, "Series", "Number");
|
||||
assertOrder(xmlContent, "Number", "Count");
|
||||
assertOrder(xmlContent, "Count", "Summary");
|
||||
assertOrder(xmlContent, "Summary", "Notes");
|
||||
assertOrder(xmlContent, "Notes", "Year");
|
||||
assertOrder(xmlContent, "Year", "Month");
|
||||
assertOrder(xmlContent, "Month", "Day");
|
||||
assertOrder(xmlContent, "Day", "Genre"); // Note: Genre is where we put categories usually, need to check if we set it.
|
||||
// We didn't set categories in this test, so Genre might be missing.
|
||||
assertOrder(xmlContent, "Day", "Web"); // Web comes later
|
||||
assertOrder(xmlContent, "Web", "PageCount");
|
||||
|
||||
// 2. Verify Formatting
|
||||
assertFalse(xmlContent.matches("(?s).*\\n\\s*\\n\\s*<.*"), "Should not have empty lines between elements");
|
||||
|
||||
// 3. Verify HTML Stripping in Summary
|
||||
assertTrue(xmlContent.contains("<Summary>On his own and out of options, Hughie resorts to extreme measures...</Summary>"));
|
||||
assertFalse(xmlContent.contains("<p>"), "HTML tags should be removed from Summary");
|
||||
|
||||
// 4. Verify Single Web URL
|
||||
assertTrue(xmlContent.contains("<Web>https://hardcover.app/books/547027</Web>"));
|
||||
// assertFalse(xmlContent.contains(","), "Web field should not be comma separated"); // Removed as other fields may contain commas
|
||||
|
||||
// 5. Verify Notes Format
|
||||
assertTrue(xmlContent.contains("[BookLore:Moods] Dark"), "Notes should contain formatted custom metadata");
|
||||
|
||||
|
||||
// 6. Verify GTIN (v2.1)
|
||||
assertTrue(xmlContent.contains("<GTIN>9781606903735</GTIN>"));
|
||||
}
|
||||
|
||||
private void assertOrder(String content, String tag1, String tag2) {
|
||||
int idx1 = content.indexOf("<" + tag1 + ">");
|
||||
int idx2 = content.indexOf("<" + tag2 + ">");
|
||||
|
||||
// Skip if one of the tags is missing from output (optional fields)
|
||||
if (idx1 == -1 || idx2 == -1) return;
|
||||
|
||||
assertTrue(idx1 < idx2, "Tag <" + tag1 + "> (pos " + idx1 + ") should appear before <" + tag2 + "> (pos " + idx2 + ")");
|
||||
}
|
||||
|
||||
private File createDummyCbz(String name) throws Exception {
|
||||
File f = tempDir.resolve(name).toFile();
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(f))) {
|
||||
ZipEntry ze = new ZipEntry("test.jpg");
|
||||
zos.putNextEntry(ze);
|
||||
zos.write(new byte[]{0});
|
||||
zos.closeEntry();
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
private String readComicInfoFromCbz(File cbz) throws Exception {
|
||||
try (ZipFile zip = new ZipFile(cbz)) {
|
||||
ZipEntry entry = zip.getEntry("ComicInfo.xml");
|
||||
try (var is = zip.getInputStream(entry)) {
|
||||
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+187
-3
@@ -3,10 +3,9 @@ package org.booklore.service.metadata.writer;
|
||||
import org.booklore.model.MetadataClearFlags;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.dto.settings.MetadataPersistenceSettings;
|
||||
import org.booklore.model.entity.AuthorEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.entity.CategoryEntity;
|
||||
import org.booklore.model.entity.*;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.model.enums.ComicCreatorRole;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -164,6 +163,68 @@ class CbxMetadataWriterTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveMetadataToFile_cbz_writesTagsRatingAndWebField() throws Exception {
|
||||
File cbz = createCbz(tempDir.resolve("tags_rating.cbz"), new String[]{"page1.jpg"});
|
||||
|
||||
BookMetadataEntity meta = new BookMetadataEntity();
|
||||
meta.setTitle("Rating Test");
|
||||
meta.setRating(8.4);
|
||||
meta.setGoodreadsId("12345");
|
||||
meta.setAsin("B00TEST123");
|
||||
|
||||
Set<TagEntity> tags = new HashSet<>();
|
||||
TagEntity tag1 = new TagEntity();
|
||||
tag1.setId(1L);
|
||||
tag1.setName("Fantasy");
|
||||
TagEntity tag2 = new TagEntity();
|
||||
tag2.setId(2L);
|
||||
tag2.setName("Epic");
|
||||
tags.add(tag1);
|
||||
tags.add(tag2);
|
||||
meta.setTags(tags);
|
||||
|
||||
Set<MoodEntity> moods = new HashSet<>();
|
||||
MoodEntity mood = new MoodEntity();
|
||||
mood.setId(1L);
|
||||
mood.setName("Dark");
|
||||
moods.add(mood);
|
||||
meta.setMoods(moods);
|
||||
|
||||
writer.saveMetadataToFile(cbz, meta, null, new MetadataClearFlags());
|
||||
|
||||
try (ZipFile zip = new ZipFile(cbz)) {
|
||||
ZipEntry ci = zip.getEntry("ComicInfo.xml");
|
||||
assertNotNull(ci);
|
||||
Document doc = parseXml(zip.getInputStream(ci));
|
||||
|
||||
String notesVal = text(doc, "Notes");
|
||||
assertNotNull(notesVal);
|
||||
assertTrue(notesVal.contains("[BookLore:Tags]"));
|
||||
assertTrue(notesVal.contains("Fantasy"));
|
||||
assertTrue(notesVal.contains("Epic"));
|
||||
|
||||
// Tags now written as dedicated element per Anansi v2.1
|
||||
String tagsVal = text(doc, "Tags");
|
||||
assertNotNull(tagsVal, "Tags should be written as standalone element per Anansi v2.1");
|
||||
assertTrue(tagsVal.contains("Fantasy") || tagsVal.contains("Epic"), "Tags should contain Fantasy or Epic");
|
||||
|
||||
String rating = text(doc, "CommunityRating");
|
||||
assertNotNull(rating);
|
||||
assertEquals("4.2", rating);
|
||||
|
||||
String web = text(doc, "Web");
|
||||
assertNotNull(web);
|
||||
assertTrue(web.contains("goodreads.com"));
|
||||
// assertTrue(web.contains("amazon.com")); // Only primary URL is stored in Web field now
|
||||
|
||||
String notes = text(doc, "Notes");
|
||||
assertNotNull(notes);
|
||||
assertTrue(notes.contains("[BookLore:Moods]"));
|
||||
assertTrue(notes.contains("Dark"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveMetadataToFile_cbz_updatesExistingComicInfo() throws Exception {
|
||||
// Create a CBZ *with* an existing ComicInfo.xml
|
||||
@@ -212,6 +273,129 @@ class CbxMetadataWriterTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveMetadataToFile_cbz_writesComicSpecificMetadata() throws Exception {
|
||||
File cbz = createCbz(tempDir.resolve("comic_meta.cbz"), new String[]{"page1.jpg"});
|
||||
|
||||
// Create metadata with ComicMetadataEntity
|
||||
BookMetadataEntity meta = new BookMetadataEntity();
|
||||
meta.setTitle("Spider-Man #1");
|
||||
meta.setSeriesName("Spider-Man");
|
||||
meta.setSeriesNumber(1.0f);
|
||||
meta.setAgeRating(13); // Teen rating
|
||||
|
||||
// Create ComicMetadataEntity with all comic-specific fields
|
||||
ComicMetadataEntity comic = ComicMetadataEntity.builder()
|
||||
.volumeNumber(2023)
|
||||
.alternateSeries("Amazing Spider-Man")
|
||||
.alternateIssue("700.1")
|
||||
.storyArc("Superior")
|
||||
.format("Single Issue")
|
||||
.imprint("Marvel Knights")
|
||||
.blackAndWhite(false)
|
||||
.manga(true)
|
||||
.readingDirection("RTL")
|
||||
.build();
|
||||
|
||||
// Characters
|
||||
ComicCharacterEntity char1 = new ComicCharacterEntity();
|
||||
char1.setId(1L);
|
||||
char1.setName("Peter Parker");
|
||||
ComicCharacterEntity char2 = new ComicCharacterEntity();
|
||||
char2.setId(2L);
|
||||
char2.setName("Mary Jane");
|
||||
Set<ComicCharacterEntity> characters = new HashSet<>();
|
||||
characters.add(char1);
|
||||
characters.add(char2);
|
||||
comic.setCharacters(characters);
|
||||
|
||||
// Teams
|
||||
ComicTeamEntity team1 = new ComicTeamEntity();
|
||||
team1.setId(1L);
|
||||
team1.setName("Avengers");
|
||||
Set<ComicTeamEntity> teams = new HashSet<>();
|
||||
teams.add(team1);
|
||||
comic.setTeams(teams);
|
||||
|
||||
// Locations
|
||||
ComicLocationEntity loc1 = new ComicLocationEntity();
|
||||
loc1.setId(1L);
|
||||
loc1.setName("New York City");
|
||||
Set<ComicLocationEntity> locations = new HashSet<>();
|
||||
locations.add(loc1);
|
||||
comic.setLocations(locations);
|
||||
|
||||
// Creators
|
||||
ComicCreatorEntity penciller = new ComicCreatorEntity();
|
||||
penciller.setId(1L);
|
||||
penciller.setName("John Romita Jr.");
|
||||
ComicCreatorEntity inker = new ComicCreatorEntity();
|
||||
inker.setId(2L);
|
||||
inker.setName("Klaus Janson");
|
||||
ComicCreatorMappingEntity pencillerMapping = ComicCreatorMappingEntity.builder()
|
||||
.creator(penciller)
|
||||
.role(ComicCreatorRole.PENCILLER)
|
||||
.comicMetadata(comic)
|
||||
.build();
|
||||
ComicCreatorMappingEntity inkerMapping = ComicCreatorMappingEntity.builder()
|
||||
.creator(inker)
|
||||
.role(ComicCreatorRole.INKER)
|
||||
.comicMetadata(comic)
|
||||
.build();
|
||||
Set<ComicCreatorMappingEntity> creatorMappings = new HashSet<>();
|
||||
creatorMappings.add(pencillerMapping);
|
||||
creatorMappings.add(inkerMapping);
|
||||
comic.setCreatorMappings(creatorMappings);
|
||||
|
||||
meta.setComicMetadata(comic);
|
||||
|
||||
writer.saveMetadataToFile(cbz, meta, null, new MetadataClearFlags());
|
||||
|
||||
try (ZipFile zip = new ZipFile(cbz)) {
|
||||
ZipEntry ci = zip.getEntry("ComicInfo.xml");
|
||||
assertNotNull(ci, "ComicInfo.xml should be present");
|
||||
Document doc = parseXml(zip.getInputStream(ci));
|
||||
|
||||
// Basic metadata
|
||||
assertEquals("Spider-Man #1", text(doc, "Title"));
|
||||
assertEquals("Spider-Man", text(doc, "Series"));
|
||||
|
||||
// Comic-specific fields
|
||||
assertEquals("2023", text(doc, "Volume"));
|
||||
assertEquals("Amazing Spider-Man", text(doc, "AlternateSeries"));
|
||||
assertEquals("700.1", text(doc, "AlternateNumber"));
|
||||
assertEquals("Superior", text(doc, "StoryArc"));
|
||||
assertEquals("Single Issue", text(doc, "Format"));
|
||||
assertEquals("Marvel Knights", text(doc, "Imprint"));
|
||||
assertEquals("No", text(doc, "BlackAndWhite"));
|
||||
assertEquals("YesAndRightToLeft", text(doc, "Manga"));
|
||||
assertEquals("Teen", text(doc, "AgeRating"));
|
||||
|
||||
// Characters, Teams, Locations
|
||||
String characters_str = text(doc, "Characters");
|
||||
assertNotNull(characters_str);
|
||||
assertTrue(characters_str.contains("Peter Parker"));
|
||||
assertTrue(characters_str.contains("Mary Jane"));
|
||||
|
||||
String teams_str = text(doc, "Teams");
|
||||
assertNotNull(teams_str);
|
||||
assertTrue(teams_str.contains("Avengers"));
|
||||
|
||||
String locations_str = text(doc, "Locations");
|
||||
assertNotNull(locations_str);
|
||||
assertTrue(locations_str.contains("New York City"));
|
||||
|
||||
// Creators
|
||||
String penciller_str = text(doc, "Penciller");
|
||||
assertNotNull(penciller_str);
|
||||
assertTrue(penciller_str.contains("John Romita Jr."));
|
||||
|
||||
String inker_str = text(doc, "Inker");
|
||||
assertNotNull(inker_str);
|
||||
assertTrue(inker_str.contains("Klaus Janson"));
|
||||
}
|
||||
}
|
||||
|
||||
// ------------- helpers -------------
|
||||
|
||||
private static File createCbz(Path path, String[] imageNames) throws Exception {
|
||||
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
package org.booklore.service.metadata.writer;
|
||||
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.entity.MoodEntity;
|
||||
import org.booklore.model.entity.TagEntity;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MetadataCopyHelperTest {
|
||||
|
||||
private BookMetadataEntity metadata;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
metadata = new BookMetadataEntity();
|
||||
}
|
||||
|
||||
@Nested
|
||||
class MoodsTests {
|
||||
@Test
|
||||
void copyMoods_whenNotLocked_callsConsumer() {
|
||||
MoodEntity mood1 = new MoodEntity();
|
||||
mood1.setName("Dark");
|
||||
MoodEntity mood2 = new MoodEntity();
|
||||
mood2.setName("Atmospheric");
|
||||
metadata.setMoods(Set.of(mood1, mood2));
|
||||
metadata.setMoodsLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Set<String>> result = new AtomicReference<>();
|
||||
|
||||
helper.copyMoods(false, result::set);
|
||||
|
||||
assertNotNull(result.get());
|
||||
assertEquals(2, result.get().size());
|
||||
assertTrue(result.get().contains("Dark"));
|
||||
assertTrue(result.get().contains("Atmospheric"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyMoods_whenLocked_doesNotCallConsumer() {
|
||||
MoodEntity mood = new MoodEntity();
|
||||
mood.setName("Dark");
|
||||
metadata.setMoods(Set.of(mood));
|
||||
metadata.setMoodsLocked(true);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Set<String>> result = new AtomicReference<>();
|
||||
|
||||
helper.copyMoods(false, result::set);
|
||||
|
||||
assertNull(result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyMoods_whenClear_passesEmptySet() {
|
||||
MoodEntity mood = new MoodEntity();
|
||||
mood.setName("Dark");
|
||||
metadata.setMoods(Set.of(mood));
|
||||
metadata.setMoodsLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Set<String>> result = new AtomicReference<>();
|
||||
|
||||
helper.copyMoods(true, result::set);
|
||||
|
||||
assertNotNull(result.get());
|
||||
assertTrue(result.get().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyMoods_filtersBlankNames() {
|
||||
MoodEntity mood1 = new MoodEntity();
|
||||
mood1.setName("Dark");
|
||||
MoodEntity mood2 = new MoodEntity();
|
||||
mood2.setName(" ");
|
||||
MoodEntity mood3 = new MoodEntity();
|
||||
mood3.setName(null);
|
||||
metadata.setMoods(Set.of(mood1, mood2, mood3));
|
||||
metadata.setMoodsLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Set<String>> result = new AtomicReference<>();
|
||||
|
||||
helper.copyMoods(false, result::set);
|
||||
|
||||
assertEquals(1, result.get().size());
|
||||
assertTrue(result.get().contains("Dark"));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class TagsTests {
|
||||
@Test
|
||||
void copyTags_whenNotLocked_callsConsumer() {
|
||||
TagEntity tag1 = new TagEntity();
|
||||
tag1.setName("Fantasy");
|
||||
TagEntity tag2 = new TagEntity();
|
||||
tag2.setName("Epic");
|
||||
metadata.setTags(Set.of(tag1, tag2));
|
||||
metadata.setTagsLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Set<String>> result = new AtomicReference<>();
|
||||
|
||||
helper.copyTags(false, result::set);
|
||||
|
||||
assertNotNull(result.get());
|
||||
assertEquals(2, result.get().size());
|
||||
assertTrue(result.get().contains("Fantasy"));
|
||||
assertTrue(result.get().contains("Epic"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyTags_whenLocked_doesNotCallConsumer() {
|
||||
TagEntity tag = new TagEntity();
|
||||
tag.setName("Fantasy");
|
||||
metadata.setTags(Set.of(tag));
|
||||
metadata.setTagsLocked(true);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Set<String>> result = new AtomicReference<>();
|
||||
|
||||
helper.copyTags(false, result::set);
|
||||
|
||||
assertNull(result.get());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class RatingTests {
|
||||
@Test
|
||||
void copyRating_whenPresent_callsConsumer() {
|
||||
metadata.setRating(8.5);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Double> result = new AtomicReference<>();
|
||||
|
||||
helper.copyRating(false, result::set);
|
||||
|
||||
assertEquals(8.5, result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyRating_whenClear_passesNull() {
|
||||
metadata.setRating(8.5);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Double> result = new AtomicReference<>(99.0);
|
||||
|
||||
helper.copyRating(true, result::set);
|
||||
|
||||
assertNull(result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAmazonRating_whenNotLocked_callsConsumer() {
|
||||
metadata.setAmazonRating(4.5);
|
||||
metadata.setAmazonRatingLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Double> result = new AtomicReference<>();
|
||||
|
||||
helper.copyAmazonRating(false, result::set);
|
||||
|
||||
assertEquals(4.5, result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAmazonRating_whenLocked_doesNotCallConsumer() {
|
||||
metadata.setAmazonRating(4.5);
|
||||
metadata.setAmazonRatingLocked(true);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Double> result = new AtomicReference<>();
|
||||
|
||||
helper.copyAmazonRating(false, result::set);
|
||||
|
||||
assertNull(result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyGoodreadsRating_whenNotLocked_callsConsumer() {
|
||||
metadata.setGoodreadsRating(4.2);
|
||||
metadata.setGoodreadsRatingLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Double> result = new AtomicReference<>();
|
||||
|
||||
helper.copyGoodreadsRating(false, result::set);
|
||||
|
||||
assertEquals(4.2, result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyHardcoverRating_whenNotLocked_callsConsumer() {
|
||||
metadata.setHardcoverRating(3.8);
|
||||
metadata.setHardcoverRatingLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Double> result = new AtomicReference<>();
|
||||
|
||||
helper.copyHardcoverRating(false, result::set);
|
||||
|
||||
assertEquals(3.8, result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyLubimyczytacRating_whenNotLocked_callsConsumer() {
|
||||
metadata.setLubimyczytacRating(7.5);
|
||||
metadata.setLubimyczytacRatingLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Double> result = new AtomicReference<>();
|
||||
|
||||
helper.copyLubimyczytacRating(false, result::set);
|
||||
|
||||
assertEquals(7.5, result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyRanobedbRating_whenNotLocked_callsConsumer() {
|
||||
metadata.setRanobedbRating(8.0);
|
||||
metadata.setRanobedbRatingLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<Double> result = new AtomicReference<>();
|
||||
|
||||
helper.copyRanobedbRating(false, result::set);
|
||||
|
||||
assertEquals(8.0, result.get());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class IdentifierTests {
|
||||
@Test
|
||||
void copyLubimyczytacId_whenNotLocked_callsConsumer() {
|
||||
metadata.setLubimyczytacId("12345");
|
||||
metadata.setLubimyczytacIdLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<String> result = new AtomicReference<>();
|
||||
|
||||
helper.copyLubimyczytacId(false, result::set);
|
||||
|
||||
assertEquals("12345", result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyLubimyczytacId_whenLocked_doesNotCallConsumer() {
|
||||
metadata.setLubimyczytacId("12345");
|
||||
metadata.setLubimyczytacIdLocked(true);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<String> result = new AtomicReference<>();
|
||||
|
||||
helper.copyLubimyczytacId(false, result::set);
|
||||
|
||||
assertNull(result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyHardcoverBookId_whenNotLocked_callsConsumer() {
|
||||
metadata.setHardcoverBookId("98765");
|
||||
metadata.setHardcoverBookIdLocked(false);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<String> result = new AtomicReference<>();
|
||||
|
||||
helper.copyHardcoverBookId(false, result::set);
|
||||
|
||||
assertEquals("98765", result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyHardcoverBookId_whenLocked_doesNotCallConsumer() {
|
||||
metadata.setHardcoverBookId("98765");
|
||||
metadata.setHardcoverBookIdLocked(true);
|
||||
|
||||
MetadataCopyHelper helper = new MetadataCopyHelper(metadata);
|
||||
AtomicReference<String> result = new AtomicReference<>();
|
||||
|
||||
helper.copyHardcoverBookId(false, result::set);
|
||||
|
||||
assertNull(result.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
+398
@@ -0,0 +1,398 @@
|
||||
package org.booklore.service.metadata.writer;
|
||||
|
||||
import org.apache.pdfbox.Loader;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.common.PDMetadata;
|
||||
import org.booklore.model.dto.BookMetadata;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.dto.settings.MetadataPersistenceSettings;
|
||||
import org.booklore.model.entity.AuthorEntity;
|
||||
import org.booklore.model.entity.BookMetadataEntity;
|
||||
import org.booklore.model.entity.MoodEntity;
|
||||
import org.booklore.model.entity.TagEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.booklore.service.metadata.extractor.PdfMetadataExtractor;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PdfMetadataWriterTest {
|
||||
|
||||
private PdfMetadataWriter writer;
|
||||
private PdfMetadataExtractor extractor;
|
||||
private Path tempDir;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
AppSettingService appSettingService = Mockito.mock(AppSettingService.class);
|
||||
AppSettings settings = new AppSettings();
|
||||
MetadataPersistenceSettings persistence = new MetadataPersistenceSettings();
|
||||
MetadataPersistenceSettings.SaveToOriginalFile save = new MetadataPersistenceSettings.SaveToOriginalFile();
|
||||
MetadataPersistenceSettings.FormatSettings pdfSettings = new MetadataPersistenceSettings.FormatSettings();
|
||||
pdfSettings.setEnabled(true);
|
||||
pdfSettings.setMaxFileSizeInMb(100);
|
||||
save.setPdf(pdfSettings);
|
||||
persistence.setSaveToOriginalFile(save);
|
||||
settings.setMetadataPersistenceSettings(persistence);
|
||||
Mockito.when(appSettingService.getAppSettings()).thenReturn(settings);
|
||||
|
||||
writer = new PdfMetadataWriter(appSettingService);
|
||||
extractor = new PdfMetadataExtractor();
|
||||
tempDir = Files.createTempDirectory("pdf_writer_test_");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() throws Exception {
|
||||
if (tempDir != null) {
|
||||
Files.walk(tempDir)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.forEach(p -> {
|
||||
try {
|
||||
Files.deleteIfExists(p);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getSupportedBookType_isPdf() {
|
||||
assertEquals(BookFileType.PDF, writer.getSupportedBookType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_basicFields_roundTrip() throws Exception {
|
||||
File pdf = createEmptyPdf("basic.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setTitle("Dead Simple Python");
|
||||
meta.setDescription("A Python programming guide");
|
||||
meta.setPublisher("No Starch Press");
|
||||
meta.setPublishedDate(LocalDate.of(2022, 11, 22));
|
||||
meta.setLanguage("en");
|
||||
meta.setPageCount(754);
|
||||
|
||||
Set<AuthorEntity> authors = new HashSet<>();
|
||||
AuthorEntity author = new AuthorEntity();
|
||||
author.setName("Jason C McDonald");
|
||||
authors.add(author);
|
||||
meta.setAuthors(authors);
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertEquals("Dead Simple Python", result.getTitle());
|
||||
assertEquals("A Python programming guide", result.getDescription());
|
||||
assertEquals("No Starch Press", result.getPublisher());
|
||||
assertEquals("en", result.getLanguage());
|
||||
assertTrue(result.getAuthors().contains("Jason C McDonald"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_validSeries_isWritten() throws Exception {
|
||||
File pdf = createEmptyPdf("series-valid.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setTitle("The Walking Dead #22");
|
||||
meta.setSeriesName("The Walking Dead");
|
||||
meta.setSeriesNumber(22f);
|
||||
meta.setSeriesTotal(193);
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertEquals("The Walking Dead", result.getSeriesName());
|
||||
assertEquals(22f, result.getSeriesNumber());
|
||||
assertEquals(193, result.getSeriesTotal());
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_seriesNameOnly_notWritten() throws Exception {
|
||||
File pdf = createEmptyPdf("series-name-only.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setTitle("Programming Book");
|
||||
meta.setSeriesName("Programming"); // Name without number - BROKEN DATA
|
||||
meta.setSeriesNumber(null);
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
// Series should NOT be written since it's incomplete
|
||||
assertNull(result.getSeriesName(), "Series name should not be written without valid number");
|
||||
assertNull(result.getSeriesNumber(), "Series number should be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_seriesNumberZero_notWritten() throws Exception {
|
||||
File pdf = createEmptyPdf("series-zero.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setTitle("Test Book");
|
||||
meta.setSeriesName("Test Series");
|
||||
meta.setSeriesNumber(0f); // Zero is invalid
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
// Series should NOT be written since number is zero
|
||||
assertNull(result.getSeriesName());
|
||||
assertNull(result.getSeriesNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_seriesNumberFormattedNicely() throws Exception {
|
||||
File pdf = createEmptyPdf("series-format.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setSeriesName("Test Series");
|
||||
meta.setSeriesNumber(22f); // Whole number
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
// Read raw XMP to verify formatting
|
||||
String xmpContent = readXmpContent(pdf);
|
||||
assertTrue(xmpContent.contains("<booklore:seriesNumber>22</booklore:seriesNumber>"),
|
||||
"Series number should be formatted as '22' not '22.00'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_seriesDecimalNumber_preserved() throws Exception {
|
||||
File pdf = createEmptyPdf("series-decimal.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setSeriesName("Test Series");
|
||||
meta.setSeriesNumber(1.5f);
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertEquals(1.5f, result.getSeriesNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_goodreadsIdNormalized() throws Exception {
|
||||
File pdf = createEmptyPdf("goodreads-id.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setGoodreadsId("52555538-dead-simple-python"); // Full slug format
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertEquals("52555538", result.getGoodreadsId(),
|
||||
"Goodreads ID should be normalized to just the numeric part");
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_goodreadsIdAlreadyNumeric_unchanged() throws Exception {
|
||||
File pdf = createEmptyPdf("goodreads-numeric.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setGoodreadsId("12345678");
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertEquals("12345678", result.getGoodreadsId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_externalIds_roundTrip() throws Exception {
|
||||
File pdf = createEmptyPdf("external-ids.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setIsbn13("9781718500921");
|
||||
meta.setIsbn10("1718500920");
|
||||
meta.setGoogleId("MPBmEAAAQBAJ");
|
||||
meta.setGoodreadsId("52555538");
|
||||
meta.setHardcoverId("dead-simple-python");
|
||||
meta.setHardcoverBookId("547027");
|
||||
meta.setAsin("B08KGS5V1R");
|
||||
meta.setComicvineId("4000-123456");
|
||||
meta.setLubimyczytacId("123456");
|
||||
meta.setRanobedbId("7890");
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertEquals("9781718500921", result.getIsbn13());
|
||||
assertEquals("1718500920", result.getIsbn10());
|
||||
assertEquals("MPBmEAAAQBAJ", result.getGoogleId());
|
||||
assertEquals("52555538", result.getGoodreadsId());
|
||||
assertEquals("dead-simple-python", result.getHardcoverId());
|
||||
assertEquals("547027", result.getHardcoverBookId());
|
||||
assertEquals("B08KGS5V1R", result.getAsin());
|
||||
assertEquals("4000-123456", result.getComicvineId());
|
||||
assertEquals("123456", result.getLubimyczytacId());
|
||||
assertEquals("7890", result.getRanobedbId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_ratings_roundTrip() throws Exception {
|
||||
File pdf = createEmptyPdf("ratings.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setGoodreadsRating(4.4);
|
||||
meta.setHardcoverRating(4.2);
|
||||
meta.setAmazonRating(4.5);
|
||||
meta.setLubimyczytacRating(8.5);
|
||||
meta.setRanobedbRating(7.8);
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertEquals(4.4, result.getGoodreadsRating(), 0.01);
|
||||
assertEquals(4.2, result.getHardcoverRating(), 0.01);
|
||||
assertEquals(4.5, result.getAmazonRating(), 0.01);
|
||||
assertEquals(8.5, result.getLubimyczytacRating(), 0.01);
|
||||
assertEquals(7.8, result.getRanobedbRating(), 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_zeroRatings_notWritten() throws Exception {
|
||||
File pdf = createEmptyPdf("zero-ratings.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setGoodreadsRating(0.0);
|
||||
meta.setHardcoverRating(0.0);
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
String xmpContent = readXmpContent(pdf);
|
||||
assertFalse(xmpContent.contains("goodreadsRating"),
|
||||
"Zero ratings should not be written to XMP");
|
||||
assertFalse(xmpContent.contains("hardcoverRating"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_tagsAndMoods_asRdfBags() throws Exception {
|
||||
File pdf = createEmptyPdf("tags-moods.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
|
||||
Set<TagEntity> tags = new HashSet<>();
|
||||
TagEntity tag1 = new TagEntity(); tag1.setName("Python");
|
||||
TagEntity tag2 = new TagEntity(); tag2.setName("Programming");
|
||||
tags.add(tag1);
|
||||
tags.add(tag2);
|
||||
meta.setTags(tags);
|
||||
|
||||
Set<MoodEntity> moods = new HashSet<>();
|
||||
MoodEntity mood1 = new MoodEntity(); mood1.setName("Educational");
|
||||
MoodEntity mood2 = new MoodEntity(); mood2.setName("Technical");
|
||||
moods.add(mood1);
|
||||
moods.add(mood2);
|
||||
meta.setMoods(moods);
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertTrue(result.getTags().contains("Python"));
|
||||
assertTrue(result.getTags().contains("Programming"));
|
||||
assertTrue(result.getMoods().contains("Educational"));
|
||||
assertTrue(result.getMoods().contains("Technical"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAndReadMetadata_subtitle_roundTrip() throws Exception {
|
||||
File pdf = createEmptyPdf("subtitle.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setSubtitle("Idiomatic Python for the Impatient Programmer");
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
BookMetadata result = extractor.extractMetadata(pdf);
|
||||
assertEquals("Idiomatic Python for the Impatient Programmer", result.getSubtitle());
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveMetadata_dateFormat_isDateOnly() throws Exception {
|
||||
File pdf = createEmptyPdf("date-format.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setPublishedDate(LocalDate.of(2021, 2, 17));
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
String xmpContent = readXmpContent(pdf);
|
||||
// CreateDate should be date-only format: 2021-02-17
|
||||
assertTrue(xmpContent.contains("<xmp:CreateDate>2021-02-17</xmp:CreateDate>"),
|
||||
"CreateDate should use date-only format (YYYY-MM-DD)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveMetadata_bookloreNamespaceUsed() throws Exception {
|
||||
File pdf = createEmptyPdf("namespace.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
meta.setSeriesName("Test");
|
||||
meta.setSeriesNumber(1f);
|
||||
meta.setIsbn13("1234567890123");
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
String xmpContent = readXmpContent(pdf);
|
||||
assertTrue(xmpContent.contains("xmlns:booklore=\"http://booklore.org/metadata/1.0/\""),
|
||||
"Booklore namespace should be declared");
|
||||
assertTrue(xmpContent.contains("<booklore:seriesName>Test</booklore:seriesName>"),
|
||||
"Series should be in booklore namespace");
|
||||
assertFalse(xmpContent.contains("calibre:"),
|
||||
"Calibre namespace should NOT be used");
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveMetadata_creatorTool_isBooklore() throws Exception {
|
||||
File pdf = createEmptyPdf("creator.pdf");
|
||||
|
||||
BookMetadataEntity meta = createBasicMetadata();
|
||||
|
||||
writer.saveMetadataToFile(pdf, meta, null, null);
|
||||
|
||||
String xmpContent = readXmpContent(pdf);
|
||||
assertTrue(xmpContent.contains("<xmp:CreatorTool>Booklore</xmp:CreatorTool>"));
|
||||
}
|
||||
|
||||
// ========== Helper Methods ==========
|
||||
|
||||
private File createEmptyPdf(String name) throws IOException {
|
||||
File pdf = tempDir.resolve(name).toFile();
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
doc.addPage(new PDPage());
|
||||
doc.save(pdf);
|
||||
}
|
||||
return pdf;
|
||||
}
|
||||
|
||||
private BookMetadataEntity createBasicMetadata() {
|
||||
BookMetadataEntity meta = new BookMetadataEntity();
|
||||
meta.setTitle("Test Book");
|
||||
return meta;
|
||||
}
|
||||
|
||||
private String readXmpContent(File pdf) throws IOException {
|
||||
try (PDDocument doc = Loader.loadPDF(pdf)) {
|
||||
PDMetadata metadata = doc.getDocumentCatalog().getMetadata();
|
||||
if (metadata == null) {
|
||||
return "";
|
||||
}
|
||||
return new String(metadata.toByteArray(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,12 +14,12 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
@@ -51,7 +51,7 @@ class LibraryRescanTaskTest {
|
||||
// Lenient stubs because not all tests use them
|
||||
lenient().when(request.getTaskId()).thenReturn("task-123");
|
||||
options = LibraryRescanOptions.builder().build();
|
||||
lenient().when(request.getOptions(LibraryRescanOptions.class)).thenReturn(options);
|
||||
lenient().when(request.getOptionsAs(LibraryRescanOptions.class)).thenReturn(options);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -19,13 +19,8 @@ import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RefreshMetadataTaskTest {
|
||||
@@ -48,7 +43,7 @@ class RefreshMetadataTaskTest {
|
||||
taskCreateRequest = mock(TaskCreateRequest.class);
|
||||
metadataRefreshRequest = MetadataRefreshRequest.builder().build();
|
||||
|
||||
when(taskCreateRequest.getOptions(MetadataRefreshRequest.class)).thenReturn(metadataRefreshRequest);
|
||||
when(taskCreateRequest.getOptionsAs(MetadataRefreshRequest.class)).thenReturn(metadataRefreshRequest);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
driver-class-name: org.h2.Driver
|
||||
username: sa
|
||||
password:
|
||||
jpa:
|
||||
database-platform: org.hibernate.dialect.H2Dialect
|
||||
hibernate:
|
||||
ddl-auto: create-drop
|
||||
properties:
|
||||
hibernate:
|
||||
format_sql: true
|
||||
flyway:
|
||||
enabled: false
|
||||
app:
|
||||
bookdrop-folder: build/bookdrop
|
||||
path-config: build/app-data
|
||||
Reference in New Issue
Block a user