Feature/manage reviews #2

Merged
Patrick FELIX-VIMALARATNAM merged 20 commits from feature/manage_reviews into main 2026-06-12 20:48:07 +02:00
11 changed files with 107 additions and 106 deletions
Showing only changes of commit 13f8cab3ed - Show all commits
@@ -10,7 +10,7 @@ import java.util.UUID;
@Builder
public class ReviewDTO {
private UUID avisId;
private UUID reviewId;
private UUID customerId;
private UUID bookId;
private Integer note;
@@ -19,6 +19,7 @@ public class ReviewConverter {
public static ReviewDTO toDTO(Review review) {
return ReviewDTO.builder()
.reviewId(review.getReviewId())
.customerId(review.getCustomerId())
.bookId(review.getBookId())
.note(review.getNote())
@@ -10,7 +10,7 @@ import java.util.UUID;
@Builder
public class Review {
private UUID avisId;
private UUID reviewId;
private UUID customerId;
private UUID bookId;
private Integer note;
@@ -18,7 +18,7 @@ public class Review {
private LocalDate purchaseDate;
public void setRandomUUID() {
this.avisId = UUID.randomUUID();
this.reviewId = UUID.randomUUID();
}
public void setRandomUUIDCustomerAndBook() {
@@ -8,18 +8,18 @@ public class ReviewNotFoundException extends RuntimeException {
public static final String THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the customer id {0} does not exists";
public static final String THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the book id {0} does not exists";
public static final String THE_REVIEWS_WITH_AVIS_ID_DOES_NOT_EXIST_MESSAGE = "The review with avis id {0} does not exists";
public static final String THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE = "The review with review id {0} does not exists";
public ReviewNotFoundException(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> avisUUID) {
super(buildMessage(customerUUID, bookUUID, avisUUID));
public ReviewNotFoundException(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> reviewUUID) {
super(buildMessage(customerUUID, bookUUID, reviewUUID));
}
private static String buildMessage(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> avisUUID) {
private static String buildMessage(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> reviewUUID) {
if (customerUUID.isPresent()) {
return MessageFormat.format(THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE, customerUUID.get());
}else if (bookUUID.isPresent()) {
return MessageFormat.format(THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE, bookUUID.get());
}
return MessageFormat.format(THE_REVIEWS_WITH_AVIS_ID_DOES_NOT_EXIST_MESSAGE, avisUUID.get());
return MessageFormat.format(THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE, reviewUUID.get());
}
}
@@ -23,8 +23,8 @@ public class ReviewRepository {
}
public Review save(Review newReview) {
Optional<Review> optionalReviewWithSameAvisId = this.findByAvisId(newReview.getAvisId());
optionalReviewWithSameAvisId.ifPresent(reviews::remove);
Optional<Review> optionalReviewWithSameReviewId = this.findByReviewId(newReview.getReviewId());
optionalReviewWithSameReviewId.ifPresent(reviews::remove);
this.reviews.add(newReview);
return newReview;
}
@@ -41,9 +41,9 @@ public class ReviewRepository {
.collect(Collectors.toCollection(ArrayList::new));
}
public Optional<Review> findByAvisId(UUID avisUUID) {
public Optional<Review> findByReviewId(UUID reviewUUID) {
return this.reviews.stream()
.filter(review -> review.getAvisId().equals(avisUUID))
.filter(review -> review.getReviewId().equals(reviewUUID))
.findFirst();
}
@@ -57,9 +57,9 @@ public class ReviewRepository {
.anyMatch(review -> review.getBookId().equals(bookUUID));
}
public boolean existsByAvisId(UUID avisUUID) {
public boolean existsByReviewId(UUID reviewUUID) {
return this.reviews.stream()
.anyMatch(review -> review.getAvisId().equals(avisUUID));
.anyMatch(review -> review.getReviewId().equals(reviewUUID));
}
public void deleteCustomerReviews(UUID customerUUID) {
@@ -26,7 +26,7 @@ public class ReviewUseCase {
ReviewValidator.validate(newReview);
Review reviewToRegister = ReviewConverter.toDomain(newReview);
Review reviewToRegistered = reviewRepository.save(reviewToRegister);
return reviewToRegistered.getAvisId();
return reviewToRegistered.getReviewId();
}
public ArrayList<ReviewDTO> findReviewByCustomerId(UUID customerId) {
@@ -43,30 +43,30 @@ public class ReviewUseCase {
.collect(Collectors.toCollection(ArrayList::new));
}
public Optional<ReviewDTO> findReviewByAvisId(UUID avisId) {
Optional<Review> optionalReview = reviewRepository.findByAvisId(avisId);
public Optional<ReviewDTO> findReviewByReviewId(UUID reviewId) {
Optional<Review> optionalReview = reviewRepository.findByReviewId(reviewId);
return optionalReview.map(ReviewConverter::toDTO);
}
public ReviewDTO updateReview(UUID avisUUID, ReviewInfo reviewInfo)
public ReviewDTO updateReview(UUID reviewUUID, ReviewInfo reviewInfo)
throws ReviewNotFoundException, NotValidReviewException {
ReviewValidator.validate(reviewInfo);
Review reviewByAvisUUID = getReviewIfDoesNotExistThrowReviewNotFoundException(
avisUUID);
Review reviewByReviewUUID = getReviewIfDoesNotExistThrowReviewNotFoundException(
reviewUUID);
Review review = Review.builder()
.avisId(avisUUID)
.customerId(reviewByAvisUUID.getCustomerId())
.bookId(reviewByAvisUUID.getBookId())
.note(reviewByAvisUUID.getNote())
.comment(reviewByAvisUUID.getComment())
.purchaseDate(reviewByAvisUUID.getPurchaseDate())
.reviewId(reviewUUID)
.customerId(reviewByReviewUUID.getCustomerId())
.bookId(reviewByReviewUUID.getBookId())
.note(reviewByReviewUUID.getNote())
.comment(reviewByReviewUUID.getComment())
.purchaseDate(reviewByReviewUUID.getPurchaseDate())
.build();
Review updatedReview = reviewRepository.save(review);
return ReviewConverter.toDTO(updatedReview);
}
public void deleteReview(UUID avisUUID) throws ReviewNotFoundException {
Review reviewToDelete = getReviewIfDoesNotExistThrowReviewNotFoundException(avisUUID);
public void deleteReview(UUID reviewUUID) throws ReviewNotFoundException {
Review reviewToDelete = getReviewIfDoesNotExistThrowReviewNotFoundException(reviewUUID);
this.reviewRepository.delete(reviewToDelete);
}
@@ -84,30 +84,30 @@ public class ReviewUseCase {
}
}
private Review getReviewIfDoesNotExistThrowReviewNotFoundException(UUID avisUUID)
private Review getReviewIfDoesNotExistThrowReviewNotFoundException(UUID reviewUUID)
throws ReviewNotFoundException {
Optional<Review> optionalReviewByAvisId = reviewRepository.findByAvisId(avisUUID);
if (optionalReviewByAvisId.isEmpty()) {
throw new ReviewNotFoundException(Optional.empty(), Optional.empty(),Optional.of(avisUUID));
Optional<Review> optionalReviewByReviewId = reviewRepository.findByReviewId(reviewUUID);
if (optionalReviewByReviewId.isEmpty()) {
throw new ReviewNotFoundException(Optional.empty(), Optional.empty(),Optional.of(reviewUUID));
}
return optionalReviewByAvisId.get();
return optionalReviewByReviewId.get();
}
private ArrayList<Review> getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(UUID customerUUID)
throws ReviewNotFoundException {
ArrayList<Review> optionalReviewByAvisId = reviewRepository.findByCustomerId(customerUUID);
if (optionalReviewByAvisId.isEmpty()) {
ArrayList<Review> optionalReviewByReviewId = reviewRepository.findByCustomerId(customerUUID);
if (optionalReviewByReviewId.isEmpty()) {
throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(),Optional.empty());
}
return optionalReviewByAvisId;
return optionalReviewByReviewId;
}
private ArrayList<Review> getReviewByBookIfDoesNotExistThrowReviewNotFoundException(UUID bookUUID)
throws ReviewNotFoundException {
ArrayList<Review> optionalReviewByAvisId = reviewRepository.findByBookId(bookUUID);
if (optionalReviewByAvisId.isEmpty()) {
ArrayList<Review> optionalReviewByReviewId = reviewRepository.findByBookId(bookUUID);
if (optionalReviewByReviewId.isEmpty()) {
throw new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(bookUUID));
}
return optionalReviewByAvisId;
return optionalReviewByReviewId;
}
}
@@ -47,7 +47,7 @@ public class ReviewConverterTest {
void shouldConvertReviewToDTO() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
Review review = Review.builder()
.avisId(UUID.randomUUID())
.reviewId(UUID.randomUUID())
.customerId(UUID.randomUUID())
.bookId(UUID.randomUUID())
.note(5)
@@ -38,17 +38,17 @@ public class ReviewTest {
@DisplayName("setRandomUUID should change the ID to a new random UUID")
void testSetRandomUUID() {
Review review = Review.builder().build();
UUID originalAvisId = review.getAvisId();
UUID originalReviewId = review.getReviewId();
UUID originalCustomerId = review.getCustomerId();
UUID originalBookId = review.getCustomerId();
review.setRandomUUID();
review.setRandomUUIDCustomerAndBook();
assertNotNull(review.getAvisId());
assertNotNull(review.getReviewId());
assertNotNull(review.getCustomerId());
assertNotNull(review.getBookId());
assertNotEquals(originalAvisId, review.getAvisId());
assertNotEquals(originalReviewId, review.getReviewId());
assertNotEquals(originalCustomerId, review.getCustomerId());
assertNotEquals(originalBookId, review.getBookId());
}
@@ -36,11 +36,11 @@ public class ReviewNotFoundExceptionTest {
@Test
@DisplayName("Exception message should contain the UUID provided for customer and book")
void testExceptionMessageContainsUUIDForCustomerAndBook() {
UUID avisUUID = UUID.randomUUID();
UUID reviewUUID = UUID.randomUUID();
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(avisUUID));
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(reviewUUID));
String expectedMessage = String.format("The review with avis id %s does not exists", avisUUID);
String expectedMessage = String.format("The review with review id %s does not exists", reviewUUID);
assertEquals(expectedMessage, exception.getMessage());
}
@@ -73,25 +73,25 @@ public class ReviewNotFoundExceptionTest {
@Test
@DisplayName("Exception should use the correct constant message format for review")
void testExceptionUsesConstantMessageReviewFormat() {
UUID avisUUID = UUID.randomUUID();
UUID reviewUUID = UUID.randomUUID();
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(avisUUID));
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(reviewUUID));
String expectedFormatWithPlaceholder = "The review with avis id {0} does not exists";
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_AVIS_ID_DOES_NOT_EXIST_MESSAGE,
String expectedFormatWithPlaceholder = "The review with review id {0} does not exists";
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE,
expectedFormatWithPlaceholder);
assertTrue(exception.getMessage().contains(avisUUID.toString()));
assertTrue(exception.getMessage().contains(reviewUUID.toString()));
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
UUID avisUUID = UUID.randomUUID();
UUID reviewUUID = UUID.randomUUID();
try {
throw new ReviewNotFoundException(Optional.empty(),Optional.empty(), Optional.of(avisUUID));
throw new ReviewNotFoundException(Optional.empty(),Optional.empty(), Optional.of(reviewUUID));
} catch (ReviewNotFoundException e) {
String expectedMessage = String.format("The review with avis id %s does not exists", avisUUID);
String expectedMessage = String.format("The review with review id %s does not exists", reviewUUID);
assertEquals(expectedMessage, e.getMessage());
}
}
@@ -99,11 +99,11 @@ public class ReviewRepositoryTest {
repository.save(review1);
LocalDate purchaseDate = LocalDate.of(2026, 5, 24);
UUID avisId = review1.getAvisId();
UUID reviewId = review1.getReviewId();
UUID customerId = UUID.randomUUID();
UUID bookId = UUID.randomUUID();
Review updatedReview = Review.builder()
.avisId(avisId)
.reviewId(reviewId)
.customerId(customerId)
.bookId(bookId)
.note(4)
@@ -114,7 +114,7 @@ public class ReviewRepositoryTest {
Review savedReview = repository.save(updatedReview);
assertEquals(1, repository.findAll().size());
assertEquals(avisId, savedReview.getAvisId());
assertEquals(reviewId, savedReview.getReviewId());
assertEquals(customerId, savedReview.getCustomerId());
assertEquals(bookId, savedReview.getBookId());
assertEquals(4, savedReview.getNote());
@@ -203,9 +203,9 @@ public class ReviewRepositoryTest {
}
@Test
@DisplayName("findByAvisId should return review with matching avis ID")
void testFindByAvisId() {
Optional<Review> foundreview = repository.findByAvisId(review1.getAvisId());
@DisplayName("findByReviewId should return review with matching review ID")
void testFindByReviewId() {
Optional<Review> foundreview = repository.findByReviewId(review1.getReviewId());
assertTrue(foundreview.isPresent());
assertEquals(review1.getNote(), foundreview.get().getNote());
@@ -213,11 +213,11 @@ public class ReviewRepositoryTest {
}
@Test
@DisplayName("findByAvisId should return empty Optional when a review with avis ID doesn't exist")
void testFindByAvisIdNotFound() {
UUID nonExistentAvisId = UUID.randomUUID();
@DisplayName("findByReviewId should return empty Optional when a review with review ID doesn't exist")
void testFindByReviewIdNotFound() {
UUID nonExistentReviewId = UUID.randomUUID();
Optional<Review> foundreview = repository.findByAvisId(nonExistentAvisId);
Optional<Review> foundreview = repository.findByReviewId(nonExistentReviewId);
assertTrue(foundreview.isEmpty());
}
@@ -259,19 +259,19 @@ public class ReviewRepositoryTest {
}
@Test
@DisplayName("existsByAvisId should return true when a review with avis ID exists")
void testExistsByAvisIdExists() {
boolean exists = repository.existsByAvisId(review1.getAvisId());
@DisplayName("existsByReviewId should return true when a review with review ID exists")
void testExistsByReviewIdExists() {
boolean exists = repository.existsByReviewId(review1.getReviewId());
assertTrue(exists);
}
@Test
@DisplayName("existsByAvisId should return false when avis ID doesn't exist")
void testExistsByAvisIdNotExists() {
UUID nonExistentAvisId = UUID.randomUUID();
@DisplayName("existsByReviewId should return false when review ID doesn't exist")
void testExistsByReviewIdNotExists() {
UUID nonExistentReviewId = UUID.randomUUID();
boolean exists = repository.existsByAvisId(nonExistentAvisId);
boolean exists = repository.existsByReviewId(nonExistentReviewId);
assertFalse(exists);
}
@@ -38,7 +38,7 @@ public class ReviewUseCaseTest {
@InjectMocks
private ReviewUseCase reviewUseCase;
private UUID avisId;
private UUID reviewId;
private UUID customerId;
private UUID bookId;
private LocalDate purchaseDate;
@@ -47,12 +47,12 @@ public class ReviewUseCaseTest {
@BeforeEach
void setUp() {
avisId = UUID.randomUUID();
reviewId = UUID.randomUUID();
customerId = UUID.randomUUID();
bookId = UUID.randomUUID();
purchaseDate = LocalDate.of(2026, 5, 24);
testReview = Review.builder()
.avisId(avisId)
.reviewId(reviewId)
.customerId(customerId)
.bookId(bookId)
.note(2)
@@ -75,7 +75,7 @@ public class ReviewUseCaseTest {
UUID registeredId = reviewUseCase.registerReview(validReviewInfo);
assertNotNull(registeredId);
assertEquals(avisId, registeredId);
assertEquals(reviewId, registeredId);
verify(reviewRepository, times(1)).save(any(Review.class));
}
@@ -148,28 +148,28 @@ public class ReviewUseCaseTest {
}
@Test
@DisplayName("Should return review when Avis ID exists")
void testFindReviewByAvisId() {
when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview));
@DisplayName("Should return review when Review ID exists")
void testFindReviewByReviewId() {
when(reviewRepository.findByReviewId(reviewId)).thenReturn(Optional.of(testReview));
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByAvisId(avisId);
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByReviewId(reviewId);
assertTrue(foundReview.isPresent());
assertEquals(testReview.getBookId(), foundReview.get().getBookId());
assertEquals(testReview.getNote(), foundReview.get().getNote());
verify(reviewRepository, times(1)).findByAvisId(avisId);
verify(reviewRepository, times(1)).findByReviewId(reviewId);
}
@Test
@DisplayName("Should return empty Optional when avis ID doesn't exist")
void testFindReviewByAvisIdNotFound() {
UUID nonExistentAvisId = UUID.randomUUID();
when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty());
@DisplayName("Should return empty Optional when review ID doesn't exist")
void testFindReviewByReviewIdNotFound() {
UUID nonExistentReviewId = UUID.randomUUID();
when(reviewRepository.findByReviewId(nonExistentReviewId)).thenReturn(Optional.empty());
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByAvisId(nonExistentAvisId);
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByReviewId(nonExistentReviewId);
assertTrue(foundReview.isEmpty());
verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId);
verify(reviewRepository, times(1)).findByReviewId(nonExistentReviewId);
}
}
@@ -180,11 +180,11 @@ public class ReviewUseCaseTest {
@Test
@DisplayName("Should update review when valid data is provided")
void testUpdateReviewWithValidData() throws ReviewNotFoundException, NotValidReviewException {
when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview));
when(reviewRepository.findByReviewId(reviewId)).thenReturn(Optional.of(testReview));
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 30);
Review updatedReview = Review.builder()
.avisId(avisId)
.reviewId(reviewId)
.customerId(customerId)
.bookId(bookId)
.note(4)
@@ -196,29 +196,29 @@ public class ReviewUseCaseTest {
ReviewInfo updateInfo = new ReviewInfo(4, "en fait c'est bien", updatePurchaseDate);
ReviewDTO result = reviewUseCase.updateReview(avisId, updateInfo);
ReviewDTO result = reviewUseCase.updateReview(reviewId, updateInfo);
assertNotNull(result);
assertEquals(customerId, result.getCustomerId());
assertEquals(4, result.getNote());
assertEquals("en fait c'est bien", result.getComment());
verify(reviewRepository, times(1)).findByAvisId(avisId);
verify(reviewRepository, times(1)).findByReviewId(reviewId);
verify(reviewRepository, times(1)).save(any(Review.class));
}
@Test
@DisplayName("Should throw exception when avis ID doesn't exist")
@DisplayName("Should throw exception when review ID doesn't exist")
void testUpdateReviewNotFound() {
UUID nonExistentAvisId = UUID.randomUUID();
when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty());
UUID nonExistentReviewId = UUID.randomUUID();
when(reviewRepository.findByReviewId(nonExistentReviewId)).thenReturn(Optional.empty());
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24);
ReviewInfo updateInfo = new ReviewInfo(3, "moyen", updatePurchaseDate);
assertThrows(ReviewNotFoundException.class,
() -> reviewUseCase.updateReview(nonExistentAvisId, updateInfo));
() -> reviewUseCase.updateReview(nonExistentReviewId, updateInfo));
verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId);
verify(reviewRepository, times(1)).findByReviewId(nonExistentReviewId);
verify(reviewRepository, never()).save(any(Review.class));
}
@@ -229,9 +229,9 @@ public class ReviewUseCaseTest {
ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate);
assertThrows(NotValidReviewException.class,
() -> reviewUseCase.updateReview(avisId, invalidUpdateInfo));
() -> reviewUseCase.updateReview(reviewId, invalidUpdateInfo));
verify(reviewRepository, never()).findByAvisId(any(UUID.class));
verify(reviewRepository, never()).findByReviewId(any(UUID.class));
verify(reviewRepository, never()).save(any(Review.class));
}
}
@@ -291,27 +291,27 @@ public class ReviewUseCaseTest {
}
@Test
@DisplayName("Should delete review when avis ID exists")
@DisplayName("Should delete review when review ID exists")
void testDeleteReview() throws ReviewNotFoundException {
when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview));
when(reviewRepository.findByReviewId(reviewId)).thenReturn(Optional.of(testReview));
doNothing().when(reviewRepository).delete(testReview);
reviewUseCase.deleteReview(avisId);
reviewUseCase.deleteReview(reviewId);
verify(reviewRepository, times(1)).findByAvisId(avisId);
verify(reviewRepository, times(1)).findByReviewId(reviewId);
verify(reviewRepository, times(1)).delete(testReview);
}
@Test
@DisplayName("Should throw exception when avis ID doesn't exist")
@DisplayName("Should throw exception when review ID doesn't exist")
void testDeleteReviewNotFound() {
UUID nonExistentAvisId = UUID.randomUUID();
when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty());
UUID nonExistentReviewId = UUID.randomUUID();
when(reviewRepository.findByReviewId(nonExistentReviewId)).thenReturn(Optional.empty());
assertThrows(ReviewNotFoundException.class,
() -> reviewUseCase.deleteReview(nonExistentAvisId));
() -> reviewUseCase.deleteReview(nonExistentReviewId));
verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId);
verify(reviewRepository, times(1)).findByReviewId(nonExistentReviewId);
verify(reviewRepository, never()).delete(any(Review.class));
}
}