fix(api): use ApiException instead of EntityNotFoundExcepion (#2302)
This commit is contained in:
@@ -11,6 +11,7 @@ public enum ApiError {
|
||||
GENERIC_BAD_REQUEST(HttpStatus.BAD_REQUEST, "%s"),
|
||||
GENERIC_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "%s"),
|
||||
|
||||
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "%s not found with ID: %d"),
|
||||
BOOK_NOT_FOUND(HttpStatus.NOT_FOUND, "Book not found with ID: %d"),
|
||||
EMAIL_PROVIDER_NOT_FOUND(HttpStatus.NOT_FOUND, "Email provider with ID %s not found"),
|
||||
DEFAULT_EMAIL_PROVIDER_NOT_FOUND(HttpStatus.NOT_FOUND, "Default email provider not found"),
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.booklore.service.book;
|
||||
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.mapper.AnnotationMapper;
|
||||
import org.booklore.model.dto.Annotation;
|
||||
import org.booklore.model.dto.CreateAnnotationRequest;
|
||||
@@ -12,7 +13,6 @@ import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.repository.AnnotationRepository;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -94,17 +94,17 @@ public class AnnotationService {
|
||||
private AnnotationEntity findAnnotationByIdAndUser(Long annotationId) {
|
||||
Long userId = getCurrentUserId();
|
||||
return annotationRepository.findByIdAndUserId(annotationId, userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Annotation not found: " + annotationId));
|
||||
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Annotation", annotationId));
|
||||
}
|
||||
|
||||
private BookEntity findBook(Long bookId) {
|
||||
return bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
}
|
||||
|
||||
private BookLoreUserEntity findUser(Long userId) {
|
||||
return userRepository.findById(userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
|
||||
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
|
||||
}
|
||||
|
||||
private void validateNoDuplicateAnnotation(String cfi, Long bookId, Long userId) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.booklore.service.book;
|
||||
|
||||
import org.booklore.config.BookmarkProperties;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.mapper.BookMarkMapper;
|
||||
import org.booklore.model.dto.BookMark;
|
||||
import org.booklore.model.dto.CreateBookMarkRequest;
|
||||
@@ -13,7 +14,6 @@ import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.APIException;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -111,17 +111,17 @@ public class BookMarkService {
|
||||
private BookMarkEntity findBookmarkByIdAndUser(Long bookmarkId) {
|
||||
Long userId = getCurrentUserId();
|
||||
return bookMarkRepository.findByIdAndUserId(bookmarkId, userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Bookmark not found: " + bookmarkId));
|
||||
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Bookmark", bookmarkId));
|
||||
}
|
||||
|
||||
private BookEntity findBook(Long bookId) {
|
||||
return bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
}
|
||||
|
||||
private BookLoreUserEntity findUser(Long userId) {
|
||||
return userRepository.findById(userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
|
||||
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
|
||||
}
|
||||
|
||||
private void validateNoDuplicateBookmark(String cfi, Long bookId, Long userId) {
|
||||
|
||||
@@ -12,7 +12,6 @@ import org.booklore.model.entity.BookNoteEntity;
|
||||
import org.booklore.repository.BookNoteRepository;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -46,13 +45,13 @@ public class BookNoteService {
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(request.getBookId()));
|
||||
|
||||
BookLoreUserEntity user = userRepository.findById(currentUser.getId())
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + currentUser.getId()));
|
||||
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(currentUser.getId()));
|
||||
|
||||
BookNoteEntity noteEntity;
|
||||
|
||||
if (request.getId() != null) {
|
||||
noteEntity = bookNoteRepository.findByIdAndUserId(request.getId(), currentUser.getId())
|
||||
.orElseThrow(() -> new EntityNotFoundException("Note not found: " + request.getId()));
|
||||
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Note", request.getId()));
|
||||
noteEntity.setTitle(request.getTitle());
|
||||
noteEntity.setContent(request.getContent());
|
||||
} else {
|
||||
@@ -71,7 +70,8 @@ public class BookNoteService {
|
||||
@Transactional
|
||||
public void deleteNote(Long noteId) {
|
||||
BookLoreUser currentUser = authenticationService.getAuthenticatedUser();
|
||||
BookNoteEntity note = bookNoteRepository.findByIdAndUserId(noteId, currentUser.getId()).orElseThrow(() -> new EntityNotFoundException("Note not found: " + noteId));
|
||||
BookNoteEntity note = bookNoteRepository.findByIdAndUserId(noteId, currentUser.getId())
|
||||
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Note", noteId));
|
||||
bookNoteRepository.delete(note);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package org.booklore.service.book;
|
||||
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.mapper.BookNoteV2Mapper;
|
||||
import org.booklore.model.dto.BookNoteV2;
|
||||
import org.booklore.model.dto.CreateBookNoteV2Request;
|
||||
@@ -12,7 +13,6 @@ import org.booklore.model.entity.BookNoteV2Entity;
|
||||
import org.booklore.repository.BookNoteV2Repository;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -92,17 +92,17 @@ public class BookNoteV2Service {
|
||||
private BookNoteV2Entity findNoteByIdAndUser(Long noteId) {
|
||||
Long userId = getCurrentUserId();
|
||||
return bookNoteV2Repository.findByIdAndUserId(noteId, userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Note not found: " + noteId));
|
||||
.orElseThrow(() -> ApiError.RESOURCE_NOT_FOUND.createException("Note", noteId));
|
||||
}
|
||||
|
||||
private BookEntity findBook(Long bookId) {
|
||||
return bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
}
|
||||
|
||||
private BookLoreUserEntity findUser(Long userId) {
|
||||
return userRepository.findById(userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
|
||||
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
|
||||
}
|
||||
|
||||
private void validateNoDuplicateNote(String cfi, Long bookId, Long userId) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package org.booklore.service.book;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.mapper.BookReviewMapper;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
@@ -98,7 +98,7 @@ public class BookReviewService {
|
||||
@Transactional
|
||||
public void delete(Long id) {
|
||||
if (!bookReviewRepository.existsById(id)) {
|
||||
throw new EntityNotFoundException("Review not found: " + id);
|
||||
throw ApiError.RESOURCE_NOT_FOUND.createException("Review", id);
|
||||
}
|
||||
bookReviewRepository.deleteById(id);
|
||||
}
|
||||
|
||||
+3
-3
@@ -2,13 +2,13 @@ package org.booklore.service.book;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.model.entity.PdfAnnotationEntity;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.PdfAnnotationRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -53,11 +53,11 @@ public class PdfAnnotationPersistenceHandler {
|
||||
|
||||
private BookEntity findBook(Long bookId) {
|
||||
return bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
}
|
||||
|
||||
private BookLoreUserEntity findUser(Long userId) {
|
||||
return userRepository.findById(userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
|
||||
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ import org.booklore.repository.BookMarkRepository;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import org.booklore.service.book.BookMarkService;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -130,7 +129,7 @@ class BookMarkServiceTest {
|
||||
when(bookMarkRepository.existsByCfiAndBookIdAndUserId("new-cfi", bookId, userId)).thenReturn(false); // No duplicate
|
||||
when(bookRepository.findById(bookId)).thenReturn(Optional.empty()); // Book doesn't exist
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> bookMarkService.createBookmark(request));
|
||||
assertThrows(APIException.class, () -> bookMarkService.createBookmark(request));
|
||||
verify(bookMarkRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@@ -149,7 +148,7 @@ class BookMarkServiceTest {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.findByIdAndUserId(bookmarkId, userId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> bookMarkService.deleteBookmark(bookmarkId));
|
||||
assertThrows(APIException.class, () -> bookMarkService.deleteBookmark(bookmarkId));
|
||||
verify(bookMarkRepository, never()).delete(any());
|
||||
}
|
||||
|
||||
@@ -183,7 +182,7 @@ class BookMarkServiceTest {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.findByIdAndUserId(bookmarkId, userId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> bookMarkService.updateBookmark(bookmarkId, updateRequest));
|
||||
assertThrows(APIException.class, () -> bookMarkService.updateBookmark(bookmarkId, updateRequest));
|
||||
verify(bookMarkRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@@ -205,7 +204,7 @@ class BookMarkServiceTest {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.findByIdAndUserId(bookmarkId, userId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> bookMarkService.getBookmarkById(bookmarkId));
|
||||
assertThrows(APIException.class, () -> bookMarkService.getBookmarkById(bookmarkId));
|
||||
verify(bookMarkRepository).findByIdAndUserId(bookmarkId, userId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.booklore.service;
|
||||
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.mapper.BookNoteMapper;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.BookNote;
|
||||
@@ -12,7 +13,6 @@ import org.booklore.repository.BookNoteRepository;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import org.booklore.service.book.BookNoteService;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -149,7 +149,7 @@ class BookNoteServiceTest {
|
||||
when(bookRepository.findById(bookId)).thenReturn(Optional.of(BookEntity.builder().id(bookId).build()));
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> service.createOrUpdateNote(req));
|
||||
assertThrows(APIException.class, () -> service.createOrUpdateNote(req));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -165,7 +165,7 @@ class BookNoteServiceTest {
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.of(BookLoreUserEntity.builder().id(userId).isDefaultPassword(false).build()));
|
||||
when(bookNoteRepository.findByIdAndUserId(noteId, userId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> service.createOrUpdateNote(req));
|
||||
assertThrows(APIException.class, () -> service.createOrUpdateNote(req));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -181,6 +181,6 @@ class BookNoteServiceTest {
|
||||
@Test
|
||||
void deleteNote_throwsIfNotFound() {
|
||||
when(bookNoteRepository.findByIdAndUserId(noteId, userId)).thenReturn(Optional.empty());
|
||||
assertThrows(EntityNotFoundException.class, () -> service.deleteNote(noteId));
|
||||
assertThrows(APIException.class, () -> service.deleteNote(noteId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ import org.booklore.repository.BookNoteV2Repository;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.UserRepository;
|
||||
import org.booklore.service.book.BookNoteV2Service;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -99,10 +98,10 @@ class BookNoteV2ServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getNoteById_throwsEntityNotFoundException_whenNotFound() {
|
||||
void getNoteById_throwsAPIException_whenNotFound() {
|
||||
when(bookNoteV2Repository.findByIdAndUserId(noteId, userId)).thenReturn(Optional.empty());
|
||||
|
||||
EntityNotFoundException ex = assertThrows(EntityNotFoundException.class, () -> service.getNoteById(noteId));
|
||||
APIException ex = assertThrows(APIException.class, () -> service.getNoteById(noteId));
|
||||
|
||||
assertTrue(ex.getMessage().contains(String.valueOf(noteId)));
|
||||
}
|
||||
@@ -213,7 +212,7 @@ class BookNoteV2ServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void createNote_throwsEntityNotFoundException_whenBookNotFound() {
|
||||
void createNote_throwssAPIException_whenBookNotFound() {
|
||||
CreateBookNoteV2Request request = CreateBookNoteV2Request.builder()
|
||||
.bookId(bookId)
|
||||
.cfi(cfi)
|
||||
@@ -223,14 +222,14 @@ class BookNoteV2ServiceTest {
|
||||
when(bookNoteV2Repository.existsByCfiAndBookIdAndUserId(cfi, bookId, userId)).thenReturn(false);
|
||||
when(bookRepository.findById(bookId)).thenReturn(Optional.empty());
|
||||
|
||||
EntityNotFoundException ex = assertThrows(EntityNotFoundException.class, () -> service.createNote(request));
|
||||
APIException ex = assertThrows(APIException.class, () -> service.createNote(request));
|
||||
|
||||
assertTrue(ex.getMessage().contains(String.valueOf(bookId)));
|
||||
verify(bookNoteV2Repository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createNote_throwsEntityNotFoundException_whenUserNotFound() {
|
||||
void createNote_throwsAPIException_whenUserNotFound() {
|
||||
CreateBookNoteV2Request request = CreateBookNoteV2Request.builder()
|
||||
.bookId(bookId)
|
||||
.cfi(cfi)
|
||||
@@ -243,7 +242,7 @@ class BookNoteV2ServiceTest {
|
||||
when(bookRepository.findById(bookId)).thenReturn(Optional.of(book));
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.empty());
|
||||
|
||||
EntityNotFoundException ex = assertThrows(EntityNotFoundException.class, () -> service.createNote(request));
|
||||
APIException ex = assertThrows(APIException.class, () -> service.createNote(request));
|
||||
|
||||
assertTrue(ex.getMessage().contains(String.valueOf(userId)));
|
||||
verify(bookNoteV2Repository, never()).save(any());
|
||||
@@ -321,14 +320,14 @@ class BookNoteV2ServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateNote_throwsEntityNotFoundException_whenNoteNotFound() {
|
||||
void updateNote_throwsAPIException_whenNoteNotFound() {
|
||||
UpdateBookNoteV2Request request = UpdateBookNoteV2Request.builder()
|
||||
.noteContent("Updated content")
|
||||
.build();
|
||||
|
||||
when(bookNoteV2Repository.findByIdAndUserId(noteId, userId)).thenReturn(Optional.empty());
|
||||
|
||||
EntityNotFoundException ex = assertThrows(EntityNotFoundException.class, () -> service.updateNote(noteId, request));
|
||||
APIException ex = assertThrows(APIException.class, () -> service.updateNote(noteId, request));
|
||||
|
||||
assertTrue(ex.getMessage().contains(String.valueOf(noteId)));
|
||||
verify(bookNoteV2Repository, never()).save(any());
|
||||
@@ -345,10 +344,10 @@ class BookNoteV2ServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteNote_throwsEntityNotFoundException_whenNotFound() {
|
||||
void deleteNote_throwsAPIException_whenNotFound() {
|
||||
when(bookNoteV2Repository.findByIdAndUserId(noteId, userId)).thenReturn(Optional.empty());
|
||||
|
||||
EntityNotFoundException ex = assertThrows(EntityNotFoundException.class, () -> service.deleteNote(noteId));
|
||||
APIException ex = assertThrows(APIException.class, () -> service.deleteNote(noteId));
|
||||
|
||||
assertTrue(ex.getMessage().contains(String.valueOf(noteId)));
|
||||
verify(bookNoteV2Repository, never()).delete(any());
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.booklore.service;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.mapper.BookReviewMapper;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.BookMetadata;
|
||||
@@ -360,10 +360,10 @@ class BookReviewServiceTest {
|
||||
|
||||
when(bookReviewRepository.existsById(reviewId)).thenReturn(false);
|
||||
|
||||
EntityNotFoundException exception = assertThrows(EntityNotFoundException.class,
|
||||
APIException exception = assertThrows(APIException.class,
|
||||
() -> service.delete(reviewId));
|
||||
|
||||
assertEquals("Review not found: " + reviewId, exception.getMessage());
|
||||
assertEquals("Review not found with ID: " + reviewId, exception.getMessage());
|
||||
verify(bookReviewRepository, never()).deleteById(reviewId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user