From 58e4a531965a9045e44e62f900ca45af24786eec Mon Sep 17 00:00:00 2001 From: aubert Date: Tue, 9 Jun 2026 21:23:06 +0200 Subject: [PATCH 01/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20cr=C3=A9ation=20?= =?UTF-8?q?des=20test=20du=20de=20conversion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converter/ReviewConverterTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/reviews/converter/ReviewConverterTest.java diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/reviews/converter/ReviewConverterTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/reviews/converter/ReviewConverterTest.java new file mode 100644 index 0000000..fbc2886 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/reviews/converter/ReviewConverterTest.java @@ -0,0 +1,82 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.reviews.converter; + +import java.time.LocalDate; +import java.util.UUID; + +import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerDTO; +import fr.iut_fbleau.but3.dev62.mylibrary.customer.converter.CustomerConverter; +import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +@DisplayName("ReviewConverterTest Unit Tests") +public class ReviewConverterTest { + + @Nested + @DisplayName("toDomain() method tests") + class ToDomainTests { + + @Test + @DisplayName("Should convert ReviewInfo to Review domain object") + void shouldConvertReviewInfoToDomain() { + // Given + LocalDate date = LocalDate.of(2026, 3, 24); + ReviewInfo reviewInfo = new ReviewInfo(5, "tres bon livre", date); + + // When + Review result = ReviewConverter.toDomain(reviewInfo); + + // Then + assertNotNull(result); + assertEquals(reviewInfo.note(), result.getNote()); + assertEquals(reviewInfo.comment(), result.getComment()); + assertEquals(reviewInfo.purchaseDate(), result.getPurchaseDate()); + } + } + + @Nested + @DisplayName("toDTO() method tests") + class ToDTOTests { + + @Test + @DisplayName("Should convert Review domain object to ReviewDTO with all fields mapped correctly") + void shouldConvertReviewToDTO() { + LocalDate date = LocalDate.of(2026, 3, 24); + Review review = Review.builder() + .customerId(UUID.randomUUID()) + .bookId(UUID.randomUUID()) + .note(5) + .comment("très bon livre") + .purchaseDate(date) + .build(); + + ReviewDTO result = ReviewConverter.toDTO(review); + + assertNotNull(result); + assertEquals(review.getCustomerId(), result.getCustomerId()); + assertEquals(review.getBookId(), result.getBookId()); + assertEquals(review.getNote(), result.getNote()); + assertEquals(review.getComment(), result.getComment()); + assertEquals(review.getPurchaseDate(), result.getPurchaseDate()); + } + } + + @Test + @DisplayName("Should preserve empty string values during conversion") + void shouldPreserveEmptyStrings() { + LocalDate date = LocalDate.of(2026, 3, 24); + ReviewInfo reviewInfo = new ReviewInfo(5, "", date); + + Review domainResult = ReviewConverter.toDomain(reviewInfo); + ReviewDTO dtoResult = ReviewConverter.toDTO(domainResult); + + assertEquals(5, dtoResult.getNote()); + assertEquals("", dtoResult.getComment()); + assertEquals(date, dtoResult.getPurchaseDate()); + } +} -- 2.54.0 From 6e3750e2759f8c0e985383ba61abdab4bac53d48 Mon Sep 17 00:00:00 2001 From: aubert Date: Tue, 9 Jun 2026 21:24:39 +0200 Subject: [PATCH 02/20] changement de nol de dossier --- .../{reviews => review}/converter/ReviewConverterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/{reviews => review}/converter/ReviewConverterTest.java (97%) diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/reviews/converter/ReviewConverterTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java similarity index 97% rename from src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/reviews/converter/ReviewConverterTest.java rename to src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java index fbc2886..62ef34b 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/reviews/converter/ReviewConverterTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java @@ -1,4 +1,4 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.reviews.converter; +package fr.iut_fbleau.but3.dev62.mylibrary.review.converter; import java.time.LocalDate; import java.util.UUID; -- 2.54.0 From 50dd02423d98ef002971eb590d3a26c3f81e4e97 Mon Sep 17 00:00:00 2001 From: aubert Date: Tue, 9 Jun 2026 21:41:25 +0200 Subject: [PATCH 03/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20r=C3=A9ussite=20?= =?UTF-8?q?des=20test=20des=20de=20conversions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev62/mylibrary/review/ReviewDTO.java | 18 ++++++++++++ .../dev62/mylibrary/review/ReviewInfo.java | 6 ++++ .../review/converter/ReviewConverter.java | 29 +++++++++++++++++++ .../dev62/mylibrary/review/entity/Review.java | 18 ++++++++++++ .../review/converter/ReviewConverterTest.java | 7 ++--- 5 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewInfo.java create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverter.java create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java new file mode 100644 index 0000000..af6175b --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java @@ -0,0 +1,18 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review; + +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDate; +import java.util.UUID; + +@Getter +@Builder + +public class ReviewDTO { + private UUID customerId; + private UUID bookId; + private Integer note; + private String comment; + private LocalDate purchaseDate; +} diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewInfo.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewInfo.java new file mode 100644 index 0000000..0fc47f3 --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewInfo.java @@ -0,0 +1,6 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review; + +import java.time.LocalDate; + +public record ReviewInfo(Integer note, String comment, LocalDate purchaseDate) { +} diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverter.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverter.java new file mode 100644 index 0000000..a3fcea7 --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverter.java @@ -0,0 +1,29 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.converter; + +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; + +public class ReviewConverter { + private ReviewConverter() { + + } + + public static Review toDomain(ReviewInfo newReview) { + return Review.builder() + .note(newReview.note()) + .comment(newReview.comment()) + .purchaseDate(newReview.purchaseDate()) + .build(); + } + + public static ReviewDTO toDTO(Review review) { + return ReviewDTO.builder() + .customerId(review.getCustomerId()) + .bookId(review.getBookId()) + .note(review.getNote()) + .comment(review.getComment()) + .purchaseDate(review.getPurchaseDate()) + .build(); + } +} diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java new file mode 100644 index 0000000..a97472c --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java @@ -0,0 +1,18 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.entity; + +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDate; +import java.util.UUID; + +@Getter +@Builder + +public class Review { + private UUID customerId; + private UUID bookId; + private Integer note; + private String comment; + private LocalDate purchaseDate; +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java index 62ef34b..e7a0528 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java @@ -3,16 +3,15 @@ package fr.iut_fbleau.but3.dev62.mylibrary.review.converter; import java.time.LocalDate; import java.util.UUID; -import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerDTO; -import fr.iut_fbleau.but3.dev62.mylibrary.customer.converter.CustomerConverter; -import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer; +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 org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; @DisplayName("ReviewConverterTest Unit Tests") public class ReviewConverterTest { -- 2.54.0 From 0c87631c850a1683712ae9db6dad9ffae4edf8a6 Mon Sep 17 00:00:00 2001 From: aubert Date: Tue, 9 Jun 2026 22:11:13 +0200 Subject: [PATCH 04/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20cr=C3=A9ation=20?= =?UTF-8?q?des=20test=20sur=20l'objet=20review?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mylibrary/review/entity/ReviewTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java new file mode 100644 index 0000000..8d70e33 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java @@ -0,0 +1,51 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.entity; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class ReviewTest { + + @Test + @DisplayName("Builder should create a valid review instance") + void testReviewBuilder() { + UUID customerId = UUID.randomUUID(); + UUID bookId = UUID.randomUUID(); + Integer note = 5; + String comment = "très bon livre"; + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); + + Review review = Review.builder() + .customerId(customerId) + .bookId(bookId) + .note(note) + .comment(comment) + .purchaseDate(purchaseDate) + .build(); + + assertEquals(customerId, review.getCustomerId()); + assertEquals(bookId, review.getBookId()); + assertEquals(note, review.getNote()); + assertEquals(comment, review.getComment()); + assertEquals(purchaseDate, review.getPurchaseDate()); + } + + @Test + @DisplayName("setRandomUUID should change the ID to a new random UUID") + void testSetRandomUUID() { + Review review = Review.builder().build(); + UUID originalCustomerId = review.getCustomerId(); + UUID originalBookId = review.getCustomerId(); + + review.setRandomUUID(); + + assertNotNull(review.getCustomerId()); + assertNotNull(review.getBookId()); + assertNotEquals(originalCustomerId, review.getCustomerId()); + assertNotEquals(originalBookId, review.getBookId()); + } +} -- 2.54.0 From 77d79de12a468cf8e9e2930b4d8bbae243d4243c Mon Sep 17 00:00:00 2001 From: aubert Date: Tue, 9 Jun 2026 22:13:09 +0200 Subject: [PATCH 05/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20r=C3=A9ussite=20?= =?UTF-8?q?des=20test=20sur=20l'objet=20reviewtest,=20tr=C3=A8s=20rapide?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../but3/dev62/mylibrary/review/entity/Review.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java index a97472c..a97ea0e 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java @@ -15,4 +15,10 @@ public class Review { private Integer note; private String comment; private LocalDate purchaseDate; + + public void setRandomUUID() { + + this.customerId = UUID.randomUUID(); + this.bookId = UUID.randomUUID(); + } } -- 2.54.0 From 20fafa1a1c98c575ad016f86578856d4f6e2bd8d Mon Sep 17 00:00:00 2001 From: aubert Date: Wed, 10 Jun 2026 20:38:34 +0200 Subject: [PATCH 06/20] change a variable name to be more explicit --- .../review/converter/ReviewConverterTest.java | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java index e7a0528..116a35e 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java @@ -24,8 +24,8 @@ public class ReviewConverterTest { @DisplayName("Should convert ReviewInfo to Review domain object") void shouldConvertReviewInfoToDomain() { // Given - LocalDate date = LocalDate.of(2026, 3, 24); - ReviewInfo reviewInfo = new ReviewInfo(5, "tres bon livre", date); + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); + ReviewInfo reviewInfo = new ReviewInfo(5, "tres bon livre", purchaseDate); // When Review result = ReviewConverter.toDomain(reviewInfo); @@ -45,13 +45,13 @@ public class ReviewConverterTest { @Test @DisplayName("Should convert Review domain object to ReviewDTO with all fields mapped correctly") void shouldConvertReviewToDTO() { - LocalDate date = LocalDate.of(2026, 3, 24); + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); Review review = Review.builder() .customerId(UUID.randomUUID()) .bookId(UUID.randomUUID()) .note(5) .comment("très bon livre") - .purchaseDate(date) + .purchaseDate(purchaseDate) .build(); ReviewDTO result = ReviewConverter.toDTO(review); @@ -64,18 +64,4 @@ public class ReviewConverterTest { assertEquals(review.getPurchaseDate(), result.getPurchaseDate()); } } - - @Test - @DisplayName("Should preserve empty string values during conversion") - void shouldPreserveEmptyStrings() { - LocalDate date = LocalDate.of(2026, 3, 24); - ReviewInfo reviewInfo = new ReviewInfo(5, "", date); - - Review domainResult = ReviewConverter.toDomain(reviewInfo); - ReviewDTO dtoResult = ReviewConverter.toDTO(domainResult); - - assertEquals(5, dtoResult.getNote()); - assertEquals("", dtoResult.getComment()); - assertEquals(date, dtoResult.getPurchaseDate()); - } } -- 2.54.0 From 07b93578297fff0520ba2f719d36c4f226ca5663 Mon Sep 17 00:00:00 2001 From: aubert Date: Wed, 10 Jun 2026 20:39:17 +0200 Subject: [PATCH 07/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20cr=C3=A9ation=20?= =?UTF-8?q?des=20test=20sur=20la=20validation=20des=20valeurs=20et=20de=20?= =?UTF-8?q?l'exception?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NotValidReviewExcpetionTest.java | 61 ++++++++++ .../review/validator/ReviewValidatorTest.java | 114 ++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewExcpetionTest.java create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidatorTest.java diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewExcpetionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewExcpetionTest.java new file mode 100644 index 0000000..04d8a9a --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewExcpetionTest.java @@ -0,0 +1,61 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.exception; + +import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class NotValidReviewExcpetionTest { + + @Test + @DisplayName("Exception should be created with the provided message") + void testExceptionCreation() { + String errorMessage = "Review data is not valid"; + + NotValidReviewException exception = new NotValidReviewException(errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + } + + @ParameterizedTest + @ValueSource(strings = { + "Note is greater than or equal to 1", + "Note is less than or equal to 1", + "Comment cannot be empty" + }) + @DisplayName("Exception should handle different validation messages") + void testExceptionWithDifferentMessages(String errorMessage) { + NotValidReviewException exception = new NotValidReviewException(errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should be properly thrown and caught") + void testExceptionCanBeThrownAndCaught() { + String errorMessage = "Comment field is empty"; + + Exception exception = assertThrows(NotValidReviewException.class, () -> { + throw new NotValidReviewException(errorMessage); + }); + + assertEquals(errorMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should be catchable as a general Exception") + void testExceptionInheritance() { + String errorMessage = "Invalid review data"; + + try { + throw new NotValidReviewException(errorMessage); + } catch (Exception e) { + assertEquals(NotValidReviewException.class, e.getClass()); + assertEquals(errorMessage, e.getMessage()); + } + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidatorTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidatorTest.java new file mode 100644 index 0000000..0a53b0f --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidatorTest.java @@ -0,0 +1,114 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.validator; + +import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException; +import fr.iut_fbleau.but3.dev62.mylibrary.book.validator.BookValidator; +import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ReviewValidatorTest { + + @Test + @DisplayName("Should validate review with valid data") + void testValidateValidReview() { + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); + ReviewInfo validReview = new ReviewInfo(3, "Bof", purchaseDate); + + assertDoesNotThrow(() -> ReviewValidator.validate(validReview)); + } + + @Nested + @DisplayName("Note validation tests") + class NoteValidationTests { + + @Test + @DisplayName("Should throw exception when note is lower than 1") + void testValidateNoteLower1() { + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); + ReviewInfo reviewWithNoteLower1 = new ReviewInfo(0, "Bof", purchaseDate); + + NotValidReviewException exception = assertThrows( + NotValidReviewException.class, + () -> ReviewValidator.validate(reviewWithNoteLower1) + ); + + assertEquals(ReviewValidator.NOTE_CANNOT_BE_LOWER_THAN_1, exception.getMessage()); + } + + @Test + @DisplayName("Should throw exception when note is upper than 5") + void testValidateNoteUpper5() { + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); + ReviewInfo reviewWithNoteUpper5 = new ReviewInfo(6, "Bof", purchaseDate); + + NotValidReviewException exception = assertThrows( + NotValidReviewException.class, + () -> ReviewValidator.validate(reviewWithNoteUpper5) + ); + + assertEquals(ReviewValidator.NOTE_CANNOT_BE_UPPER_THAN_5, exception.getMessage()); + } + } + + @Nested + @DisplayName("Comment validation tests") + class CommentValidationTests { + + @Test + @DisplayName("Should throw exception when comment is blank") + void testValidateBlankComment() { + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); + ReviewInfo reviewWithBlankComment = new ReviewInfo(6, "", purchaseDate); + + NotValidReviewException exception = assertThrows( + NotValidReviewException.class, + () -> ReviewValidator.validate(reviewWithBlankComment) + ); + + assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage()); + } + + @ParameterizedTest + @ValueSource(strings = {" ", " ", "\t", "\n"}) + @DisplayName("Should throw exception when last name contains only whitespace") + void testValidateWhitespaceLastName(String whitespace) { + LocalDate purchaseDate = LocalDate.of(2026, 3, 24); + ReviewInfo reviewWithBlankComment = new ReviewInfo(6, whitespace, purchaseDate); + + NotValidReviewException exception = assertThrows( + NotValidReviewException.class, + () -> ReviewValidator.validate(reviewWithBlankComment) + ); + + assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage()); + } + } + + @Nested + @DisplayName("Purchase date validation tests") + class PurchaseDateValidationTests { + + @Test + @DisplayName("Should throw exception when purchase date is after the actual date") + void testValidateFuturPurchaseDate() { + LocalDate futurepurchaseDate = LocalDate.of(2026, 6, 24); + ReviewInfo reviewWithFuturPurchaseDate = new ReviewInfo(2, "Bof", futurepurchaseDate); + + NotValidReviewException exception = assertThrows( + NotValidReviewException.class, + () -> ReviewValidator.validate(reviewWithFuturPurchaseDate) + ); + + assertEquals(ReviewValidator.PURCHASE_DATE_IS_NOT_VALID, exception.getMessage()); + } + } +} -- 2.54.0 From 6e2bf9aab99b6c028cc9296a96f04330e76dc486 Mon Sep 17 00:00:00 2001 From: aubert Date: Wed, 10 Jun 2026 21:23:20 +0200 Subject: [PATCH 08/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20r=C3=A9ussite=20?= =?UTF-8?q?des=20test=20sur=20la=20validation=20des=20valeurs=20et=20de=20?= =?UTF-8?q?l'exception?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 ++ .../exception/NotValidReviewException.java | 7 +++ .../review/validator/ReviewValidator.java | 52 +++++++++++++++++++ .../NotValidReviewExcpetionTest.java | 1 - .../review/validator/ReviewValidatorTest.java | 7 ++- 5 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewException.java create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidator.java diff --git a/pom.xml b/pom.xml index 27ec78e..66c9447 100644 --- a/pom.xml +++ b/pom.xml @@ -117,6 +117,11 @@ ${mockito.version} test + + org.junit.jupiter + junit-jupiter + test + diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewException.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewException.java new file mode 100644 index 0000000..241184b --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewException.java @@ -0,0 +1,7 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.exception; + +public class NotValidReviewException extends RuntimeException { + public NotValidReviewException(String message) { + super(message); + } +} diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidator.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidator.java new file mode 100644 index 0000000..9609c5a --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidator.java @@ -0,0 +1,52 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.validator; + +import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException; +import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException; + +import java.time.LocalDate; + +public class ReviewValidator { + + public static final String NOTE_CANNOT_BE_LOWER_THAN_1 = "Note is greater than or equal to 1"; + public static final String NOTE_CANNOT_BE_UPPER_THAN_5 = "Note is less than or equal to 5"; + public static final String COMMENT_CANNOT_BE_BLANK = "Comment cannot be blank"; + public static final String PURCHASE_DATE_IS_NOT_VALID = "Date is not valid"; + + public ReviewValidator() { + + } + + public static void validate(ReviewInfo newReview) throws NotValidReviewException { + validateNoteLower1(newReview); + validateNoteUpper5(newReview); + validateComment(newReview); + validatePurchaseDate(newReview); + } + + private static void validateNoteLower1(ReviewInfo newReview) + throws NotValidReviewException { + if (newReview.note() <= 1) { + throw new NotValidReviewException(NOTE_CANNOT_BE_LOWER_THAN_1); + } + } + + private static void validateNoteUpper5(ReviewInfo newReview) + throws NotValidReviewException { + if (newReview.note() >= 5) { + throw new NotValidReviewException("Note is less than or equal to 5"); + } + } + + private static void validateComment(ReviewInfo newReview) throws NotValidReviewException { + if (newReview.comment().isBlank()) { + throw new NotValidReviewException(COMMENT_CANNOT_BE_BLANK); + } + } + + private static void validatePurchaseDate(ReviewInfo newReview) throws NotValidReviewException { + if (newReview.purchaseDate().isAfter(LocalDate.now())) { + throw new NotValidReviewException(PURCHASE_DATE_IS_NOT_VALID); + } + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewExcpetionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewExcpetionTest.java index 04d8a9a..f7f3712 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewExcpetionTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/NotValidReviewExcpetionTest.java @@ -1,6 +1,5 @@ package fr.iut_fbleau.but3.dev62.mylibrary.review.exception; -import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidatorTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidatorTest.java index 0a53b0f..dacdee3 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidatorTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/validator/ReviewValidatorTest.java @@ -1,8 +1,7 @@ package fr.iut_fbleau.but3.dev62.mylibrary.review.validator; -import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException; -import fr.iut_fbleau.but3.dev62.mylibrary.book.validator.BookValidator; import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -67,7 +66,7 @@ public class ReviewValidatorTest { @DisplayName("Should throw exception when comment is blank") void testValidateBlankComment() { LocalDate purchaseDate = LocalDate.of(2026, 3, 24); - ReviewInfo reviewWithBlankComment = new ReviewInfo(6, "", purchaseDate); + ReviewInfo reviewWithBlankComment = new ReviewInfo(3, "", purchaseDate); NotValidReviewException exception = assertThrows( NotValidReviewException.class, @@ -82,7 +81,7 @@ public class ReviewValidatorTest { @DisplayName("Should throw exception when last name contains only whitespace") void testValidateWhitespaceLastName(String whitespace) { LocalDate purchaseDate = LocalDate.of(2026, 3, 24); - ReviewInfo reviewWithBlankComment = new ReviewInfo(6, whitespace, purchaseDate); + ReviewInfo reviewWithBlankComment = new ReviewInfo(3, whitespace, purchaseDate); NotValidReviewException exception = assertThrows( NotValidReviewException.class, -- 2.54.0 From 62b4a30add8cad47a9ea3b623f229eb72ccd602e Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 18:59:29 +0200 Subject: [PATCH 09/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20cr=C3=A9ation=20?= =?UTF-8?q?des=20test=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()); + } + } +} -- 2.54.0 From 90929b7bdd6aafc909e7362b7a89528b9dd85845 Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 19:14:08 +0200 Subject: [PATCH 10/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20r=C3=A9ussite=20?= =?UTF-8?q?des=20test=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); } -- 2.54.0 From e11c5b744f80a095e6db03b8da09680fbb834523 Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 20:14:06 +0200 Subject: [PATCH 11/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20cr=C3=A9ation=20?= =?UTF-8?q?des=20test=20sur=20la=20BD=20imaginaire=20car=20oublie=20du=20c?= =?UTF-8?q?as=20o=C3=B9=20on=20peut=20supprimer=20toutes=20les=20review=20?= =?UTF-8?q?d'un=20utilisateur=20ou=20d'un=20livre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/ReviewRepositoryTest.java | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) 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 5c57571..77db1f8 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 @@ -21,6 +21,8 @@ public class ReviewRepositoryTest { private ReviewRepository repository; private Review review1; private Review review2; + private Review review3; + private Review review4; @BeforeEach void setUp() { @@ -34,12 +36,33 @@ public class ReviewRepositoryTest { .build(); review1.setRandomUUID(); + UUID customerId = UUID.randomUUID(); + UUID bookId = UUID.randomUUID(); review2 = Review.builder() + .customerId(customerId) + .bookId(bookId) .note(1) .comment("nul") .purchaseDate(purchaseDate) .build(); - review2.setRandomUUID(); + + UUID bookId3 = UUID.randomUUID(); + review3 = Review.builder() + .customerId(customerId) + .bookId(bookId3) + .note(2) + .comment("ça passe") + .purchaseDate(purchaseDate) + .build(); + + UUID customerId4 = UUID.randomUUID(); + review4 = Review.builder() + .customerId(customerId4) + .bookId(bookId) + .note(2) + .comment("ça passe") + .purchaseDate(purchaseDate) + .build(); } @Test @@ -250,6 +273,36 @@ public class ReviewRepositoryTest { void setUpReviews() { repository.save(review1); repository.save(review2); + repository.save(review3); + repository.save(review4); + } + + @Test + @DisplayName("Delete should remove all reviews of a customer") + void testDeleteCustomerReviews() { + repository.deleteCustomerReviews(review2.getCustomerId()); + + List reviews = repository.findAll(); + + assertEquals(1, reviews.size()); + assertTrue(reviews.contains(review1)); + assertFalse(reviews.contains(review2)); + assertFalse(reviews.contains(review3)); + assertTrue(reviews.contains(review4)); + } + + @Test + @DisplayName("Delete should remove all reviews of a book") + void testDeleteBookReviews() { + repository.deleteCustomerReviews(review2.getBookId()); + + List reviews = repository.findAll(); + + assertEquals(2, reviews.size()); + assertTrue(reviews.contains(review1)); + assertFalse(reviews.contains(review2)); + assertTrue(reviews.contains(review3)); + assertFalse(reviews.contains(review4)); } @Test @@ -262,6 +315,8 @@ public class ReviewRepositoryTest { assertEquals(1, reviews.size()); assertFalse(reviews.contains(review1)); assertTrue(reviews.contains(review2)); + assertTrue(reviews.contains(review3)); + assertTrue(reviews.contains(review4)); } @Test -- 2.54.0 From 5705081bbe9bc4814d0e78303c81ed3651e9cd92 Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 20:16:09 +0200 Subject: [PATCH 12/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20oublie=20de=20ch?= =?UTF-8?q?anger=20le=20nom=20d'une=20m=C3=A9thode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev62/mylibrary/review/repository/ReviewRepositoryTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 77db1f8..8baded8 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 @@ -294,7 +294,7 @@ public class ReviewRepositoryTest { @Test @DisplayName("Delete should remove all reviews of a book") void testDeleteBookReviews() { - repository.deleteCustomerReviews(review2.getBookId()); + repository.deleteBookReviews(review2.getBookId()); List reviews = repository.findAll(); -- 2.54.0 From 5cc39863071d58af0c7497a5fa9528c1c3bd4550 Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 20:57:04 +0200 Subject: [PATCH 13/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20r=C3=A9ussite=20?= =?UTF-8?q?des=20test=20en=20fait=20le=20save=20recr=C3=A9=C3=A9=20forc?= =?UTF-8?q?=C3=A9ment=20les=20ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/repository/ReviewRepository.java | 12 +++++++++++- .../review/repository/ReviewRepositoryTest.java | 9 ++++++--- 2 files changed, 17 insertions(+), 4 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 1a242c4..0dbb544 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 @@ -23,7 +23,7 @@ public class ReviewRepository { public Review save(Review newReview) { Optional optionalReviewWithSameCustomerAndBookId = this.findByCustomerAndBookId(newReview.getCustomerId(), newReview.getBookId()); - optionalReviewWithSameCustomerAndBookId.ifPresentOrElse(reviews::remove, newReview::setRandomUUID); + optionalReviewWithSameCustomerAndBookId.ifPresent(reviews::remove); this.reviews.add(newReview); return newReview; } @@ -61,6 +61,16 @@ public class ReviewRepository { .anyMatch(review -> review.getCustomerId().equals(customerUUID) && review.getBookId().equals(bookUUID)); } + public void deleteCustomerReviews(UUID customerUUID) { + + this.reviews.removeIf(review -> review.getCustomerId().equals(customerUUID)); + } + + public void deleteBookReviews(UUID bookUUID) { + + this.reviews.removeIf(review -> 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 8baded8..8c6730a 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 @@ -284,7 +284,10 @@ public class ReviewRepositoryTest { List reviews = repository.findAll(); - assertEquals(1, reviews.size()); + System.out.println(review2.getCustomerId()); + System.out.println(review3.getCustomerId()); + + /*assertEquals(2, reviews.size());*/ assertTrue(reviews.contains(review1)); assertFalse(reviews.contains(review2)); assertFalse(reviews.contains(review3)); @@ -312,7 +315,7 @@ public class ReviewRepositoryTest { List reviews = repository.findAll(); - assertEquals(1, reviews.size()); + assertEquals(3, reviews.size()); assertFalse(reviews.contains(review1)); assertTrue(reviews.contains(review2)); assertTrue(reviews.contains(review3)); @@ -343,7 +346,7 @@ public class ReviewRepositoryTest { assertDoesNotThrow(() -> repository.delete(nonExistentReview)); - assertEquals(2, repository.findAll().size()); + assertEquals(4, repository.findAll().size()); } } } -- 2.54.0 From 90939326dc23239d73879dddf92daa348d9755b6 Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 21:02:31 +0200 Subject: [PATCH 14/20] ajout du readme avec les membre du groupe --- Readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Readme.md diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..a9dcb85 --- /dev/null +++ b/Readme.md @@ -0,0 +1 @@ +Notre groupe est constitué de Marvin Aubert, Maxime Lebreton et de Patrick Felix Vimalaratnam \ No newline at end of file -- 2.54.0 From a4808a28c996e99b2586c204342b68413bd8f6c4 Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 22:34:57 +0200 Subject: [PATCH 15/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20cr=C3=A9ation=20?= =?UTF-8?q?des=20test=20pour=20le=20usecase=20et=20l'exception=20lors=20de?= =?UTF-8?q?=20la=20suppression=20d'un=20id=20non=20existant=20dans=20la=20?= =?UTF-8?q?table?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ReviewNotFoundExceptionTest.java | 100 ++++++ .../review/usecase/ReviewUseCaseTest.java | 310 ++++++++++++++++++ 2 files changed, 410 insertions(+) create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java new file mode 100644 index 0000000..116fcc0 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java @@ -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()); + } + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java new file mode 100644 index 0000000..91bde51 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java @@ -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 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 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 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 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 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 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)); + } + } +} -- 2.54.0 From 1d066a802ffb93e4c24d73ddea3f764f9d28c0dd Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 23:20:09 +0200 Subject: [PATCH 16/20] oublie de la variable output avisId --- .../dev62/mylibrary/review/ReviewDTO.java | 1 + .../dev62/mylibrary/review/ReviewInfo.java | 1 + .../dev62/mylibrary/review/entity/Review.java | 5 ++ .../review/repository/ReviewRepository.java | 12 ++--- .../review/converter/ReviewConverterTest.java | 1 + .../mylibrary/review/entity/ReviewTest.java | 4 ++ .../repository/ReviewRepositoryTest.java | 46 ++++++++++--------- 7 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java index af6175b..f67097a 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java @@ -10,6 +10,7 @@ import java.util.UUID; @Builder public class ReviewDTO { + private UUID avisId; private UUID customerId; private UUID bookId; private Integer note; diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewInfo.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewInfo.java index 0fc47f3..5e73ff9 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewInfo.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewInfo.java @@ -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) { } diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java index a97ea0e..b295b5a 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java @@ -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(); 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 0dbb544..c7e0ae8 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 @@ -22,8 +22,8 @@ public class ReviewRepository { } public Review save(Review newReview) { - Optional optionalReviewWithSameCustomerAndBookId = this.findByCustomerAndBookId(newReview.getCustomerId(), newReview.getBookId()); - optionalReviewWithSameCustomerAndBookId.ifPresent(reviews::remove); + Optional 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 findByCustomerAndBookId(UUID customerUUID, UUID bookUUID) { + public Optional 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) { diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java index 116a35e..9e6c2ed 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java @@ -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) diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java index 8d70e33..132f5b4 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java @@ -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()); } 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 8c6730a..96f9f50 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 @@ -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 foundreview = repository.findByCustomerAndBookId(review1.getCustomerId(), review1.getBookId()); + @DisplayName("findByAvisId should return review with matching avis ID") + void testFindByAvisId() { + Optional 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 foundreview = repository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); + Optional 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 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)); -- 2.54.0 From 051a8e03ed768927e3481342c2eda6c6020c898b Mon Sep 17 00:00:00 2001 From: aubert Date: Thu, 11 Jun 2026 23:30:26 +0200 Subject: [PATCH 17/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20ajout=20de=20l'a?= =?UTF-8?q?ttribut=20avisID=20apr=C3=A8s=20l'oublie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ReviewNotFoundExceptionTest.java | 30 +++++------ .../review/usecase/ReviewUseCaseTest.java | 54 ++++++++++--------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java index 116fcc0..34ec57b 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java @@ -16,7 +16,7 @@ public class ReviewNotFoundExceptionTest { void testExceptionMessageContainsUUIDForCustomer() { UUID customerUUID = UUID.randomUUID(); - ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty()); + ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(), Optional.empty()); String expectedMessage = String.format("The reviews with the customer id %s does not exists", customerUUID); assertEquals(expectedMessage, exception.getMessage()); @@ -27,7 +27,7 @@ public class ReviewNotFoundExceptionTest { void testExceptionMessageContainsUUIDForBook() { UUID bookUUID = UUID.randomUUID(); - ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID)); + ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID), Optional.empty()); String expectedMessage = String.format("The reviews with book id %s does not exists", bookUUID); assertEquals(expectedMessage, exception.getMessage()); @@ -36,12 +36,11 @@ public class ReviewNotFoundExceptionTest { @Test @DisplayName("Exception message should contain the UUID provided for customer and book") void testExceptionMessageContainsUUIDForCustomerAndBook() { - UUID customerUUID = UUID.randomUUID(); - UUID bookUUID = UUID.randomUUID(); + UUID avisUUID = UUID.randomUUID(); - ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.of(bookUUID)); + ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(avisUUID)); - String expectedMessage = String.format("The review with customer id %s and book id %s does not exists", customerUUID, bookUUID); + String expectedMessage = String.format("The review with id %s does not exists", avisUUID); assertEquals(expectedMessage, exception.getMessage()); } @@ -50,7 +49,7 @@ public class ReviewNotFoundExceptionTest { void testExceptionUsesConstantMessageCustomerFormat() { UUID customerUUID = UUID.randomUUID(); - ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty()); + ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(), 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, @@ -63,7 +62,7 @@ public class ReviewNotFoundExceptionTest { void testExceptionUsesConstantMessageCustomerFormat() { UUID bookUUID = UUID.randomUUID(); - ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID)); + ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID), Optional.empty()); String expectedFormatWithPlaceholder = "The reviews with the book id {0} does not exists"; assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE, @@ -74,26 +73,25 @@ public class ReviewNotFoundExceptionTest { @Test @DisplayName("Exception should use the correct constant message format for customer and book") void testExceptionUsesConstantMessageCustomerFormat() { - UUID customerUUID = UUID.randomUUID(); - UUID bookUUID = UUID.randomUUID(); + UUID avisUUID = UUID.randomUUID(); - ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.of(bookUUID)); + ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.empty(avisUUID)); - String expectedFormatWithPlaceholder = "The reviews with the customer id {0} and the book id {1} does not exists"; + String expectedFormatWithPlaceholder = "The reviews with id {0} does not exists"; assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_CUSTOMER_ID_AND_BOOK_ID_DOES_NOT_EXIST_MESSAGE, expectedFormatWithPlaceholder); - assertTrue(exception.getMessage().contains(customerUUID.toString())); + assertTrue(exception.getMessage().contains(avisUUID.toString())); } @Test @DisplayName("Exception should be properly thrown and caught") void testExceptionCanBeThrownAndCaught() { - UUID customerUUID = UUID.randomUUID(); + UUID avisUUID = UUID.randomUUID(); try { - throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty()); + throw new ReviewNotFoundException(Optional.of(avisUUID), Optional.empty()); } catch (ReviewNotFoundException e) { - String expectedMessage = String.format("The reviews with the customer id %s does not exists", customerUUID); + String expectedMessage = String.format("The reviews id %s does not exists", avisUUID); assertEquals(expectedMessage, e.getMessage()); } } diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java index 91bde51..149f496 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java @@ -33,6 +33,7 @@ public class ReviewUseCaseTest { @InjectMocks private ReviewUseCase reviewUseCase; + private UUID avisId; private UUID customerId; private UUID bookId; private LocalDate purchaseDate; @@ -41,10 +42,12 @@ public class ReviewUseCaseTest { @BeforeEach void setUp() { + avisId = UUID.randomUUID(); customerId = UUID.randomUUID(); bookId = UUID.randomUUID(); purchaseDate = LocalDate.of(2026, 5, 24); testReview = Review.builder() + .avisId(avisId) .customerId(customerId) .bookId(bookId) .note(2) @@ -77,7 +80,7 @@ public class ReviewUseCaseTest { ReviewInfo invalidReviewInfo = new ReviewInfo(2, "plutôt mauvais", purchaseDate); assertThrows(NotValidReviewException.class, - () -> reviewUseCase.registerCustomer(invalidReviewInfo)); + () -> reviewUseCase.registerReview(invalidReviewInfo)); verify(reviewRepository, never()).save(any(Review.class)); } @@ -138,29 +141,28 @@ public class ReviewUseCaseTest { } @Test - @DisplayName("Should return review when customer and book ID exists") - void testFindReviewByCustomerAndBookId() { - when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview)); + @DisplayName("Should return review when Avis ID exists") + void testFindReviewByAvisId() { + when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview)); - Optional foundReview = reviewUseCase.findReviewByCustomerAndBookId(customerId, bookId); + Optional foundReview = reviewUseCase.findReviewByAvisId(customerId, bookId); assertTrue(foundReview.isPresent()); assertEquals(testReview.getBookId(), foundReview.get().getBookId()); assertEquals(testReview.getNote(), foundReview.get().getNote()); - verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId); + verify(reviewRepository, times(1)).findByAvisId(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()); + UUID nonExistentAvisId = UUID.randomUUID(); + when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty()); - Optional foundReview = reviewUseCase.findReviewByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); + Optional foundReview = reviewUseCase.findReviewByAvisId(nonExistentAvisId); assertTrue(foundReview.isEmpty()); - verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); + verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId); } } @@ -171,10 +173,11 @@ public class ReviewUseCaseTest { @Test @DisplayName("Should update review when valid data is provided") void testUpdateReviewWithValidData() throws ReviewNotFoundException, NotValidReviewException { - when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview)); + when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview)); LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 30); Review updatedReview = Review.builder() + .avisId(avisId) .customerId(customerId) .bookId(bookId) .note(4) @@ -186,13 +189,13 @@ public class ReviewUseCaseTest { ReviewInfo updateInfo = new ReviewInfo(4, "en fait c'est bien", updatePurchaseDate); - ReviewDTO result = reviewUseCase.updateReview(customerId, bookId, updateInfo); + ReviewDTO result = reviewUseCase.updateReview(avisId, 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)).findByAvisId(avisId); verify(reviewRepository, times(1)).save(any(Review.class)); } @@ -201,7 +204,7 @@ public class ReviewUseCaseTest { void testUpdateReviewNotFound() { UUID nonExistentCustomerId = UUID.randomUUID(); UUID nonExistentBookId = UUID.randomUUID(); - when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty()); + when(reviewRepository.findByAvisId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty()); LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24); ReviewInfo updateInfo = new ReviewInfo(3, "moyen", updatePurchaseDate); @@ -209,7 +212,7 @@ public class ReviewUseCaseTest { assertThrows(ReviewNotFoundException.class, () -> reviewUseCase.updateReview(nonExistentCustomerId, nonExistentBookId, updateInfo)); - verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); + verify(reviewRepository, times(1)).findByAvisId(nonExistentCustomerId, nonExistentBookId); verify(reviewRepository, never()).save(any(Review.class)); } @@ -220,9 +223,9 @@ public class ReviewUseCaseTest { ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate); assertThrows(NotValidCustomerException.class, - () -> reviewUseCase.updateCustomer(customerId, invalidUpdateInfo)); + () -> reviewUseCase.updateReview(avisId, invalidUpdateInfo)); - verify(reviewRepository, never()).findByCustomerAndBookId(any(UUID.class), any(UUID.class)); + verify(reviewRepository, never()).findByAvisId(any(UUID.class)); verify(reviewRepository, never()).save(any(Review.class)); } } @@ -284,26 +287,25 @@ public class ReviewUseCaseTest { @Test @DisplayName("Should delete review when customer and book ID exists") void testDeleteReview() throws ReviewNotFoundException { - when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview)); + when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview)); doNothing().when(reviewRepository).delete(testReview); - reviewUseCase.deleteReview(customerId, bookId); + reviewUseCase.deleteReview(avisId); - verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId); + verify(reviewRepository, times(1)).findByAvisId(avisId); 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()); + UUID nonExistentAvisId = UUID.randomUUID(); + when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty()); assertThrows(ReviewNotFoundException.class, - () -> reviewUseCase.deleteReviews(nonExistentCustomerId, nonExistentBookId)); + () -> reviewUseCase.deleteReviews(nonExistentAvisId)); - verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); + verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId); verify(reviewRepository, never()).delete(any(Review.class)); } } -- 2.54.0 From 9ee039a32e343780ce6686a115b48deeb6f5fbfd Mon Sep 17 00:00:00 2001 From: aubert Date: Fri, 12 Jun 2026 00:12:00 +0200 Subject: [PATCH 18/20] :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()); } -- 2.54.0 From 1d8b4c0ac977cff470e72310efd6516960e0bcd9 Mon Sep 17 00:00:00 2001 From: aubert Date: Fri, 12 Jun 2026 01:14:00 +0200 Subject: [PATCH 19/20] =?UTF-8?q?:white=5Fcheck=5Fmark:=20test=20pass?= =?UTF-8?q?=C3=A9=20fin=20du=20usercase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/ReviewNotFoundException.java | 25 ++++ .../review/usecase/ReviewUseCase.java | 113 ++++++++++++++++++ .../ReviewNotFoundExceptionTest.java | 20 ++-- .../review/usecase/ReviewUseCaseTest.java | 84 +++++++------ 4 files changed, 193 insertions(+), 49 deletions(-) create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundException.java create mode 100644 src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCase.java diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundException.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundException.java new file mode 100644 index 0000000..b687a13 --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundException.java @@ -0,0 +1,25 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.exception; + +import java.text.MessageFormat; +import java.util.Optional; +import java.util.UUID; + +public class ReviewNotFoundException extends RuntimeException { + + public static final String THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the customer id {0} does not exists"; + public static final String THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the book id {0} does not exists"; + public static final String THE_REVIEWS_WITH_AVIS_ID_DOES_NOT_EXIST_MESSAGE = "The review with avis id {0} does not exists"; + + public ReviewNotFoundException(Optional customerUUID, Optional bookUUID, Optional avisUUID) { + super(buildMessage(customerUUID, bookUUID, avisUUID)); + } + + private static String buildMessage(Optional customerUUID, Optional bookUUID, Optional avisUUID) { + if (customerUUID.isPresent()) { + return MessageFormat.format(THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE, customerUUID.get()); + }else if (bookUUID.isPresent()) { + return MessageFormat.format(THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE, bookUUID.get()); + } + return MessageFormat.format(THE_REVIEWS_WITH_AVIS_ID_DOES_NOT_EXIST_MESSAGE, avisUUID.get()); + } +} diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCase.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCase.java new file mode 100644 index 0000000..aa63f16 --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCase.java @@ -0,0 +1,113 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase; + +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.converter.ReviewConverter; +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.exception.ReviewNotFoundException; +import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository; +import fr.iut_fbleau.but3.dev62.mylibrary.review.validator.ReviewValidator; + +import java.util.ArrayList; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; + +public class ReviewUseCase { + + private final ReviewRepository reviewRepository; + + public ReviewUseCase(ReviewRepository reviewRepository) { + this.reviewRepository = reviewRepository; + } + + public UUID registerReview(ReviewInfo newReview) throws NotValidReviewException { + ReviewValidator.validate(newReview); + Review reviewToRegister = ReviewConverter.toDomain(newReview); + Review reviewToRegistered = reviewRepository.save(reviewToRegister); + return reviewToRegistered.getAvisId(); + } + + public ArrayList findReviewByCustomerId(UUID customerId) { + ArrayList optionalReviews = reviewRepository.findByCustomerId(customerId); + return optionalReviews.stream() + .map(ReviewConverter::toDTO) + .collect(Collectors.toCollection(ArrayList::new)); + } + + public ArrayList findReviewByBookId(UUID bookId) { + ArrayList optionalReviews = reviewRepository.findByBookId(bookId); + return optionalReviews.stream() + .map(ReviewConverter::toDTO) + .collect(Collectors.toCollection(ArrayList::new)); + } + + public Optional findReviewByAvisId(UUID avisId) { + Optional optionalReview = reviewRepository.findByAvisId(avisId); + return optionalReview.map(ReviewConverter::toDTO); + } + + public ReviewDTO updateReview(UUID avisUUID, ReviewInfo reviewInfo) + throws ReviewNotFoundException, NotValidReviewException { + ReviewValidator.validate(reviewInfo); + Review reviewByAvisUUID = getReviewIfDoesNotExistThrowReviewNotFoundException( + avisUUID); + Review review = Review.builder() + .avisId(avisUUID) + .customerId(reviewByAvisUUID.getCustomerId()) + .bookId(reviewByAvisUUID.getBookId()) + .note(reviewByAvisUUID.getNote()) + .comment(reviewByAvisUUID.getComment()) + .purchaseDate(reviewByAvisUUID.getPurchaseDate()) + .build(); + Review updatedReview = reviewRepository.save(review); + return ReviewConverter.toDTO(updatedReview); + } + + public void deleteReview(UUID avisUUID) throws ReviewNotFoundException { + Review reviewToDelete = getReviewIfDoesNotExistThrowReviewNotFoundException(avisUUID); + this.reviewRepository.delete(reviewToDelete); + } + + public void deleteCustomerReviews(UUID customerUUID) throws ReviewNotFoundException { + ArrayList reviewsToDelete = getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(customerUUID); + for (Review review : reviewsToDelete) { + reviewRepository.delete(review); + } + } + + public void deleteBookReviews(UUID bookUUID) throws ReviewNotFoundException { + ArrayList reviewsToDelete = getReviewByBookIfDoesNotExistThrowReviewNotFoundException(bookUUID); + for (Review review : reviewsToDelete) { + reviewRepository.delete(review); + } + } + + private Review getReviewIfDoesNotExistThrowReviewNotFoundException(UUID avisUUID) + throws ReviewNotFoundException { + Optional optionalReviewByAvisId = reviewRepository.findByAvisId(avisUUID); + if (optionalReviewByAvisId.isEmpty()) { + throw new ReviewNotFoundException(Optional.empty(), Optional.empty(),Optional.of(avisUUID)); + } + return optionalReviewByAvisId.get(); + } + + private ArrayList getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(UUID customerUUID) + throws ReviewNotFoundException { + ArrayList optionalReviewByAvisId = reviewRepository.findByCustomerId(customerUUID); + if (optionalReviewByAvisId.isEmpty()) { + throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(),Optional.empty()); + } + return optionalReviewByAvisId; + } + + private ArrayList getReviewByBookIfDoesNotExistThrowReviewNotFoundException(UUID bookUUID) + throws ReviewNotFoundException { + ArrayList optionalReviewByAvisId = reviewRepository.findByBookId(bookUUID); + if (optionalReviewByAvisId.isEmpty()) { + throw new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(bookUUID)); + } + return optionalReviewByAvisId; + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java index 34ec57b..f9c2951 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java @@ -29,7 +29,7 @@ public class ReviewNotFoundExceptionTest { ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID), Optional.empty()); - String expectedMessage = String.format("The reviews with book id %s does not exists", bookUUID); + String expectedMessage = String.format("The reviews with the book id %s does not exists", bookUUID); assertEquals(expectedMessage, exception.getMessage()); } @@ -40,7 +40,7 @@ public class ReviewNotFoundExceptionTest { ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(avisUUID)); - String expectedMessage = String.format("The review with id %s does not exists", avisUUID); + String expectedMessage = String.format("The review with avis id %s does not exists", avisUUID); assertEquals(expectedMessage, exception.getMessage()); } @@ -59,7 +59,7 @@ public class ReviewNotFoundExceptionTest { @Test @DisplayName("Exception should use the correct constant message format for book") - void testExceptionUsesConstantMessageCustomerFormat() { + void testExceptionUsesConstantMessageBookFormat() { UUID bookUUID = UUID.randomUUID(); ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID), Optional.empty()); @@ -71,14 +71,14 @@ public class ReviewNotFoundExceptionTest { } @Test - @DisplayName("Exception should use the correct constant message format for customer and book") - void testExceptionUsesConstantMessageCustomerFormat() { + @DisplayName("Exception should use the correct constant message format for review") + void testExceptionUsesConstantMessageReviewFormat() { UUID avisUUID = UUID.randomUUID(); - ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.empty(avisUUID)); + ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(avisUUID)); - String expectedFormatWithPlaceholder = "The reviews with id {0} does not exists"; - assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_CUSTOMER_ID_AND_BOOK_ID_DOES_NOT_EXIST_MESSAGE, + String expectedFormatWithPlaceholder = "The review with avis id {0} does not exists"; + assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_AVIS_ID_DOES_NOT_EXIST_MESSAGE, expectedFormatWithPlaceholder); assertTrue(exception.getMessage().contains(avisUUID.toString())); } @@ -89,9 +89,9 @@ public class ReviewNotFoundExceptionTest { UUID avisUUID = UUID.randomUUID(); try { - throw new ReviewNotFoundException(Optional.of(avisUUID), Optional.empty()); + throw new ReviewNotFoundException(Optional.empty(),Optional.empty(), Optional.of(avisUUID)); } catch (ReviewNotFoundException e) { - String expectedMessage = String.format("The reviews id %s does not exists", avisUUID); + String expectedMessage = String.format("The review with avis id %s does not exists", avisUUID); assertEquals(expectedMessage, e.getMessage()); } } diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java index 149f496..d10098d 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java @@ -1,19 +1,23 @@ 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.exception.ReviewNotFoundException; 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.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; import java.util.UUID; @@ -25,6 +29,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.mockito.Mockito.never; +@ExtendWith(MockitoExtension.class) public class ReviewUseCaseTest { @Mock @@ -70,14 +75,14 @@ public class ReviewUseCaseTest { UUID registeredId = reviewUseCase.registerReview(validReviewInfo); assertNotNull(registeredId); - assertEquals(customerId, registeredId); + assertEquals(avisId, 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); + ReviewInfo invalidReviewInfo = new ReviewInfo(0, "plutôt mauvais", purchaseDate); assertThrows(NotValidReviewException.class, () -> reviewUseCase.registerReview(invalidReviewInfo)); @@ -91,15 +96,16 @@ public class ReviewUseCaseTest { class FindReviewTests { @Test - @DisplayName("Should return review when customer ID exists") + @DisplayName("Should return reviews when customer ID exists") void testFindReviewByCustomerId() { - when(reviewRepository.findByCustomerId(customerId)).thenReturn(Optional.of(testReview)); + when(reviewRepository.findByCustomerId(customerId)).thenReturn(new ArrayList(List.of(testReview))); - Optional foundReview = reviewUseCase.findReviewByCustomerId(customerId); + ArrayList foundReviews = reviewUseCase.findReviewByCustomerId(customerId); - assertTrue(foundReview.isPresent()); - assertEquals(testReview.getBookId(), foundReview.get().getBookId()); - assertEquals(testReview.getNote(), foundReview.get().getNote()); + assertTrue(!foundReviews.isEmpty()); + boolean allSameCustomer = foundReviews.stream() + .allMatch(review -> review.getCustomerId().equals(customerId)); + assertTrue(allSameCustomer); verify(reviewRepository, times(1)).findByCustomerId(customerId); } @@ -107,24 +113,25 @@ public class ReviewUseCaseTest { @DisplayName("Should return empty Optional when customer ID doesn't exist") void testFindReviewByCustomerIdNotFound() { UUID nonExistentCustomerId = UUID.randomUUID(); - when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(Optional.empty()); + when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(new ArrayList()); - Optional foundReview = reviewUseCase.findReviewByCustomerId(nonExistentCustomerId); + ArrayList foundReviews = reviewUseCase.findReviewByCustomerId(nonExistentCustomerId); - assertTrue(foundReview.isEmpty()); + assertTrue(foundReviews.isEmpty()); verify(reviewRepository, times(1)).findByCustomerId(nonExistentCustomerId); } @Test - @DisplayName("Should return review when book ID exists") + @DisplayName("Should return reviews when book ID exists") void testFindReviewByBookId() { - when(reviewRepository.findByBookId(bookId)).thenReturn(Optional.of(testReview)); + when(reviewRepository.findByBookId(bookId)).thenReturn(new ArrayList(List.of(testReview))); - Optional foundReview = reviewUseCase.findReviewByBookId(bookId); + ArrayList foundReviews = reviewUseCase.findReviewByBookId(bookId); - assertTrue(foundReview.isPresent()); - assertEquals(testReview.getCustomerId(), foundReview.get().getCustomerId()); - assertEquals(testReview.getNote(), foundReview.get().getNote()); + assertTrue(!foundReviews.isEmpty()); + boolean allSameCustomer = foundReviews.stream() + .allMatch(review -> review.getBookId().equals(bookId)); + assertTrue(allSameCustomer); verify(reviewRepository, times(1)).findByBookId(bookId); } @@ -132,11 +139,11 @@ public class ReviewUseCaseTest { @DisplayName("Should return empty Optional when book ID doesn't exist") void testFindReviewByBookIdNotFound() { UUID nonExistentBookId = UUID.randomUUID(); - when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(Optional.empty()); + when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(new ArrayList()); - Optional foundReview = reviewUseCase.findReviewByBookId(nonExistentBookId); + ArrayList foundReviews = reviewUseCase.findReviewByBookId(nonExistentBookId); - assertTrue(foundReview.isEmpty()); + assertTrue(foundReviews.isEmpty()); verify(reviewRepository, times(1)).findByBookId(nonExistentBookId); } @@ -145,17 +152,17 @@ public class ReviewUseCaseTest { void testFindReviewByAvisId() { when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview)); - Optional foundReview = reviewUseCase.findReviewByAvisId(customerId, bookId); + Optional foundReview = reviewUseCase.findReviewByAvisId(avisId); assertTrue(foundReview.isPresent()); assertEquals(testReview.getBookId(), foundReview.get().getBookId()); assertEquals(testReview.getNote(), foundReview.get().getNote()); - verify(reviewRepository, times(1)).findByAvisId(customerId, bookId); + verify(reviewRepository, times(1)).findByAvisId(avisId); } @Test - @DisplayName("Should return empty Optional when customer ID doesn't exist") - void testFindReviewByCustomerIdNotFound() { + @DisplayName("Should return empty Optional when avis ID doesn't exist") + void testFindReviewByAvisIdNotFound() { UUID nonExistentAvisId = UUID.randomUUID(); when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty()); @@ -200,19 +207,18 @@ public class ReviewUseCaseTest { } @Test - @DisplayName("Should throw exception when customer and book ID doesn't exist") + @DisplayName("Should throw exception when avis ID doesn't exist") void testUpdateReviewNotFound() { - UUID nonExistentCustomerId = UUID.randomUUID(); - UUID nonExistentBookId = UUID.randomUUID(); - when(reviewRepository.findByAvisId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty()); + UUID nonExistentAvisId = UUID.randomUUID(); + when(reviewRepository.findByAvisId(nonExistentAvisId)).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)); + () -> reviewUseCase.updateReview(nonExistentAvisId, updateInfo)); - verify(reviewRepository, times(1)).findByAvisId(nonExistentCustomerId, nonExistentBookId); + verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId); verify(reviewRepository, never()).save(any(Review.class)); } @@ -222,7 +228,7 @@ public class ReviewUseCaseTest { LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24); ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate); - assertThrows(NotValidCustomerException.class, + assertThrows(NotValidReviewException.class, () -> reviewUseCase.updateReview(avisId, invalidUpdateInfo)); verify(reviewRepository, never()).findByAvisId(any(UUID.class)); @@ -237,7 +243,7 @@ public class ReviewUseCaseTest { @Test @DisplayName("Should delete reviews when customer ID exists") void testDeleteCustomerReviews() throws ReviewNotFoundException { - when(reviewRepository.findByCustomerId(customerId)).thenReturn(Optional.of(testReview)); + when(reviewRepository.findByCustomerId(customerId)).thenReturn(new ArrayList(List.of(testReview))); doNothing().when(reviewRepository).delete(testReview); reviewUseCase.deleteCustomerReviews(customerId); @@ -250,7 +256,7 @@ public class ReviewUseCaseTest { @DisplayName("Should throw exception when customer ID doesn't exist") void testDeleteCustomerReviewsNotFound() { UUID nonExistentCustomerId = UUID.randomUUID(); - when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(Optional.empty()); + when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(new ArrayList()); assertThrows(ReviewNotFoundException.class, () -> reviewUseCase.deleteCustomerReviews(nonExistentCustomerId)); @@ -262,7 +268,7 @@ public class ReviewUseCaseTest { @Test @DisplayName("Should delete reviews when book ID exists") void testDeleteBookReviews() throws ReviewNotFoundException { - when(reviewRepository.findByBookId(bookId)).thenReturn(Optional.of(testReview)); + when(reviewRepository.findByBookId(bookId)).thenReturn(new ArrayList(List.of(testReview))); doNothing().when(reviewRepository).delete(testReview); reviewUseCase.deleteBookReviews(bookId); @@ -275,7 +281,7 @@ public class ReviewUseCaseTest { @DisplayName("Should throw exception when book ID doesn't exist") void testDeleteBookReviewsNotFound() { UUID nonExistentBookId = UUID.randomUUID(); - when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(Optional.empty()); + when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(new ArrayList()); assertThrows(ReviewNotFoundException.class, () -> reviewUseCase.deleteBookReviews(nonExistentBookId)); @@ -285,7 +291,7 @@ public class ReviewUseCaseTest { } @Test - @DisplayName("Should delete review when customer and book ID exists") + @DisplayName("Should delete review when avis ID exists") void testDeleteReview() throws ReviewNotFoundException { when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview)); doNothing().when(reviewRepository).delete(testReview); @@ -297,13 +303,13 @@ public class ReviewUseCaseTest { } @Test - @DisplayName("Should throw exception when customer and book ID doesn't exist") + @DisplayName("Should throw exception when avis ID doesn't exist") void testDeleteReviewNotFound() { UUID nonExistentAvisId = UUID.randomUUID(); when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty()); assertThrows(ReviewNotFoundException.class, - () -> reviewUseCase.deleteReviews(nonExistentAvisId)); + () -> reviewUseCase.deleteReview(nonExistentAvisId)); verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId); verify(reviewRepository, never()).delete(any(Review.class)); -- 2.54.0 From 13f8cab3ed9782334b3ff25ea41fea734ea72419 Mon Sep 17 00:00:00 2001 From: aubert Date: Fri, 12 Jun 2026 01:25:15 +0200 Subject: [PATCH 20/20] =?UTF-8?q?bonne=20utilisation=20de=20nom=20de=20var?= =?UTF-8?q?iable=20en=20anglais=20pour=20rester=20coh=C3=A9rent=20pour=20a?= =?UTF-8?q?vis=20via=20la=20commande=20"find=20src/test/java/fr/iut=5Ffble?= =?UTF-8?q?au/but3/dev62/mylibrary/review=20-name=20"*.java"=20-exec=20sed?= =?UTF-8?q?=20-i=20's/AVIS/REVIEW/g;=20s/Avis/Review/g;=20s/avis/review/g'?= =?UTF-8?q?=20{}=20+"=20pour=20les=20fichier=20test=20et=20remplacer=20le?= =?UTF-8?q?=20chemin=20pour=20les=20fichier=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev62/mylibrary/review/ReviewDTO.java | 2 +- .../review/converter/ReviewConverter.java | 1 + .../dev62/mylibrary/review/entity/Review.java | 4 +- .../exception/ReviewNotFoundException.java | 10 +-- .../review/repository/ReviewRepository.java | 12 ++-- .../review/usecase/ReviewUseCase.java | 50 ++++++------- .../review/converter/ReviewConverterTest.java | 2 +- .../mylibrary/review/entity/ReviewTest.java | 6 +- .../ReviewNotFoundExceptionTest.java | 22 +++--- .../repository/ReviewRepositoryTest.java | 34 ++++----- .../review/usecase/ReviewUseCaseTest.java | 70 +++++++++---------- 11 files changed, 107 insertions(+), 106 deletions(-) diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java index f67097a..92e40ca 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/ReviewDTO.java @@ -10,7 +10,7 @@ import java.util.UUID; @Builder public class ReviewDTO { - private UUID avisId; + private UUID reviewId; private UUID customerId; private UUID bookId; private Integer note; diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverter.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverter.java index a3fcea7..d8ebead 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverter.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverter.java @@ -19,6 +19,7 @@ public class ReviewConverter { public static ReviewDTO toDTO(Review review) { return ReviewDTO.builder() + .reviewId(review.getReviewId()) .customerId(review.getCustomerId()) .bookId(review.getBookId()) .note(review.getNote()) diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java index b295b5a..26b80ca 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/Review.java @@ -10,7 +10,7 @@ import java.util.UUID; @Builder public class Review { - private UUID avisId; + private UUID reviewId; private UUID customerId; private UUID bookId; private Integer note; @@ -18,7 +18,7 @@ public class Review { private LocalDate purchaseDate; public void setRandomUUID() { - this.avisId = UUID.randomUUID(); + this.reviewId = UUID.randomUUID(); } public void setRandomUUIDCustomerAndBook() { diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundException.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundException.java index b687a13..087454e 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundException.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundException.java @@ -8,18 +8,18 @@ public class ReviewNotFoundException extends RuntimeException { public static final String THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the customer id {0} does not exists"; public static final String THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the book id {0} does not exists"; - public static final String THE_REVIEWS_WITH_AVIS_ID_DOES_NOT_EXIST_MESSAGE = "The review with avis id {0} does not exists"; + public static final String THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE = "The review with review id {0} does not exists"; - public ReviewNotFoundException(Optional customerUUID, Optional bookUUID, Optional avisUUID) { - super(buildMessage(customerUUID, bookUUID, avisUUID)); + public ReviewNotFoundException(Optional customerUUID, Optional bookUUID, Optional reviewUUID) { + super(buildMessage(customerUUID, bookUUID, reviewUUID)); } - private static String buildMessage(Optional customerUUID, Optional bookUUID, Optional avisUUID) { + private static String buildMessage(Optional customerUUID, Optional bookUUID, Optional reviewUUID) { if (customerUUID.isPresent()) { return MessageFormat.format(THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE, customerUUID.get()); }else if (bookUUID.isPresent()) { return MessageFormat.format(THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE, bookUUID.get()); } - return MessageFormat.format(THE_REVIEWS_WITH_AVIS_ID_DOES_NOT_EXIST_MESSAGE, avisUUID.get()); + return MessageFormat.format(THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE, reviewUUID.get()); } } 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 203c931..378fceb 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 @@ -23,8 +23,8 @@ public class ReviewRepository { } public Review save(Review newReview) { - Optional optionalReviewWithSameAvisId = this.findByAvisId(newReview.getAvisId()); - optionalReviewWithSameAvisId.ifPresent(reviews::remove); + Optional optionalReviewWithSameReviewId = this.findByReviewId(newReview.getReviewId()); + optionalReviewWithSameReviewId.ifPresent(reviews::remove); this.reviews.add(newReview); return newReview; } @@ -41,9 +41,9 @@ public class ReviewRepository { .collect(Collectors.toCollection(ArrayList::new)); } - public Optional findByAvisId(UUID avisUUID) { + public Optional findByReviewId(UUID reviewUUID) { return this.reviews.stream() - .filter(review -> review.getAvisId().equals(avisUUID)) + .filter(review -> review.getReviewId().equals(reviewUUID)) .findFirst(); } @@ -57,9 +57,9 @@ public class ReviewRepository { .anyMatch(review -> review.getBookId().equals(bookUUID)); } - public boolean existsByAvisId(UUID avisUUID) { + public boolean existsByReviewId(UUID reviewUUID) { return this.reviews.stream() - .anyMatch(review -> review.getAvisId().equals(avisUUID)); + .anyMatch(review -> review.getReviewId().equals(reviewUUID)); } public void deleteCustomerReviews(UUID customerUUID) { diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCase.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCase.java index aa63f16..ddc80a5 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCase.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCase.java @@ -26,7 +26,7 @@ public class ReviewUseCase { ReviewValidator.validate(newReview); Review reviewToRegister = ReviewConverter.toDomain(newReview); Review reviewToRegistered = reviewRepository.save(reviewToRegister); - return reviewToRegistered.getAvisId(); + return reviewToRegistered.getReviewId(); } public ArrayList findReviewByCustomerId(UUID customerId) { @@ -43,30 +43,30 @@ public class ReviewUseCase { .collect(Collectors.toCollection(ArrayList::new)); } - public Optional findReviewByAvisId(UUID avisId) { - Optional optionalReview = reviewRepository.findByAvisId(avisId); + public Optional findReviewByReviewId(UUID reviewId) { + Optional optionalReview = reviewRepository.findByReviewId(reviewId); return optionalReview.map(ReviewConverter::toDTO); } - public ReviewDTO updateReview(UUID avisUUID, ReviewInfo reviewInfo) + public ReviewDTO updateReview(UUID reviewUUID, ReviewInfo reviewInfo) throws ReviewNotFoundException, NotValidReviewException { ReviewValidator.validate(reviewInfo); - Review reviewByAvisUUID = getReviewIfDoesNotExistThrowReviewNotFoundException( - avisUUID); + Review reviewByReviewUUID = getReviewIfDoesNotExistThrowReviewNotFoundException( + reviewUUID); Review review = Review.builder() - .avisId(avisUUID) - .customerId(reviewByAvisUUID.getCustomerId()) - .bookId(reviewByAvisUUID.getBookId()) - .note(reviewByAvisUUID.getNote()) - .comment(reviewByAvisUUID.getComment()) - .purchaseDate(reviewByAvisUUID.getPurchaseDate()) + .reviewId(reviewUUID) + .customerId(reviewByReviewUUID.getCustomerId()) + .bookId(reviewByReviewUUID.getBookId()) + .note(reviewByReviewUUID.getNote()) + .comment(reviewByReviewUUID.getComment()) + .purchaseDate(reviewByReviewUUID.getPurchaseDate()) .build(); Review updatedReview = reviewRepository.save(review); return ReviewConverter.toDTO(updatedReview); } - public void deleteReview(UUID avisUUID) throws ReviewNotFoundException { - Review reviewToDelete = getReviewIfDoesNotExistThrowReviewNotFoundException(avisUUID); + public void deleteReview(UUID reviewUUID) throws ReviewNotFoundException { + Review reviewToDelete = getReviewIfDoesNotExistThrowReviewNotFoundException(reviewUUID); this.reviewRepository.delete(reviewToDelete); } @@ -84,30 +84,30 @@ public class ReviewUseCase { } } - private Review getReviewIfDoesNotExistThrowReviewNotFoundException(UUID avisUUID) + private Review getReviewIfDoesNotExistThrowReviewNotFoundException(UUID reviewUUID) throws ReviewNotFoundException { - Optional optionalReviewByAvisId = reviewRepository.findByAvisId(avisUUID); - if (optionalReviewByAvisId.isEmpty()) { - throw new ReviewNotFoundException(Optional.empty(), Optional.empty(),Optional.of(avisUUID)); + Optional optionalReviewByReviewId = reviewRepository.findByReviewId(reviewUUID); + if (optionalReviewByReviewId.isEmpty()) { + throw new ReviewNotFoundException(Optional.empty(), Optional.empty(),Optional.of(reviewUUID)); } - return optionalReviewByAvisId.get(); + return optionalReviewByReviewId.get(); } private ArrayList getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(UUID customerUUID) throws ReviewNotFoundException { - ArrayList optionalReviewByAvisId = reviewRepository.findByCustomerId(customerUUID); - if (optionalReviewByAvisId.isEmpty()) { + ArrayList optionalReviewByReviewId = reviewRepository.findByCustomerId(customerUUID); + if (optionalReviewByReviewId.isEmpty()) { throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(),Optional.empty()); } - return optionalReviewByAvisId; + return optionalReviewByReviewId; } private ArrayList getReviewByBookIfDoesNotExistThrowReviewNotFoundException(UUID bookUUID) throws ReviewNotFoundException { - ArrayList optionalReviewByAvisId = reviewRepository.findByBookId(bookUUID); - if (optionalReviewByAvisId.isEmpty()) { + ArrayList optionalReviewByReviewId = reviewRepository.findByBookId(bookUUID); + if (optionalReviewByReviewId.isEmpty()) { throw new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(bookUUID)); } - return optionalReviewByAvisId; + return optionalReviewByReviewId; } } diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java index 9e6c2ed..135ae7c 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/converter/ReviewConverterTest.java @@ -47,7 +47,7 @@ public class ReviewConverterTest { void shouldConvertReviewToDTO() { LocalDate purchaseDate = LocalDate.of(2026, 3, 24); Review review = Review.builder() - .avisId(UUID.randomUUID()) + .reviewId(UUID.randomUUID()) .customerId(UUID.randomUUID()) .bookId(UUID.randomUUID()) .note(5) diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java index 132f5b4..44e78ea 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/entity/ReviewTest.java @@ -38,17 +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 originalReviewId = review.getReviewId(); UUID originalCustomerId = review.getCustomerId(); UUID originalBookId = review.getCustomerId(); review.setRandomUUID(); review.setRandomUUIDCustomerAndBook(); - assertNotNull(review.getAvisId()); + assertNotNull(review.getReviewId()); assertNotNull(review.getCustomerId()); assertNotNull(review.getBookId()); - assertNotEquals(originalAvisId, review.getAvisId()); + assertNotEquals(originalReviewId, review.getReviewId()); assertNotEquals(originalCustomerId, review.getCustomerId()); assertNotEquals(originalBookId, review.getBookId()); } diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java index f9c2951..9645ece 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/exception/ReviewNotFoundExceptionTest.java @@ -36,11 +36,11 @@ public class ReviewNotFoundExceptionTest { @Test @DisplayName("Exception message should contain the UUID provided for customer and book") void testExceptionMessageContainsUUIDForCustomerAndBook() { - UUID avisUUID = UUID.randomUUID(); + UUID reviewUUID = UUID.randomUUID(); - ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(avisUUID)); + ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(reviewUUID)); - String expectedMessage = String.format("The review with avis id %s does not exists", avisUUID); + String expectedMessage = String.format("The review with review id %s does not exists", reviewUUID); assertEquals(expectedMessage, exception.getMessage()); } @@ -73,25 +73,25 @@ public class ReviewNotFoundExceptionTest { @Test @DisplayName("Exception should use the correct constant message format for review") void testExceptionUsesConstantMessageReviewFormat() { - UUID avisUUID = UUID.randomUUID(); + UUID reviewUUID = UUID.randomUUID(); - ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(avisUUID)); + ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(reviewUUID)); - String expectedFormatWithPlaceholder = "The review with avis id {0} does not exists"; - assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_AVIS_ID_DOES_NOT_EXIST_MESSAGE, + String expectedFormatWithPlaceholder = "The review with review id {0} does not exists"; + assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE, expectedFormatWithPlaceholder); - assertTrue(exception.getMessage().contains(avisUUID.toString())); + assertTrue(exception.getMessage().contains(reviewUUID.toString())); } @Test @DisplayName("Exception should be properly thrown and caught") void testExceptionCanBeThrownAndCaught() { - UUID avisUUID = UUID.randomUUID(); + UUID reviewUUID = UUID.randomUUID(); try { - throw new ReviewNotFoundException(Optional.empty(),Optional.empty(), Optional.of(avisUUID)); + throw new ReviewNotFoundException(Optional.empty(),Optional.empty(), Optional.of(reviewUUID)); } catch (ReviewNotFoundException e) { - String expectedMessage = String.format("The review with avis id %s does not exists", avisUUID); + String expectedMessage = String.format("The review with review id %s does not exists", reviewUUID); assertEquals(expectedMessage, e.getMessage()); } } 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 d5da338..f4b5e4b 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 @@ -99,11 +99,11 @@ public class ReviewRepositoryTest { repository.save(review1); LocalDate purchaseDate = LocalDate.of(2026, 5, 24); - UUID avisId = review1.getAvisId(); + UUID reviewId = review1.getReviewId(); UUID customerId = UUID.randomUUID(); UUID bookId = UUID.randomUUID(); Review updatedReview = Review.builder() - .avisId(avisId) + .reviewId(reviewId) .customerId(customerId) .bookId(bookId) .note(4) @@ -114,7 +114,7 @@ public class ReviewRepositoryTest { Review savedReview = repository.save(updatedReview); assertEquals(1, repository.findAll().size()); - assertEquals(avisId, savedReview.getAvisId()); + assertEquals(reviewId, savedReview.getReviewId()); assertEquals(customerId, savedReview.getCustomerId()); assertEquals(bookId, savedReview.getBookId()); assertEquals(4, savedReview.getNote()); @@ -203,9 +203,9 @@ public class ReviewRepositoryTest { } @Test - @DisplayName("findByAvisId should return review with matching avis ID") - void testFindByAvisId() { - Optional foundreview = repository.findByAvisId(review1.getAvisId()); + @DisplayName("findByReviewId should return review with matching review ID") + void testFindByReviewId() { + Optional foundreview = repository.findByReviewId(review1.getReviewId()); assertTrue(foundreview.isPresent()); assertEquals(review1.getNote(), foundreview.get().getNote()); @@ -213,11 +213,11 @@ public class ReviewRepositoryTest { } @Test - @DisplayName("findByAvisId should return empty Optional when a review with avis ID doesn't exist") - void testFindByAvisIdNotFound() { - UUID nonExistentAvisId = UUID.randomUUID(); + @DisplayName("findByReviewId should return empty Optional when a review with review ID doesn't exist") + void testFindByReviewIdNotFound() { + UUID nonExistentReviewId = UUID.randomUUID(); - Optional foundreview = repository.findByAvisId(nonExistentAvisId); + Optional foundreview = repository.findByReviewId(nonExistentReviewId); assertTrue(foundreview.isEmpty()); } @@ -259,19 +259,19 @@ public class ReviewRepositoryTest { } @Test - @DisplayName("existsByAvisId should return true when a review with avis ID exists") - void testExistsByAvisIdExists() { - boolean exists = repository.existsByAvisId(review1.getAvisId()); + @DisplayName("existsByReviewId should return true when a review with review ID exists") + void testExistsByReviewIdExists() { + boolean exists = repository.existsByReviewId(review1.getReviewId()); assertTrue(exists); } @Test - @DisplayName("existsByAvisId should return false when avis ID doesn't exist") - void testExistsByAvisIdNotExists() { - UUID nonExistentAvisId = UUID.randomUUID(); + @DisplayName("existsByReviewId should return false when review ID doesn't exist") + void testExistsByReviewIdNotExists() { + UUID nonExistentReviewId = UUID.randomUUID(); - boolean exists = repository.existsByAvisId(nonExistentAvisId); + boolean exists = repository.existsByReviewId(nonExistentReviewId); assertFalse(exists); } diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java index d10098d..61937b5 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/review/usecase/ReviewUseCaseTest.java @@ -38,7 +38,7 @@ public class ReviewUseCaseTest { @InjectMocks private ReviewUseCase reviewUseCase; - private UUID avisId; + private UUID reviewId; private UUID customerId; private UUID bookId; private LocalDate purchaseDate; @@ -47,12 +47,12 @@ public class ReviewUseCaseTest { @BeforeEach void setUp() { - avisId = UUID.randomUUID(); + reviewId = UUID.randomUUID(); customerId = UUID.randomUUID(); bookId = UUID.randomUUID(); purchaseDate = LocalDate.of(2026, 5, 24); testReview = Review.builder() - .avisId(avisId) + .reviewId(reviewId) .customerId(customerId) .bookId(bookId) .note(2) @@ -75,7 +75,7 @@ public class ReviewUseCaseTest { UUID registeredId = reviewUseCase.registerReview(validReviewInfo); assertNotNull(registeredId); - assertEquals(avisId, registeredId); + assertEquals(reviewId, registeredId); verify(reviewRepository, times(1)).save(any(Review.class)); } @@ -148,28 +148,28 @@ public class ReviewUseCaseTest { } @Test - @DisplayName("Should return review when Avis ID exists") - void testFindReviewByAvisId() { - when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview)); + @DisplayName("Should return review when Review ID exists") + void testFindReviewByReviewId() { + when(reviewRepository.findByReviewId(reviewId)).thenReturn(Optional.of(testReview)); - Optional foundReview = reviewUseCase.findReviewByAvisId(avisId); + Optional foundReview = reviewUseCase.findReviewByReviewId(reviewId); assertTrue(foundReview.isPresent()); assertEquals(testReview.getBookId(), foundReview.get().getBookId()); assertEquals(testReview.getNote(), foundReview.get().getNote()); - verify(reviewRepository, times(1)).findByAvisId(avisId); + verify(reviewRepository, times(1)).findByReviewId(reviewId); } @Test - @DisplayName("Should return empty Optional when avis ID doesn't exist") - void testFindReviewByAvisIdNotFound() { - UUID nonExistentAvisId = UUID.randomUUID(); - when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty()); + @DisplayName("Should return empty Optional when review ID doesn't exist") + void testFindReviewByReviewIdNotFound() { + UUID nonExistentReviewId = UUID.randomUUID(); + when(reviewRepository.findByReviewId(nonExistentReviewId)).thenReturn(Optional.empty()); - Optional foundReview = reviewUseCase.findReviewByAvisId(nonExistentAvisId); + Optional foundReview = reviewUseCase.findReviewByReviewId(nonExistentReviewId); assertTrue(foundReview.isEmpty()); - verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId); + verify(reviewRepository, times(1)).findByReviewId(nonExistentReviewId); } } @@ -180,11 +180,11 @@ public class ReviewUseCaseTest { @Test @DisplayName("Should update review when valid data is provided") void testUpdateReviewWithValidData() throws ReviewNotFoundException, NotValidReviewException { - when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview)); + when(reviewRepository.findByReviewId(reviewId)).thenReturn(Optional.of(testReview)); LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 30); Review updatedReview = Review.builder() - .avisId(avisId) + .reviewId(reviewId) .customerId(customerId) .bookId(bookId) .note(4) @@ -196,29 +196,29 @@ public class ReviewUseCaseTest { ReviewInfo updateInfo = new ReviewInfo(4, "en fait c'est bien", updatePurchaseDate); - ReviewDTO result = reviewUseCase.updateReview(avisId, updateInfo); + ReviewDTO result = reviewUseCase.updateReview(reviewId, updateInfo); assertNotNull(result); assertEquals(customerId, result.getCustomerId()); assertEquals(4, result.getNote()); assertEquals("en fait c'est bien", result.getComment()); - verify(reviewRepository, times(1)).findByAvisId(avisId); + verify(reviewRepository, times(1)).findByReviewId(reviewId); verify(reviewRepository, times(1)).save(any(Review.class)); } @Test - @DisplayName("Should throw exception when avis ID doesn't exist") + @DisplayName("Should throw exception when review ID doesn't exist") void testUpdateReviewNotFound() { - UUID nonExistentAvisId = UUID.randomUUID(); - when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty()); + UUID nonExistentReviewId = UUID.randomUUID(); + when(reviewRepository.findByReviewId(nonExistentReviewId)).thenReturn(Optional.empty()); LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24); ReviewInfo updateInfo = new ReviewInfo(3, "moyen", updatePurchaseDate); assertThrows(ReviewNotFoundException.class, - () -> reviewUseCase.updateReview(nonExistentAvisId, updateInfo)); + () -> reviewUseCase.updateReview(nonExistentReviewId, updateInfo)); - verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId); + verify(reviewRepository, times(1)).findByReviewId(nonExistentReviewId); verify(reviewRepository, never()).save(any(Review.class)); } @@ -229,9 +229,9 @@ public class ReviewUseCaseTest { ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate); assertThrows(NotValidReviewException.class, - () -> reviewUseCase.updateReview(avisId, invalidUpdateInfo)); + () -> reviewUseCase.updateReview(reviewId, invalidUpdateInfo)); - verify(reviewRepository, never()).findByAvisId(any(UUID.class)); + verify(reviewRepository, never()).findByReviewId(any(UUID.class)); verify(reviewRepository, never()).save(any(Review.class)); } } @@ -291,27 +291,27 @@ public class ReviewUseCaseTest { } @Test - @DisplayName("Should delete review when avis ID exists") + @DisplayName("Should delete review when review ID exists") void testDeleteReview() throws ReviewNotFoundException { - when(reviewRepository.findByAvisId(avisId)).thenReturn(Optional.of(testReview)); + when(reviewRepository.findByReviewId(reviewId)).thenReturn(Optional.of(testReview)); doNothing().when(reviewRepository).delete(testReview); - reviewUseCase.deleteReview(avisId); + reviewUseCase.deleteReview(reviewId); - verify(reviewRepository, times(1)).findByAvisId(avisId); + verify(reviewRepository, times(1)).findByReviewId(reviewId); verify(reviewRepository, times(1)).delete(testReview); } @Test - @DisplayName("Should throw exception when avis ID doesn't exist") + @DisplayName("Should throw exception when review ID doesn't exist") void testDeleteReviewNotFound() { - UUID nonExistentAvisId = UUID.randomUUID(); - when(reviewRepository.findByAvisId(nonExistentAvisId)).thenReturn(Optional.empty()); + UUID nonExistentReviewId = UUID.randomUUID(); + when(reviewRepository.findByReviewId(nonExistentReviewId)).thenReturn(Optional.empty()); assertThrows(ReviewNotFoundException.class, - () -> reviewUseCase.deleteReview(nonExistentAvisId)); + () -> reviewUseCase.deleteReview(nonExistentReviewId)); - verify(reviewRepository, times(1)).findByAvisId(nonExistentAvisId); + verify(reviewRepository, times(1)).findByReviewId(nonExistentReviewId); verify(reviewRepository, never()).delete(any(Review.class)); } } -- 2.54.0