fix(api): use ApiException instead of EntityNotFoundExcepion (#2302)
This commit is contained in:
@@ -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