forked from pierront/mylibrary-template
Compare commits
2 Commits
6e2bf9aab9
...
90929b7bdd
| Author | SHA1 | Date | |
|---|---|---|---|
| 90929b7bdd | |||
| 62b4a30add |
+68
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
+294
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user