Skip to content

Commit

Permalink
fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antonio.torre committed Dec 18, 2023
1 parent 91a995a commit 64552df
Showing 1 changed file with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
MongoExceptionHandlerTest.TestController.class, ErrorManager.class})
class MongoExceptionHandlerTest {

public static final ErrorDTO EXPECTED_DEFAULT_ERROR = new ErrorDTO("Error", "Something gone wrong");
public static final ErrorDTO EXPECTED_TOO_MANY_REQUESTS_ERROR = new ErrorDTO("TOO_MANY_REQUESTS", "Too Many Requests");

@Autowired
private WebTestClient webTestClient;

Expand Down Expand Up @@ -66,7 +69,6 @@ void handleUncategorizedMongoDbException() {
doThrow(
new UncategorizedMongoDbException(mongoQueryException.getMessage(), mongoQueryException))
.when(testControllerSpy).testEndpoint();
ErrorDTO expectedErrorDefault = new ErrorDTO("TOO_MANY_REQUESTS","TOO_MANY_REQUESTS");

webTestClient.get()
.uri(uriBuilder -> uriBuilder.path("/test").build())
Expand All @@ -75,7 +77,7 @@ void handleUncategorizedMongoDbException() {
.expectHeader().exists(HttpHeaders.RETRY_AFTER)
.expectHeader().valueEquals(HttpHeaders.RETRY_AFTER, "1")
.expectHeader().valueEquals("Retry-After-Ms", "34")
.expectBody(ErrorDTO.class).isEqualTo(expectedErrorDefault);
.expectBody(ErrorDTO.class).isEqualTo(EXPECTED_TOO_MANY_REQUESTS_ERROR);
}

@Test
Expand All @@ -94,7 +96,6 @@ void handleTooManyWriteDbException() {
doThrow(
new DataIntegrityViolationException(mongoWriteException.getMessage(), mongoWriteException))
.when(testControllerSpy).testEndpoint();
ErrorDTO expectedErrorDefault = new ErrorDTO("TOO_MANY_REQUESTS","TOO_MANY_REQUESTS");

webTestClient.get()
.uri(uriBuilder -> uriBuilder.path("/test").build())
Expand All @@ -103,7 +104,7 @@ void handleTooManyWriteDbException() {
.expectHeader().exists(HttpHeaders.RETRY_AFTER)
.expectHeader().valueEquals(HttpHeaders.RETRY_AFTER, "1")
.expectHeader().valueEquals("Retry-After-Ms", "34")
.expectBody(ErrorDTO.class).isEqualTo(expectedErrorDefault);
.expectBody(ErrorDTO.class).isEqualTo(EXPECTED_TOO_MANY_REQUESTS_ERROR);
}

@Test
Expand All @@ -112,26 +113,22 @@ void handleUncategorizedMongoDbExceptionNotRequestRateTooLarge() {
doThrow(new UncategorizedMongoDbException("DUMMY", new Exception()))
.when(testControllerSpy).testEndpoint();

ErrorDTO expectedErrorDefault = new ErrorDTO("Error","Something gone wrong");

webTestClient.get()
.uri(uriBuilder -> uriBuilder.path("/test").build())
.exchange()
.expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR)
.expectBody(ErrorDTO.class).isEqualTo(expectedErrorDefault);
.expectBody(ErrorDTO.class).isEqualTo(EXPECTED_DEFAULT_ERROR);
}

@Test
void handleMongoRequestRateTooLargeRetryExpiredException() {
doThrow(new MongoRequestRateTooLargeRetryExpiredException("FLOWNAME",3,3,0,100,34L,new Exception()))
.when(testControllerSpy).testEndpoint();

ErrorDTO expectedErrorDefault = new ErrorDTO("TOO_MANY_REQUESTS","TOO_MANY_REQUESTS");

webTestClient.get()
.uri(uriBuilder -> uriBuilder.path("/test").build())
.exchange()
.expectStatus().isEqualTo(HttpStatus.TOO_MANY_REQUESTS)
.expectBody(ErrorDTO.class).isEqualTo(expectedErrorDefault);
.expectBody(ErrorDTO.class).isEqualTo(EXPECTED_TOO_MANY_REQUESTS_ERROR);
}
}

0 comments on commit 64552df

Please sign in to comment.