ajout de l'attribut avisID après l'oublie

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