forked from pierront/mylibrary-template
Compare commits
2 Commits
90939326dc
...
1d066a802f
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d066a802f | |||
| a4808a28c9 |
@@ -10,6 +10,7 @@ import java.util.UUID;
|
||||
@Builder
|
||||
|
||||
public class ReviewDTO {
|
||||
private UUID avisId;
|
||||
private UUID customerId;
|
||||
private UUID bookId;
|
||||
private Integer note;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
public record ReviewInfo(Integer note, String comment, LocalDate purchaseDate) {
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.UUID;
|
||||
@Builder
|
||||
|
||||
public class Review {
|
||||
private UUID avisId;
|
||||
private UUID customerId;
|
||||
private UUID bookId;
|
||||
private Integer note;
|
||||
@@ -17,6 +18,10 @@ public class Review {
|
||||
private LocalDate purchaseDate;
|
||||
|
||||
public void setRandomUUID() {
|
||||
this.avisId = UUID.randomUUID();
|
||||
}
|
||||
|
||||
public void setRandomUUIDCustomerAndBook() {
|
||||
|
||||
this.customerId = UUID.randomUUID();
|
||||
this.bookId = UUID.randomUUID();
|
||||
|
||||
+6
-6
@@ -22,8 +22,8 @@ public class ReviewRepository {
|
||||
}
|
||||
|
||||
public Review save(Review newReview) {
|
||||
Optional<Review> optionalReviewWithSameCustomerAndBookId = this.findByCustomerAndBookId(newReview.getCustomerId(), newReview.getBookId());
|
||||
optionalReviewWithSameCustomerAndBookId.ifPresent(reviews::remove);
|
||||
Optional<Review> optionalReviewWithSameAvisId = this.findByAvisId(newReview.getAvisId());
|
||||
optionalReviewWithSameAvisId.ifPresent(reviews::remove);
|
||||
this.reviews.add(newReview);
|
||||
return newReview;
|
||||
}
|
||||
@@ -40,9 +40,9 @@ public class ReviewRepository {
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public Optional<Review> findByCustomerAndBookId(UUID customerUUID, UUID bookUUID) {
|
||||
public Optional<Review> findByAvisId(UUID avisUUID) {
|
||||
return this.reviews.stream()
|
||||
.filter(review -> review.getCustomerId().equals(customerUUID) && review.getBookId().equals(bookUUID))
|
||||
.filter(review -> review.getAvisId().equals(avisUUID))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@@ -56,9 +56,9 @@ public class ReviewRepository {
|
||||
.anyMatch(review -> review.getBookId().equals(bookUUID));
|
||||
}
|
||||
|
||||
public boolean existsByCustomerAndBookId(UUID customerUUID, UUID bookUUID) {
|
||||
public boolean existsByAvisId(UUID avisUUID) {
|
||||
return this.reviews.stream()
|
||||
.anyMatch(review -> review.getCustomerId().equals(customerUUID) && review.getBookId().equals(bookUUID));
|
||||
.anyMatch(review -> review.getAvisId().equals(avisUUID));
|
||||
}
|
||||
|
||||
public void deleteCustomerReviews(UUID customerUUID) {
|
||||
|
||||
+1
@@ -47,6 +47,7 @@ public class ReviewConverterTest {
|
||||
void shouldConvertReviewToDTO() {
|
||||
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||
Review review = Review.builder()
|
||||
.avisId(UUID.randomUUID())
|
||||
.customerId(UUID.randomUUID())
|
||||
.bookId(UUID.randomUUID())
|
||||
.note(5)
|
||||
|
||||
@@ -38,13 +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 originalCustomerId = review.getCustomerId();
|
||||
UUID originalBookId = review.getCustomerId();
|
||||
|
||||
review.setRandomUUID();
|
||||
review.setRandomUUIDCustomerAndBook();
|
||||
|
||||
assertNotNull(review.getAvisId());
|
||||
assertNotNull(review.getCustomerId());
|
||||
assertNotNull(review.getBookId());
|
||||
assertNotEquals(originalAvisId, review.getAvisId());
|
||||
assertNotEquals(originalCustomerId, review.getCustomerId());
|
||||
assertNotEquals(originalBookId, review.getBookId());
|
||||
}
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ReviewNotFoundExceptionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the UUID provided for customer")
|
||||
void testExceptionMessageContainsUUIDForCustomer() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||
|
||||
String expectedMessage = String.format("The reviews with the customer id %s does not exists", customerUUID);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the UUID provided for book")
|
||||
void testExceptionMessageContainsUUIDForBook() {
|
||||
UUID bookUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID));
|
||||
|
||||
String expectedMessage = String.format("The reviews with 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();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.of(bookUUID));
|
||||
|
||||
String expectedMessage = String.format("The review with customer id %s and book id %s does not exists", customerUUID, bookUUID);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format for customer")
|
||||
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), 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,
|
||||
expectedFormatWithPlaceholder);
|
||||
assertTrue(exception.getMessage().contains(customerUUID.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format for book")
|
||||
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||
UUID bookUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID));
|
||||
|
||||
String expectedFormatWithPlaceholder = "The reviews with the book id {0} does not exists";
|
||||
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE,
|
||||
expectedFormatWithPlaceholder);
|
||||
assertTrue(exception.getMessage().contains(bookUUID.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format for customer and book")
|
||||
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
UUID bookUUID = UUID.randomUUID();
|
||||
|
||||
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.of(bookUUID));
|
||||
|
||||
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,
|
||||
expectedFormatWithPlaceholder);
|
||||
assertTrue(exception.getMessage().contains(customerUUID.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be properly thrown and caught")
|
||||
void testExceptionCanBeThrownAndCaught() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
|
||||
try {
|
||||
throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||
} catch (ReviewNotFoundException e) {
|
||||
String expectedMessage = String.format("The reviews with the customer id %s does not exists", customerUUID);
|
||||
assertEquals(expectedMessage, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
+24
-22
@@ -35,6 +35,7 @@ public class ReviewRepositoryTest {
|
||||
.purchaseDate(purchaseDate)
|
||||
.build();
|
||||
review1.setRandomUUID();
|
||||
review1.setRandomUUIDCustomerAndBook();
|
||||
|
||||
UUID customerId = UUID.randomUUID();
|
||||
UUID bookId = UUID.randomUUID();
|
||||
@@ -45,6 +46,7 @@ public class ReviewRepositoryTest {
|
||||
.comment("nul")
|
||||
.purchaseDate(purchaseDate)
|
||||
.build();
|
||||
review2.setRandomUUID();
|
||||
|
||||
UUID bookId3 = UUID.randomUUID();
|
||||
review3 = Review.builder()
|
||||
@@ -54,6 +56,7 @@ public class ReviewRepositoryTest {
|
||||
.comment("ça passe")
|
||||
.purchaseDate(purchaseDate)
|
||||
.build();
|
||||
review3.setRandomUUID();
|
||||
|
||||
UUID customerId4 = UUID.randomUUID();
|
||||
review4 = Review.builder()
|
||||
@@ -63,6 +66,7 @@ public class ReviewRepositoryTest {
|
||||
.comment("ça passe")
|
||||
.purchaseDate(purchaseDate)
|
||||
.build();
|
||||
review4.setRandomUUID();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -94,9 +98,11 @@ public class ReviewRepositoryTest {
|
||||
repository.save(review1);
|
||||
|
||||
LocalDate purchaseDate = LocalDate.of(2026, 5, 24);
|
||||
UUID customerId = review1.getCustomerId();
|
||||
UUID bookId = review1.getBookId();
|
||||
UUID avisId = review1.getAvisId();
|
||||
UUID customerId = UUID.randomUUID();
|
||||
UUID bookId = UUID.randomUUID();
|
||||
Review updatedReview = Review.builder()
|
||||
.avisId(avisId)
|
||||
.customerId(customerId)
|
||||
.bookId(bookId)
|
||||
.note(4)
|
||||
@@ -107,6 +113,7 @@ public class ReviewRepositoryTest {
|
||||
Review savedReview = repository.save(updatedReview);
|
||||
|
||||
assertEquals(1, repository.findAll().size());
|
||||
assertEquals(avisId, savedReview.getAvisId());
|
||||
assertEquals(customerId, savedReview.getCustomerId());
|
||||
assertEquals(bookId, savedReview.getBookId());
|
||||
assertEquals(4, savedReview.getNote());
|
||||
@@ -189,9 +196,9 @@ public class ReviewRepositoryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("findByCustomerAndBookId should return review with matching customer and book ID")
|
||||
void testFindByCustomerAndBookId() {
|
||||
Optional<Review> foundreview = repository.findByCustomerAndBookId(review1.getCustomerId(), review1.getBookId());
|
||||
@DisplayName("findByAvisId should return review with matching avis ID")
|
||||
void testFindByAvisId() {
|
||||
Optional<Review> foundreview = repository.findByAvisId(review1.getAvisId());
|
||||
|
||||
assertTrue(foundreview.isPresent());
|
||||
assertEquals(review1.getNote(), foundreview.get().getNote());
|
||||
@@ -199,12 +206,11 @@ public class ReviewRepositoryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("findByCustomerAndBookId should return empty Optional when a review with customer and book ID doesn't exist")
|
||||
void testFindByCustomerAndBookIdNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
@DisplayName("findByAvisId should return empty Optional when a review with avis ID doesn't exist")
|
||||
void testFindByAvisIdNotFound() {
|
||||
UUID nonExistentAvisId = UUID.randomUUID();
|
||||
|
||||
Optional<Review> foundreview = repository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
Optional<Review> foundreview = repository.findByAvisId(nonExistentAvisId);
|
||||
|
||||
assertTrue(foundreview.isEmpty());
|
||||
}
|
||||
@@ -246,20 +252,19 @@ public class ReviewRepositoryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("existsByCustomerAndBookId should return true when a review with customer and book ID exists")
|
||||
void testExistsByCustomerAndBookIdExists() {
|
||||
boolean exists = repository.existsByCustomerAndBookId(review1.getCustomerId(), review1.getBookId());
|
||||
@DisplayName("existsByAvisId should return true when a review with avis ID exists")
|
||||
void testExistsByAvisIdExists() {
|
||||
boolean exists = repository.existsByAvisId(review1.getAvisId());
|
||||
|
||||
assertTrue(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("existsByCustomerAndBookId should return false when customer and book ID doesn't exist")
|
||||
void testExistsByCustomerAndBookIdNotExists() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
@DisplayName("existsByAvisId should return false when avis ID doesn't exist")
|
||||
void testExistsByAvisIdNotExists() {
|
||||
UUID nonExistentAvisId = UUID.randomUUID();
|
||||
|
||||
boolean exists = repository.existsByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
boolean exists = repository.existsByAvisId(nonExistentAvisId);
|
||||
|
||||
assertFalse(exists);
|
||||
}
|
||||
@@ -284,10 +289,7 @@ public class ReviewRepositoryTest {
|
||||
|
||||
List<Review> reviews = repository.findAll();
|
||||
|
||||
System.out.println(review2.getCustomerId());
|
||||
System.out.println(review3.getCustomerId());
|
||||
|
||||
/*assertEquals(2, reviews.size());*/
|
||||
assertEquals(2, reviews.size());
|
||||
assertTrue(reviews.contains(review1));
|
||||
assertFalse(reviews.contains(review2));
|
||||
assertFalse(reviews.contains(review3));
|
||||
|
||||
+310
@@ -0,0 +1,310 @@
|
||||
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.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.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.never;
|
||||
|
||||
public class ReviewUseCaseTest {
|
||||
|
||||
@Mock
|
||||
private ReviewRepository reviewRepository;
|
||||
|
||||
@InjectMocks
|
||||
private ReviewUseCase reviewUseCase;
|
||||
|
||||
private UUID customerId;
|
||||
private UUID bookId;
|
||||
private LocalDate purchaseDate;
|
||||
private Review testReview;
|
||||
private ReviewInfo validReviewInfo;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
customerId = UUID.randomUUID();
|
||||
bookId = UUID.randomUUID();
|
||||
purchaseDate = LocalDate.of(2026, 5, 24);
|
||||
testReview = Review.builder()
|
||||
.customerId(customerId)
|
||||
.bookId(bookId)
|
||||
.note(2)
|
||||
.comment("plutôt mauvais")
|
||||
.purchaseDate(purchaseDate)
|
||||
.build();
|
||||
|
||||
validReviewInfo = new ReviewInfo(2, "plutôt mauvais", purchaseDate);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Register review tests")
|
||||
class RegisterReviewTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should register review when valid data is provided")
|
||||
void testRegisterReviewWithValidData() throws NotValidReviewException {
|
||||
when(reviewRepository.save(any(Review.class))).thenReturn(testReview);
|
||||
|
||||
UUID registeredId = reviewUseCase.registerReview(validReviewInfo);
|
||||
|
||||
assertNotNull(registeredId);
|
||||
assertEquals(customerId, 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);
|
||||
|
||||
assertThrows(NotValidReviewException.class,
|
||||
() -> reviewUseCase.registerCustomer(invalidReviewInfo));
|
||||
|
||||
verify(reviewRepository, never()).save(any(Review.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Find review tests")
|
||||
class FindReviewTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return review when customer ID exists")
|
||||
void testFindReviewByCustomerId() {
|
||||
when(reviewRepository.findByCustomerId(customerId)).thenReturn(Optional.of(testReview));
|
||||
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerId(customerId);
|
||||
|
||||
assertTrue(foundReview.isPresent());
|
||||
assertEquals(testReview.getBookId(), foundReview.get().getBookId());
|
||||
assertEquals(testReview.getNote(), foundReview.get().getNote());
|
||||
verify(reviewRepository, times(1)).findByCustomerId(customerId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when customer ID doesn't exist")
|
||||
void testFindReviewByCustomerIdNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(Optional.empty());
|
||||
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerId(nonExistentCustomerId);
|
||||
|
||||
assertTrue(foundReview.isEmpty());
|
||||
verify(reviewRepository, times(1)).findByCustomerId(nonExistentCustomerId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return review when book ID exists")
|
||||
void testFindReviewByBookId() {
|
||||
when(reviewRepository.findByBookId(bookId)).thenReturn(Optional.of(testReview));
|
||||
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByBookId(bookId);
|
||||
|
||||
assertTrue(foundReview.isPresent());
|
||||
assertEquals(testReview.getCustomerId(), foundReview.get().getCustomerId());
|
||||
assertEquals(testReview.getNote(), foundReview.get().getNote());
|
||||
verify(reviewRepository, times(1)).findByBookId(bookId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when book ID doesn't exist")
|
||||
void testFindReviewByBookIdNotFound() {
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(Optional.empty());
|
||||
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByBookId(nonExistentBookId);
|
||||
|
||||
assertTrue(foundReview.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));
|
||||
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerAndBookId(customerId, bookId);
|
||||
|
||||
assertTrue(foundReview.isPresent());
|
||||
assertEquals(testReview.getBookId(), foundReview.get().getBookId());
|
||||
assertEquals(testReview.getNote(), foundReview.get().getNote());
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when customer ID doesn't exist")
|
||||
void testFindReviewByCustomerIdNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty());
|
||||
|
||||
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
|
||||
assertTrue(foundReview.isEmpty());
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Update review tests")
|
||||
class UpdateReviewTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should update review when valid data is provided")
|
||||
void testUpdateReviewWithValidData() throws ReviewNotFoundException, NotValidReviewException {
|
||||
when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview));
|
||||
|
||||
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 30);
|
||||
Review updatedReview = Review.builder()
|
||||
.customerId(customerId)
|
||||
.bookId(bookId)
|
||||
.note(4)
|
||||
.comment("en fait c'est bien")
|
||||
.purchaseDate(updatePurchaseDate)
|
||||
.build();
|
||||
|
||||
when(reviewRepository.save(any(Review.class))).thenReturn(updatedReview);
|
||||
|
||||
ReviewInfo updateInfo = new ReviewInfo(4, "en fait c'est bien", updatePurchaseDate);
|
||||
|
||||
ReviewDTO result = reviewUseCase.updateReview(customerId, bookId, 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)).save(any(Review.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when customer and book ID doesn't exist")
|
||||
void testUpdateReviewNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).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));
|
||||
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
verify(reviewRepository, never()).save(any(Review.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when update data is not valid")
|
||||
void testUpdateReviewWithInvalidData() {
|
||||
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24);
|
||||
ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate);
|
||||
|
||||
assertThrows(NotValidCustomerException.class,
|
||||
() -> reviewUseCase.updateCustomer(customerId, invalidUpdateInfo));
|
||||
|
||||
verify(reviewRepository, never()).findByCustomerAndBookId(any(UUID.class), any(UUID.class));
|
||||
verify(reviewRepository, never()).save(any(Review.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Delete review tests")
|
||||
class DeleteReviewTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should delete reviews when customer ID exists")
|
||||
void testDeleteCustomerReviews() throws ReviewNotFoundException {
|
||||
when(reviewRepository.findByCustomerId(customerId)).thenReturn(Optional.of(testReview));
|
||||
doNothing().when(reviewRepository).delete(testReview);
|
||||
|
||||
reviewUseCase.deleteCustomerReviews(customerId);
|
||||
|
||||
verify(reviewRepository, times(1)).findByCustomerId(customerId);
|
||||
verify(reviewRepository, times(1)).delete(testReview);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when customer ID doesn't exist")
|
||||
void testDeleteCustomerReviewsNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(ReviewNotFoundException.class,
|
||||
() -> reviewUseCase.deleteCustomerReviews(nonExistentCustomerId));
|
||||
|
||||
verify(reviewRepository, times(1)).findByCustomerId(nonExistentCustomerId);
|
||||
verify(reviewRepository, never()).delete(any(Review.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should delete reviews when book ID exists")
|
||||
void testDeleteBookReviews() throws ReviewNotFoundException {
|
||||
when(reviewRepository.findByBookId(bookId)).thenReturn(Optional.of(testReview));
|
||||
doNothing().when(reviewRepository).delete(testReview);
|
||||
|
||||
reviewUseCase.deleteBookReviews(bookId);
|
||||
|
||||
verify(reviewRepository, times(1)).findByBookId(bookId);
|
||||
verify(reviewRepository, times(1)).delete(testReview);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when book ID doesn't exist")
|
||||
void testDeleteBookReviewsNotFound() {
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(ReviewNotFoundException.class,
|
||||
() -> reviewUseCase.deleteBookReviews(nonExistentBookId));
|
||||
|
||||
verify(reviewRepository, times(1)).findByBookId(nonExistentBookId);
|
||||
verify(reviewRepository, never()).delete(any(Review.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should delete review when customer and book ID exists")
|
||||
void testDeleteReview() throws ReviewNotFoundException {
|
||||
when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview));
|
||||
doNothing().when(reviewRepository).delete(testReview);
|
||||
|
||||
reviewUseCase.deleteReview(customerId, bookId);
|
||||
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId);
|
||||
verify(reviewRepository, times(1)).delete(testReview);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when customer and book ID doesn't exist")
|
||||
void testDeleteReviewNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(ReviewNotFoundException.class,
|
||||
() -> reviewUseCase.deleteReviews(nonExistentCustomerId, nonExistentBookId));
|
||||
|
||||
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
verify(reviewRepository, never()).delete(any(Review.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user