Tout review test et code

This commit is contained in:
Kroccmou
2025-06-14 01:06:30 +02:00
parent 554f6f1661
commit 9df099691e
22 changed files with 624 additions and 8 deletions

View File

@@ -0,0 +1,15 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review;
import lombok.Builder;
import lombok.Getter;
import java.util.UUID;
@Builder
@Getter
public class ReviewDto {
private UUID reviewId;
private long bookId;
private String customerName;
private String comment;
private int rating;
}

View File

@@ -0,0 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review;
import lombok.Builder;
import lombok.Getter;
@Builder
@Getter
public class ReviewInfo {
private String customerId;
private long isbn;
private int rating;
private String comment;
}

View File

@@ -0,0 +1,31 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.converter;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDto;
import lombok.experimental.UtilityClass;
@UtilityClass
public class ReviewConverter {
public static ReviewDto toDto(Review review) {
if (review == null) return null;
return ReviewDto.builder()
.reviewId(review.getReviewId())
.bookId(review.getBookId())
.customerName(review.getCustomerName())
.comment(review.getComment())
.rating(review.getRating())
.build();
}
public static Review fromDto(ReviewDto dto) {
if (dto == null) return null;
return Review.builder()
.reviewId(dto.getReviewId())
.bookId(dto.getBookId())
.customerName(dto.getCustomerName())
.comment(dto.getComment())
.rating(dto.getRating())
.build();
}
}

View File

@@ -0,0 +1,29 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.entity;
import lombok.Builder;
import lombok.Getter;
import java.util.Objects;
import java.util.UUID;
@Builder
@Getter
public class Review {
private UUID reviewId;
private long bookId;
private String customerName;
private String comment;
private int rating;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Review)) return false;
Review review = (Review) o;
return Objects.equals(reviewId, review.reviewId);
}
@Override
public int hashCode() {
return Objects.hash(reviewId);
}
}

View File

@@ -0,0 +1,10 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
public class DuplicateReviewException extends RuntimeException {
public static final String REVIEW_ALREADY_EXISTS = "A review already exists for customer %s and book %s";
public DuplicateReviewException(String customerId, long bookId) {
super(String.format(REVIEW_ALREADY_EXISTS, customerId, String.valueOf(bookId)));
}
}

View File

@@ -0,0 +1,8 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
public class InvalidReviewException extends RuntimeException {
public InvalidReviewException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,10 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
public class ReviewNotAllowedException extends RuntimeException {
public static final String REVIEW_NOT_ALLOWED = "Customer %s is not allowed to review book %s";
public ReviewNotAllowedException(String customerId, long bookId) {
super(String.format(REVIEW_NOT_ALLOWED, customerId, String.valueOf(bookId)));
}
}

View File

@@ -0,0 +1,12 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
import java.text.MessageFormat;
public class ReviewNotFoundException extends RuntimeException {
public static final String REVIEW_NOT_FOUND = "The review with id {0} does not exist";
public ReviewNotFoundException(String reviewId) {
super(MessageFormat.format(REVIEW_NOT_FOUND, reviewId));
}
}

View File

@@ -0,0 +1,55 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.repository;
import java.util.*;
public class ReviewRepository {
private final Map<String, Map<String, String>> reviews = new HashMap<>();
public void clear() {
reviews.clear();
}
public void save(Map<String, String> review) {
reviews.put(review.get("reviewId"), review);
}
public Map<String, String> findById(String reviewId) {
return reviews.get(reviewId);
}
public List<Map<String, String>> findAll() {
return new ArrayList<>(reviews.values());
}
public void deleteById(String reviewId) {
reviews.remove(reviewId);
}
public int count() {
return reviews.size();
}
public boolean existsById(String reviewId) {
return reviews.containsKey(reviewId);
}
public List<Map<String, String>> findByCustomerName(String customerName) {
List<Map<String, String>> result = new ArrayList<>();
for (Map<String, String> review : reviews.values()) {
if (review.get("customerName").equals(customerName)) {
result.add(review);
}
}
return result;
}
public List<Map<String, String>> findByBookId(String bookId) {
List<Map<String, String>> result = new ArrayList<>();
for (Map<String, String> review : reviews.values()) {
if (review.get("bookId").equals(bookId)) {
result.add(review);
}
}
return result;
}
}

View File

@@ -0,0 +1,53 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.review.validator.ReviewValidator;
import java.util.*;
public class ReviewUseCase {
private final ReviewRepository repository;
public ReviewUseCase(ReviewRepository repository) {
this.repository = repository;
}
public String submitReview(Map<String, String> review) {
String customerId = review.get("customerId");
String isbn = review.get("isbn");
String rating = review.get("rating");
if (!ReviewValidator.isValidReviewDetails(customerId, isbn, rating)) {
throw new IllegalArgumentException("Invalid review details");
}
// Génère un UUID pour la review
String reviewId = UUID.randomUUID().toString();
review.put("reviewId", reviewId);
repository.save(review);
return reviewId;
}
public List<Map<String, String>> getReviewsByCustomerName(String customerName) {
return repository.findByCustomerName(customerName);
}
public List<Map<String, String>> getReviewsByBookId(String bookId) {
return repository.findByBookId(bookId);
}
public void deleteReview(String reviewId) {
if (!ReviewValidator.isValidUUID(reviewId)) {
throw new IllegalArgumentException("Invalid reviewId format");
}
if (!repository.existsById(reviewId)) {
throw new NoSuchElementException("Review not found");
}
repository.deleteById(reviewId);
}
public int countReviews() {
return repository.count();
}
}

View File

@@ -0,0 +1,22 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
import java.util.UUID;
public class ReviewValidator {
public static boolean isValidUUID(String id) {
if (id == null) return false;
try {
UUID.fromString(id);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
public static boolean isValidReviewDetails(String customerId, String isbn, String rating) {
return customerId != null && !customerId.isBlank()
&& isbn != null && !isbn.isBlank()
&& rating != null && !rating.isBlank();
}
}

View File

@@ -9,6 +9,7 @@ import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import java.util.*;
import java.util.UUID;
public class ReviewSteps {
@@ -43,7 +44,10 @@ public class ReviewSteps {
public void theReviewSystemHasTheFollowingReviews(DataTable dataTable) {
reviews.clear();
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
reviews.put(row.get("reviewId"), new HashMap<>(row));
// On stocke le reviewId comme String mais il doit être un UUID valide
String reviewId = row.get("reviewId");
UUID.fromString(reviewId); // Valide que c'est bien un UUID
reviews.put(reviewId, new HashMap<>(row));
}
assertEquals(dataTable.asMaps().size(), reviews.size());
}
@@ -91,7 +95,7 @@ public class ReviewSteps {
lastReviewError = "customer hasn't purchased the book";
return;
}
String newReviewId = "rev-" + (reviews.size() + 1);
String newReviewId = UUID.randomUUID().toString();
Map<String, String> review = new HashMap<>();
review.put("reviewId", newReviewId);
review.put("bookId", isbnStr);
@@ -193,6 +197,13 @@ public class ReviewSteps {
@When("I try to delete the review with id {string}")
public void iTryToDeleteTheReviewWithId(String reviewId) {
try {
UUID.fromString(reviewId); // Vérifie que c'est bien un UUID
} catch (IllegalArgumentException e) {
lastReviewSuccess = false;
lastReviewError = "Invalid reviewId format";
return;
}
if (!reviews.containsKey(reviewId)) {
lastReviewSuccess = false;
lastReviewError = "Review not found";

View File

@@ -0,0 +1,64 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.converter;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDto;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
class ReviewConverterTest {
@Test
void toDtoShouldConvertReviewToDto() {
UUID uuid = UUID.randomUUID();
Review review = Review.builder()
.reviewId(uuid)
.bookId(978333333L)
.customerName("Carol White")
.comment("Great book!")
.rating(5)
.build();
ReviewDto dto = ReviewConverter.toDto(review);
assertNotNull(dto);
assertEquals(uuid, dto.getReviewId());
assertEquals(978333333L, dto.getBookId());
assertEquals("Carol White", dto.getCustomerName());
assertEquals("Great book!", dto.getComment());
assertEquals(5, dto.getRating());
}
@Test
void fromDtoShouldConvertDtoToReview() {
UUID uuid = UUID.randomUUID();
ReviewDto dto = ReviewDto.builder()
.reviewId(uuid)
.bookId(978333333L)
.customerName("Carol White")
.comment("Great book!")
.rating(5)
.build();
Review review = ReviewConverter.fromDto(dto);
assertNotNull(review);
assertEquals(uuid, review.getReviewId());
assertEquals(978333333L, review.getBookId());
assertEquals("Carol White", review.getCustomerName());
assertEquals("Great book!", review.getComment());
assertEquals(5, review.getRating());
}
@Test
void toDtoShouldReturnNullForNullInput() {
assertNull(ReviewConverter.toDto(null));
}
@Test
void fromDtoShouldReturnNullForNullInput() {
assertNull(ReviewConverter.fromDto(null));
}
}

View File

@@ -0,0 +1,42 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.entity;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
class ReviewTest {
@Test
void builderShouldCreateValidReview() {
UUID uuid = UUID.randomUUID();
Review review = Review.builder()
.reviewId(uuid)
.bookId(978333333L)
.customerName("Carol White")
.comment("Great book!")
.rating(5)
.build();
assertEquals(uuid, review.getReviewId());
assertEquals(978333333L, review.getBookId());
assertEquals("Carol White", review.getCustomerName());
assertEquals("Great book!", review.getComment());
assertEquals(5, review.getRating());
}
@Test
void reviewsWithSameIdShouldBeEqual() {
UUID uuid = UUID.randomUUID();
Review r1 = Review.builder().reviewId(uuid).build();
Review r2 = Review.builder().reviewId(uuid).build();
assertEquals(r1, r2);
assertEquals(r1.hashCode(), r2.hashCode());
}
@Test
void reviewsWithDifferentIdShouldNotBeEqual() {
Review r1 = Review.builder().reviewId(UUID.randomUUID()).build();
Review r2 = Review.builder().reviewId(UUID.randomUUID()).build();
assertNotEquals(r1, r2);
}
}

View File

@@ -0,0 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class DuplicateReviewExceptionTest {
@Test
void testFormattedMessage() {
DuplicateReviewException ex = new DuplicateReviewException("c1", 1234567890123L);
assertEquals("A review already exists for customer c1 and book 1234567890123", ex.getMessage());
}
}

View File

@@ -0,0 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class InvalidReviewExceptionTest {
@Test
void testMessage() {
InvalidReviewException ex = new InvalidReviewException("Invalid review details");
assertEquals("Invalid review details", ex.getMessage());
}
}

View File

@@ -0,0 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ReviewNotAllowedExceptionTest {
@Test
void testFormattedMessage() {
ReviewNotAllowedException ex = new ReviewNotAllowedException("c2", 9876543210123L);
assertEquals("Customer c2 is not allowed to review book 9876543210123", ex.getMessage());
}
}

View File

@@ -0,0 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ReviewNotFoundExceptionTest {
@Test
void testFormattedMessage() {
ReviewNotFoundException ex = new ReviewNotFoundException("rev-42");
assertEquals("The review with id rev-42 does not exist", ex.getMessage());
}
}

View File

@@ -0,0 +1,72 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.repository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
public class ReviewRepositoryTest {
private ReviewRepository repository;
@BeforeEach
void setUp() {
repository = new ReviewRepository();
}
@Test
void testSaveAndFindById() {
Map<String, String> review = new HashMap<>();
review.put("reviewId", "11111111-1111-1111-1111-111111111111");
review.put("customerName", "Alice Smith");
review.put("bookId", "978333333");
repository.save(review);
Map<String, String> found = repository.findById("11111111-1111-1111-1111-111111111111");
assertNotNull(found);
assertEquals("Alice Smith", found.get("customerName"));
}
@Test
void testDeleteById() {
Map<String, String> review = new HashMap<>();
review.put("reviewId", "22222222-2222-2222-2222-222222222222");
repository.save(review);
assertTrue(repository.existsById("22222222-2222-2222-2222-222222222222"));
repository.deleteById("22222222-2222-2222-2222-222222222222");
assertFalse(repository.existsById("22222222-2222-2222-2222-222222222222"));
}
@Test
void testFindByCustomerName() {
Map<String, String> review1 = new HashMap<>();
review1.put("reviewId", "1");
review1.put("customerName", "Bob");
Map<String, String> review2 = new HashMap<>();
review2.put("reviewId", "2");
review2.put("customerName", "Alice");
repository.save(review1);
repository.save(review2);
List<Map<String, String>> found = repository.findByCustomerName("Alice");
assertEquals(1, found.size());
assertEquals("2", found.get(0).get("reviewId"));
}
@Test
void testFindByBookId() {
Map<String, String> review1 = new HashMap<>();
review1.put("reviewId", "1");
review1.put("bookId", "A");
Map<String, String> review2 = new HashMap<>();
review2.put("reviewId", "2");
review2.put("bookId", "B");
repository.save(review1);
repository.save(review2);
List<Map<String, String>> found = repository.findByBookId("A");
assertEquals(1, found.size());
assertEquals("1", found.get(0).get("reviewId"));
}
}

View File

@@ -0,0 +1,91 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
public class ReviewUseCaseTest {
private ReviewRepository repository;
private ReviewUseCase useCase;
@BeforeEach
void setUp() {
repository = new ReviewRepository();
useCase = new ReviewUseCase(repository);
}
@Test
void testSubmitReviewAndCount() {
Map<String, String> review = new HashMap<>();
review.put("customerId", "c1");
review.put("isbn", "978333333");
review.put("rating", "5");
review.put("customerName", "Alice Smith");
review.put("comment", "Super livre !");
String reviewId = useCase.submitReview(review);
assertNotNull(reviewId);
assertEquals(1, useCase.countReviews());
}
@Test
void testSubmitReviewInvalidDetails() {
Map<String, String> review = new HashMap<>();
review.put("customerId", "");
review.put("isbn", "978333333");
review.put("rating", "5");
assertThrows(IllegalArgumentException.class, () -> useCase.submitReview(review));
}
@Test
void testDeleteReview() {
Map<String, String> review = new HashMap<>();
review.put("customerId", "c1");
review.put("isbn", "978333333");
review.put("rating", "5");
review.put("customerName", "Alice Smith");
review.put("comment", "Super livre !");
String reviewId = useCase.submitReview(review);
assertEquals(1, useCase.countReviews());
useCase.deleteReview(reviewId);
assertEquals(0, useCase.countReviews());
}
@Test
void testDeleteReviewInvalidUUID() {
assertThrows(IllegalArgumentException.class, () -> useCase.deleteReview("not-a-uuid"));
}
@Test
void testDeleteReviewNotFound() {
String uuid = UUID.randomUUID().toString();
assertThrows(NoSuchElementException.class, () -> useCase.deleteReview(uuid));
}
@Test
void testGetReviewsByCustomerName() {
Map<String, String> review1 = new HashMap<>();
review1.put("customerId", "c1");
review1.put("isbn", "978333333");
review1.put("rating", "5");
review1.put("customerName", "Alice Smith");
review1.put("comment", "Super livre !");
useCase.submitReview(review1);
Map<String, String> review2 = new HashMap<>();
review2.put("customerId", "c2");
review2.put("isbn", "978444444");
review2.put("rating", "4");
review2.put("customerName", "Bob White");
review2.put("comment", "Bien !");
useCase.submitReview(review2);
List<Map<String, String>> aliceReviews = useCase.getReviewsByCustomerName("Alice Smith");
assertEquals(1, aliceReviews.size());
assertEquals("Alice Smith", aliceReviews.get(0).get("customerName"));
}
}

View File

@@ -0,0 +1,26 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ReviewValidatorTest {
@Test
void testIsValidUUID() {
assertTrue(ReviewValidator.isValidUUID("11111111-1111-1111-1111-111111111111"));
assertFalse(ReviewValidator.isValidUUID("not-a-uuid"));
assertFalse(ReviewValidator.isValidUUID(""));
assertFalse(ReviewValidator.isValidUUID(null));
}
@Test
void testIsValidReviewDetails() {
assertTrue(ReviewValidator.isValidReviewDetails("id", "isbn", "5"));
assertFalse(ReviewValidator.isValidReviewDetails("", "isbn", "5"));
assertFalse(ReviewValidator.isValidReviewDetails("id", "", "5"));
assertFalse(ReviewValidator.isValidReviewDetails("id", "isbn", ""));
assertFalse(ReviewValidator.isValidReviewDetails(null, "isbn", "5"));
assertFalse(ReviewValidator.isValidReviewDetails("id", null, "5"));
assertFalse(ReviewValidator.isValidReviewDetails("id", "isbn", null));
}
}

View File

@@ -13,7 +13,7 @@ Feature: Manage reviews
| 978555555 | Book D | Author D | PubD | 2022-03-10 | 25.0 | 2 | DE |
And the review system has the following reviews:
| reviewId | bookId | customerName | comment | rating |
| rev-1 | 978333333 | Carol White | Great book! | 5 |
| 11111111-1111-1111-1111-111111111111 | 978333333 | Carol White | Great book! | 5 |
And the review system has the following purchases:
| customerId | bookId | purchaseDate |
| 33333333-3333-3333-3333-333333333333 | 978333333 | 2021-01-01 |
@@ -30,7 +30,7 @@ Feature: Manage reviews
When I request all reviews by customer "33333333-3333-3333-3333-333333333333"
Then I receive the following reviews:
| reviewId | bookId | customerName | comment | rating |
| rev-1 | 978333333 | Carol White | Great book! | 5 |
| 11111111-1111-1111-1111-111111111111 | 978333333 | Carol White | Great book! | 5 |
Scenario: Attempt to submit a review for a book not purchased
When I try to submit a new review with the following information:
@@ -75,6 +75,6 @@ Feature: Manage reviews
And I receive a review error message containing "Book or customer not found"
Scenario: Attempt to delete a review with unknown ID
When I try to delete the review with id "unknown-review-id"
When I try to delete the review with id "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
Then the review deletion fails
And I receive a review error message containing "Review not found"