modification pour meilleur logique du cas de recherche de review par livre ou utilisateur

This commit is contained in:
2026-06-12 00:12:00 +02:00
parent 051a8e03ed
commit 9ee039a32e
2 changed files with 23 additions and 15 deletions
@@ -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) {
@@ -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());
} }