Skip to content

Commit

Permalink
Task 24 : Add UserNotFoundException in GlobalExceptionHandler and its…
Browse files Browse the repository at this point in the history
… test into GlobalExceptionHandlerTest
  • Loading branch information
Rapter1990 committed Jul 1, 2024
1 parent 656a116 commit a8f547d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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.ConstraintViolationException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -124,4 +125,17 @@ protected ResponseEntity<Object> handleEmailAlreadyExistsException(final EmailAl
return new ResponseEntity<>(customError, HttpStatus.CONFLICT);
}

@ExceptionHandler(UserNotFoundException.class)
protected ResponseEntity<Object> handleUserNotFoundException(final UserNotFoundException ex) {

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,6 +54,8 @@ void givenMethodArgumentNotValidException_handleMethodArgumentNotValid_throwCust
mockException, new HttpHeaders(), HttpStatus.BAD_REQUEST, null);

CustomError actualError = (CustomError) responseEntity.getBody();

// Verify
checkCustomError(expectedError, actualError);

}
Expand Down Expand Up @@ -93,6 +96,8 @@ void givenConstraintViolationException_whenHandlePathVariableErrors_throwCustomE
ResponseEntity<Object> responseEntity = globalExceptionHandler.handlePathVariableErrors(mockException);

CustomError actualError = (CustomError) responseEntity.getBody();

// Verify
checkCustomError(expectedError, actualError);

}
Expand All @@ -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);

}
Expand All @@ -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) {
Expand All @@ -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);
}

}

0 comments on commit a8f547d

Please sign in to comment.