forked from pierront/mylibrary-template
Feature/manage reviews #2
+14
-16
@@ -16,7 +16,7 @@ public class ReviewNotFoundExceptionTest {
|
||||
void testExceptionMessageContainsUUIDForCustomer() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(), Optional.empty());
|
||||
|
||||
String expectedMessage = String.format("The reviews with the customer id %s does not exists", customerUUID);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
@@ -27,7 +27,7 @@ public class ReviewNotFoundExceptionTest {
|
||||
void testExceptionMessageContainsUUIDForBook() {
|
||||
UUID bookUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID));
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID), Optional.empty());
|
||||
|
||||
String expectedMessage = String.format("The reviews with book id %s does not exists", bookUUID);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
@@ -36,12 +36,11 @@ public class ReviewNotFoundExceptionTest {
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the UUID provided for customer and book")
|
||||
void testExceptionMessageContainsUUIDForCustomerAndBook() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
UUID bookUUID = UUID.randomUUID();
|
||||
UUID avisUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.of(bookUUID));
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(avisUUID));
|
||||
|
||||
String expectedMessage = String.format("The review with customer id %s and book id %s does not exists", customerUUID, bookUUID);
|
||||
String expectedMessage = String.format("The review with id %s does not exists", avisUUID);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@@ -50,7 +49,7 @@ public class ReviewNotFoundExceptionTest {
|
||||
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(), Optional.empty());
|
||||
|
||||
String expectedFormatWithPlaceholder = "The reviews with the customer id {0} does not exists";
|
||||
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE,
|
||||
@@ -63,7 +62,7 @@ public class ReviewNotFoundExceptionTest {
|
||||
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||
UUID bookUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID));
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID), Optional.empty());
|
||||
|
||||
String expectedFormatWithPlaceholder = "The reviews with the book id {0} does not exists";
|
||||
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE,
|
||||
@@ -74,26 +73,25 @@ public class ReviewNotFoundExceptionTest {
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format for customer and book")
|
||||
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
UUID bookUUID = UUID.randomUUID();
|
||||
UUID avisUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.of(bookUUID));
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.empty(avisUUID));
|
||||
|
||||
String expectedFormatWithPlaceholder = "The reviews with the customer id {0} and the book id {1} does not exists";
|
||||
String expectedFormatWithPlaceholder = "The reviews with id {0} does not exists";
|
||||
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_CUSTOMER_ID_AND_BOOK_ID_DOES_NOT_EXIST_MESSAGE,
|
||||
expectedFormatWithPlaceholder);
|
||||
assertTrue(exception.getMessage().contains(customerUUID.toString()));
|
||||
assertTrue(exception.getMessage().contains(avisUUID.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be properly thrown and caught")
|
||||
void testExceptionCanBeThrownAndCaught() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
UUID avisUUID = UUID.randomUUID();
|
||||
|
||||
try {
|
||||
throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||
throw new ReviewNotFoundException(Optional.of(avisUUID), Optional.empty());
|
||||
} catch (ReviewNotFoundException e) {
|
||||
String expectedMessage = String.format("The reviews with the customer id %s does not exists", customerUUID);
|
||||
String expectedMessage = String.format("The reviews id %s does not exists", avisUUID);
|
||||
assertEquals(expectedMessage, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
+28
-26
@@ -33,6 +33,7 @@ public class ReviewUseCaseTest {
|
||||
@InjectMocks
|
||||
private ReviewUseCase reviewUseCase;
|
||||
|
||||
private UUID avisId;
|
||||
private UUID customerId;
|
||||
private UUID bookId;
|
||||
private LocalDate purchaseDate;
|
||||
@@ -41,10 +42,12 @@ public class ReviewUseCaseTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
avisId = UUID.randomUUID();
|
||||
customerId = UUID.randomUUID();
|
||||
bookId = UUID.randomUUID();
|
||||
purchaseDate = LocalDate.of(2026, 5, 24);
|
||||
testReview = Review.builder()
|
||||
.avisId(avisId)
|
||||
.customerId(customerId)
|
||||
.bookId(bookId)
|
||||
.note(2)
|
||||
@@ -77,7 +80,7 @@ public class ReviewUseCaseTest {
|
||||
ReviewInfo invalidReviewInfo = new ReviewInfo(2, "plutôt mauvais", purchaseDate);
|
||||
|
||||
assertThrows(NotValidReviewException.class,
|
||||
() -> reviewUseCase.registerCustomer(invalidReviewInfo));
|
||||
() -> reviewUseCase.registerReview(invalidReviewInfo));
|
||||
|
||||
verify(reviewRepository, never()).save(any(Review.class));
|
||||
}
|
||||
@@ -138,29 +141,28 @@ public class ReviewUseCaseTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return review when customer and book ID exists")
|
||||
void testFindReviewByCustomerAndBookId() {
|
||||
when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview));
|
||||
@DisplayName("Should return review when Avis ID exists")
|
||||
void testFindReviewByAvisId() {
|
||||
when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview));
|
||||
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerAndBookId(customerId, bookId);
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByAvisId(customerId, bookId);
|
||||
|
||||
assertTrue(foundReview.isPresent());
|
||||
assertEquals(testReview.getBookId(), foundReview.get().getBookId());
|
||||
assertEquals(testReview.getNote(), foundReview.get().getNote());
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId);
|
||||
verify(reviewRepository, times(1)).findByAvisId(customerId, bookId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when customer ID doesn't exist")
|
||||
void testFindReviewByCustomerIdNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty());
|
||||
UUID nonExistentAvisId = UUID.randomUUID();
|
||||
when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty());
|
||||
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByAvisId(nonExistentAvisId);
|
||||
|
||||
assertTrue(foundReview.isEmpty());
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,10 +173,11 @@ public class ReviewUseCaseTest {
|
||||
@Test
|
||||
@DisplayName("Should update review when valid data is provided")
|
||||
void testUpdateReviewWithValidData() throws ReviewNotFoundException, NotValidReviewException {
|
||||
when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview));
|
||||
when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview));
|
||||
|
||||
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 30);
|
||||
Review updatedReview = Review.builder()
|
||||
.avisId(avisId)
|
||||
.customerId(customerId)
|
||||
.bookId(bookId)
|
||||
.note(4)
|
||||
@@ -186,13 +189,13 @@ public class ReviewUseCaseTest {
|
||||
|
||||
ReviewInfo updateInfo = new ReviewInfo(4, "en fait c'est bien", updatePurchaseDate);
|
||||
|
||||
ReviewDTO result = reviewUseCase.updateReview(customerId, bookId, updateInfo);
|
||||
ReviewDTO result = reviewUseCase.updateReview(avisId, updateInfo);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(customerId, result.getCustomerId());
|
||||
assertEquals(4, result.getNote());
|
||||
assertEquals("en fait c'est bien", result.getComment());
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId);
|
||||
verify(reviewRepository, times(1)).findByAvisId(avisId);
|
||||
verify(reviewRepository, times(1)).save(any(Review.class));
|
||||
}
|
||||
|
||||
@@ -201,7 +204,7 @@ public class ReviewUseCaseTest {
|
||||
void testUpdateReviewNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty());
|
||||
when(reviewRepository.findByAvisId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty());
|
||||
|
||||
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24);
|
||||
ReviewInfo updateInfo = new ReviewInfo(3, "moyen", updatePurchaseDate);
|
||||
@@ -209,7 +212,7 @@ public class ReviewUseCaseTest {
|
||||
assertThrows(ReviewNotFoundException.class,
|
||||
() -> reviewUseCase.updateReview(nonExistentCustomerId, nonExistentBookId, updateInfo));
|
||||
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
verify(reviewRepository, times(1)).findByAvisId(nonExistentCustomerId, nonExistentBookId);
|
||||
verify(reviewRepository, never()).save(any(Review.class));
|
||||
}
|
||||
|
||||
@@ -220,9 +223,9 @@ public class ReviewUseCaseTest {
|
||||
ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate);
|
||||
|
||||
assertThrows(NotValidCustomerException.class,
|
||||
() -> reviewUseCase.updateCustomer(customerId, invalidUpdateInfo));
|
||||
() -> reviewUseCase.updateReview(avisId, invalidUpdateInfo));
|
||||
|
||||
verify(reviewRepository, never()).findByCustomerAndBookId(any(UUID.class), any(UUID.class));
|
||||
verify(reviewRepository, never()).findByAvisId(any(UUID.class));
|
||||
verify(reviewRepository, never()).save(any(Review.class));
|
||||
}
|
||||
}
|
||||
@@ -284,26 +287,25 @@ public class ReviewUseCaseTest {
|
||||
@Test
|
||||
@DisplayName("Should delete review when customer and book ID exists")
|
||||
void testDeleteReview() throws ReviewNotFoundException {
|
||||
when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview));
|
||||
when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview));
|
||||
doNothing().when(reviewRepository).delete(testReview);
|
||||
|
||||
reviewUseCase.deleteReview(customerId, bookId);
|
||||
reviewUseCase.deleteReview(avisId);
|
||||
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId);
|
||||
verify(reviewRepository, times(1)).findByAvisId(avisId);
|
||||
verify(reviewRepository, times(1)).delete(testReview);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when customer and book ID doesn't exist")
|
||||
void testDeleteReviewNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty());
|
||||
UUID nonExistentAvisId = UUID.randomUUID();
|
||||
when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(ReviewNotFoundException.class,
|
||||
() -> reviewUseCase.deleteReviews(nonExistentCustomerId, nonExistentBookId));
|
||||
() -> reviewUseCase.deleteReviews(nonExistentAvisId));
|
||||
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId);
|
||||
verify(reviewRepository, never()).delete(any(Review.class));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user