From 62b4a30add8cad47a9ea3b623f229eb72ccd602e Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 18:59:29 +0200 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20cr=C3=A9ation=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 --- .../repository/ReviewRepositoryTest.java | 294 ++++++++++++++++++ 1 file changed, 294 insertions(+) create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepositoryTest.java 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 new file mode 100644 index 0000000..0c07186 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/repository/ReviewRepositoryTest.java @@ -0,0 +1,294 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.repository; + +import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review; +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 java.time.LocalDate; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ReviewRepositoryTest { + + private ReviewRepository repository; + private Review review1; + private Review review2; + + @BeforeEach + void setUp() { + repository = new ReviewRepository(); + + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); + review1 = Review.builder() + .note(1) + .comment("nul") + .purchaseDate(purchaseDate) + .build(); + review1.setRandomUUID(); + + review2 = Review.builder() + .note(1) + .comment("nul") + .purchaseDate(purchaseDate) + .build(); + review2.setRandomUUID(); + } + + @Test + @DisplayName("New repository should be empty") + void testNewRepositoryIsEmpty() { + List reviews = repository.findAll(); + + assertTrue(reviews.isEmpty()); + assertEquals(0, reviews.size()); + } + + @Nested + @DisplayName("Save operations") + class SaveOperations { + + @Test + @DisplayName("Save should add a new review") + void testSaveNewReview() { + Review savedReview = repository.save(review1); + + assertEquals(1, repository.findAll().size()); + assertEquals(review1.getCustomerId(), savedReview.getCustomerId()); + assertEquals(review1.getBookId(), savedReview.getBookId()); + } + + @Test + @DisplayName("Save should update existing review with same customer and book ID") + void testSaveUpdatesExistingReview() { + repository.save(review1); + + LocalDate purchaseDate = LocalDate.of(2026, 5, 24); + UUID customerId = review1.getCustomerId(); + UUID bookId = review1.getBookId(); + Review updatedReview = Review.builder() + .customerId(customerId) + .bookId(bookId) + .note(4) + .comment("pas mal") + .purchaseDate(purchaseDate) + .build(); + + Review savedReview = repository.save(updatedReview); + + assertEquals(1, repository.findAll().size()); + assertEquals(customerId, savedReview.getCustomerId()); + assertEquals(bookId, savedReview.getBookId()); + assertEquals(4, savedReview.getNote()); + assertEquals("pas mal", savedReview.getComment()); + assertEquals(purchaseDate, savedReview.getPurchaseDate()); + } + + @Test + @DisplayName("Save multiple review should add all of them") + void testSaveMultipleReviews() { + repository.save(review1); + repository.save(review2); + + List reviews = repository.findAll(); + + assertEquals(2, reviews.size()); + assertTrue(reviews.contains(review1)); + assertTrue(reviews.contains(review2)); + } + } + + @Nested + @DisplayName("Find operations") + class FindOperations { + + @BeforeEach + void setUpReviews() { + repository.save(review1); + repository.save(review2); + } + + @Test + @DisplayName("FindAll should return all reviews") + void testFindAll() { + List reviews = repository.findAll(); + + assertEquals(2, reviews.size()); + assertTrue(reviews.contains(review1)); + assertTrue(reviews.contains(review2)); + } + + @Test + @DisplayName("findByCustomerId should return review with matching customer ID") + void testFindByCustomerId() { + Optional foundreview = repository.findByCustomerId(review1.getCustomerId()); + + assertTrue(foundreview.isPresent()); + assertEquals(review1.getNote(), foundreview.get().getNote()); + assertEquals(review1.getComment(), foundreview.get().getComment()); + } + + @Test + @DisplayName("findByCustomerId should return empty Optional when a review with customer ID doesn't exist") + void testFindByCustomerIdNotFound() { + UUID nonExistentCustomerId = UUID.randomUUID(); + + Optional foundreview = repository.findByCustomerId(nonExistentCustomerId); + + assertTrue(foundreview.isEmpty()); + } + + @Test + @DisplayName("findByBookId should return review with matching book ID") + void testFindByBookId() { + Optional foundreview = repository.findByBookId(review1.getBookId()); + + assertTrue(foundreview.isPresent()); + assertEquals(review1.getNote(), foundreview.get().getNote()); + assertEquals(review1.getComment(), foundreview.get().getComment()); + } + + @Test + @DisplayName("findByBookId should return empty Optional when a review with book ID doesn't exist") + void testFindByBookIdNotFound() { + UUID nonExistentBookId = UUID.randomUUID(); + + Optional foundreview = repository.findByBookId(nonExistentBookId); + + assertTrue(foundreview.isEmpty()); + } + + @Test + @DisplayName("findByCustomerAndBookId should return review with matching customer and book ID") + void testFindByCustomerAndBookId() { + Optional foundreview = repository.findByCustomerAndBookId(review1.getCustomerId(), review1.getBookId()); + + assertTrue(foundreview.isPresent()); + assertEquals(review1.getNote(), foundreview.get().getNote()); + assertEquals(review1.getComment(), foundreview.get().getComment()); + } + + @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(); + + Optional foundreview = repository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); + + assertTrue(foundreview.isEmpty()); + } + + @Test + @DisplayName("ExistsByCustomerId should return true when a review with customer ID exists") + void testExistsByCustomerIdExists() { + boolean exists = repository.ExistsByCustomerId(review1.getCustomerId()); + + assertTrue(exists); + } + + @Test + @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); + + assertFalse(exists); + } + + @Test + @DisplayName("ExistsByBookId should return true when a review with book ID exists") + void testExistsByBookIdExists() { + boolean exists = repository.ExistsByBookId(review1.getBookId()); + + assertTrue(exists); + } + + @Test + @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); + + assertFalse(exists); + } + + @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()); + + 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(); + + boolean exists = repository.ExistsByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); + + assertFalse(exists); + } + } + + @Nested + @DisplayName("Delete operations") + class DeleteOperations { + + @BeforeEach + void setUpReviews() { + repository.save(review1); + repository.save(review2); + } + + @Test + @DisplayName("Delete should remove the specified review") + void testDelete() { + repository.delete(review1); + + List reviews = repository.findAll(); + + assertEquals(1, reviews.size()); + assertFalse(reviews.contains(review1)); + assertTrue(reviews.contains(review2)); + } + + @Test + @DisplayName("DeleteAll should remove all reviews") + void testDeleteAll() { + repository.deleteAll(); + + List reviews = repository.findAll(); + + assertTrue(reviews.isEmpty()); + assertEquals(0, reviews.size()); + } + + @Test + @DisplayName("Delete should not throw exception when review doesn't exist") + void testDeleteNonExistentReview() { + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); + Review nonExistentReview = Review.builder() + .note(1) + .comment("nul") + .purchaseDate(purchaseDate) + .build(); + nonExistentReview.setRandomUUID(); + + assertDoesNotThrow(() -> repository.delete(nonExistentReview)); + + assertEquals(2, repository.findAll().size()); + } + } +}