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