-
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 18 : Revise GlobalExceptionHandler and GlobalExceptionHandlerTest
- Loading branch information
1 parent
5791a78
commit 47d58d4
Showing
3 changed files
with
76 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DATABASE_USERNAME=root | ||
DATABASE_PASSWORD=ippavlova_1990 |
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 |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
import com.springboot.ratelimiter.base.AbstractRestControllerTest; | ||
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 jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.ConstraintViolationException; | ||
import jakarta.validation.Path; | ||
|
@@ -11,11 +13,9 @@ | |
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BeanPropertyBindingResult; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
|
||
import java.lang.reflect.Method; | ||
import java.time.LocalDateTime; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
@@ -119,6 +119,40 @@ void givenRuntimeException_whenHandleRuntimeException_throwCustomError() { | |
|
||
} | ||
|
||
@Test | ||
void givenRateLimitExceededException_whenHandleRateLimitExceededException_throwCustomError() { | ||
|
||
// Given | ||
RateLimitExceededException mockException = new RateLimitExceededException("Too many requests"); | ||
|
||
// When | ||
CustomError expectedError = CustomError.builder() | ||
.time(LocalDateTime.now()) | ||
.httpStatus(HttpStatus.TOO_MANY_REQUESTS) | ||
.header(CustomError.Header.VALIDATION_ERROR.getName()) | ||
.message("Too many requests") | ||
.build(); | ||
|
||
// Then | ||
ResponseEntity<?> responseEntity = globalExceptionHandler.handleRateLimitExceededException(mockException); | ||
|
||
CustomError actualError = (CustomError) responseEntity.getBody(); | ||
checkCustomError(expectedError, actualError); | ||
|
||
} | ||
|
||
@Test | ||
void givenEmailAlreadyExistsException_whenHandleEmailAlreadyExistsException_throwCustomError() { | ||
// Given | ||
EmailAlreadyExistsException mockException = new EmailAlreadyExistsException("[email protected]"); | ||
|
||
// When | ||
ResponseEntity<?> responseEntity = globalExceptionHandler.handleEmailAlreadyExistsException(mockException); | ||
|
||
// Then | ||
assertCustomError(responseEntity, HttpStatus.CONFLICT, "Email already exists: [email protected]"); | ||
} | ||
|
||
private void checkCustomError(CustomError expectedError, CustomError actualError) { | ||
|
||
assertThat(actualError).isNotNull(); | ||
|
@@ -141,4 +175,14 @@ 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); | ||
} | ||
|
||
} |