fix(hardcover): use injected restclient with expected timeouts (#2320)

This commit is contained in:
James Ward
2026-08-12 04:05:29 -04:00
committed by GitHub
parent 41fa0c107a
commit 9d15192c18
4 changed files with 20 additions and 19 deletions
@@ -43,12 +43,14 @@ public class HardcoverSyncService {
private final ThreadLocal<String> currentApiToken = new ThreadLocal<>();
@Autowired
public HardcoverSyncService(HardcoverSyncSettingsService hardcoverSyncSettingsService, BookRepository bookRepository) {
public HardcoverSyncService(
HardcoverSyncSettingsService hardcoverSyncSettingsService,
BookRepository bookRepository,
RestClient restClient
) {
this.hardcoverSyncSettingsService = hardcoverSyncSettingsService;
this.bookRepository = bookRepository;
this.restClient = RestClient.builder()
.baseUrl(HARDCOVER_API_URL)
.build();
this.restClient = restClient;
}
/**
@@ -795,7 +797,7 @@ public class HardcoverSyncService {
private Map<String, Object> executeGraphQL(GraphQLRequest request) {
try {
return restClient.post()
.uri("")
.uri(HARDCOVER_API_URL)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + getApiToken())
.body(request)
@@ -16,7 +16,7 @@ import java.util.concurrent.atomic.AtomicLong;
@Slf4j
@Service
public class HardcoverBookSearchService {
private static final String GRAPHQL_URI = "https://api.hardcover.app/v1/graphql";
public static final int DEFAULT_PER_PAGE = 10;
private static final long INITIAL_DELAY_MS = 1000;
private static final long MAX_DELAY_MS = 15000;
@@ -27,11 +27,9 @@ public class HardcoverBookSearchService {
private final AtomicLong lastRequestTime = new AtomicLong(0);
private final AtomicLong successCount = new AtomicLong(0);
public HardcoverBookSearchService(AppSettingService appSettingService) {
public HardcoverBookSearchService(AppSettingService appSettingService, RestClient restClient) {
this.appSettingService = appSettingService;
this.restClient = RestClient.builder()
.baseUrl("https://api.hardcover.app/v1/graphql")
.build();
this.restClient = restClient;
}
public List<GraphQLResponse.BookWithEditions> searchBookByIsbn(List<String> isbn) {
@@ -169,7 +167,7 @@ public class HardcoverBookSearchService {
try {
T response = restClient.post()
.uri("")
.uri(GRAPHQL_URI)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + apiToken)
.body(body)
@@ -6,10 +6,10 @@ import org.booklore.model.entity.BookMetadataEntity;
import org.booklore.repository.BookRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
@@ -48,6 +48,7 @@ class HardcoverSyncServiceTest {
@Mock
private RestClient.ResponseSpec responseSpec;
@InjectMocks
private HardcoverSyncService service;
private BookEntity testBook;
@@ -60,12 +61,7 @@ class HardcoverSyncServiceTest {
@BeforeEach
void setUp() throws Exception {
// Create service with mocked dependencies
service = new HardcoverSyncService(hardcoverSyncSettingsService, bookRepository);
// Inject our mocked restClient using reflection
Field restClientField = HardcoverSyncService.class.getDeclaredField("restClient");
restClientField.setAccessible(true);
restClientField.set(service, restClient);
service = new HardcoverSyncService(hardcoverSyncSettingsService, bookRepository, restClient);
testBook = new BookEntity();
testBook.setId(TEST_BOOK_ID);
@@ -7,8 +7,10 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.web.client.RestClient;
import java.util.List;
@@ -20,12 +22,15 @@ class HardcoverBookSearchServiceTest {
@Mock
private AppSettingService appSettingService;
@Mock
private RestClient restClient;
@InjectMocks
private HardcoverBookSearchService searchService;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
searchService = new HardcoverBookSearchService(appSettingService);
}
private void setupAppSettingsWithToken(String token) {