From 90929b7bdd6aafc909e7362b7a89528b9dd85845 Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 19:14:08 +0200 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20r=C3=A9ussite=20des=20?= =?UTF-8?q?test=20sur=20la=20BD=20imaginaire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/repository/ReviewRepository.java | 68 +++++++++++++++++++ .../repository/ReviewRepositoryTest.java | 24 +++---- 2 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepository.java 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 new file mode 100644 index 0000000..1a242c4 --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepository.java @@ -0,0 +1,68 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.repository; + +import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public class ReviewRepository { + + private final List reviews = new ArrayList<>(); + + public List findAll() { + + return reviews; + } + + public void deleteAll() { + + reviews.clear(); + } + + public Review save(Review newReview) { + Optional optionalReviewWithSameCustomerAndBookId = this.findByCustomerAndBookId(newReview.getCustomerId(), newReview.getBookId()); + optionalReviewWithSameCustomerAndBookId.ifPresentOrElse(reviews::remove, newReview::setRandomUUID); + this.reviews.add(newReview); + return newReview; + } + + public Optional findByCustomerId(UUID customerUUID) { + return this.reviews.stream() + .filter(review -> review.getCustomerId().equals(customerUUID)) + .findFirst(); + } + + public Optional findByBookId(UUID bookUUID) { + return this.reviews.stream() + .filter(review -> review.getBookId().equals(bookUUID)) + .findFirst(); + } + + public Optional findByCustomerAndBookId(UUID customerUUID, UUID bookUUID) { + return this.reviews.stream() + .filter(review -> review.getCustomerId().equals(customerUUID) && review.getBookId().equals(bookUUID)) + .findFirst(); + } + + public boolean existsByCustomerId(UUID customerUUID) { + return this.reviews.stream() + .anyMatch(review -> review.getCustomerId().equals(customerUUID)); + } + + public boolean existsByBookId(UUID bookUUID) { + return this.reviews.stream() + .anyMatch(review -> review.getBookId().equals(bookUUID)); + } + + public boolean existsByCustomerAndBookId(UUID customerUUID, UUID bookUUID) { + return this.reviews.stream() + .anyMatch(review -> review.getCustomerId().equals(customerUUID) && review.getBookId().equals(bookUUID)); + } + + public void delete(Review review) { + + this.reviews.remove(review); + } +} 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 0c07186..5c57571 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 @@ -187,56 +187,56 @@ public class ReviewRepositoryTest { } @Test - @DisplayName("ExistsByCustomerId should return true when a review with customer ID exists") + @DisplayName("existsByCustomerId should return true when a review with customer ID exists") void testExistsByCustomerIdExists() { - boolean exists = repository.ExistsByCustomerId(review1.getCustomerId()); + boolean exists = repository.existsByCustomerId(review1.getCustomerId()); assertTrue(exists); } @Test - @DisplayName("ExistsByCustomerId should return false when a review with customer ID doesn't exist") + @DisplayName("existsByCustomerId should return false when a review with customer ID doesn't exist") void testExistsByCustomerIdNotExists() { UUID nonExistentCustomerId = UUID.randomUUID(); - boolean exists = repository.ExistsByCustomerId(nonExistentCustomerId); + boolean exists = repository.existsByCustomerId(nonExistentCustomerId); assertFalse(exists); } @Test - @DisplayName("ExistsByBookId should return true when a review with book ID exists") + @DisplayName("existsByBookId should return true when a review with book ID exists") void testExistsByBookIdExists() { - boolean exists = repository.ExistsByBookId(review1.getBookId()); + boolean exists = repository.existsByBookId(review1.getBookId()); assertTrue(exists); } @Test - @DisplayName("ExistsByBookId should return false when a review with book ID doesn't exist") + @DisplayName("existsByBookId should return false when a review with book ID doesn't exist") void testExistsByBookIdNotExists() { UUID nonExistentBookId = UUID.randomUUID(); - boolean exists = repository.ExistsByBookId(nonExistentBookId); + boolean exists = repository.existsByBookId(nonExistentBookId); assertFalse(exists); } @Test - @DisplayName("ExistsByCustomerAndBookId should return true when a review with customer and book ID exists") + @DisplayName("existsByCustomerAndBookId should return true when a review with customer and book ID exists") void testExistsByCustomerAndBookIdExists() { - boolean exists = repository.ExistsByCustomerAndBookId(review1.getCustomerId(), review1.getBookId()); + boolean exists = repository.existsByCustomerAndBookId(review1.getCustomerId(), review1.getBookId()); assertTrue(exists); } @Test - @DisplayName("ExistsByCustomerAndBookId should return false when customer and book ID doesn't exist") + @DisplayName("existsByCustomerAndBookId should return false when customer and book ID doesn't exist") void testExistsByCustomerAndBookIdNotExists() { UUID nonExistentCustomerId = UUID.randomUUID(); UUID nonExistentBookId = UUID.randomUUID(); - boolean exists = repository.ExistsByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); + boolean exists = repository.existsByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); assertFalse(exists); }