diff --git a/pom.xml b/pom.xml index 7fd8674..86a2fe0 100644 --- a/pom.xml +++ b/pom.xml @@ -37,7 +37,8 @@ 1.18.36 2.0.17 1.5.18 - 5.11.4 + 5.12.2 + 5.17.0 thischwa @@ -117,6 +118,12 @@ ${junit5.version} test + + org.mockito + mockito-junit-jupiter + ${mockito-junit5.version} + test + ch.qos.logback logback-classic diff --git a/src/main/java/codes/thischwa/cf/ResponseValidator.java b/src/main/java/codes/thischwa/cf/ResponseValidator.java index 31a081d..dbb6b23 100644 --- a/src/main/java/codes/thischwa/cf/ResponseValidator.java +++ b/src/main/java/codes/thischwa/cf/ResponseValidator.java @@ -14,9 +14,9 @@ import java.util.stream.Collectors; *
  • It checks whether the API response was successful by analyzing the associated response * metadata. If the response indicates failure, an exception is thrown with descriptive error * messages. - *
  • It validates the number of results in the API response payload to detect unexpected counts. - * Depending on the configuration, discrepancies in result count or an empty result may trigger - * exceptions. + *
  • If a {@link RecordMultipleResponse} is used, it validates the number of results in the API + * response payload to detect unexpected counts. Depending on the configuration, + * discrepancies in the result count or an empty result may trigger exceptions. * */ class ResponseValidator { diff --git a/src/test/java/codes/thischwa/cf/ResponseValidatorTest.java b/src/test/java/codes/thischwa/cf/ResponseValidatorTest.java new file mode 100644 index 0000000..978cb6b --- /dev/null +++ b/src/test/java/codes/thischwa/cf/ResponseValidatorTest.java @@ -0,0 +1,99 @@ +package codes.thischwa.cf; + +import codes.thischwa.cf.model.AbstractResponse; +import codes.thischwa.cf.model.RecordMultipleResponse; +import codes.thischwa.cf.model.ResponseResultInfo; +import codes.thischwa.cf.model.ResultInfo; +import java.util.Arrays; +import lombok.Getter; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class ResponseValidatorTest { + + @Mock + private AbstractResponse mockResponse; + + @Mock + private ResponseResultInfo mockResultInfo; + + @Mock + private RecordMultipleResponse mockMultipleResponse; + + private ResponseValidator validatorWithException; + private ResponseValidator validatorWithoutException; + + @BeforeEach + void setUp() { + validatorWithException = new ResponseValidator(true); + validatorWithoutException = new ResponseValidator(false); + } + + @Test + void validateSuccessfulResponse() { + when(mockResponse.getResponseResultInfo()).thenReturn(mockResultInfo); + when(mockResultInfo.isSuccess()).thenReturn(true); + + assertDoesNotThrow(() -> validatorWithException.validate(mockResponse, false)); + } + + @Test + void validateFailedResponse() { + when(mockResponse.getResponseResultInfo()).thenReturn(mockResultInfo); + when(mockResultInfo.isSuccess()).thenReturn(false); + when(mockResultInfo.getErrors()).thenReturn(Arrays.asList("Fehler 1", "Fehler 2")); + + CloudflareApiException exception = assertThrows(CloudflareApiException.class, + () -> validatorWithException.validate(mockResponse, false)); + assertTrue(exception.getMessage().contains("Fehler 1")); + assertTrue(exception.getMessage().contains("Fehler 2")); + } + + @Test + void validateSingleResultExpectedButMultipleFound() { + when(mockMultipleResponse.getResponseResultInfo()).thenReturn(mockResultInfo); + when(mockResultInfo.isSuccess()).thenReturn(true); + when(mockMultipleResponse.getResultInfo()).thenReturn(new TestResultInfo(2)); + + CloudflareApiException exception = assertThrows(CloudflareApiException.class, + () -> validatorWithException.validate(mockMultipleResponse, true)); + assertTrue(exception.getMessage().contains("Unexpected result count: 2")); + } + + @Test + void validateEmptyResultWithExceptionEnabled() { + when(mockMultipleResponse.getResponseResultInfo()).thenReturn(mockResultInfo); + when(mockResultInfo.isSuccess()).thenReturn(true); + when(mockMultipleResponse.getResultInfo()).thenReturn(new TestResultInfo(0)); + + assertThrows(CloudflareNotFoundException.class, + () -> validatorWithException.validate(mockMultipleResponse, false)); + } + + @Test + void validateEmptyResultWithExceptionDisabled() { + when(mockMultipleResponse.getResponseResultInfo()).thenReturn(mockResultInfo); + when(mockResultInfo.isSuccess()).thenReturn(true); + when(mockMultipleResponse.getResultInfo()).thenReturn(new TestResultInfo(0)); + + assertDoesNotThrow(() -> validatorWithoutException.validate(mockMultipleResponse, true)); + } + + @Getter + private static class TestResultInfo extends ResultInfo { + private final int totalCount; + + TestResultInfo(int totalCount) { + this.totalCount = totalCount; + } + } +} \ No newline at end of file