réussite des test sur la BD imaginaire

This commit is contained in:
2026-06-11 19:14:08 +02:00
parent 62b4a30add
commit 90929b7bdd
2 changed files with 80 additions and 12 deletions
@@ -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<Review> reviews = new ArrayList<>();
public List<Review> findAll() {
return reviews;
}
public void deleteAll() {
reviews.clear();
}
public Review save(Review newReview) {
Optional<Review> optionalReviewWithSameCustomerAndBookId = this.findByCustomerAndBookId(newReview.getCustomerId(), newReview.getBookId());
optionalReviewWithSameCustomerAndBookId.ifPresentOrElse(reviews::remove, newReview::setRandomUUID);
this.reviews.add(newReview);
return newReview;
}
public Optional<Review> findByCustomerId(UUID customerUUID) {
return this.reviews.stream()
.filter(review -> review.getCustomerId().equals(customerUUID))
.findFirst();
}
public Optional<Review> findByBookId(UUID bookUUID) {
return this.reviews.stream()
.filter(review -> review.getBookId().equals(bookUUID))
.findFirst();
}
public Optional<Review> 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);
}
}
@@ -187,56 +187,56 @@ public class ReviewRepositoryTest {
} }
@Test @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() { void testExistsByCustomerIdExists() {
boolean exists = repository.ExistsByCustomerId(review1.getCustomerId()); boolean exists = repository.existsByCustomerId(review1.getCustomerId());
assertTrue(exists); assertTrue(exists);
} }
@Test @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() { void testExistsByCustomerIdNotExists() {
UUID nonExistentCustomerId = UUID.randomUUID(); UUID nonExistentCustomerId = UUID.randomUUID();
boolean exists = repository.ExistsByCustomerId(nonExistentCustomerId); boolean exists = repository.existsByCustomerId(nonExistentCustomerId);
assertFalse(exists); assertFalse(exists);
} }
@Test @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() { void testExistsByBookIdExists() {
boolean exists = repository.ExistsByBookId(review1.getBookId()); boolean exists = repository.existsByBookId(review1.getBookId());
assertTrue(exists); assertTrue(exists);
} }
@Test @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() { void testExistsByBookIdNotExists() {
UUID nonExistentBookId = UUID.randomUUID(); UUID nonExistentBookId = UUID.randomUUID();
boolean exists = repository.ExistsByBookId(nonExistentBookId); boolean exists = repository.existsByBookId(nonExistentBookId);
assertFalse(exists); assertFalse(exists);
} }
@Test @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() { void testExistsByCustomerAndBookIdExists() {
boolean exists = repository.ExistsByCustomerAndBookId(review1.getCustomerId(), review1.getBookId()); boolean exists = repository.existsByCustomerAndBookId(review1.getCustomerId(), review1.getBookId());
assertTrue(exists); assertTrue(exists);
} }
@Test @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() { void testExistsByCustomerAndBookIdNotExists() {
UUID nonExistentCustomerId = UUID.randomUUID(); UUID nonExistentCustomerId = UUID.randomUUID();
UUID nonExistentBookId = UUID.randomUUID(); UUID nonExistentBookId = UUID.randomUUID();
boolean exists = repository.ExistsByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId); boolean exists = repository.existsByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
assertFalse(exists); assertFalse(exists);
} }