Skip to content

Commit

Permalink
Task 18 : Revise GlobalExceptionHandler and GlobalExceptionHandlerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 committed Jun 29, 2024
1 parent 5791a78 commit 47d58d4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_USERNAME=root
DATABASE_PASSWORD=ippavlova_1990
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.springboot.ratelimiter.common.exception;

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.ConstraintViolationException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -96,4 +98,30 @@ protected ResponseEntity<?> handleRuntimeException(final RuntimeException runtim

}

@ExceptionHandler(RateLimitExceededException.class)
protected ResponseEntity<Object> handleRateLimitExceededException(final RateLimitExceededException ex) {

CustomError customError = CustomError.builder()
.time(LocalDateTime.now())
.httpStatus(HttpStatus.TOO_MANY_REQUESTS)
.header(CustomError.Header.VALIDATION_ERROR.getName())
.message(ex.getMessage())
.build();

return new ResponseEntity<>(customError, HttpStatus.TOO_MANY_REQUESTS);
}

@ExceptionHandler(EmailAlreadyExistsException.class)
protected ResponseEntity<Object> handleEmailAlreadyExistsException(final EmailAlreadyExistsException ex) {

CustomError customError = CustomError.builder()
.time(LocalDateTime.now())
.httpStatus(HttpStatus.CONFLICT)
.header(CustomError.Header.VALIDATION_ERROR.getName())
.message(ex.getMessage())
.build();

return new ResponseEntity<>(customError, HttpStatus.CONFLICT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}

}

0 comments on commit 47d58d4

Please sign in to comment.