création des test sur la BD imaginaire

This commit is contained in:
2026-06-11 18:59:29 +02:00
parent 6e2bf9aab9
commit 62b4a30add
@@ -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<Review> 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<Review> 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<Review> 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<Review> 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<Review> foundreview = repository.findByCustomerId(nonExistentCustomerId);
assertTrue(foundreview.isEmpty());
}
@Test
@DisplayName("findByBookId should return review with matching book ID")
void testFindByBookId() {
Optional<Review> 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<Review> foundreview = repository.findByBookId(nonExistentBookId);
assertTrue(foundreview.isEmpty());
}
@Test
@DisplayName("findByCustomerAndBookId should return review with matching customer and book ID")
void testFindByCustomerAndBookId() {
Optional<Review> 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<Review> 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<Review> 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<Review> 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());
}
}
}