feat(hardcover): get Hardcover metadata from a specific edition when ISBN is provided (#2678)
* fix: clean ISBN should allow ISBNs to have an X at the end * hardcover mood filter overload for just list of tags * When ISBN is given, fetch the book directly by the edition with that ISBN, and provide metadata from that edition * chore: improved formatting * PR improvements * replaced CachedTag with GraphQL specific class * moved hardcover cached tag to shared model
This commit is contained in:
+125
-6
@@ -42,9 +42,9 @@ public class HardcoverParser implements BookParser {
|
|||||||
boolean searchByIsbn = isbnCleaned != null && !isbnCleaned.isBlank();
|
boolean searchByIsbn = isbnCleaned != null && !isbnCleaned.isBlank();
|
||||||
|
|
||||||
if (searchByIsbn) {
|
if (searchByIsbn) {
|
||||||
log.info("Hardcover: Fetching metadata using ISBN {}", fetchMetadataRequest.getIsbn());
|
log.info("Hardcover: Fetching metadata using ISBN {}", isbnCleaned);
|
||||||
List<GraphQLResponse.Hit> hits = hardcoverBookSearchService.searchBooks(fetchMetadataRequest.getIsbn());
|
List<GraphQLResponse.BookWithEditions> hits = hardcoverBookSearchService.searchBookByIsbn(isbnCleaned);
|
||||||
return processHits(hits, fetchMetadataRequest, true);
|
return processBooksWithEditions(hits);
|
||||||
}
|
}
|
||||||
|
|
||||||
String title = fetchMetadataRequest.getTitle();
|
String title = fetchMetadataRequest.getTitle();
|
||||||
@@ -79,6 +79,27 @@ public class HardcoverParser implements BookParser {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<BookMetadata> processBooksWithEditions(List<GraphQLResponse.BookWithEditions> books) {
|
||||||
|
if (books == null || books.isEmpty()) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<BookMetadata> results = new ArrayList<>();
|
||||||
|
for (GraphQLResponse.BookWithEditions book : books) {
|
||||||
|
if (book.getEditions() == null || book.getEditions().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (GraphQLResponse.Edition edition : book.getEditions()) {
|
||||||
|
log.debug("Processing edition '{}' with id '{}' of book '{}'", edition.getTitle(), edition.getId(), book.getTitle());
|
||||||
|
BookMetadata metadata = mapEditionToMetadata(edition, book);
|
||||||
|
if (metadata != null) {
|
||||||
|
results.add(metadata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
private List<BookMetadata> processHits(List<GraphQLResponse.Hit> hits, FetchMetadataRequest request, boolean searchByIsbn) {
|
private List<BookMetadata> processHits(List<GraphQLResponse.Hit> hits, FetchMetadataRequest request, boolean searchByIsbn) {
|
||||||
if (hits == null || hits.isEmpty()) {
|
if (hits == null || hits.isEmpty()) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
@@ -183,6 +204,104 @@ public class HardcoverParser implements BookParser {
|
|||||||
|
|
||||||
metadata.setThumbnailUrl(doc.getImage() != null ? doc.getImage().getUrl() : null);
|
metadata.setThumbnailUrl(doc.getImage() != null ? doc.getImage().getUrl() : null);
|
||||||
metadata.setProvider(MetadataProvider.Hardcover);
|
metadata.setProvider(MetadataProvider.Hardcover);
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
private BookMetadata mapEditionToMetadata(GraphQLResponse.Edition edition, GraphQLResponse.BookWithEditions book) {
|
||||||
|
BookMetadata metadata = new BookMetadata();
|
||||||
|
metadata.setHardcoverId(book.getSlug());
|
||||||
|
|
||||||
|
Integer bookId = book.getId();
|
||||||
|
if (bookId != null) {
|
||||||
|
metadata.setHardcoverBookId(bookId.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata.setTitle(edition.getTitle());
|
||||||
|
metadata.setSubtitle(edition.getSubtitle());
|
||||||
|
metadata.setDescription(book.getDescription());
|
||||||
|
|
||||||
|
if (edition.getCachedContributors() != null) {
|
||||||
|
metadata.setAuthors(edition.getCachedContributors().stream()
|
||||||
|
.map(GraphQLResponse.Contributor::getAuthor)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.map(GraphQLResponse.Author::getName)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (book.getFeaturedBookSeries() != null && book.getFeaturedBookSeries().getSeries() != null) {
|
||||||
|
metadata.setSeriesName(book.getFeaturedBookSeries().getSeries().getName());
|
||||||
|
metadata.setSeriesTotal(book.getFeaturedBookSeries().getSeries().getBooksCount());
|
||||||
|
|
||||||
|
if (book.getFeaturedBookSeries().getPosition() != null) {
|
||||||
|
try {
|
||||||
|
metadata.setSeriesNumber(Float.parseFloat(String.valueOf(book.getFeaturedBookSeries().getPosition())));
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (book.getRating() != null) {
|
||||||
|
metadata.setHardcoverRating(
|
||||||
|
BigDecimal.valueOf(book.getRating()).setScale(2, RoundingMode.HALF_UP).doubleValue()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
metadata.setHardcoverReviewCount(book.getRatingsCount());
|
||||||
|
metadata.setPageCount(edition.getPages());
|
||||||
|
|
||||||
|
if (edition.getReleaseDate() != null) {
|
||||||
|
try {
|
||||||
|
metadata.setPublishedDate(LocalDate.parse(edition.getReleaseDate()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug("Could not parse release date: {}", edition.getReleaseDate());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the language from the edition
|
||||||
|
if (edition.getLanguage() != null && edition.getLanguage().getCode2() != null) {
|
||||||
|
metadata.setLanguage(edition.getLanguage().getCode2());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the Publisher from the edition
|
||||||
|
if (edition.getPublisher() != null && edition.getPublisher().getName() != null) {
|
||||||
|
metadata.setPublisher(edition.getPublisher().getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
GraphQLResponse.CachedTags cachedTags = book.getCachedTags();
|
||||||
|
|
||||||
|
if (cachedTags != null && cachedTags.getMood() != null && !cachedTags.getMood().isEmpty()) {
|
||||||
|
Set<String> basicFilteredMoods = HardcoverMoodFilter.filterMoodsWithCounts(cachedTags.getMood());
|
||||||
|
metadata.setMoods(basicFilteredMoods.stream()
|
||||||
|
.map(WordUtils::capitalizeFully)
|
||||||
|
.collect(Collectors.toCollection(LinkedHashSet::new)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cachedTags != null && cachedTags.getGenre() != null && !cachedTags.getGenre().isEmpty()) {
|
||||||
|
Set<String> filteredGenres = HardcoverMoodFilter.filterGenresWithCounts(cachedTags.getGenre());
|
||||||
|
metadata.setCategories(filteredGenres.stream()
|
||||||
|
.map(WordUtils::capitalizeFully)
|
||||||
|
.collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cachedTags != null && cachedTags.getTag() != null && !cachedTags.getTag().isEmpty()) {
|
||||||
|
Set<String> filteredTags = HardcoverMoodFilter.filterTagsWithCounts(cachedTags.getTag());
|
||||||
|
metadata.setTags(filteredTags.stream()
|
||||||
|
.map(WordUtils::capitalizeFully)
|
||||||
|
.collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata.setIsbn10(edition.getIsbn10());
|
||||||
|
metadata.setIsbn13(edition.getIsbn13());
|
||||||
|
|
||||||
|
// If only one ISBN is provided, calculate the other
|
||||||
|
if (metadata.getIsbn10() != null && metadata.getIsbn13() == null) {
|
||||||
|
metadata.setIsbn13(BookUtils.isbn10To13(edition.getIsbn10()));
|
||||||
|
} else if (metadata.getIsbn13() != null && metadata.getIsbn10() == null) {
|
||||||
|
metadata.setIsbn10(BookUtils.isbn13to10(edition.getIsbn13()));
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata.setThumbnailUrl(edition.getImage() != null ? edition.getImage().getUrl() : null);
|
||||||
|
metadata.setProvider(MetadataProvider.Hardcover);
|
||||||
|
|
||||||
return metadata;
|
return metadata;
|
||||||
}
|
}
|
||||||
@@ -282,9 +401,9 @@ public class HardcoverParser implements BookParser {
|
|||||||
if (StringUtils.isBlank(inputIsbn)) {
|
if (StringUtils.isBlank(inputIsbn)) {
|
||||||
// If we didn't search by ISBN, use first ISBN from results
|
// If we didn't search by ISBN, use first ISBN from results
|
||||||
matchingIsbn = doc.getIsbns().stream()
|
matchingIsbn = doc.getIsbns().stream()
|
||||||
.filter(isbn -> isbn.length() == 10 || isbn.length() == 13)
|
.filter(isbn -> isbn.length() == 10 || isbn.length() == 13)
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
} else if (doc.getIsbns().contains(inputIsbn)) {
|
} else if (doc.getIsbns().contains(inputIsbn)) {
|
||||||
// If we searched by ISBN and it matches a result perfectly, use that
|
// If we searched by ISBN and it matches a result perfectly, use that
|
||||||
matchingIsbn = inputIsbn;
|
matchingIsbn = inputIsbn;
|
||||||
|
|||||||
@@ -4,10 +4,15 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
public class ParserUtils {
|
public class ParserUtils {
|
||||||
|
|
||||||
private static final Pattern NON_DIGIT_PATTERN = Pattern.compile("[^0-9]");
|
private static final Pattern NON_ISBN_CHAR_PATTERN = Pattern.compile("[^0-9Xx]");
|
||||||
|
|
||||||
public static String cleanIsbn(String isbn) {
|
public static String cleanIsbn(String isbn) {
|
||||||
if (isbn == null) return null;
|
if (isbn == null) return null;
|
||||||
return NON_DIGIT_PATTERN.matcher(isbn).replaceAll("");
|
String cleaned = NON_ISBN_CHAR_PATTERN.matcher(isbn).replaceAll("");
|
||||||
|
// Normalize 'x' to 'X' for ISBN-10 check digit
|
||||||
|
if (cleaned.length() == 10 && cleaned.endsWith("x")) {
|
||||||
|
cleaned = cleaned.substring(0, 9) + "X";
|
||||||
|
}
|
||||||
|
return cleaned;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+133
@@ -23,6 +23,7 @@ public class GraphQLResponse {
|
|||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public static class Data {
|
public static class Data {
|
||||||
private Search search;
|
private Search search;
|
||||||
|
private List<BookWithEditions> books;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@@ -160,6 +161,82 @@ public class GraphQLResponse {
|
|||||||
private Integer usersReadCount;
|
private Integer usersReadCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class BookWithEditions {
|
||||||
|
private Integer id;
|
||||||
|
private String slug;
|
||||||
|
private String title;
|
||||||
|
private String subtitle;
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
@JsonProperty("cached_contributors")
|
||||||
|
private List<Contributor> cachedContributors;
|
||||||
|
|
||||||
|
@JsonProperty("featured_book_series")
|
||||||
|
private FeaturedSeries featuredBookSeries;
|
||||||
|
|
||||||
|
private Double rating;
|
||||||
|
|
||||||
|
@JsonProperty("ratings_count")
|
||||||
|
private Integer ratingsCount;
|
||||||
|
|
||||||
|
@JsonProperty("reviews_count")
|
||||||
|
private Integer reviewsCount;
|
||||||
|
|
||||||
|
private Integer pages;
|
||||||
|
|
||||||
|
@JsonProperty("release_date")
|
||||||
|
private String releaseDate;
|
||||||
|
|
||||||
|
@JsonProperty("release_year")
|
||||||
|
private Integer releaseYear;
|
||||||
|
|
||||||
|
private Image image;
|
||||||
|
|
||||||
|
@JsonProperty("cached_tags")
|
||||||
|
private CachedTags cachedTags;
|
||||||
|
|
||||||
|
private List<Edition> editions;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class Edition {
|
||||||
|
private Integer id;
|
||||||
|
private String title;
|
||||||
|
private String subtitle;
|
||||||
|
|
||||||
|
@JsonProperty("cached_contributors")
|
||||||
|
private List<Contributor> cachedContributors;
|
||||||
|
|
||||||
|
private Integer pages;
|
||||||
|
|
||||||
|
@JsonProperty("release_date")
|
||||||
|
private String releaseDate;
|
||||||
|
|
||||||
|
@JsonProperty("release_year")
|
||||||
|
private Integer releaseYear;
|
||||||
|
|
||||||
|
private Image image;
|
||||||
|
|
||||||
|
private Publisher publisher;
|
||||||
|
|
||||||
|
@JsonProperty("isbn_10")
|
||||||
|
private String isbn10;
|
||||||
|
|
||||||
|
@JsonProperty("isbn_13")
|
||||||
|
private String isbn13;
|
||||||
|
|
||||||
|
private Language language;
|
||||||
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@@ -189,4 +266,60 @@ public class GraphQLResponse {
|
|||||||
@JsonProperty("books_count")
|
@JsonProperty("books_count")
|
||||||
private Integer booksCount;
|
private Integer booksCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class Contributor {
|
||||||
|
private Author author;
|
||||||
|
private String contribution;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class Author {
|
||||||
|
private Integer id;
|
||||||
|
private String slug;
|
||||||
|
private String name;
|
||||||
|
private Image image;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class CachedTags {
|
||||||
|
@JsonProperty("Genre")
|
||||||
|
private List<HardcoverCachedTag> genre;
|
||||||
|
|
||||||
|
@JsonProperty("Mood")
|
||||||
|
private List<HardcoverCachedTag> mood;
|
||||||
|
|
||||||
|
@JsonProperty("Tag")
|
||||||
|
private List<HardcoverCachedTag> tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class Publisher {
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public static class Language {
|
||||||
|
private String code2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-23
@@ -18,27 +18,5 @@ public class HardcoverBookDetails {
|
|||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
@JsonProperty("cached_tags")
|
@JsonProperty("cached_tags")
|
||||||
private Map<String, List<CachedTag>> cachedTags;
|
private Map<String, List<HardcoverCachedTag>> cachedTags;
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
||||||
public static class CachedTag {
|
|
||||||
private String tag;
|
|
||||||
|
|
||||||
@JsonProperty("tagSlug")
|
|
||||||
private String tagSlug;
|
|
||||||
|
|
||||||
private String category;
|
|
||||||
|
|
||||||
@JsonProperty("categorySlug")
|
|
||||||
private String categorySlug;
|
|
||||||
|
|
||||||
@JsonProperty("spoilerRatio")
|
|
||||||
private Double spoilerRatio;
|
|
||||||
|
|
||||||
private Integer count;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+69
@@ -35,6 +35,75 @@ public class HardcoverBookSearchService {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<GraphQLResponse.BookWithEditions> searchBookByIsbn(String isbn) {
|
||||||
|
String apiToken = getApiToken();
|
||||||
|
if (apiToken == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
GraphQLRequest body = new GraphQLRequest();
|
||||||
|
body.setQuery("""
|
||||||
|
query BookSearchByIsbn($isbn: String!) {
|
||||||
|
books(
|
||||||
|
where: {editions: {_or: [{isbn_13: {_eq: $isbn}}, {isbn_10: {_eq: $isbn}}]}}
|
||||||
|
) {
|
||||||
|
id
|
||||||
|
slug
|
||||||
|
title
|
||||||
|
subtitle
|
||||||
|
description
|
||||||
|
cached_contributors
|
||||||
|
featured_book_series {
|
||||||
|
series {
|
||||||
|
name
|
||||||
|
books_count
|
||||||
|
}
|
||||||
|
position
|
||||||
|
}
|
||||||
|
rating
|
||||||
|
ratings_count
|
||||||
|
reviews_count
|
||||||
|
pages
|
||||||
|
release_date
|
||||||
|
release_year
|
||||||
|
image {
|
||||||
|
url
|
||||||
|
}
|
||||||
|
cached_tags
|
||||||
|
editions(where: {_or: [{isbn_13: {_eq: $isbn}}, {isbn_10: {_eq: $isbn}}]}) {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
subtitle
|
||||||
|
cached_contributors
|
||||||
|
pages
|
||||||
|
release_date
|
||||||
|
release_year
|
||||||
|
image {
|
||||||
|
url
|
||||||
|
}
|
||||||
|
publisher {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
isbn_10
|
||||||
|
isbn_13
|
||||||
|
language {
|
||||||
|
code2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}""");
|
||||||
|
body.setVariables(java.util.Map.of("isbn", isbn));
|
||||||
|
|
||||||
|
GraphQLResponse response = executeRequest(body, GraphQLResponse.class, apiToken);
|
||||||
|
if (response == null || response.getData() == null ||
|
||||||
|
response.getData().getBooks() == null ||
|
||||||
|
response.getData().getBooks().isEmpty()) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.getData().getBooks();
|
||||||
|
}
|
||||||
|
|
||||||
public List<GraphQLResponse.Hit> searchBooks(String query) {
|
public List<GraphQLResponse.Hit> searchBooks(String query) {
|
||||||
return searchBooks(query, DEFAULT_PER_PAGE);
|
return searchBooks(query, DEFAULT_PER_PAGE);
|
||||||
}
|
}
|
||||||
|
|||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
package org.booklore.service.metadata.parser.hardcover;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class HardcoverCachedTag {
|
||||||
|
private String tag;
|
||||||
|
|
||||||
|
@JsonProperty("tagSlug")
|
||||||
|
private String tagSlug;
|
||||||
|
|
||||||
|
private String category;
|
||||||
|
|
||||||
|
@JsonProperty("categorySlug")
|
||||||
|
private String categorySlug;
|
||||||
|
|
||||||
|
@JsonProperty("spoilerRatio")
|
||||||
|
private Double spoilerRatio;
|
||||||
|
|
||||||
|
private Integer count;
|
||||||
|
}
|
||||||
|
|
||||||
+23
-14
@@ -38,12 +38,15 @@ public class HardcoverMoodFilter {
|
|||||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<String> filterMoodsWithCounts(Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags) {
|
public static Set<String> filterMoodsWithCounts(Map<String, List<HardcoverCachedTag>> cachedTags) {
|
||||||
if (cachedTags == null || cachedTags.isEmpty()) {
|
if (cachedTags == null || cachedTags.isEmpty()) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<HardcoverBookDetails.CachedTag> moodTags = cachedTags.get("Mood");
|
return filterMoodsWithCounts(cachedTags.get("Mood"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Set<String> filterMoodsWithCounts(List<HardcoverCachedTag> moodTags) {
|
||||||
if (moodTags == null || moodTags.isEmpty()) {
|
if (moodTags == null || moodTags.isEmpty()) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
@@ -51,7 +54,7 @@ public class HardcoverMoodFilter {
|
|||||||
// Find the maximum vote count
|
// Find the maximum vote count
|
||||||
int maxCount = moodTags.stream()
|
int maxCount = moodTags.stream()
|
||||||
.filter(tag -> tag.getCount() != null)
|
.filter(tag -> tag.getCount() != null)
|
||||||
.mapToInt(HardcoverBookDetails.CachedTag::getCount)
|
.mapToInt(HardcoverCachedTag::getCount)
|
||||||
.max()
|
.max()
|
||||||
.orElse(0);
|
.orElse(0);
|
||||||
|
|
||||||
@@ -62,10 +65,10 @@ public class HardcoverMoodFilter {
|
|||||||
|
|
||||||
int minCountThreshold = Math.max(MIN_VOTE_COUNT, (int) (maxCount * MIN_VOTE_RATIO));
|
int minCountThreshold = Math.max(MIN_VOTE_COUNT, (int) (maxCount * MIN_VOTE_RATIO));
|
||||||
|
|
||||||
List<HardcoverBookDetails.CachedTag> filteredMoods = moodTags.stream()
|
List<HardcoverCachedTag> filteredMoods = moodTags.stream()
|
||||||
.filter(tag -> tag.getCount() != null && tag.getCount() >= minCountThreshold)
|
.filter(tag -> tag.getCount() != null && tag.getCount() >= minCountThreshold)
|
||||||
.filter(tag -> tag.getTag() != null)
|
.filter(tag -> tag.getTag() != null)
|
||||||
.sorted(Comparator.comparingInt(HardcoverBookDetails.CachedTag::getCount).reversed())
|
.sorted(Comparator.comparingInt(HardcoverCachedTag::getCount).reversed())
|
||||||
.limit(MAX_MOODS)
|
.limit(MAX_MOODS)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
@@ -73,16 +76,19 @@ public class HardcoverMoodFilter {
|
|||||||
moodTags.size(), filteredMoods.size(), maxCount, minCountThreshold);
|
moodTags.size(), filteredMoods.size(), maxCount, minCountThreshold);
|
||||||
|
|
||||||
return filteredMoods.stream()
|
return filteredMoods.stream()
|
||||||
.map(HardcoverBookDetails.CachedTag::getTag)
|
.map(HardcoverCachedTag::getTag)
|
||||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<String> filterGenresWithCounts(Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags) {
|
public static Set<String> filterGenresWithCounts(Map<String, List<HardcoverCachedTag>> cachedTags) {
|
||||||
if (cachedTags == null || cachedTags.isEmpty()) {
|
if (cachedTags == null || cachedTags.isEmpty()) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<HardcoverBookDetails.CachedTag> genreTags = cachedTags.get("Genre");
|
return filterGenresWithCounts(cachedTags.get("Genre"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Set<String> filterGenresWithCounts(List<HardcoverCachedTag> genreTags) {
|
||||||
if (genreTags == null || genreTags.isEmpty()) {
|
if (genreTags == null || genreTags.isEmpty()) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
@@ -91,18 +97,21 @@ public class HardcoverMoodFilter {
|
|||||||
return genreTags.stream()
|
return genreTags.stream()
|
||||||
.filter(tag -> tag.getCount() != null && tag.getCount() >= 1)
|
.filter(tag -> tag.getCount() != null && tag.getCount() >= 1)
|
||||||
.filter(tag -> tag.getTag() != null)
|
.filter(tag -> tag.getTag() != null)
|
||||||
.sorted(Comparator.comparingInt(HardcoverBookDetails.CachedTag::getCount).reversed())
|
.sorted(Comparator.comparingInt(HardcoverCachedTag::getCount).reversed())
|
||||||
.limit(10) // Allow more genres
|
.limit(10) // Allow more genres
|
||||||
.map(HardcoverBookDetails.CachedTag::getTag)
|
.map(HardcoverCachedTag::getTag)
|
||||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<String> filterTagsWithCounts(Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags) {
|
public static Set<String> filterTagsWithCounts(Map<String, List<HardcoverCachedTag>> cachedTags) {
|
||||||
if (cachedTags == null || cachedTags.isEmpty()) {
|
if (cachedTags == null || cachedTags.isEmpty()) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<HardcoverBookDetails.CachedTag> tagList = cachedTags.get("Tag");
|
return filterTagsWithCounts(cachedTags.get("Tag"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Set<String> filterTagsWithCounts(List<HardcoverCachedTag> tagList) {
|
||||||
if (tagList == null || tagList.isEmpty()) {
|
if (tagList == null || tagList.isEmpty()) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
@@ -111,9 +120,9 @@ public class HardcoverMoodFilter {
|
|||||||
return tagList.stream()
|
return tagList.stream()
|
||||||
.filter(tag -> tag.getCount() != null && tag.getCount() >= 2)
|
.filter(tag -> tag.getCount() != null && tag.getCount() >= 2)
|
||||||
.filter(tag -> tag.getTag() != null)
|
.filter(tag -> tag.getTag() != null)
|
||||||
.sorted(Comparator.comparingInt(HardcoverBookDetails.CachedTag::getCount).reversed())
|
.sorted(Comparator.comparingInt(HardcoverCachedTag::getCount).reversed())
|
||||||
.limit(10)
|
.limit(10)
|
||||||
.map(HardcoverBookDetails.CachedTag::getTag)
|
.map(HardcoverCachedTag::getTag)
|
||||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+295
-19
@@ -7,6 +7,7 @@ import org.booklore.model.enums.MetadataProvider;
|
|||||||
import org.booklore.service.metadata.parser.hardcover.GraphQLResponse;
|
import org.booklore.service.metadata.parser.hardcover.GraphQLResponse;
|
||||||
import org.booklore.service.metadata.parser.hardcover.HardcoverBookDetails;
|
import org.booklore.service.metadata.parser.hardcover.HardcoverBookDetails;
|
||||||
import org.booklore.service.metadata.parser.hardcover.HardcoverBookSearchService;
|
import org.booklore.service.metadata.parser.hardcover.HardcoverBookSearchService;
|
||||||
|
import org.booklore.service.metadata.parser.hardcover.HardcoverCachedTag;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
@@ -23,7 +24,7 @@ import static org.mockito.Mockito.*;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for HardcoverParser.
|
* Unit tests for HardcoverParser.
|
||||||
*
|
* <p>
|
||||||
* These tests verify:
|
* These tests verify:
|
||||||
* - Combined title+author search strategy for better reliability
|
* - Combined title+author search strategy for better reliability
|
||||||
* - ISBN search behavior
|
* - ISBN search behavior
|
||||||
@@ -126,17 +127,159 @@ class HardcoverParserTest {
|
|||||||
.isbn("978-0316769488")
|
.isbn("978-0316769488")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
GraphQLResponse.Hit hit = createHitWithAuthor("The Catcher in the Rye", "J.D. Salinger");
|
GraphQLResponse.BookWithEditions hit = createBookWithEditions();
|
||||||
hit.getDocument().setIsbns(List.of("9780316769488", "0316769487"));
|
hit.getEditions().get(0).setIsbn13("9780316769488");
|
||||||
when(hardcoverBookSearchService.searchBooks("978-0316769488"))
|
hit.getEditions().get(0).setIsbn10("0316769487");
|
||||||
|
when(hardcoverBookSearchService.searchBookByIsbn("9780316769488"))
|
||||||
.thenReturn(List.of(hit));
|
.thenReturn(List.of(hit));
|
||||||
|
|
||||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
|
|
||||||
verify(hardcoverBookSearchService).searchBooks("978-0316769488");
|
verify(hardcoverBookSearchService).searchBookByIsbn("9780316769488");
|
||||||
verify(hardcoverBookSearchService, never()).searchBooks(contains("title"));
|
verify(hardcoverBookSearchService, never()).searchBooks(contains("title"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty list when ISBN search returns book with no editions")
|
||||||
|
void fetchMetadata_isbnSearchNoEditions_returnsEmptyList() {
|
||||||
|
Book book = Book.builder().title("Any Title").build();
|
||||||
|
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||||
|
.title("Any Title")
|
||||||
|
.isbn("9780316769488")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
GraphQLResponse.BookWithEditions bookWithNoEditions = new GraphQLResponse.BookWithEditions();
|
||||||
|
bookWithNoEditions.setId(12345);
|
||||||
|
bookWithNoEditions.setTitle("Test Book");
|
||||||
|
bookWithNoEditions.setEditions(Collections.emptyList());
|
||||||
|
|
||||||
|
when(hardcoverBookSearchService.searchBookByIsbn("9780316769488"))
|
||||||
|
.thenReturn(List.of(bookWithNoEditions));
|
||||||
|
|
||||||
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
|
|
||||||
|
assertThat(results).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty list when ISBN search returns book with null editions")
|
||||||
|
void fetchMetadata_isbnSearchNullEditions_returnsEmptyList() {
|
||||||
|
Book book = Book.builder().title("Any Title").build();
|
||||||
|
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||||
|
.title("Any Title")
|
||||||
|
.isbn("9780316769488")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
GraphQLResponse.BookWithEditions bookWithNullEditions = new GraphQLResponse.BookWithEditions();
|
||||||
|
bookWithNullEditions.setId(12345);
|
||||||
|
bookWithNullEditions.setTitle("Test Book");
|
||||||
|
bookWithNullEditions.setEditions(null);
|
||||||
|
|
||||||
|
when(hardcoverBookSearchService.searchBookByIsbn("9780316769488"))
|
||||||
|
.thenReturn(List.of(bookWithNullEditions));
|
||||||
|
|
||||||
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
|
|
||||||
|
assertThat(results).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty list when ISBN search returns null")
|
||||||
|
void fetchMetadata_isbnSearchReturnsNull_returnsEmptyList() {
|
||||||
|
Book book = Book.builder().title("Any Title").build();
|
||||||
|
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||||
|
.title("Any Title")
|
||||||
|
.isbn("9780316769488")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(hardcoverBookSearchService.searchBookByIsbn("9780316769488"))
|
||||||
|
.thenReturn(null);
|
||||||
|
|
||||||
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
|
|
||||||
|
assertThat(results).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return all editions when ISBN search returns book with multiple editions")
|
||||||
|
void fetchMetadata_isbnSearchMultipleEditions_returnsAllEditions() {
|
||||||
|
Book book = Book.builder().title("Any Title").build();
|
||||||
|
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||||
|
.title("Any Title")
|
||||||
|
.isbn("9780316769488")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
GraphQLResponse.BookWithEditions bookWithEditions = createBookWithEditions();
|
||||||
|
|
||||||
|
// Add a second edition
|
||||||
|
GraphQLResponse.Edition secondEdition = new GraphQLResponse.Edition();
|
||||||
|
secondEdition.setId(2);
|
||||||
|
secondEdition.setTitle("Test Book - Paperback Edition");
|
||||||
|
secondEdition.setIsbn13("9780316769489");
|
||||||
|
secondEdition.setIsbn10("0316769489");
|
||||||
|
secondEdition.setPages(400);
|
||||||
|
|
||||||
|
GraphQLResponse.Author author = new GraphQLResponse.Author();
|
||||||
|
author.setName("Test Author");
|
||||||
|
GraphQLResponse.Contributor contributor = new GraphQLResponse.Contributor();
|
||||||
|
contributor.setAuthor(author);
|
||||||
|
secondEdition.setCachedContributors(List.of(contributor));
|
||||||
|
|
||||||
|
bookWithEditions.setEditions(List.of(bookWithEditions.getEditions().get(0), secondEdition));
|
||||||
|
|
||||||
|
when(hardcoverBookSearchService.searchBookByIsbn("9780316769488"))
|
||||||
|
.thenReturn(List.of(bookWithEditions));
|
||||||
|
|
||||||
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
|
|
||||||
|
assertThat(results).hasSize(2);
|
||||||
|
assertThat(results.get(0).getTitle()).isEqualTo("Test Book - Hardcover Edition");
|
||||||
|
assertThat(results.get(1).getTitle()).isEqualTo("Test Book - Paperback Edition");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should handle edition with null optional fields gracefully")
|
||||||
|
void fetchMetadata_editionWithNullOptionalFields_handlesGracefully() {
|
||||||
|
Book book = Book.builder().title("Any Title").build();
|
||||||
|
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||||
|
.title("Any Title")
|
||||||
|
.isbn("9780316769488")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
GraphQLResponse.BookWithEditions bookWithEditions = new GraphQLResponse.BookWithEditions();
|
||||||
|
bookWithEditions.setId(12345);
|
||||||
|
bookWithEditions.setSlug("test-book");
|
||||||
|
bookWithEditions.setTitle("Test Book");
|
||||||
|
bookWithEditions.setDescription("A description");
|
||||||
|
bookWithEditions.setRating(4.0);
|
||||||
|
bookWithEditions.setRatingsCount(50);
|
||||||
|
|
||||||
|
// Edition with null language, publisher, and cachedContributors
|
||||||
|
GraphQLResponse.Edition edition = new GraphQLResponse.Edition();
|
||||||
|
edition.setId(1);
|
||||||
|
edition.setTitle("Test Book Edition");
|
||||||
|
edition.setIsbn13("9780316769488");
|
||||||
|
edition.setLanguage(null);
|
||||||
|
edition.setPublisher(null);
|
||||||
|
edition.setCachedContributors(null);
|
||||||
|
edition.setImage(null);
|
||||||
|
|
||||||
|
bookWithEditions.setEditions(List.of(edition));
|
||||||
|
|
||||||
|
when(hardcoverBookSearchService.searchBookByIsbn("9780316769488"))
|
||||||
|
.thenReturn(List.of(bookWithEditions));
|
||||||
|
|
||||||
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
|
|
||||||
|
assertThat(results).hasSize(1);
|
||||||
|
BookMetadata metadata = results.get(0);
|
||||||
|
assertThat(metadata.getTitle()).isEqualTo("Test Book Edition");
|
||||||
|
assertThat(metadata.getLanguage()).isNull();
|
||||||
|
assertThat(metadata.getPublisher()).isNull();
|
||||||
|
assertThat(metadata.getAuthors()).isNullOrEmpty();
|
||||||
|
assertThat(metadata.getThumbnailUrl()).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Should search title-only when no author provided")
|
@DisplayName("Should search title-only when no author provided")
|
||||||
void fetchMetadata_noAuthor_searchesTitleOnly() {
|
void fetchMetadata_noAuthor_searchesTitleOnly() {
|
||||||
@@ -210,12 +353,16 @@ class HardcoverParserTest {
|
|||||||
.author("Wrong Author") // Should be ignored
|
.author("Wrong Author") // Should be ignored
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
GraphQLResponse.Hit hit = createHitWithAuthor("Any Book", "Correct Author");
|
GraphQLResponse.BookWithEditions bookWithEditions = createBookWithEditions();
|
||||||
when(hardcoverBookSearchService.searchBooks("123456789X"))
|
// The book has "Test Author" but request has "Wrong Author" - should still return results
|
||||||
.thenReturn(List.of(hit));
|
when(hardcoverBookSearchService.searchBookByIsbn("123456789X"))
|
||||||
|
.thenReturn(List.of(bookWithEditions));
|
||||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
|
|
||||||
assertThat(results).hasSize(1); // Should not filter out
|
assertThat(results).hasSize(1); // Should not filter out
|
||||||
|
verify(hardcoverBookSearchService).searchBookByIsbn("123456789X");
|
||||||
|
|
||||||
|
assertThat(results.get(0).getAuthors()).contains("Test Author");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,11 +376,10 @@ class HardcoverParserTest {
|
|||||||
Book book = Book.builder().title("Test").build();
|
Book book = Book.builder().title("Test").build();
|
||||||
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||||
.title("Test")
|
.title("Test")
|
||||||
.isbn("9781234567897")
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
GraphQLResponse.Hit hit = createFullyPopulatedHit();
|
GraphQLResponse.Hit hit = createFullyPopulatedHit();
|
||||||
when(hardcoverBookSearchService.searchBooks("9781234567897"))
|
when(hardcoverBookSearchService.searchBooks("Test"))
|
||||||
.thenReturn(List.of(hit));
|
.thenReturn(List.of(hit));
|
||||||
|
|
||||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
@@ -253,8 +399,48 @@ class HardcoverParserTest {
|
|||||||
assertThat(metadata.getSeriesName()).isEqualTo("Test Series");
|
assertThat(metadata.getSeriesName()).isEqualTo("Test Series");
|
||||||
assertThat(metadata.getSeriesNumber()).isEqualTo(2.0f);
|
assertThat(metadata.getSeriesNumber()).isEqualTo(2.0f);
|
||||||
assertThat(metadata.getSeriesTotal()).isEqualTo(5);
|
assertThat(metadata.getSeriesTotal()).isEqualTo(5);
|
||||||
|
assertThat(metadata.getIsbn13()).isEqualTo("9781111111113");
|
||||||
|
assertThat(metadata.getIsbn10()).isEqualTo("1111111111");
|
||||||
|
assertThat(metadata.getProvider()).isEqualTo(MetadataProvider.Hardcover);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should map all basic metadata fields correctly when searching with ISBN")
|
||||||
|
void fetchMetadata_fullDocument_mapsAllFields_fromISBN() {
|
||||||
|
Book book = Book.builder().title("Test").build();
|
||||||
|
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||||
|
.title("Test")
|
||||||
|
.isbn("9781234567897")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
GraphQLResponse.BookWithEditions hit = createBookWithEditions();
|
||||||
|
when(hardcoverBookSearchService.searchBookByIsbn("9781234567897"))
|
||||||
|
.thenReturn(List.of(hit));
|
||||||
|
|
||||||
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
|
|
||||||
|
assertThat(results).hasSize(1);
|
||||||
|
BookMetadata metadata = results.get(0);
|
||||||
|
|
||||||
|
assertThat(metadata.getTitle()).isEqualTo("Test Book - Hardcover Edition");
|
||||||
|
assertThat(metadata.getSubtitle()).isEqualTo("A Subtitle");
|
||||||
|
assertThat(metadata.getDescription()).isEqualTo("A description");
|
||||||
|
assertThat(metadata.getHardcoverId()).isEqualTo("test-book-slug");
|
||||||
|
assertThat(metadata.getHardcoverBookId()).isEqualTo("12345");
|
||||||
|
assertThat(metadata.getHardcoverRating()).isEqualTo(4.25);
|
||||||
|
assertThat(metadata.getHardcoverReviewCount()).isEqualTo(100);
|
||||||
|
assertThat(metadata.getPageCount()).isEqualTo(350);
|
||||||
|
assertThat(metadata.getAuthors()).contains("Test Author");
|
||||||
|
assertThat(metadata.getSeriesName()).isEqualTo("Test Series");
|
||||||
|
assertThat(metadata.getSeriesNumber()).isEqualTo(2.0f);
|
||||||
|
assertThat(metadata.getSeriesTotal()).isEqualTo(5);
|
||||||
assertThat(metadata.getIsbn13()).isEqualTo("9781234567897");
|
assertThat(metadata.getIsbn13()).isEqualTo("9781234567897");
|
||||||
assertThat(metadata.getIsbn10()).isEqualTo("123456789X");
|
assertThat(metadata.getIsbn10()).isEqualTo("123456789X");
|
||||||
|
assertThat(metadata.getPublisher()).isEqualTo("Test Publisher");
|
||||||
|
assertThat(metadata.getLanguage()).isEqualTo("en");
|
||||||
|
assertThat(metadata.getMoods()).containsExactlyInAnyOrder("Adventurous", "Exciting");
|
||||||
|
assertThat(metadata.getCategories()).containsExactlyInAnyOrder("Fiction", "Fantasy");
|
||||||
|
assertThat(metadata.getTags()).containsExactlyInAnyOrder("Epic", "Quest");
|
||||||
assertThat(metadata.getProvider()).isEqualTo(MetadataProvider.Hardcover);
|
assertThat(metadata.getProvider()).isEqualTo(MetadataProvider.Hardcover);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,8 +453,8 @@ class HardcoverParserTest {
|
|||||||
.isbn("123456789X")
|
.isbn("123456789X")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
GraphQLResponse.Hit hit = createFullyPopulatedHit();
|
GraphQLResponse.BookWithEditions hit = createBookWithEditions();
|
||||||
when(hardcoverBookSearchService.searchBooks("123456789X"))
|
when(hardcoverBookSearchService.searchBookByIsbn("123456789X"))
|
||||||
.thenReturn(List.of(hit));
|
.thenReturn(List.of(hit));
|
||||||
|
|
||||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
@@ -286,8 +472,11 @@ class HardcoverParserTest {
|
|||||||
.isbn("9791111111112")
|
.isbn("9791111111112")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
GraphQLResponse.Hit hit = createFullyPopulatedHit();
|
GraphQLResponse.BookWithEditions hit = createBookWithEditions();
|
||||||
when(hardcoverBookSearchService.searchBooks("9791111111112"))
|
hit.getEditions().get(0).setIsbn13("9791111111112");
|
||||||
|
hit.getEditions().get(0).setIsbn10(null);
|
||||||
|
|
||||||
|
when(hardcoverBookSearchService.searchBookByIsbn("9791111111112"))
|
||||||
.thenReturn(List.of(hit));
|
.thenReturn(List.of(hit));
|
||||||
|
|
||||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||||
@@ -599,16 +788,16 @@ class HardcoverParserTest {
|
|||||||
details.setId(12345);
|
details.setId(12345);
|
||||||
details.setTitle("Test Book");
|
details.setTitle("Test Book");
|
||||||
|
|
||||||
Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags = new HashMap<>();
|
Map<String, List<HardcoverCachedTag>> cachedTags = new HashMap<>();
|
||||||
|
|
||||||
List<HardcoverBookDetails.CachedTag> moods = new ArrayList<>();
|
List<HardcoverCachedTag> moods = new ArrayList<>();
|
||||||
moods.add(createCachedTag("sad", 15));
|
moods.add(createCachedTag("sad", 15));
|
||||||
moods.add(createCachedTag("dark", 12));
|
moods.add(createCachedTag("dark", 12));
|
||||||
moods.add(createCachedTag("emotional", 8));
|
moods.add(createCachedTag("emotional", 8));
|
||||||
moods.add(createCachedTag("funny", 2)); // Low count, should be filtered
|
moods.add(createCachedTag("funny", 2)); // Low count, should be filtered
|
||||||
cachedTags.put("Mood", moods);
|
cachedTags.put("Mood", moods);
|
||||||
|
|
||||||
List<HardcoverBookDetails.CachedTag> genres = new ArrayList<>();
|
List<HardcoverCachedTag> genres = new ArrayList<>();
|
||||||
genres.add(createCachedTag("Fiction", 10));
|
genres.add(createCachedTag("Fiction", 10));
|
||||||
genres.add(createCachedTag("Drama", 8));
|
genres.add(createCachedTag("Drama", 8));
|
||||||
cachedTags.put("Genre", genres);
|
cachedTags.put("Genre", genres);
|
||||||
@@ -617,8 +806,95 @@ class HardcoverParserTest {
|
|||||||
return details;
|
return details;
|
||||||
}
|
}
|
||||||
|
|
||||||
private HardcoverBookDetails.CachedTag createCachedTag(String tag, int count) {
|
private GraphQLResponse.BookWithEditions createBookWithEditions() {
|
||||||
HardcoverBookDetails.CachedTag cachedTag = new HardcoverBookDetails.CachedTag();
|
GraphQLResponse.BookWithEditions book = new GraphQLResponse.BookWithEditions();
|
||||||
|
|
||||||
|
book.setId(12345);
|
||||||
|
book.setSlug("test-book-slug");
|
||||||
|
book.setTitle("Test Book");
|
||||||
|
book.setSubtitle("A Subtitle");
|
||||||
|
book.setDescription("A description");
|
||||||
|
book.setRating(4.25);
|
||||||
|
book.setRatingsCount(100);
|
||||||
|
book.setReviewsCount(50);
|
||||||
|
book.setPages(350);
|
||||||
|
book.setReleaseDate("2023-01-15");
|
||||||
|
book.setReleaseYear(2023);
|
||||||
|
|
||||||
|
// Series info
|
||||||
|
GraphQLResponse.Series series = new GraphQLResponse.Series();
|
||||||
|
series.setName("Test Series");
|
||||||
|
series.setBooksCount(5);
|
||||||
|
|
||||||
|
GraphQLResponse.FeaturedSeries featuredSeries = new GraphQLResponse.FeaturedSeries();
|
||||||
|
featuredSeries.setSeries(series);
|
||||||
|
featuredSeries.setPosition(2);
|
||||||
|
book.setFeaturedBookSeries(featuredSeries);
|
||||||
|
|
||||||
|
GraphQLResponse.Image image = new GraphQLResponse.Image();
|
||||||
|
image.setUrl("https://example.com/cover.jpg");
|
||||||
|
book.setImage(image);
|
||||||
|
|
||||||
|
// Cached contributors for the book
|
||||||
|
GraphQLResponse.Author bookAuthor = new GraphQLResponse.Author();
|
||||||
|
bookAuthor.setId(1);
|
||||||
|
bookAuthor.setSlug("test-author");
|
||||||
|
bookAuthor.setName("Test Author");
|
||||||
|
|
||||||
|
GraphQLResponse.Contributor bookContributor = new GraphQLResponse.Contributor();
|
||||||
|
bookContributor.setAuthor(bookAuthor);
|
||||||
|
bookContributor.setContribution("Author");
|
||||||
|
book.setCachedContributors(List.of(bookContributor));
|
||||||
|
|
||||||
|
// Editions
|
||||||
|
GraphQLResponse.Edition edition = new GraphQLResponse.Edition();
|
||||||
|
edition.setId(1);
|
||||||
|
edition.setTitle("Test Book - Hardcover Edition");
|
||||||
|
edition.setSubtitle("A Subtitle");
|
||||||
|
edition.setPages(350);
|
||||||
|
edition.setReleaseDate("2023-01-15");
|
||||||
|
edition.setReleaseYear(2023);
|
||||||
|
|
||||||
|
// Cached contributors for the edition
|
||||||
|
GraphQLResponse.Author editionAuthor = new GraphQLResponse.Author();
|
||||||
|
editionAuthor.setId(1);
|
||||||
|
editionAuthor.setSlug("test-author");
|
||||||
|
editionAuthor.setName("Test Author");
|
||||||
|
|
||||||
|
GraphQLResponse.Contributor editionContributor = new GraphQLResponse.Contributor();
|
||||||
|
editionContributor.setAuthor(editionAuthor);
|
||||||
|
editionContributor.setContribution("Author");
|
||||||
|
edition.setCachedContributors(List.of(editionContributor));
|
||||||
|
|
||||||
|
GraphQLResponse.Image editionImage = new GraphQLResponse.Image();
|
||||||
|
editionImage.setUrl("https://example.com/edition-cover.jpg");
|
||||||
|
edition.setImage(editionImage);
|
||||||
|
|
||||||
|
edition.setIsbn10("123456789X");
|
||||||
|
edition.setIsbn13("9781234567897");
|
||||||
|
|
||||||
|
GraphQLResponse.Publisher publisher = new GraphQLResponse.Publisher();
|
||||||
|
publisher.setName("Test Publisher");
|
||||||
|
edition.setPublisher(publisher);
|
||||||
|
|
||||||
|
GraphQLResponse.Language language = new GraphQLResponse.Language();
|
||||||
|
language.setCode2("en");
|
||||||
|
edition.setLanguage(language);
|
||||||
|
|
||||||
|
book.setEditions(List.of(edition));
|
||||||
|
|
||||||
|
// Cached tags for the book (moods, genres, tags)
|
||||||
|
GraphQLResponse.CachedTags cachedTags = new GraphQLResponse.CachedTags();
|
||||||
|
cachedTags.setMood(List.of(createCachedTag("adventurous", 15), createCachedTag("exciting", 12), createCachedTag("novotes", 0)));
|
||||||
|
cachedTags.setGenre(List.of(createCachedTag("fiction", 20), createCachedTag("fantasy", 18), createCachedTag("novotes", 0)));
|
||||||
|
cachedTags.setTag(List.of(createCachedTag("epic", 10), createCachedTag("quest", 8), createCachedTag("novotes", 0)));
|
||||||
|
book.setCachedTags(cachedTags);
|
||||||
|
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
|
||||||
|
private HardcoverCachedTag createCachedTag(String tag, int count) {
|
||||||
|
HardcoverCachedTag cachedTag = new HardcoverCachedTag();
|
||||||
cachedTag.setTag(tag);
|
cachedTag.setTag(tag);
|
||||||
cachedTag.setCount(count);
|
cachedTag.setCount(count);
|
||||||
return cachedTag;
|
return cachedTag;
|
||||||
|
|||||||
+13
-13
@@ -86,14 +86,14 @@ class HardcoverMoodFilterTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("filterMoodsWithCounts should return empty set for null cached_tags")
|
@DisplayName("filterMoodsWithCounts should return empty set for null cached_tags")
|
||||||
void filterMoodsWithCounts_nullCachedTags_returnsEmptySet() {
|
void filterMoodsWithCounts_nullCachedTags_returnsEmptySet() {
|
||||||
Set<String> result = HardcoverMoodFilter.filterMoodsWithCounts(null);
|
Set<String> result = HardcoverMoodFilter.filterMoodsWithCounts((Map<String, List<HardcoverCachedTag>>) null);
|
||||||
assertThat(result).isEmpty();
|
assertThat(result).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("filterMoodsWithCounts should return empty set when no Mood category")
|
@DisplayName("filterMoodsWithCounts should return empty set when no Mood category")
|
||||||
void filterMoodsWithCounts_noMoodCategory_returnsEmptySet() {
|
void filterMoodsWithCounts_noMoodCategory_returnsEmptySet() {
|
||||||
Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags = new HashMap<>();
|
Map<String, List<HardcoverCachedTag>> cachedTags = new HashMap<>();
|
||||||
cachedTags.put("Genre", List.of(createCachedTag("Fiction", 10)));
|
cachedTags.put("Genre", List.of(createCachedTag("Fiction", 10)));
|
||||||
|
|
||||||
Set<String> result = HardcoverMoodFilter.filterMoodsWithCounts(cachedTags);
|
Set<String> result = HardcoverMoodFilter.filterMoodsWithCounts(cachedTags);
|
||||||
@@ -108,7 +108,7 @@ class HardcoverMoodFilterTest {
|
|||||||
// "sad" has 17 votes, "funny" has 4 votes
|
// "sad" has 17 votes, "funny" has 4 votes
|
||||||
// With max=17 and MIN_VOTE_RATIO=0.15, threshold is ~3
|
// With max=17 and MIN_VOTE_RATIO=0.15, threshold is ~3
|
||||||
// But "funny" at 4 votes should still be filtered if we use higher threshold
|
// But "funny" at 4 votes should still be filtered if we use higher threshold
|
||||||
Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags = new HashMap<>();
|
Map<String, List<HardcoverCachedTag>> cachedTags = new HashMap<>();
|
||||||
cachedTags.put("Mood", List.of(
|
cachedTags.put("Mood", List.of(
|
||||||
createCachedTag("sad", 17),
|
createCachedTag("sad", 17),
|
||||||
createCachedTag("dark", 16),
|
createCachedTag("dark", 16),
|
||||||
@@ -129,8 +129,8 @@ class HardcoverMoodFilterTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("filterMoodsWithCounts should respect MAX_MOODS limit")
|
@DisplayName("filterMoodsWithCounts should respect MAX_MOODS limit")
|
||||||
void filterMoodsWithCounts_manyHighVoteMoods_limitsToMax() {
|
void filterMoodsWithCounts_manyHighVoteMoods_limitsToMax() {
|
||||||
Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags = new HashMap<>();
|
Map<String, List<HardcoverCachedTag>> cachedTags = new HashMap<>();
|
||||||
List<HardcoverBookDetails.CachedTag> moodTags = new ArrayList<>();
|
List<HardcoverCachedTag> moodTags = new ArrayList<>();
|
||||||
// Create 10 moods all with high vote counts
|
// Create 10 moods all with high vote counts
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
moodTags.add(createCachedTag("mood" + i, 20 - i));
|
moodTags.add(createCachedTag("mood" + i, 20 - i));
|
||||||
@@ -145,7 +145,7 @@ class HardcoverMoodFilterTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("filterMoodsWithCounts should order moods by vote count (highest first)")
|
@DisplayName("filterMoodsWithCounts should order moods by vote count (highest first)")
|
||||||
void filterMoodsWithCounts_orderedByVoteCount() {
|
void filterMoodsWithCounts_orderedByVoteCount() {
|
||||||
Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags = new HashMap<>();
|
Map<String, List<HardcoverCachedTag>> cachedTags = new HashMap<>();
|
||||||
cachedTags.put("Mood", List.of(
|
cachedTags.put("Mood", List.of(
|
||||||
createCachedTag("emotional", 5),
|
createCachedTag("emotional", 5),
|
||||||
createCachedTag("sad", 15),
|
createCachedTag("sad", 15),
|
||||||
@@ -162,8 +162,8 @@ class HardcoverMoodFilterTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("filterMoodsWithCounts should handle null vote counts")
|
@DisplayName("filterMoodsWithCounts should handle null vote counts")
|
||||||
void filterMoodsWithCounts_nullVoteCounts_handlesGracefully() {
|
void filterMoodsWithCounts_nullVoteCounts_handlesGracefully() {
|
||||||
Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags = new HashMap<>();
|
Map<String, List<HardcoverCachedTag>> cachedTags = new HashMap<>();
|
||||||
HardcoverBookDetails.CachedTag tagWithNullCount = createCachedTag("mysterious", null);
|
HardcoverCachedTag tagWithNullCount = createCachedTag("mysterious", null);
|
||||||
cachedTags.put("Mood", List.of(
|
cachedTags.put("Mood", List.of(
|
||||||
createCachedTag("sad", 10),
|
createCachedTag("sad", 10),
|
||||||
tagWithNullCount,
|
tagWithNullCount,
|
||||||
@@ -179,7 +179,7 @@ class HardcoverMoodFilterTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("filterMoodsWithCounts should return empty when all votes are zero")
|
@DisplayName("filterMoodsWithCounts should return empty when all votes are zero")
|
||||||
void filterMoodsWithCounts_allZeroVotes_returnsEmpty() {
|
void filterMoodsWithCounts_allZeroVotes_returnsEmpty() {
|
||||||
Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags = new HashMap<>();
|
Map<String, List<HardcoverCachedTag>> cachedTags = new HashMap<>();
|
||||||
cachedTags.put("Mood", List.of(
|
cachedTags.put("Mood", List.of(
|
||||||
createCachedTag("sad", 0),
|
createCachedTag("sad", 0),
|
||||||
createCachedTag("dark", 0)
|
createCachedTag("dark", 0)
|
||||||
@@ -193,7 +193,7 @@ class HardcoverMoodFilterTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("filterGenresWithCounts should return filtered genres")
|
@DisplayName("filterGenresWithCounts should return filtered genres")
|
||||||
void filterGenresWithCounts_validData_returnsFiltered() {
|
void filterGenresWithCounts_validData_returnsFiltered() {
|
||||||
Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags = new HashMap<>();
|
Map<String, List<HardcoverCachedTag>> cachedTags = new HashMap<>();
|
||||||
cachedTags.put("Genre", List.of(
|
cachedTags.put("Genre", List.of(
|
||||||
createCachedTag("Fiction", 10),
|
createCachedTag("Fiction", 10),
|
||||||
createCachedTag("War", 8),
|
createCachedTag("War", 8),
|
||||||
@@ -210,7 +210,7 @@ class HardcoverMoodFilterTest {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("filterTagsWithCounts should filter tags with low votes")
|
@DisplayName("filterTagsWithCounts should filter tags with low votes")
|
||||||
void filterTagsWithCounts_mixedVotes_filtersLowVotes() {
|
void filterTagsWithCounts_mixedVotes_filtersLowVotes() {
|
||||||
Map<String, List<HardcoverBookDetails.CachedTag>> cachedTags = new HashMap<>();
|
Map<String, List<HardcoverCachedTag>> cachedTags = new HashMap<>();
|
||||||
cachedTags.put("Tag", List.of(
|
cachedTags.put("Tag", List.of(
|
||||||
createCachedTag("Loveable Characters", 13),
|
createCachedTag("Loveable Characters", 13),
|
||||||
createCachedTag("Strong Character Development", 9),
|
createCachedTag("Strong Character Development", 9),
|
||||||
@@ -249,8 +249,8 @@ class HardcoverMoodFilterTest {
|
|||||||
/**
|
/**
|
||||||
* Helper method to create a CachedTag for testing.
|
* Helper method to create a CachedTag for testing.
|
||||||
*/
|
*/
|
||||||
private HardcoverBookDetails.CachedTag createCachedTag(String tag, Integer count) {
|
private HardcoverCachedTag createCachedTag(String tag, Integer count) {
|
||||||
HardcoverBookDetails.CachedTag cachedTag = new HardcoverBookDetails.CachedTag();
|
HardcoverCachedTag cachedTag = new HardcoverCachedTag();
|
||||||
cachedTag.setTag(tag);
|
cachedTag.setTag(tag);
|
||||||
cachedTag.setCount(count);
|
cachedTag.setCount(count);
|
||||||
return cachedTag;
|
return cachedTag;
|
||||||
|
|||||||
Reference in New Issue
Block a user