fix(metadata): read proper description field for LubimyCzytac (#877)
This commit is contained in:
@@ -232,7 +232,7 @@ public class LubimyCzytacParser implements BookParser {
|
|||||||
metadata.setLanguage(mapLanguage(language));
|
metadata.setLanguage(mapLanguage(language));
|
||||||
}
|
}
|
||||||
|
|
||||||
Element descElement = doc.selectFirst(".collapse-content");
|
Element descElement = doc.selectFirst("#book-description");
|
||||||
if (descElement != null) {
|
if (descElement != null) {
|
||||||
String description = descElement.text();
|
String description = descElement.text();
|
||||||
if (description != null && !description.isBlank()) {
|
if (description != null && !description.isBlank()) {
|
||||||
|
|||||||
+74
-14
@@ -6,29 +6,54 @@ import org.booklore.model.dto.request.FetchMetadataRequest;
|
|||||||
import org.booklore.model.dto.settings.AppSettings;
|
import org.booklore.model.dto.settings.AppSettings;
|
||||||
import org.booklore.model.dto.settings.MetadataProviderSettings;
|
import org.booklore.model.dto.settings.MetadataProviderSettings;
|
||||||
import org.booklore.service.appsettings.AppSettingService;
|
import org.booklore.service.appsettings.AppSettingService;
|
||||||
|
import org.jsoup.Connection;
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
import org.jsoup.nodes.Document;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
class LubimyCzytacParserTest {
|
class LubimyCzytacParserTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private AppSettingService appSettingService;
|
private AppSettingService appSettingService;
|
||||||
|
|
||||||
|
private MockedStatic<Jsoup> mockJsoup;
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private LubimyCzytacParser parser;
|
private LubimyCzytacParser parser;
|
||||||
|
|
||||||
|
private String exampleSearchHtmlFixture;
|
||||||
|
|
||||||
|
private String exampleBookHtmlFixture;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() throws IOException {
|
||||||
MockitoAnnotations.openMocks(this);
|
exampleSearchHtmlFixture = readFixture("example-search.html");
|
||||||
|
exampleBookHtmlFixture = readFixture("example-book.html");
|
||||||
|
|
||||||
|
mockJsoup = mockStatic(Jsoup.class);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
mockJsoup.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -118,16 +143,15 @@ class LubimyCzytacParserTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled("Integration test - requires network access to LubimyCzytac.pl")
|
void testFetchMetadata_parsesBook() throws Exception {
|
||||||
void testFetchMetadata_Integration_RealBook() {
|
|
||||||
// Given
|
// Given
|
||||||
Book book = Book.builder()
|
Book book = Book.builder()
|
||||||
.title("Wiedźmin")
|
.title("Sklepy cynamonowe")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||||
.title("Wiedźmin")
|
.title("Sklepy cynamonowe")
|
||||||
.author("Andrzej Sapkowski")
|
.author("Bruno Schulz")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Mock enabled provider
|
// Mock enabled provider
|
||||||
@@ -138,6 +162,10 @@ class LubimyCzytacParserTest {
|
|||||||
providerSettings.setLubimyczytac(lubimyCzytac);
|
providerSettings.setLubimyczytac(lubimyCzytac);
|
||||||
appSettings.setMetadataProviderSettings(providerSettings);
|
appSettings.setMetadataProviderSettings(providerSettings);
|
||||||
|
|
||||||
|
// Two expected URLs
|
||||||
|
mockResponse("https://lubimyczytac.pl/szukaj/ksiazki", exampleSearchHtmlFixture);
|
||||||
|
mockResponse("https://lubimyczytac.pl/ksiazka/", exampleBookHtmlFixture);
|
||||||
|
|
||||||
when(appSettingService.getAppSettings()).thenReturn(appSettings);
|
when(appSettingService.getAppSettings()).thenReturn(appSettings);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
@@ -147,10 +175,42 @@ class LubimyCzytacParserTest {
|
|||||||
assertNotNull(results);
|
assertNotNull(results);
|
||||||
assertFalse(results.isEmpty(), "Should return results for real book");
|
assertFalse(results.isEmpty(), "Should return results for real book");
|
||||||
|
|
||||||
BookMetadata firstResult = results.getFirst();
|
BookMetadata result = results.getFirst();
|
||||||
assertNotNull(firstResult.getTitle(), "Title should be present");
|
assertEquals("Sklepy cynamonowe", result.getTitle());
|
||||||
assertNotNull(firstResult.getLubimyczytacId(), "LubimyCzytac ID should be present");
|
assertNull(result.getIsbn10());
|
||||||
assertTrue(firstResult.getAuthors() != null && !firstResult.getAuthors().isEmpty(),
|
assertEquals("9788384258149", result.getIsbn13());
|
||||||
"Authors should be present");
|
assertEquals("5223841", result.getLubimyczytacId());
|
||||||
|
assertNotNull(result.getAuthors());
|
||||||
|
assertEquals(1, result.getAuthors().size());
|
||||||
|
assertEquals("Bruno Schulz", result.getAuthors().getFirst());
|
||||||
|
|
||||||
|
// The description is very long, but we need to make sure we're in the right ballpark.
|
||||||
|
assertEquals("Jedno z najoryginalniejszych", result.getDescription().substring(0, 28));
|
||||||
|
assertEquals(827, result.getDescription().length());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readFixture(String fixtureName) throws IOException {
|
||||||
|
String filename = Paths.get("lubimyczytac", fixtureName + ".fixture").toString();
|
||||||
|
|
||||||
|
try (InputStream is = getClass().getClassLoader().getResourceAsStream(filename)) {
|
||||||
|
assert is != null;
|
||||||
|
|
||||||
|
return new String(is.readAllBytes(), StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mockResponse(String urlPrefix, String response) throws Exception {
|
||||||
|
Connection mockConnection = mock(Connection.class);
|
||||||
|
|
||||||
|
when(mockConnection.userAgent(any())).thenReturn(mockConnection);
|
||||||
|
when(mockConnection.timeout(anyInt())).thenReturn(mockConnection);
|
||||||
|
|
||||||
|
// There may be a better way to get the parse to work here.
|
||||||
|
// However, this was the quickest and simplest way I could find.
|
||||||
|
mockJsoup.when(() -> Jsoup.parse(response)).thenCallRealMethod();
|
||||||
|
|
||||||
|
when(mockConnection.get()).thenAnswer(i -> Jsoup.parse(response));
|
||||||
|
|
||||||
|
mockJsoup.when(() -> Jsoup.connect(startsWith(urlPrefix))).thenReturn(mockConnection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user