Compare commits

..

3 Commits

6 changed files with 248 additions and 96 deletions
@@ -0,0 +1,25 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
import java.text.MessageFormat;
import java.util.Optional;
import java.util.UUID;
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 ReviewNotFoundException(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> avisUUID) {
super(buildMessage(customerUUID, bookUUID, avisUUID));
}
private static String buildMessage(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> avisUUID) {
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());
}
}
@@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
public class ReviewRepository { public class ReviewRepository {
@@ -28,16 +29,16 @@ public class ReviewRepository {
return newReview; return newReview;
} }
public Optional<Review> findByCustomerId(UUID customerUUID) { public ArrayList<Review> findByCustomerId(UUID customerUUID) {
return this.reviews.stream() return this.reviews.stream()
.filter(review -> review.getCustomerId().equals(customerUUID)) .filter(review -> review.getCustomerId().equals(customerUUID))
.findFirst(); .collect(Collectors.toCollection(ArrayList::new));
} }
public Optional<Review> findByBookId(UUID bookUUID) { public ArrayList<Review> findByBookId(UUID bookUUID) {
return this.reviews.stream() return this.reviews.stream()
.filter(review -> review.getBookId().equals(bookUUID)) .filter(review -> review.getBookId().equals(bookUUID))
.findFirst(); .collect(Collectors.toCollection(ArrayList::new));
} }
public Optional<Review> findByAvisId(UUID avisUUID) { public Optional<Review> findByAvisId(UUID avisUUID) {
@@ -0,0 +1,113 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.review.converter.ReviewConverter;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.ReviewNotFoundException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.review.validator.ReviewValidator;
import java.util.ArrayList;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
public class ReviewUseCase {
private final ReviewRepository reviewRepository;
public ReviewUseCase(ReviewRepository reviewRepository) {
this.reviewRepository = reviewRepository;
}
public UUID registerReview(ReviewInfo newReview) throws NotValidReviewException {
ReviewValidator.validate(newReview);
Review reviewToRegister = ReviewConverter.toDomain(newReview);
Review reviewToRegistered = reviewRepository.save(reviewToRegister);
return reviewToRegistered.getAvisId();
}
public ArrayList<ReviewDTO> findReviewByCustomerId(UUID customerId) {
ArrayList<Review> optionalReviews = reviewRepository.findByCustomerId(customerId);
return optionalReviews.stream()
.map(ReviewConverter::toDTO)
.collect(Collectors.toCollection(ArrayList::new));
}
public ArrayList<ReviewDTO> findReviewByBookId(UUID bookId) {
ArrayList<Review> optionalReviews = reviewRepository.findByBookId(bookId);
return optionalReviews.stream()
.map(ReviewConverter::toDTO)
.collect(Collectors.toCollection(ArrayList::new));
}
public Optional<ReviewDTO> findReviewByAvisId(UUID avisId) {
Optional<Review> optionalReview = reviewRepository.findByAvisId(avisId);
return optionalReview.map(ReviewConverter::toDTO);
}
public ReviewDTO updateReview(UUID avisUUID, ReviewInfo reviewInfo)
throws ReviewNotFoundException, NotValidReviewException {
ReviewValidator.validate(reviewInfo);
Review reviewByAvisUUID = getReviewIfDoesNotExistThrowReviewNotFoundException(
avisUUID);
Review review = Review.builder()
.avisId(avisUUID)
.customerId(reviewByAvisUUID.getCustomerId())
.bookId(reviewByAvisUUID.getBookId())
.note(reviewByAvisUUID.getNote())
.comment(reviewByAvisUUID.getComment())
.purchaseDate(reviewByAvisUUID.getPurchaseDate())
.build();
Review updatedReview = reviewRepository.save(review);
return ReviewConverter.toDTO(updatedReview);
}
public void deleteReview(UUID avisUUID) throws ReviewNotFoundException {
Review reviewToDelete = getReviewIfDoesNotExistThrowReviewNotFoundException(avisUUID);
this.reviewRepository.delete(reviewToDelete);
}
public void deleteCustomerReviews(UUID customerUUID) throws ReviewNotFoundException {
ArrayList<Review> reviewsToDelete = getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(customerUUID);
for (Review review : reviewsToDelete) {
reviewRepository.delete(review);
}
}
public void deleteBookReviews(UUID bookUUID) throws ReviewNotFoundException {
ArrayList<Review> reviewsToDelete = getReviewByBookIfDoesNotExistThrowReviewNotFoundException(bookUUID);
for (Review review : reviewsToDelete) {
reviewRepository.delete(review);
}
}
private Review getReviewIfDoesNotExistThrowReviewNotFoundException(UUID avisUUID)
throws ReviewNotFoundException {
Optional<Review> optionalReviewByAvisId = reviewRepository.findByAvisId(avisUUID);
if (optionalReviewByAvisId.isEmpty()) {
throw new ReviewNotFoundException(Optional.empty(), Optional.empty(),Optional.of(avisUUID));
}
return optionalReviewByAvisId.get();
}
private ArrayList<Review> getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(UUID customerUUID)
throws ReviewNotFoundException {
ArrayList<Review> optionalReviewByAvisId = reviewRepository.findByCustomerId(customerUUID);
if (optionalReviewByAvisId.isEmpty()) {
throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(),Optional.empty());
}
return optionalReviewByAvisId;
}
private ArrayList<Review> getReviewByBookIfDoesNotExistThrowReviewNotFoundException(UUID bookUUID)
throws ReviewNotFoundException {
ArrayList<Review> optionalReviewByAvisId = reviewRepository.findByBookId(bookUUID);
if (optionalReviewByAvisId.isEmpty()) {
throw new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(bookUUID));
}
return optionalReviewByAvisId;
}
}
@@ -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,21 +27,20 @@ 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 the book id %s does not exists", bookUUID);
assertEquals(expectedMessage, exception.getMessage()); assertEquals(expectedMessage, exception.getMessage());
} }
@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 avis 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,
@@ -60,10 +59,10 @@ public class ReviewNotFoundExceptionTest {
@Test @Test
@DisplayName("Exception should use the correct constant message format for book") @DisplayName("Exception should use the correct constant message format for book")
void testExceptionUsesConstantMessageCustomerFormat() { void testExceptionUsesConstantMessageBookFormat() {
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,
@@ -72,28 +71,27 @@ 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 review")
void testExceptionUsesConstantMessageCustomerFormat() { void testExceptionUsesConstantMessageReviewFormat() {
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 expectedFormatWithPlaceholder = "The reviews with the customer id {0} and the book id {1} does not exists"; String expectedFormatWithPlaceholder = "The review with avis id {0} does not exists";
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_CUSTOMER_ID_AND_BOOK_ID_DOES_NOT_EXIST_MESSAGE, assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_AVIS_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.empty(),Optional.empty(), Optional.of(avisUUID));
} 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 review with avis id %s does not exists", avisUUID);
assertEquals(expectedMessage, e.getMessage()); assertEquals(expectedMessage, e.getMessage());
} }
} }
@@ -7,6 +7,7 @@ import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@@ -143,6 +144,8 @@ public class ReviewRepositoryTest {
void setUpReviews() { void setUpReviews() {
repository.save(review1); repository.save(review1);
repository.save(review2); repository.save(review2);
repository.save(review3);
repository.save(review4);
} }
@Test @Test
@@ -150,19 +153,22 @@ public class ReviewRepositoryTest {
void testFindAll() { void testFindAll() {
List<Review> reviews = repository.findAll(); List<Review> reviews = repository.findAll();
assertEquals(2, reviews.size()); assertEquals(4, reviews.size());
assertTrue(reviews.contains(review1)); assertTrue(reviews.contains(review1));
assertTrue(reviews.contains(review2)); assertTrue(reviews.contains(review2));
assertTrue(reviews.contains(review3));
assertTrue(reviews.contains(review4));
} }
@Test @Test
@DisplayName("findByCustomerId should return review with matching customer ID") @DisplayName("findByCustomerId should return review with matching customer ID")
void testFindByCustomerId() { void testFindByCustomerId() {
Optional<Review> foundreview = repository.findByCustomerId(review1.getCustomerId()); ArrayList<Review> foundreviews = repository.findByCustomerId(review2.getCustomerId());
assertTrue(foundreview.isPresent()); assertTrue(!foundreviews.isEmpty());
assertEquals(review1.getNote(), foundreview.get().getNote()); boolean allSameCustomer = foundreviews.stream()
assertEquals(review1.getComment(), foundreview.get().getComment()); .allMatch(review -> review.getCustomerId().equals(review2.getCustomerId()));
assertTrue(allSameCustomer);
} }
@Test @Test
@@ -170,7 +176,7 @@ public class ReviewRepositoryTest {
void testFindByCustomerIdNotFound() { void testFindByCustomerIdNotFound() {
UUID nonExistentCustomerId = UUID.randomUUID(); UUID nonExistentCustomerId = UUID.randomUUID();
Optional<Review> foundreview = repository.findByCustomerId(nonExistentCustomerId); ArrayList<Review> foundreview = repository.findByCustomerId(nonExistentCustomerId);
assertTrue(foundreview.isEmpty()); assertTrue(foundreview.isEmpty());
} }
@@ -178,11 +184,12 @@ public class ReviewRepositoryTest {
@Test @Test
@DisplayName("findByBookId should return review with matching book ID") @DisplayName("findByBookId should return review with matching book ID")
void testFindByBookId() { void testFindByBookId() {
Optional<Review> foundreview = repository.findByBookId(review1.getBookId()); ArrayList<Review> foundreviews = repository.findByBookId(review2.getBookId());
assertTrue(foundreview.isPresent()); assertTrue(!foundreviews.isEmpty());
assertEquals(review1.getNote(), foundreview.get().getNote()); boolean allSameCustomer = foundreviews.stream()
assertEquals(review1.getComment(), foundreview.get().getComment()); .allMatch(review -> review.getBookId().equals(review2.getBookId()));
assertTrue(allSameCustomer);
} }
@Test @Test
@@ -190,7 +197,7 @@ public class ReviewRepositoryTest {
void testFindByBookIdNotFound() { void testFindByBookIdNotFound() {
UUID nonExistentBookId = UUID.randomUUID(); UUID nonExistentBookId = UUID.randomUUID();
Optional<Review> foundreview = repository.findByBookId(nonExistentBookId); ArrayList<Review> foundreview = repository.findByBookId(nonExistentBookId);
assertTrue(foundreview.isEmpty()); assertTrue(foundreview.isEmpty());
} }
@@ -1,19 +1,23 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase; package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO; import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo; import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review; import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException; import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.ReviewNotFoundException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository; import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@@ -25,6 +29,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
@ExtendWith(MockitoExtension.class)
public class ReviewUseCaseTest { public class ReviewUseCaseTest {
@Mock @Mock
@@ -33,6 +38,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 +47,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)
@@ -67,17 +75,17 @@ public class ReviewUseCaseTest {
UUID registeredId = reviewUseCase.registerReview(validReviewInfo); UUID registeredId = reviewUseCase.registerReview(validReviewInfo);
assertNotNull(registeredId); assertNotNull(registeredId);
assertEquals(customerId, registeredId); assertEquals(avisId, registeredId);
verify(reviewRepository, times(1)).save(any(Review.class)); verify(reviewRepository, times(1)).save(any(Review.class));
} }
@Test @Test
@DisplayName("Should throw exception when review data is not valid") @DisplayName("Should throw exception when review data is not valid")
void testRegisterReviewWithInvalidData() { void testRegisterReviewWithInvalidData() {
ReviewInfo invalidReviewInfo = new ReviewInfo(2, "plutôt mauvais", purchaseDate); ReviewInfo invalidReviewInfo = new ReviewInfo(0, "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));
} }
@@ -88,15 +96,16 @@ public class ReviewUseCaseTest {
class FindReviewTests { class FindReviewTests {
@Test @Test
@DisplayName("Should return review when customer ID exists") @DisplayName("Should return reviews when customer ID exists")
void testFindReviewByCustomerId() { void testFindReviewByCustomerId() {
when(reviewRepository.findByCustomerId(customerId)).thenReturn(Optional.of(testReview)); when(reviewRepository.findByCustomerId(customerId)).thenReturn(new ArrayList<Review>(List.of(testReview)));
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerId(customerId); ArrayList<ReviewDTO> foundReviews = reviewUseCase.findReviewByCustomerId(customerId);
assertTrue(foundReview.isPresent()); assertTrue(!foundReviews.isEmpty());
assertEquals(testReview.getBookId(), foundReview.get().getBookId()); boolean allSameCustomer = foundReviews.stream()
assertEquals(testReview.getNote(), foundReview.get().getNote()); .allMatch(review -> review.getCustomerId().equals(customerId));
assertTrue(allSameCustomer);
verify(reviewRepository, times(1)).findByCustomerId(customerId); verify(reviewRepository, times(1)).findByCustomerId(customerId);
} }
@@ -104,24 +113,25 @@ public class ReviewUseCaseTest {
@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 nonExistentCustomerId = UUID.randomUUID();
when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(Optional.empty()); when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(new ArrayList<Review>());
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerId(nonExistentCustomerId); ArrayList<ReviewDTO> foundReviews = reviewUseCase.findReviewByCustomerId(nonExistentCustomerId);
assertTrue(foundReview.isEmpty()); assertTrue(foundReviews.isEmpty());
verify(reviewRepository, times(1)).findByCustomerId(nonExistentCustomerId); verify(reviewRepository, times(1)).findByCustomerId(nonExistentCustomerId);
} }
@Test @Test
@DisplayName("Should return review when book ID exists") @DisplayName("Should return reviews when book ID exists")
void testFindReviewByBookId() { void testFindReviewByBookId() {
when(reviewRepository.findByBookId(bookId)).thenReturn(Optional.of(testReview)); when(reviewRepository.findByBookId(bookId)).thenReturn(new ArrayList<Review>(List.of(testReview)));
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByBookId(bookId); ArrayList<ReviewDTO> foundReviews = reviewUseCase.findReviewByBookId(bookId);
assertTrue(foundReview.isPresent()); assertTrue(!foundReviews.isEmpty());
assertEquals(testReview.getCustomerId(), foundReview.get().getCustomerId()); boolean allSameCustomer = foundReviews.stream()
assertEquals(testReview.getNote(), foundReview.get().getNote()); .allMatch(review -> review.getBookId().equals(bookId));
assertTrue(allSameCustomer);
verify(reviewRepository, times(1)).findByBookId(bookId); verify(reviewRepository, times(1)).findByBookId(bookId);
} }
@@ -129,38 +139,37 @@ public class ReviewUseCaseTest {
@DisplayName("Should return empty Optional when book ID doesn't exist") @DisplayName("Should return empty Optional when book ID doesn't exist")
void testFindReviewByBookIdNotFound() { void testFindReviewByBookIdNotFound() {
UUID nonExistentBookId = UUID.randomUUID(); UUID nonExistentBookId = UUID.randomUUID();
when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(Optional.empty()); when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(new ArrayList<Review>());
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByBookId(nonExistentBookId); ArrayList<ReviewDTO> foundReviews = reviewUseCase.findReviewByBookId(nonExistentBookId);
assertTrue(foundReview.isEmpty()); assertTrue(foundReviews.isEmpty());
verify(reviewRepository, times(1)).findByBookId(nonExistentBookId); verify(reviewRepository, times(1)).findByBookId(nonExistentBookId);
} }
@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(avisId);
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(avisId);
} }
@Test @Test
@DisplayName("Should return empty Optional when customer ID doesn't exist") @DisplayName("Should return empty Optional when avis ID doesn't exist")
void testFindReviewByCustomerIdNotFound() { void testFindReviewByAvisIdNotFound() {
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 +180,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,30 +196,29 @@ 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));
} }
@Test @Test
@DisplayName("Should throw exception when customer and book ID doesn't exist") @DisplayName("Should throw exception when avis ID doesn't exist")
void testUpdateReviewNotFound() { void testUpdateReviewNotFound() {
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());
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);
assertThrows(ReviewNotFoundException.class, assertThrows(ReviewNotFoundException.class,
() -> reviewUseCase.updateReview(nonExistentCustomerId, nonExistentBookId, updateInfo)); () -> reviewUseCase.updateReview(nonExistentAvisId, updateInfo));
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId);
verify(reviewRepository, never()).save(any(Review.class)); verify(reviewRepository, never()).save(any(Review.class));
} }
@@ -219,10 +228,10 @@ public class ReviewUseCaseTest {
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24); LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24);
ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate); ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate);
assertThrows(NotValidCustomerException.class, assertThrows(NotValidReviewException.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));
} }
} }
@@ -234,7 +243,7 @@ public class ReviewUseCaseTest {
@Test @Test
@DisplayName("Should delete reviews when customer ID exists") @DisplayName("Should delete reviews when customer ID exists")
void testDeleteCustomerReviews() throws ReviewNotFoundException { void testDeleteCustomerReviews() throws ReviewNotFoundException {
when(reviewRepository.findByCustomerId(customerId)).thenReturn(Optional.of(testReview)); when(reviewRepository.findByCustomerId(customerId)).thenReturn(new ArrayList<Review>(List.of(testReview)));
doNothing().when(reviewRepository).delete(testReview); doNothing().when(reviewRepository).delete(testReview);
reviewUseCase.deleteCustomerReviews(customerId); reviewUseCase.deleteCustomerReviews(customerId);
@@ -247,7 +256,7 @@ public class ReviewUseCaseTest {
@DisplayName("Should throw exception when customer ID doesn't exist") @DisplayName("Should throw exception when customer ID doesn't exist")
void testDeleteCustomerReviewsNotFound() { void testDeleteCustomerReviewsNotFound() {
UUID nonExistentCustomerId = UUID.randomUUID(); UUID nonExistentCustomerId = UUID.randomUUID();
when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(Optional.empty()); when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(new ArrayList<Review>());
assertThrows(ReviewNotFoundException.class, assertThrows(ReviewNotFoundException.class,
() -> reviewUseCase.deleteCustomerReviews(nonExistentCustomerId)); () -> reviewUseCase.deleteCustomerReviews(nonExistentCustomerId));
@@ -259,7 +268,7 @@ public class ReviewUseCaseTest {
@Test @Test
@DisplayName("Should delete reviews when book ID exists") @DisplayName("Should delete reviews when book ID exists")
void testDeleteBookReviews() throws ReviewNotFoundException { void testDeleteBookReviews() throws ReviewNotFoundException {
when(reviewRepository.findByBookId(bookId)).thenReturn(Optional.of(testReview)); when(reviewRepository.findByBookId(bookId)).thenReturn(new ArrayList<Review>(List.of(testReview)));
doNothing().when(reviewRepository).delete(testReview); doNothing().when(reviewRepository).delete(testReview);
reviewUseCase.deleteBookReviews(bookId); reviewUseCase.deleteBookReviews(bookId);
@@ -272,7 +281,7 @@ public class ReviewUseCaseTest {
@DisplayName("Should throw exception when book ID doesn't exist") @DisplayName("Should throw exception when book ID doesn't exist")
void testDeleteBookReviewsNotFound() { void testDeleteBookReviewsNotFound() {
UUID nonExistentBookId = UUID.randomUUID(); UUID nonExistentBookId = UUID.randomUUID();
when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(Optional.empty()); when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(new ArrayList<Review>());
assertThrows(ReviewNotFoundException.class, assertThrows(ReviewNotFoundException.class,
() -> reviewUseCase.deleteBookReviews(nonExistentBookId)); () -> reviewUseCase.deleteBookReviews(nonExistentBookId));
@@ -282,28 +291,27 @@ public class ReviewUseCaseTest {
} }
@Test @Test
@DisplayName("Should delete review when customer and book ID exists") @DisplayName("Should delete review when avis 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 avis 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.deleteReview(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));
} }
} }