refactor: make PagingRequest an immutable record
Build and Analyse / build-and-analyse (push) Failing after 1m17s

- Replace Lombok-based PagingRequest with an immutable record (page(), perPage())
- Centralize paging limits (DEFAULT_PAGE_SIZE, MAX_RECORDS_PER_PAGE, MIN/MAX_ZONES_PER_PAGE)
- Add forRecords(), forZones(), defaultPaging(), next() and withPage(); remove setters and getPagingParams()
- Include invalid values in validation messages, reject null endpoints in addQueryString()
- Use an internal page size of 1000 when fetching all DNS records
- Adapt CfDnsClient, tests, README and changelog

Co-authored-by: Junie <junie@jetbrains.com>
This commit is contained in:
thischwaandJunie committed 2026-11-07 11:55:12 +01:00
1 parent 6ab318063f
commit be0512111d
9 files changed
+187 -187

No files matched your search

@@ -87,17 +87,10 @@ import org.jetbrains.annotations.Nullable;
@Slf4j
public class CfDnsClient extends CfBasicHttpClient {
/**
* Page size used when fetching all zones. Cloudflare allows 5 to 50 zones per page.
*/
static final int ZONES_PER_PAGE = 50;
private static final int MIN_ZONES_PER_PAGE = 5;
/**
* Page size used when fetching all DNS records.
*/
static final int MAX_RECORDS_PER_PAGE = 1000;
static final int RECORDS_PER_PAGE = 1000;
/**
* Sort order for listing DNS records. Cloudflare doesn't guarantee a stable order without it,
@@ -131,7 +124,7 @@ public class CfDnsClient extends CfBasicHttpClient {
* @param auth The authentication mechanism to use (ApiTokenAuth or EmailKeyAuth)
*/
CfDnsClient(boolean emptyResultThrowsException, String baseUrl, CfDnsClientBuilder.CfAuth auth) {
this(emptyResultThrowsException, baseUrl, auth, MAX_RECORDS_PER_PAGE);
this(emptyResultThrowsException, baseUrl, auth, RECORDS_PER_PAGE);
}
/**
@@ -235,7 +228,8 @@ public class CfDnsClient extends CfBasicHttpClient {
* @throws CloudflareApiException If an error occurs during the API request or response handling.
*/
public List<ZoneEntity> zoneList() throws CloudflareApiException {
return getAllPages(CfRequest.ZONE_LIST.buildPath(), ZoneMultipleResponse.class, ZONES_PER_PAGE);
return getAllPages(CfRequest.ZONE_LIST.buildPath(), ZoneMultipleResponse.class,
PagingRequest.DEFAULT_PAGE_SIZE);
}
/**
@@ -250,10 +244,13 @@ public class CfDnsClient extends CfBasicHttpClient {
* @throws IllegalArgumentException if the page size is outside the range 5 to 50
*/
public List<ZoneEntity> zoneList(PagingRequest pagingRequest) throws CloudflareApiException {
if (pagingRequest.getPerPage() < MIN_ZONES_PER_PAGE
|| pagingRequest.getPerPage() > ZONES_PER_PAGE) {
throw new IllegalArgumentException("Zone page size must be between " + MIN_ZONES_PER_PAGE
+ " and " + ZONES_PER_PAGE);
Objects.requireNonNull(pagingRequest, "pagingRequest must not be null");
int perPage = pagingRequest.perPage();
if (perPage < PagingRequest.MIN_ZONES_PER_PAGE
|| perPage > PagingRequest.MAX_ZONES_PER_PAGE) {
throw new IllegalArgumentException("Zone page size must be between "
+ PagingRequest.MIN_ZONES_PER_PAGE + " and " + PagingRequest.MAX_ZONES_PER_PAGE
+ ", but was " + perPage);
}
String endpoint = pagingRequest.addQueryString(CfRequest.ZONE_LIST.buildPath());
ZoneMultipleResponse response = getRequest(endpoint, ZoneMultipleResponse.class);
@@ -299,7 +296,7 @@ public class CfDnsClient extends CfBasicHttpClient {
*
* @param zone The zone entity containing information about the target zone.
* @param pagingRequest The paging request containing parameters such as page size and number.
* If {@code null}, all pages are fetched using the maximum supported page size.
* If {@code null}, all pages are fetched using an internal page size of 1,000.
* For a specific page, Cloudflare allows a page size between 1 and 5,000,000.
* @return A list of RecordEntity objects representing the DNS records of the specified zone.
* @throws CloudflareApiException If an error occurs during the API request or response processing.
@@ -309,7 +306,7 @@ public class CfDnsClient extends CfBasicHttpClient {
String path = appendQuery(CfRequest.RECORD_LIST.buildPath(zone.getId()), RECORD_ORDER);
if (pagingRequest == null) {
return filterAndSetZoneRecords(zone, null,
getAllPages(path, RecordMultipleResponse.class, PagingRequest.MAX_PER_PAGE));
getAllPages(path, RecordMultipleResponse.class, recordsPerPage));
}
RecordMultipleResponse resp = getRequest(pagingRequest.addQueryString(path), RecordMultipleResponse.class);
checkResponse(resp);
@@ -682,9 +679,10 @@ public class CfDnsClient extends CfBasicHttpClient {
Map<String, E> byId = new LinkedHashMap<>();
List<E> withoutId = new ArrayList<>();
int expectedCount = -1;
int page = 1;
PagingRequest pagingRequest = new PagingRequest(1, perPage);
while (true) {
String pagedEndpoint = PagingRequest.of(page, perPage).addQueryString(endpoint);
int page = pagingRequest.page();
String pagedEndpoint = pagingRequest.addQueryString(endpoint);
R resp = getRequest(pagedEndpoint, responseType);
checkResponse(resp);
List<E> pageResult = resp.getResult();
@@ -709,7 +707,7 @@ public class CfDnsClient extends CfBasicHttpClient {
all.addAll(withoutId);
return new PagingResult<>(all, info == null || page == 1 ? -1 : expectedCount);
}
page++;
pagingRequest = pagingRequest.next();
}
}
@@ -195,7 +195,7 @@ public class CfDnsClientBuilder {
"No authentication configured, use withApiTokenAuth(...) or withEmailKeyAuth(...)");
}
String url = baseUrl == null ? DEFAULT_BASEURL : baseUrl;
return new CfDnsClient(emptyResultThrowsException, url, auth, CfDnsClient.MAX_RECORDS_PER_PAGE,
return new CfDnsClient(emptyResultThrowsException, url, auth, CfDnsClient.RECORDS_PER_PAGE,
connectTimeout, responseTimeout);
}
@@ -37,7 +37,7 @@ public interface ZoneOperations {
/**
* Lists all DNS records within the zone, optionally filtered by types.
*
* @param types optional DNS record types to filter by
* @param types optional DNS record types to filter by
* @return a list of RecordEntity objects matching the criteria
* @throws CloudflareApiException if an error occurs while retrieving records
*/
@@ -1,130 +1,112 @@
package codes.thischwa.cf.model;
import java.util.Map;
import lombok.Data;
import java.util.Objects;
/**
* Represents a request model for paginated data.
* Immutable pagination parameters for Cloudflare list endpoints. Page numbers start at 1.
*
* <p>This class encapsulates the page number and the number of items per page for a paginated
* request, along with utility methods for constructing and retrieving pagination parameters. Page
* numbers must be at least 1, and page sizes must be between 1 and 5,000,000 for DNS record requests.
* Zone requests have the more restrictive Cloudflare limit of 5 to 50 items per page.
* <p>Use {@link #forZones(int, int)} for zone requests and {@link #forRecords(int, int)} for DNS
* record requests. For example:
*
* <p>Key functionalities:
*
* <ul>
* <li>Creating a {@code PagingRequest} instance with specific pagination values using the {@code
* of} method.
* <li>Creating a default {@code PagingRequest} with predefined pagination values.
* <li>Retrieving pagination parameters as a key-value map.
* <li>Generating a query string representation for the pagination parameters.
* </ul>
* <pre>{@code
* List<RecordEntity> records = client.recordList(zone, PagingRequest.forRecords(1, 100));
* List<ZoneEntity> zones = client.zoneList(PagingRequest.forZones(1, 50));
* }</pre>
*/
@Data
public class PagingRequest {
/**
* Default page size for a single request, which is within the limits for zones and DNS records.
*/
private static final int DEFAULT_PAGE_SIZE = 50;
public record PagingRequest(int page, int perPage) {
/**
* Maximum number of DNS records allowed per page by Cloudflare.
*/
public static final int MAX_PER_PAGE = 5_000_000;
/** Default page size, valid for zones and DNS records. */
public static final int DEFAULT_PAGE_SIZE = 50;
private int page;
private int perPage;
/** Maximum number of DNS records allowed per page by Cloudflare. */
public static final int MAX_RECORDS_PER_PAGE = 5_000_000;
PagingRequest(int page, int perPage) {
this.page = validatePage(page);
this.perPage = validatePerPage(perPage);
/** Minimum number of zones allowed per page by Cloudflare. */
public static final int MIN_ZONES_PER_PAGE = 5;
/** Maximum number of zones allowed per page by Cloudflare. */
public static final int MAX_ZONES_PER_PAGE = 50;
/** Validates the page number and page size. */
public PagingRequest {
if (page < 1) {
throw new IllegalArgumentException("page must be at least 1, but was " + page);
}
if (perPage < 1 || perPage > MAX_RECORDS_PER_PAGE) {
throw new IllegalArgumentException("perPage must be between 1 and " + MAX_RECORDS_PER_PAGE
+ ", but was " + perPage);
}
}
/**
* Creates a new {@code PagingRequest} instance with the specified page number and items per page.
* The page must be at least 1, and the page size must be between 1 and 5,000,000. Zone requests
* have a more restrictive page-size range of 5 to 50, enforced by {@code zoneList}.
* Creates pagination parameters for a DNS record request.
*
* @param page the page number to be requested
* @param perPage the number of items to be included per page
* @return a new {@code PagingRequest} instance with the provided parameters
* @throws IllegalArgumentException if the page or page size is outside the allowed range
* @param page the 1-based page number
* @param perPage the number of records per page, from 1 to {@value #MAX_RECORDS_PER_PAGE}
* @return the validated pagination request
* @throws IllegalArgumentException if either value is outside its allowed range
*/
public static PagingRequest of(int page, int perPage) {
public static PagingRequest forRecords(int page, int perPage) {
return new PagingRequest(page, perPage);
}
/**
* Creates a default {@code PagingRequest} instance for page 1 with 50 items per page. This is a
* request for one page; it does not fetch additional pages.
* Creates pagination parameters for a zone request.
*
* @return a default {@code PagingRequest} instance with predefined pagination parameters
* @param page the 1-based page number
* @param perPage the number of zones per page, from {@value #MIN_ZONES_PER_PAGE} to
* {@value #MAX_ZONES_PER_PAGE}
* @return the validated pagination request
* @throws IllegalArgumentException if either value is outside its allowed range
*/
public static PagingRequest forZones(int page, int perPage) {
if (perPage < MIN_ZONES_PER_PAGE || perPage > MAX_ZONES_PER_PAGE) {
throw new IllegalArgumentException("Zone page size must be between " + MIN_ZONES_PER_PAGE
+ " and " + MAX_ZONES_PER_PAGE + ", but was " + perPage);
}
return new PagingRequest(page, perPage);
}
/**
* Creates a request for page 1 with {@value #DEFAULT_PAGE_SIZE} items per page.
* This requests one page; it does not fetch additional pages.
*
* @return the default pagination request
*/
public static PagingRequest defaultPaging() {
return new PagingRequest(1, DEFAULT_PAGE_SIZE);
}
/**
* Sets the page number.
* Returns a request for the next page, retaining the current page size.
*
* @param page the page number, which must be at least 1
* @throws IllegalArgumentException if the page number is less than 1
* @return the next page request
*/
public void setPage(int page) {
this.page = validatePage(page);
public PagingRequest next() {
return new PagingRequest(page + 1, perPage);
}
/**
* Sets the number of items per page.
* Returns a copy of this request with the specified page number.
*
* @param perPage the page size, between 1 and 5,000,000 for DNS record requests
* @throws IllegalArgumentException if the page size is outside the allowed range
* @param page the 1-based page number
* @return the updated pagination request
*/
public void setPerPage(int perPage) {
this.perPage = validatePerPage(perPage);
public PagingRequest withPage(int page) {
return new PagingRequest(page, perPage);
}
/**
* Retrieves the pagination parameters in a key-value map format.
* Appends Cloudflare-compatible pagination parameters ({@code page} and {@code per_page}) to an
* endpoint.
*
* @return a map containing the pagination parameters, where the key "page" indicates the current
* page number and the key "perPage" indicates the number of items per page. The map key is
* intentionally named "perPage"; Cloudflare's query parameter is "per_page". Use
* {@link #addQueryString(String)} to build a Cloudflare-compatible query string.
*/
public Map<String, String> getPagingParams() {
return Map.of("page", String.valueOf(page), "perPage", String.valueOf(perPage));
}
/**
* Appends a query string with pagination parameters ({@code page} and {@code per_page}) to the
* provided endpoint. Unlike {@link #getPagingParams()}, this method uses Cloudflare's
* {@code per_page} query parameter name.
*
* @param endpoint the base URL or API endpoint to which the query string will be appended
* @return the complete URL with the appended query string for pagination
* @param endpoint the base URL or API endpoint, optionally containing a query string
* @return the endpoint with pagination parameters appended
* @throws NullPointerException if {@code endpoint} is {@code null}
*/
public String addQueryString(String endpoint) {
return endpoint + queryString(endpoint.contains("?"));
}
private String queryString(boolean add) {
String qs = "page=" + page + "&per_page=" + perPage;
return add ? "&" + qs : "?" + qs;
}
private static int validatePage(int page) {
if (page < 1) {
throw new IllegalArgumentException("page must be at least 1");
}
return page;
}
private static int validatePerPage(int perPage) {
if (perPage < 1 || perPage > MAX_PER_PAGE) {
throw new IllegalArgumentException("perPage must be between 1 and " + MAX_PER_PAGE);
}
return perPage;
Objects.requireNonNull(endpoint, "endpoint must not be null");
return endpoint + (endpoint.contains("?") ? "&" : "?")
+ "page=" + page + "&per_page=" + perPage;
}
}
@@ -109,7 +109,7 @@ public class CfClientTest {
assertFalse(zones.isEmpty());
assertEquals(ZONE_STR, zones.get(0).getName());
zones = client.zoneList(PagingRequest.of(1, 50));
zones = client.zoneList(PagingRequest.forZones(1, 50));
assertNotNull(zones);
assertFalse(zones.isEmpty());
assertEquals(ZONE_STR, zones.get(0).getName());
@@ -502,17 +502,17 @@ public class CfClientTest {
}
// Test paging with page size of 5
PagingRequest page1Request = PagingRequest.of(1, 5);
PagingRequest page1Request = PagingRequest.forRecords(1, 5);
List<RecordEntity> page1Records = client.recordList(zone, page1Request);
assertEquals(5, page1Records.size(), "First page should contain 5 records");
// 2nd page should also contain 5 records (if we have at least 12 total)
PagingRequest page2Request = PagingRequest.of(2, 5);
PagingRequest page2Request = PagingRequest.forRecords(2, 5);
List<RecordEntity> page2Records = client.recordList(zone, page2Request);
assertEquals(5, page2Records.size(), "Second page should contain at least 5 records");
// 3rd page should contain 2 records
PagingRequest page3Request = PagingRequest.of(3, 5);
PagingRequest page3Request = PagingRequest.forRecords(3, 5);
List<RecordEntity> page3Records = client.recordList(zone, page3Request);
assertEquals(2, page3Records.size(), "Third page should contain 2 records");
@@ -128,8 +128,8 @@ class CfDnsClientMockTest {
CfDnsClient spyClient = spy(client);
doReturn(mockResponse).when(spyClient).getRequest(anyString(), eq(ZoneMultipleResponse.class));
spyClient.zoneList(PagingRequest.of(1, 5));
spyClient.zoneList(PagingRequest.of(1, 50));
spyClient.zoneList(PagingRequest.forZones(1, 5));
spyClient.zoneList(PagingRequest.forZones(1, 50));
verify(spyClient).getRequest(eq("/zones?page=1&per_page=5"), eq(ZoneMultipleResponse.class));
verify(spyClient).getRequest(eq("/zones?page=1&per_page=50"), eq(ZoneMultipleResponse.class));
@@ -140,9 +140,9 @@ class CfDnsClientMockTest {
CfDnsClient spyClient = spy(client);
assertThrows(IllegalArgumentException.class,
() -> spyClient.zoneList(PagingRequest.of(1, 4)));
() -> spyClient.zoneList(new PagingRequest(1, 4)));
assertThrows(IllegalArgumentException.class,
() -> spyClient.zoneList(PagingRequest.of(1, 51)));
() -> spyClient.zoneList(new PagingRequest(1, 51)));
verify(spyClient, never()).getRequest(anyString(), eq(ZoneMultipleResponse.class));
}
@@ -217,7 +217,7 @@ class CfDnsClientMockTest {
@Test
void testRecordList_WithPaging() throws Exception {
ZoneEntity zone = createTestZone();
PagingRequest pagingRequest = PagingRequest.of(10, 1);
PagingRequest pagingRequest = PagingRequest.forRecords(10, 1);
RecordMultipleResponse mockResponse = new RecordMultipleResponse();
mockResponse.setResultInfo(createResultInfo(1));
@@ -484,10 +484,10 @@ class CfDnsClientMockTest {
CfDnsClient spyClient = spy(client);
doReturn(zoneResponse(List.of(zone("z1", "a.com"), zone("z2", "b.com")),
new ResultInfo(1, 2, 2, 2, 3)))
.when(spyClient).getRequest(eq("/zones?page=1&per_page=" + CfDnsClient.ZONES_PER_PAGE),
.when(spyClient).getRequest(eq("/zones?page=1&per_page=" + PagingRequest.DEFAULT_PAGE_SIZE),
eq(ZoneMultipleResponse.class));
doReturn(zoneResponse(List.of(zone("z3", "c.com")), new ResultInfo(2, 2, 2, 1, 3)))
.when(spyClient).getRequest(eq("/zones?page=2&per_page=" + CfDnsClient.ZONES_PER_PAGE),
.when(spyClient).getRequest(eq("/zones?page=2&per_page=" + PagingRequest.DEFAULT_PAGE_SIZE),
eq(ZoneMultipleResponse.class));
List<ZoneEntity> zones = spyClient.zoneList();
@@ -499,7 +499,7 @@ class CfDnsClientMockTest {
@Test
void testRecordListByType_FetchesAllPages() throws Exception {
ZoneEntity zone = createTestZone();
String base = "/zones/" + TEST_ZONE_ID + "/dns_records?type=A&order=name&direction=asc&page=%d&per_page=" + CfDnsClient.MAX_RECORDS_PER_PAGE;
String base = "/zones/" + TEST_ZONE_ID + "/dns_records?type=A&order=name&direction=asc&page=%d&per_page=" + CfDnsClient.RECORDS_PER_PAGE;
CfDnsClient spyClient = spy(client);
doReturn(recordResponse(List.of(RecordEntity.build("a.example.com", RecordType.A, 60, "1.1.1.1")),
new ResultInfo(1, 1, 2, 1, 2)))
@@ -518,7 +518,7 @@ class CfDnsClientMockTest {
void testRecordListBySld_FetchesAllPages() throws Exception {
ZoneEntity zone = createTestZone();
String base = "/zones/" + TEST_ZONE_ID + "/dns_records?name=test.example.com&order=name&direction=asc&page=%d&per_page="
+ CfDnsClient.MAX_RECORDS_PER_PAGE;
+ CfDnsClient.RECORDS_PER_PAGE;
CfDnsClient spyClient = spy(client);
doReturn(recordResponse(List.of(RecordEntity.build("test.example.com", RecordType.A, 60, "1.1.1.1")),
new ResultInfo(1, 1, 2, 1, 2)))
@@ -558,7 +558,7 @@ class CfDnsClientMockTest {
verify(spyClient).getRequest(
eq("/zones/" + TEST_ZONE_ID + "/dns_records?name=example.com&order=name&direction=asc&page=1&per_page="
+ CfDnsClient.MAX_RECORDS_PER_PAGE),
+ CfDnsClient.RECORDS_PER_PAGE),
eq(RecordMultipleResponse.class));
}
@@ -618,7 +618,7 @@ class CfDnsClientMockTest {
assertEquals(1, spyClient.recordList(createTestZone(), null).size());
verify(spyClient).getRequest(eq("/zones/" + TEST_ZONE_ID
+ "/dns_records?order=name&direction=asc&page=1&per_page=" + PagingRequest.MAX_PER_PAGE),
+ "/dns_records?order=name&direction=asc&page=1&per_page=" + CfDnsClient.RECORDS_PER_PAGE),
eq(RecordMultipleResponse.class));
}
@@ -628,7 +628,7 @@ class CfDnsClientMockTest {
doReturn(recordResponse(List.of(), new ResultInfo(0)))
.when(spyClient).getRequest(anyString(), eq(RecordMultipleResponse.class));
spyClient.recordList(createTestZone(), PagingRequest.of(2, 42));
spyClient.recordList(createTestZone(), PagingRequest.forRecords(2, 42));
verify(spyClient).getRequest(eq("/zones/" + TEST_ZONE_ID
+ "/dns_records?order=name&direction=asc&page=2&per_page=42"),
@@ -645,10 +645,10 @@ class CfDnsClientMockTest {
spyClient.recordListBySubdomain(createTestZone(), "www", RecordType.TXT);
verify(spyClient).getRequest(eq("/zones/" + TEST_ZONE_ID + "/dns_records?type=MX&order=name&direction=asc&page=1&per_page="
+ CfDnsClient.MAX_RECORDS_PER_PAGE), eq(RecordMultipleResponse.class));
+ CfDnsClient.RECORDS_PER_PAGE), eq(RecordMultipleResponse.class));
verify(spyClient).getRequest(eq("/zones/" + TEST_ZONE_ID
+ "/dns_records?name=www.example.com&type=TXT&order=name&direction=asc&page=1&per_page="
+ CfDnsClient.MAX_RECORDS_PER_PAGE), eq(RecordMultipleResponse.class));
+ CfDnsClient.RECORDS_PER_PAGE), eq(RecordMultipleResponse.class));
}
@Test
@@ -669,7 +669,7 @@ class CfDnsClientMockTest {
assertEquals(List.of("A", "AAAA"), records.stream().map(RecordEntity::getType).toList());
verify(spyClient).getRequest(eq("/zones/" + TEST_ZONE_ID + "/dns_records?order=name&direction=asc&page=1&per_page="
+ CfDnsClient.MAX_RECORDS_PER_PAGE), eq(RecordMultipleResponse.class));
+ CfDnsClient.RECORDS_PER_PAGE), eq(RecordMultipleResponse.class));
}
private static RecordEntity fetchedMxRecord() {
@@ -803,10 +803,10 @@ class CfDnsClientMockTest {
spyClient.recordListBySubdomain(createTestZone(), "0/26");
verify(spyClient).getRequest(eq("/zones/" + TEST_ZONE_ID
+ "/dns_records?name=%24%28reboot%29.example.com&order=name&direction=asc&page=1&per_page=" + CfDnsClient.MAX_RECORDS_PER_PAGE),
+ "/dns_records?name=%24%28reboot%29.example.com&order=name&direction=asc&page=1&per_page=" + CfDnsClient.RECORDS_PER_PAGE),
eq(RecordMultipleResponse.class));
verify(spyClient).getRequest(eq("/zones/" + TEST_ZONE_ID
+ "/dns_records?name=0%2F26.example.com&order=name&direction=asc&page=1&per_page=" + CfDnsClient.MAX_RECORDS_PER_PAGE),
+ "/dns_records?name=0%2F26.example.com&order=name&direction=asc&page=1&per_page=" + CfDnsClient.RECORDS_PER_PAGE),
eq(RecordMultipleResponse.class));
}
@@ -832,7 +832,7 @@ class CfDnsClientMockTest {
private static String recordsPage(int page) {
return "/zones/" + TEST_ZONE_ID + "/dns_records?order=name&direction=asc&page=" + page
+ "&per_page=" + PagingRequest.MAX_PER_PAGE;
+ "&per_page=" + CfDnsClient.RECORDS_PER_PAGE;
}
@Test
@@ -1,62 +1,80 @@
package codes.thischwa.cf.model;
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.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PagingRequestTest {
public class PagingRequestTest {
@Test
void testBuildPath() {
String result = PagingRequest.defaultPaging().addQueryString("/zones");
assertEquals("/zones?page=1&per_page=50", result);
}
@Test
void testBuildPath() {
String result = PagingRequest.defaultPaging().addQueryString("/zones");
assertEquals("/zones?page=1&per_page=50", result);
}
@Test
void testBuildPathWithExistingQuery() {
String result = PagingRequest.forRecords(10, 100).addQueryString("/zones?foo=bar");
assertEquals("/zones?foo=bar&page=10&per_page=100", result);
}
@Test
void testPagingRequestBoundaries() {
assertDoesNotThrow(() -> PagingRequest.of(1, 1));
assertDoesNotThrow(() -> PagingRequest.of(1, 5_000_000));
assertThrows(IllegalArgumentException.class, () -> PagingRequest.of(0, 1));
assertThrows(IllegalArgumentException.class, () -> PagingRequest.of(1, 0));
assertThrows(IllegalArgumentException.class, () -> PagingRequest.of(1, 5_000_001));
}
@Test
void testPagingRequestBoundaries() {
assertDoesNotThrow(() -> PagingRequest.forRecords(1, 1));
assertDoesNotThrow(() -> PagingRequest.forRecords(1, PagingRequest.MAX_RECORDS_PER_PAGE));
assertThrows(IllegalArgumentException.class, () -> PagingRequest.forRecords(0, 1));
assertThrows(IllegalArgumentException.class, () -> PagingRequest.forRecords(1, 0));
assertThrows(IllegalArgumentException.class,
() -> PagingRequest.forRecords(1, PagingRequest.MAX_RECORDS_PER_PAGE + 1));
}
@Test
void testSettersValidatePagingValues() {
PagingRequest request = PagingRequest.of(1, 50);
@Test
void testZonePageSizeBoundaries() {
assertDoesNotThrow(() -> PagingRequest.forZones(1, PagingRequest.MIN_ZONES_PER_PAGE));
assertDoesNotThrow(() -> PagingRequest.forZones(1, PagingRequest.MAX_ZONES_PER_PAGE));
assertThrows(IllegalArgumentException.class,
() -> PagingRequest.forZones(1, PagingRequest.MIN_ZONES_PER_PAGE - 1));
assertThrows(IllegalArgumentException.class,
() -> PagingRequest.forZones(1, PagingRequest.MAX_ZONES_PER_PAGE + 1));
}
assertThrows(IllegalArgumentException.class, () -> request.setPage(0));
assertThrows(IllegalArgumentException.class, () -> request.setPerPage(5_000_001));
assertEquals(1, request.getPage());
assertEquals(50, request.getPerPage());
}
@Test
void testNextAndWithPageReturnNewRequests() {
PagingRequest request = PagingRequest.forRecords(2, 50);
@Test
void testBuildPathAdditional() {
String result = new PagingRequest( 10, 100).addQueryString("/zones?foo=bar");
assertEquals("/zones?foo=bar&page=10&per_page=100", result);
}
assertEquals(PagingRequest.forRecords(3, 50), request.next());
assertEquals(PagingRequest.forRecords(5, 50), request.withPage(5));
assertEquals(PagingRequest.forRecords(2, 50), request);
}
@Test
void testGetPagingParams() {
PagingRequest request = PagingRequest.of(2, 50);
java.util.Map<String, String> params = request.getPagingParams();
assertEquals("2", params.get("page"));
assertEquals("50", params.get("perPage"));
}
@Test
void testRecordMethods() {
PagingRequest first = PagingRequest.forRecords(1, 10);
PagingRequest second = PagingRequest.forRecords(1, 10);
@Test
void testLombokMethods() {
PagingRequest req1 = PagingRequest.of(1, 10);
PagingRequest req2 = PagingRequest.of(1, 10);
assertEquals(req1, req2);
assertEquals(req1.hashCode(), req2.hashCode());
assertTrue(req1.toString().contains("page=1"));
req1.setPage(5);
assertEquals(5, req1.getPage());
req1.setPerPage(20);
assertEquals(20, req1.getPerPage());
}
assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode());
assertTrue(first.toString().contains("page=1"));
assertEquals(1, first.page());
assertEquals(10, first.perPage());
}
@Test
void testValidationMessagesContainInvalidValues() {
IllegalArgumentException pageException = assertThrows(IllegalArgumentException.class,
() -> PagingRequest.forRecords(0, 10));
assertTrue(pageException.getMessage().contains("0"));
IllegalArgumentException perPageException = assertThrows(IllegalArgumentException.class,
() -> PagingRequest.forRecords(1, 0));
assertTrue(perPageException.getMessage().contains("0"));
}
@Test
void testAddQueryStringRejectsNullEndpoint() {
assertThrows(NullPointerException.class, () -> PagingRequest.defaultPaging().addQueryString(null));
}
}