Compare commits

..

3 Commits

2 changed files with 72 additions and 4 deletions
@@ -23,7 +23,7 @@ public class ReviewRepository {
public Review save(Review newReview) {
Optional<Review> 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);
@@ -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,39 @@ 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<Review> reviews = repository.findAll();
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));
assertTrue(reviews.contains(review4));
}
@Test
@DisplayName("Delete should remove all reviews of a book")
void testDeleteBookReviews() {
repository.deleteBookReviews(review2.getBookId());
List<Review> reviews = repository.findAll();
assertEquals(2, reviews.size());
assertTrue(reviews.contains(review1));
assertFalse(reviews.contains(review2));
assertTrue(reviews.contains(review3));
assertFalse(reviews.contains(review4));
}
@Test
@@ -259,9 +315,11 @@ public class ReviewRepositoryTest {
List<Review> reviews = repository.findAll();
assertEquals(1, reviews.size());
assertEquals(3, reviews.size());
assertFalse(reviews.contains(review1));
assertTrue(reviews.contains(review2));
assertTrue(reviews.contains(review3));
assertTrue(reviews.contains(review4));
}
@Test
@@ -288,7 +346,7 @@ public class ReviewRepositoryTest {
assertDoesNotThrow(() -> repository.delete(nonExistentReview));
assertEquals(2, repository.findAll().size());
assertEquals(4, repository.findAll().size());
}
}
}