Add unit tests for ResponseValidator with Mockito integration
Introduced `ResponseValidatorTest` to comprehensively test the `ResponseValidator` class. Enhanced the project dependencies by updating JUnit to 5.12.2 and integrating Mockito with `mockito-junit-jupiter` 5.17.0 for mocking support. Clarified Javadoc for handling `RecordMultipleResponse` cases in `ResponseValidator`.
This commit is contained in:
@@ -37,7 +37,8 @@
|
||||
<lombok.version>1.18.36</lombok.version>
|
||||
<slf4j.version>2.0.17</slf4j.version>
|
||||
<logback-classic.version>1.5.18</logback-classic.version>
|
||||
<junit5.version>5.11.4</junit5.version>
|
||||
<junit5.version>5.12.2</junit5.version>
|
||||
<mockito-junit5.version>5.17.0</mockito-junit5.version>
|
||||
|
||||
<!-- sonarqube -->
|
||||
<sonar.organization>thischwa</sonar.organization>
|
||||
@@ -117,6 +118,12 @@
|
||||
<version>${junit5.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>${mockito-junit5.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
|
||||
@@ -14,9 +14,9 @@ import java.util.stream.Collectors;
|
||||
* <li>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.
|
||||
* <li>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.
|
||||
* <li>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.
|
||||
* </ul>
|
||||
*/
|
||||
class ResponseValidator {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user