forked from pierront/mylibrary-template
Feature/manage reviews #2
@@ -10,6 +10,7 @@ import java.util.UUID;
|
||||
@Builder
|
||||
|
||||
public class ReviewDTO {
|
||||
private UUID avisId;
|
||||
private UUID customerId;
|
||||
private UUID bookId;
|
||||
private Integer note;
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
+6
-6
@@ -22,8 +22,8 @@ public class ReviewRepository {
|
||||
}
|
||||
|
||||
public Review save(Review newReview) {
|
||||
Optional<Review> optionalReviewWithSameCustomerAndBookId = this.findByCustomerAndBookId(newReview.getCustomerId(), newReview.getBookId());
|
||||
optionalReviewWithSameCustomerAndBookId.ifPresent(reviews::remove);
|
||||
Optional<Review> 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<Review> findByCustomerAndBookId(UUID customerUUID, UUID bookUUID) {
|
||||
public Optional<Review> 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) {
|
||||
|
||||
+1
@@ -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)
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
+24
-22
@@ -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<Review> foundreview = repository.findByCustomerAndBookId(review1.getCustomerId(), review1.getBookId());
|
||||
@DisplayName("findByAvisId should return review with matching avis ID")
|
||||
void testFindByAvisId() {
|
||||
Optional<Review> 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<Review> foundreview = repository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
Optional<Review> 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<Review> 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));
|
||||
|
||||
Reference in New Issue
Block a user