oublie de la variable output avisId

This commit is contained in:
2026-06-11 23:20:09 +02:00
parent a4808a28c9
commit 1d066a802f
7 changed files with 42 additions and 28 deletions
@@ -10,6 +10,7 @@ import java.util.UUID;
@Builder @Builder
public class ReviewDTO { public class ReviewDTO {
private UUID avisId;
private UUID customerId; private UUID customerId;
private UUID bookId; private UUID bookId;
private Integer note; private Integer note;
@@ -1,6 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review; package fr.iut_fbleau.but3.dev62.mylibrary.review;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.UUID;
public record ReviewInfo(Integer note, String comment, LocalDate purchaseDate) { public record ReviewInfo(Integer note, String comment, LocalDate purchaseDate) {
} }
@@ -10,6 +10,7 @@ import java.util.UUID;
@Builder @Builder
public class Review { public class Review {
private UUID avisId;
private UUID customerId; private UUID customerId;
private UUID bookId; private UUID bookId;
private Integer note; private Integer note;
@@ -17,6 +18,10 @@ public class Review {
private LocalDate purchaseDate; private LocalDate purchaseDate;
public void setRandomUUID() { public void setRandomUUID() {
this.avisId = UUID.randomUUID();
}
public void setRandomUUIDCustomerAndBook() {
this.customerId = UUID.randomUUID(); this.customerId = UUID.randomUUID();
this.bookId = UUID.randomUUID(); this.bookId = UUID.randomUUID();
@@ -22,8 +22,8 @@ public class ReviewRepository {
} }
public Review save(Review newReview) { public Review save(Review newReview) {
Optional<Review> optionalReviewWithSameCustomerAndBookId = this.findByCustomerAndBookId(newReview.getCustomerId(), newReview.getBookId()); Optional<Review> optionalReviewWithSameAvisId = this.findByAvisId(newReview.getAvisId());
optionalReviewWithSameCustomerAndBookId.ifPresent(reviews::remove); optionalReviewWithSameAvisId.ifPresent(reviews::remove);
this.reviews.add(newReview); this.reviews.add(newReview);
return newReview; return newReview;
} }
@@ -40,9 +40,9 @@ public class ReviewRepository {
.findFirst(); .findFirst();
} }
public Optional<Review> findByCustomerAndBookId(UUID customerUUID, UUID bookUUID) { public Optional<Review> findByAvisId(UUID avisUUID) {
return this.reviews.stream() return this.reviews.stream()
.filter(review -> review.getCustomerId().equals(customerUUID) && review.getBookId().equals(bookUUID)) .filter(review -> review.getAvisId().equals(avisUUID))
.findFirst(); .findFirst();
} }
@@ -56,9 +56,9 @@ public class ReviewRepository {
.anyMatch(review -> review.getBookId().equals(bookUUID)); .anyMatch(review -> review.getBookId().equals(bookUUID));
} }
public boolean existsByCustomerAndBookId(UUID customerUUID, UUID bookUUID) { public boolean existsByAvisId(UUID avisUUID) {
return this.reviews.stream() 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) { public void deleteCustomerReviews(UUID customerUUID) {
@@ -47,6 +47,7 @@ public class ReviewConverterTest {
void shouldConvertReviewToDTO() { void shouldConvertReviewToDTO() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24); LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
Review review = Review.builder() Review review = Review.builder()
.avisId(UUID.randomUUID())
.customerId(UUID.randomUUID()) .customerId(UUID.randomUUID())
.bookId(UUID.randomUUID()) .bookId(UUID.randomUUID())
.note(5) .note(5)
@@ -38,13 +38,17 @@ public class ReviewTest {
@DisplayName("setRandomUUID should change the ID to a new random UUID") @DisplayName("setRandomUUID should change the ID to a new random UUID")
void testSetRandomUUID() { void testSetRandomUUID() {
Review review = Review.builder().build(); Review review = Review.builder().build();
UUID originalAvisId = review.getAvisId();
UUID originalCustomerId = review.getCustomerId(); UUID originalCustomerId = review.getCustomerId();
UUID originalBookId = review.getCustomerId(); UUID originalBookId = review.getCustomerId();
review.setRandomUUID(); review.setRandomUUID();
review.setRandomUUIDCustomerAndBook();
assertNotNull(review.getAvisId());
assertNotNull(review.getCustomerId()); assertNotNull(review.getCustomerId());
assertNotNull(review.getBookId()); assertNotNull(review.getBookId());
assertNotEquals(originalAvisId, review.getAvisId());
assertNotEquals(originalCustomerId, review.getCustomerId()); assertNotEquals(originalCustomerId, review.getCustomerId());
assertNotEquals(originalBookId, review.getBookId()); assertNotEquals(originalBookId, review.getBookId());
} }
@@ -35,6 +35,7 @@ public class ReviewRepositoryTest {
.purchaseDate(purchaseDate) .purchaseDate(purchaseDate)
.build(); .build();
review1.setRandomUUID(); review1.setRandomUUID();
review1.setRandomUUIDCustomerAndBook();
UUID customerId = UUID.randomUUID(); UUID customerId = UUID.randomUUID();
UUID bookId = UUID.randomUUID(); UUID bookId = UUID.randomUUID();
@@ -45,6 +46,7 @@ public class ReviewRepositoryTest {
.comment("nul") .comment("nul")
.purchaseDate(purchaseDate) .purchaseDate(purchaseDate)
.build(); .build();
review2.setRandomUUID();
UUID bookId3 = UUID.randomUUID(); UUID bookId3 = UUID.randomUUID();
review3 = Review.builder() review3 = Review.builder()
@@ -54,6 +56,7 @@ public class ReviewRepositoryTest {
.comment("ça passe") .comment("ça passe")
.purchaseDate(purchaseDate) .purchaseDate(purchaseDate)
.build(); .build();
review3.setRandomUUID();
UUID customerId4 = UUID.randomUUID(); UUID customerId4 = UUID.randomUUID();
review4 = Review.builder() review4 = Review.builder()
@@ -63,6 +66,7 @@ public class ReviewRepositoryTest {
.comment("ça passe") .comment("ça passe")
.purchaseDate(purchaseDate) .purchaseDate(purchaseDate)
.build(); .build();
review4.setRandomUUID();
} }
@Test @Test
@@ -94,9 +98,11 @@ public class ReviewRepositoryTest {
repository.save(review1); repository.save(review1);
LocalDate purchaseDate = LocalDate.of(2026, 5, 24); LocalDate purchaseDate = LocalDate.of(2026, 5, 24);
UUID customerId = review1.getCustomerId(); UUID avisId = review1.getAvisId();
UUID bookId = review1.getBookId(); UUID customerId = UUID.randomUUID();
UUID bookId = UUID.randomUUID();
Review updatedReview = Review.builder() Review updatedReview = Review.builder()
.avisId(avisId)
.customerId(customerId) .customerId(customerId)
.bookId(bookId) .bookId(bookId)
.note(4) .note(4)
@@ -107,6 +113,7 @@ public class ReviewRepositoryTest {
Review savedReview = repository.save(updatedReview); Review savedReview = repository.save(updatedReview);
assertEquals(1, repository.findAll().size()); assertEquals(1, repository.findAll().size());
assertEquals(avisId, savedReview.getAvisId());
assertEquals(customerId, savedReview.getCustomerId()); assertEquals(customerId, savedReview.getCustomerId());
assertEquals(bookId, savedReview.getBookId()); assertEquals(bookId, savedReview.getBookId());
assertEquals(4, savedReview.getNote()); assertEquals(4, savedReview.getNote());
@@ -189,9 +196,9 @@ public class ReviewRepositoryTest {
} }
@Test @Test
@DisplayName("findByCustomerAndBookId should return review with matching customer and book ID") @DisplayName("findByAvisId should return review with matching avis ID")
void testFindByCustomerAndBookId() { void testFindByAvisId() {
Optional<Review> foundreview = repository.findByCustomerAndBookId(review1.getCustomerId(), review1.getBookId()); Optional<Review> foundreview = repository.findByAvisId(review1.getAvisId());
assertTrue(foundreview.isPresent()); assertTrue(foundreview.isPresent());
assertEquals(review1.getNote(), foundreview.get().getNote()); assertEquals(review1.getNote(), foundreview.get().getNote());
@@ -199,12 +206,11 @@ public class ReviewRepositoryTest {
} }
@Test @Test
@DisplayName("findByCustomerAndBookId should return empty Optional when a review with customer and book ID doesn't exist") @DisplayName("findByAvisId should return empty Optional when a review with avis ID doesn't exist")
void testFindByCustomerAndBookIdNotFound() { void testFindByAvisIdNotFound() {
UUID nonExistentCustomerId = UUID.randomUUID(); UUID nonExistentAvisId = UUID.randomUUID();
UUID nonExistentBookId = UUID.randomUUID();
Optional<Review> foundreview = repository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); Optional<Review> foundreview = repository.findByAvisId(nonExistentAvisId);
assertTrue(foundreview.isEmpty()); assertTrue(foundreview.isEmpty());
} }
@@ -246,20 +252,19 @@ public class ReviewRepositoryTest {
} }
@Test @Test
@DisplayName("existsByCustomerAndBookId should return true when a review with customer and book ID exists") @DisplayName("existsByAvisId should return true when a review with avis ID exists")
void testExistsByCustomerAndBookIdExists() { void testExistsByAvisIdExists() {
boolean exists = repository.existsByCustomerAndBookId(review1.getCustomerId(), review1.getBookId()); boolean exists = repository.existsByAvisId(review1.getAvisId());
assertTrue(exists); assertTrue(exists);
} }
@Test @Test
@DisplayName("existsByCustomerAndBookId should return false when customer and book ID doesn't exist") @DisplayName("existsByAvisId should return false when avis ID doesn't exist")
void testExistsByCustomerAndBookIdNotExists() { void testExistsByAvisIdNotExists() {
UUID nonExistentCustomerId = UUID.randomUUID(); UUID nonExistentAvisId = UUID.randomUUID();
UUID nonExistentBookId = UUID.randomUUID();
boolean exists = repository.existsByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); boolean exists = repository.existsByAvisId(nonExistentAvisId);
assertFalse(exists); assertFalse(exists);
} }
@@ -284,10 +289,7 @@ public class ReviewRepositoryTest {
List<Review> reviews = repository.findAll(); List<Review> reviews = repository.findAll();
System.out.println(review2.getCustomerId()); assertEquals(2, reviews.size());
System.out.println(review3.getCustomerId());
/*assertEquals(2, reviews.size());*/
assertTrue(reviews.contains(review1)); assertTrue(reviews.contains(review1));
assertFalse(reviews.contains(review2)); assertFalse(reviews.contains(review2));
assertFalse(reviews.contains(review3)); assertFalse(reviews.contains(review3));