Refactor ResultInfo to use Java record
Replaced the ResultInfo class with a Java record for conciseness and immutability. Adjusted related validation logic and method calls to align with the new record structure. Minor updates to Javadoc documentation were also included.
This commit is contained in:
@@ -43,11 +43,11 @@ class ResponseValidator {
|
||||
private void validateResultCount(AbstractResponse resp, boolean singleResultExpected)
|
||||
throws CloudflareApiException {
|
||||
if (resp instanceof RecordMultipleResponse respMulti) {
|
||||
if (singleResultExpected && respMulti.getResultInfo().getTotalCount() > 1) {
|
||||
if (singleResultExpected && respMulti.getResultInfo().totalCount() > 1) {
|
||||
throw new CloudflareApiException(
|
||||
"Unexpected result count: " + respMulti.getResultInfo().getTotalCount());
|
||||
"Unexpected result count: " + respMulti.getResultInfo().totalCount());
|
||||
}
|
||||
if (emptyResultThrowsException && respMulti.getResultInfo().getTotalCount() == 0) {
|
||||
if (emptyResultThrowsException && respMulti.getResultInfo().totalCount() == 0) {
|
||||
throw new CloudflareNotFoundException("No result found");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package codes.thischwa.cf.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Represents metadata for paginated results.
|
||||
*
|
||||
@@ -16,11 +14,16 @@ import lombok.Data;
|
||||
* <li><b>totalCount:</b> The total number of results across all pages.
|
||||
* </ul>
|
||||
*/
|
||||
@Data
|
||||
public class ResultInfo {
|
||||
private int page;
|
||||
private int perPage;
|
||||
private int totalPages;
|
||||
private int count;
|
||||
private int totalCount;
|
||||
|
||||
public record ResultInfo(int page, int perPage, int totalPages, int count, int totalCount) {
|
||||
|
||||
/**
|
||||
* Constructs a ResultInfo instance with the specified total count and default values for other
|
||||
* fields. Just to use in tests!
|
||||
*
|
||||
* @param totalCount the total number of results across all pages
|
||||
*/
|
||||
public ResultInfo(int totalCount) {
|
||||
this(0, 0, 0, 0, totalCount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ class ResponseValidatorTest {
|
||||
void validateSingleResultExpectedButMultipleFound() {
|
||||
when(mockMultipleResponse.getResponseResultInfo()).thenReturn(mockResultInfo);
|
||||
when(mockResultInfo.isSuccess()).thenReturn(true);
|
||||
when(mockMultipleResponse.getResultInfo()).thenReturn(new TestResultInfo(2));
|
||||
when(mockMultipleResponse.getResultInfo()).thenReturn(new ResultInfo(2));
|
||||
|
||||
CloudflareApiException exception = assertThrows(CloudflareApiException.class,
|
||||
() -> validatorWithException.validate(mockMultipleResponse, true));
|
||||
@@ -73,7 +73,7 @@ class ResponseValidatorTest {
|
||||
void validateEmptyResultWithExceptionEnabled() {
|
||||
when(mockMultipleResponse.getResponseResultInfo()).thenReturn(mockResultInfo);
|
||||
when(mockResultInfo.isSuccess()).thenReturn(true);
|
||||
when(mockMultipleResponse.getResultInfo()).thenReturn(new TestResultInfo(0));
|
||||
when(mockMultipleResponse.getResultInfo()).thenReturn(new ResultInfo(0));
|
||||
|
||||
assertThrows(CloudflareNotFoundException.class,
|
||||
() -> validatorWithException.validate(mockMultipleResponse, false));
|
||||
@@ -83,17 +83,8 @@ class ResponseValidatorTest {
|
||||
void validateEmptyResultWithExceptionDisabled() {
|
||||
when(mockMultipleResponse.getResponseResultInfo()).thenReturn(mockResultInfo);
|
||||
when(mockResultInfo.isSuccess()).thenReturn(true);
|
||||
when(mockMultipleResponse.getResultInfo()).thenReturn(new TestResultInfo(0));
|
||||
when(mockMultipleResponse.getResultInfo()).thenReturn(new ResultInfo(0));
|
||||
|
||||
assertDoesNotThrow(() -> validatorWithoutException.validate(mockMultipleResponse, true));
|
||||
}
|
||||
|
||||
@Getter
|
||||
private static class TestResultInfo extends ResultInfo {
|
||||
private final int totalCount;
|
||||
|
||||
TestResultInfo(int totalCount) {
|
||||
this.totalCount = totalCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user