-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task 24 : Add UserNotFoundException in GlobalExceptionHandler and its…
… test into GlobalExceptionHandlerTest
- Loading branch information
1 parent
656a116
commit a8f547d
Showing
2 changed files
with
64 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import com.springboot.ratelimiter.common.exception.error.CustomError; | ||
import com.springboot.ratelimiter.common.exception.ratelimit.RateLimitExceededException; | ||
import com.springboot.ratelimiter.common.exception.user.EmailAlreadyExistsException; | ||
import com.springboot.ratelimiter.common.exception.user.UserNotFoundException; | ||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.ConstraintViolationException; | ||
import jakarta.validation.Path; | ||
|
@@ -53,6 +54,8 @@ void givenMethodArgumentNotValidException_handleMethodArgumentNotValid_throwCust | |
mockException, new HttpHeaders(), HttpStatus.BAD_REQUEST, null); | ||
|
||
CustomError actualError = (CustomError) responseEntity.getBody(); | ||
|
||
// Verify | ||
checkCustomError(expectedError, actualError); | ||
|
||
} | ||
|
@@ -93,6 +96,8 @@ void givenConstraintViolationException_whenHandlePathVariableErrors_throwCustomE | |
ResponseEntity<Object> responseEntity = globalExceptionHandler.handlePathVariableErrors(mockException); | ||
|
||
CustomError actualError = (CustomError) responseEntity.getBody(); | ||
|
||
// Verify | ||
checkCustomError(expectedError, actualError); | ||
|
||
} | ||
|
@@ -103,18 +108,20 @@ void givenRuntimeException_whenHandleRuntimeException_throwCustomError() { | |
// Given | ||
RuntimeException mockException = new RuntimeException("Runtime exception"); | ||
|
||
// When | ||
CustomError expectedError = CustomError.builder() | ||
.time(LocalDateTime.now()) | ||
.httpStatus(HttpStatus.NOT_FOUND) | ||
.header(CustomError.Header.API_ERROR.getName()) | ||
.message("Runtime exception") | ||
.build(); | ||
|
||
// Then | ||
// When | ||
ResponseEntity<?> responseEntity = globalExceptionHandler.handleRuntimeException(mockException); | ||
|
||
// Then | ||
CustomError actualError = (CustomError) responseEntity.getBody(); | ||
|
||
// Verify | ||
checkCustomError(expectedError, actualError); | ||
|
||
} | ||
|
@@ -133,24 +140,63 @@ void givenRateLimitExceededException_whenHandleRateLimitExceededException_throwC | |
.message("Too many requests") | ||
.build(); | ||
|
||
// Then | ||
// When | ||
ResponseEntity<?> responseEntity = globalExceptionHandler.handleRateLimitExceededException(mockException); | ||
|
||
// Then | ||
CustomError actualError = (CustomError) responseEntity.getBody(); | ||
|
||
// Verify | ||
checkCustomError(expectedError, actualError); | ||
|
||
} | ||
|
||
@Test | ||
void givenEmailAlreadyExistsException_whenHandleEmailAlreadyExistsException_throwCustomError() { | ||
|
||
// Given | ||
EmailAlreadyExistsException mockException = new EmailAlreadyExistsException("[email protected]"); | ||
|
||
CustomError expectedError = CustomError.builder() | ||
.time(LocalDateTime.now()) | ||
.httpStatus(HttpStatus.CONFLICT) | ||
.header(CustomError.Header.VALIDATION_ERROR.getName()) | ||
.message("Email already exists: [email protected]") | ||
.build(); | ||
|
||
// When | ||
ResponseEntity<?> responseEntity = globalExceptionHandler.handleEmailAlreadyExistsException(mockException); | ||
|
||
// Then | ||
assertCustomError(responseEntity, HttpStatus.CONFLICT, "Email already exists: [email protected]"); | ||
CustomError actualError = (CustomError) responseEntity.getBody(); | ||
|
||
// Verify | ||
checkCustomError(expectedError, actualError); | ||
|
||
} | ||
|
||
@Test | ||
void givenUserNotFoundException_whenHandleUserNotFoundException_throwCustomError() { | ||
|
||
// Given | ||
UserNotFoundException mockException = new UserNotFoundException("123"); | ||
|
||
CustomError expectedError = CustomError.builder() | ||
.time(LocalDateTime.now()) | ||
.httpStatus(HttpStatus.CONFLICT) | ||
.header(CustomError.Header.NOT_FOUND.getName()) | ||
.message("No user was found with ID: 123") | ||
.build(); | ||
|
||
// When | ||
ResponseEntity<?> responseEntity = globalExceptionHandler.handleUserNotFoundException(mockException); | ||
|
||
// Then | ||
CustomError actualError = (CustomError) responseEntity.getBody(); | ||
|
||
// Verify | ||
checkCustomError(expectedError, actualError); | ||
|
||
} | ||
|
||
private void checkCustomError(CustomError expectedError, CustomError actualError) { | ||
|
@@ -175,14 +221,4 @@ private void checkCustomError(CustomError expectedError, CustomError actualError | |
} | ||
} | ||
|
||
private void assertCustomError(ResponseEntity<?> responseEntity, HttpStatus expectedStatus, String expectedMessage) { | ||
CustomError actualError = (CustomError) responseEntity.getBody(); | ||
|
||
assertThat(actualError).isNotNull(); | ||
assertThat(actualError.getTime()).isNotNull(); | ||
assertThat(actualError.getHttpStatus()).isEqualTo(expectedStatus); | ||
assertThat(actualError.getHeader()).isEqualTo(CustomError.Header.VALIDATION_ERROR.getName()); | ||
assertThat(actualError.getMessage()).isEqualTo(expectedMessage); | ||
} | ||
|
||
} |