From 9ee039a32e343780ce6686a115b48deeb6f5fbfd Mon Sep 17 00:00:00 2001 From: aubert Date: Fri, 12 Jun 2026 00:12:00 +0200 Subject: [PATCH] :white_check_mark: modification pour meilleur logique du cas de recherche de review par livre ou utilisateur --- .../review/repository/ReviewRepository.java | 9 +++--- .../repository/ReviewRepositoryTest.java | 29 ++++++++++++------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepository.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepository.java index c7e0ae8..203c931 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepository.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepository.java @@ -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 findByCustomerId(UUID customerUUID) { + public ArrayList findByCustomerId(UUID customerUUID) { return this.reviews.stream() .filter(review -> review.getCustomerId().equals(customerUUID)) - .findFirst(); + .collect(Collectors.toCollection(ArrayList::new)); } - public Optional findByBookId(UUID bookUUID) { + public ArrayList findByBookId(UUID bookUUID) { return this.reviews.stream() .filter(review -> review.getBookId().equals(bookUUID)) - .findFirst(); + .collect(Collectors.toCollection(ArrayList::new)); } public Optional findByAvisId(UUID avisUUID) { diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepositoryTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepositoryTest.java index 96f9f50..d5da338 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepositoryTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepositoryTest.java @@ -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 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 foundreview = repository.findByCustomerId(review1.getCustomerId()); + ArrayList 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 foundreview = repository.findByCustomerId(nonExistentCustomerId); + ArrayList 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 foundreview = repository.findByBookId(review1.getBookId()); + ArrayList 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 foundreview = repository.findByBookId(nonExistentBookId); + ArrayList foundreview = repository.findByBookId(nonExistentBookId); assertTrue(foundreview.isEmpty()); }