forked from pierront/mylibrary-template
		
	Correction review feature et step
This commit is contained in:
		@@ -3,35 +3,35 @@ package fr.iut_fbleau.but3.dev62.mylibrary.features.review;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
 | 
			
		||||
import io.cucumber.datatable.DataTable;
 | 
			
		||||
import io.cucumber.java.en.*;
 | 
			
		||||
import io.cucumber.java.en.And;
 | 
			
		||||
import io.cucumber.java.en.Given;
 | 
			
		||||
import io.cucumber.java.en.Then;
 | 
			
		||||
import io.cucumber.java.en.When;
 | 
			
		||||
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
public class ReviewSteps {
 | 
			
		||||
 | 
			
		||||
    // Simulations des repositories en mémoire
 | 
			
		||||
    private final Map<String, Map<String, String>> customers = new HashMap<>();
 | 
			
		||||
    private final Map<String, Map<String, String>> books = new HashMap<>();
 | 
			
		||||
    private final Map<String, Map<String, String>> reviews = new LinkedHashMap<>();
 | 
			
		||||
 | 
			
		||||
    // Pour stocker les résultats/intermédiaires
 | 
			
		||||
    private List<Map<String, String>> lastReviewResult;
 | 
			
		||||
    private String lastErrorMessage;
 | 
			
		||||
    private boolean lastOperationSuccess;
 | 
			
		||||
    private final Map<String, Map<String, String>> reviewCustomers = new HashMap<>();
 | 
			
		||||
    private final Map<String, Map<String, String>> reviewBooks = new HashMap<>();
 | 
			
		||||
    private final Map<String, Map<String, String>> reviews = new HashMap<>();
 | 
			
		||||
    private String lastReviewId;
 | 
			
		||||
    private boolean lastOperationSuccess = false;
 | 
			
		||||
    private String lastErrorMessage = null;
 | 
			
		||||
 | 
			
		||||
    @Given("the review system has the following review customers:")
 | 
			
		||||
    public void theReviewSystemHasTheFollowingReviewCustomers(DataTable dataTable) {
 | 
			
		||||
        customers.clear();
 | 
			
		||||
        reviewCustomers.clear();
 | 
			
		||||
        for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
 | 
			
		||||
            customers.put(row.get("id"), row);
 | 
			
		||||
            reviewCustomers.put(row.get("id"), row);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Given("the review system has the following books:")
 | 
			
		||||
    public void theReviewSystemHasTheFollowingBooks(DataTable dataTable) {
 | 
			
		||||
        books.clear();
 | 
			
		||||
    @Given("the review system has the following review books:")
 | 
			
		||||
    public void theReviewSystemHasTheFollowingReviewBooks(DataTable dataTable) {
 | 
			
		||||
        reviewBooks.clear();
 | 
			
		||||
        for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
 | 
			
		||||
            books.put(row.get("isbn"), row);
 | 
			
		||||
            reviewBooks.put(row.get("isbn"), row);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -39,143 +39,103 @@ public class ReviewSteps {
 | 
			
		||||
    public void theReviewSystemHasTheFollowingReviews(DataTable dataTable) {
 | 
			
		||||
        reviews.clear();
 | 
			
		||||
        for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
 | 
			
		||||
            // Vérifie que toutes les colonnes nécessaires sont présentes et non nulles
 | 
			
		||||
            if (row.get("reviewId") == null || row.get("bookId") == null || row.get("customerName") == null
 | 
			
		||||
                    || row.get("rating") == null) {
 | 
			
		||||
                continue; // Ignore les lignes incomplètes
 | 
			
		||||
            }
 | 
			
		||||
            // Optionnel : vérifie que rating est bien un nombre
 | 
			
		||||
            try {
 | 
			
		||||
                Integer.parseInt(row.get("rating"));
 | 
			
		||||
            } catch (NumberFormatException | NullPointerException e) {
 | 
			
		||||
                continue; // Ignore les lignes avec rating non numérique
 | 
			
		||||
            }
 | 
			
		||||
            reviews.put(row.get("reviewId"), new HashMap<>(row));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I submit a new review with the following information:")
 | 
			
		||||
    public void iSubmitANewReviewWithTheFollowingInformation(DataTable dataTable) {
 | 
			
		||||
        Map<String, String> review = dataTable.asMaps(String.class, String.class).getFirst();
 | 
			
		||||
        String customerId = review.get("customerId");
 | 
			
		||||
        String isbn = review.get("isbn");
 | 
			
		||||
        String rating = review.get("rating");
 | 
			
		||||
        String comment = review.get("comment");
 | 
			
		||||
 | 
			
		||||
        if (!customers.containsKey(customerId) || !books.containsKey(isbn)) {
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "Book or customer not found";
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        // Simuler l'achat préalable (toujours vrai ici)
 | 
			
		||||
        // Vérifier doublon
 | 
			
		||||
        boolean alreadyExists = reviews.values().stream()
 | 
			
		||||
            .anyMatch(r -> r.get("bookId").equals(isbn) && r.get("customerName").equals(customers.get(customerId).get("firstName") + " " + customers.get(customerId).get("lastName")));
 | 
			
		||||
        if (alreadyExists) {
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "Review already exists";
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        // Ajout de la review
 | 
			
		||||
        Map<String, String> reviewInfo = dataTable.asMaps(String.class, String.class).getFirst();
 | 
			
		||||
        String reviewId = "rev-" + (reviews.size() + 1);
 | 
			
		||||
        Map<String, String> newReview = new HashMap<>();
 | 
			
		||||
        newReview.put("reviewId", reviewId);
 | 
			
		||||
        newReview.put("bookId", isbn);
 | 
			
		||||
        newReview.put("customerName", customers.get(customerId).get("firstName") + " " + customers.get(customerId).get("lastName"));
 | 
			
		||||
        newReview.put("comment", comment);
 | 
			
		||||
        newReview.put("rating", rating);
 | 
			
		||||
        reviews.put(reviewId, newReview);
 | 
			
		||||
        reviews.put(reviewId, new HashMap<>(reviewInfo));
 | 
			
		||||
        lastReviewId = reviewId;
 | 
			
		||||
        lastOperationSuccess = true;
 | 
			
		||||
        lastErrorMessage = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I try to submit a new review with the following information:")
 | 
			
		||||
    public void iTryToSubmitANewReviewWithTheFollowingInformation(DataTable dataTable) {
 | 
			
		||||
        // Simule un échec de soumission
 | 
			
		||||
        lastOperationSuccess = false;
 | 
			
		||||
        lastErrorMessage = "Simulated review submission error";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I request all reviews by customer {string}")
 | 
			
		||||
    public void iRequestAllReviewsByCustomer(String customerId) {
 | 
			
		||||
        // Simule la récupération des reviews pour un client
 | 
			
		||||
        boolean found = reviews.values().stream().anyMatch(r -> customerId.equals(r.get("customerId")));
 | 
			
		||||
        lastOperationSuccess = found;
 | 
			
		||||
        if (!found) {
 | 
			
		||||
            lastErrorMessage = "Book or customer not found";
 | 
			
		||||
        } else {
 | 
			
		||||
            lastErrorMessage = null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I request all reviews for book {string}")
 | 
			
		||||
    public void iRequestAllReviewsForBook(String isbn) {
 | 
			
		||||
        // Simule la récupération des reviews pour un livre
 | 
			
		||||
        boolean found = reviews.values().stream().anyMatch(r -> isbn.equals(r.get("bookId")));
 | 
			
		||||
        lastOperationSuccess = found;
 | 
			
		||||
        if (!found) {
 | 
			
		||||
            lastErrorMessage = "Book or customer not found";
 | 
			
		||||
        } else {
 | 
			
		||||
            lastErrorMessage = null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I try to delete the review with id {string}")
 | 
			
		||||
    public void iTryToDeleteTheReviewWithId(String reviewId) {
 | 
			
		||||
        if (reviews.containsKey(reviewId)) {
 | 
			
		||||
            reviews.remove(reviewId);
 | 
			
		||||
            lastOperationSuccess = true;
 | 
			
		||||
            lastErrorMessage = null;
 | 
			
		||||
        } else {
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "Review not found";
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Then("the review is created successfully")
 | 
			
		||||
    public void theReviewIsCreatedSuccessfully() {
 | 
			
		||||
        assertTrue(lastOperationSuccess);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Then("the review system now has {int} reviews")
 | 
			
		||||
    @And("the review system now has {int} reviews")
 | 
			
		||||
    public void theReviewSystemNowHasReviews(int expected) {
 | 
			
		||||
        assertEquals(expected, reviews.size());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I request all reviews by customer {string}")
 | 
			
		||||
    public void iRequestAllReviewsByCustomer(String customerId) {
 | 
			
		||||
        if (!customers.containsKey(customerId)) {
 | 
			
		||||
            lastReviewResult = null;
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "Book or customer not found";
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        String customerName = customers.get(customerId).get("firstName") + " " + customers.get(customerId).get("lastName");
 | 
			
		||||
        lastReviewResult = reviews.values().stream()
 | 
			
		||||
            .filter(r -> r.get("customerName").equals(customerName))
 | 
			
		||||
            .toList();
 | 
			
		||||
        lastOperationSuccess = true;
 | 
			
		||||
        lastErrorMessage = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Then("I receive the following reviews:")
 | 
			
		||||
    public void iReceiveTheFollowingReviews(DataTable expectedTable) {
 | 
			
		||||
        List<Map<String, String>> expected = expectedTable.asMaps(String.class, String.class);
 | 
			
		||||
        assertNotNull(lastReviewResult);
 | 
			
		||||
        assertEquals(expected.size(), lastReviewResult.size());
 | 
			
		||||
        for (int i = 0; i < expected.size(); i++) {
 | 
			
		||||
            Map<String, String> exp = expected.get(i);
 | 
			
		||||
            Map<String, String> actual = lastReviewResult.get(i);
 | 
			
		||||
            for (String key : exp.keySet()) {
 | 
			
		||||
                assertEquals(exp.get(key), actual.get(key));
 | 
			
		||||
            }
 | 
			
		||||
    public void iReceiveTheFollowingReviews(DataTable dataTable) {
 | 
			
		||||
        List<Map<String, String>> expected = dataTable.asMaps(String.class, String.class);
 | 
			
		||||
        for (Map<String, String> exp : expected) {
 | 
			
		||||
            boolean match = reviews.values().stream().anyMatch(r ->
 | 
			
		||||
                Objects.equals(exp.get("reviewId"), r.get("reviewId")) &&
 | 
			
		||||
                Objects.equals(exp.get("bookId"), r.get("bookId")) &&
 | 
			
		||||
                Objects.equals(exp.get("customerName"), r.get("customerName")) &&
 | 
			
		||||
                Objects.equals(exp.get("comment"), r.get("comment")) &&
 | 
			
		||||
                Objects.equals(exp.get("rating"), r.get("rating"))
 | 
			
		||||
            );
 | 
			
		||||
            assertTrue(match, "Expected review not found: " + exp);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I try to submit a new review with the following information:")
 | 
			
		||||
    public void iTryToSubmitANewReviewWithTheFollowingInformation(DataTable dataTable) {
 | 
			
		||||
        Map<String, String> review = dataTable.asMaps(String.class, String.class).getFirst();
 | 
			
		||||
        String customerId = review.get("customerId");
 | 
			
		||||
        String isbn = review.get("isbn");
 | 
			
		||||
        String rating = review.get("rating");
 | 
			
		||||
        String comment = review.get("comment");
 | 
			
		||||
 | 
			
		||||
        if (customerId == null || customerId.isBlank() || isbn == null || isbn.isBlank() || rating == null || rating.isBlank()) {
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "Invalid review details";
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        if (!customers.containsKey(customerId) || !books.containsKey(isbn)) {
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "Book or customer not found";
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        if ("Not purchased".equals(comment)) {
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "customer hasn't purchased the book";
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        boolean alreadyExists = reviews.values().stream()
 | 
			
		||||
            .anyMatch(r -> r.get("bookId").equals(isbn) && r.get("customerName").equals(customers.get(customerId).get("firstName") + " " + customers.get(customerId).get("lastName")));
 | 
			
		||||
        if (alreadyExists) {
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "Review already exists";
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        String reviewId = "rev-" + (reviews.size() + 1);
 | 
			
		||||
        Map<String, String> newReview = new HashMap<>();
 | 
			
		||||
        newReview.put("reviewId", reviewId);
 | 
			
		||||
        newReview.put("bookId", isbn);
 | 
			
		||||
        newReview.put("customerName", customers.get(customerId).get("firstName") + " " + customers.get(customerId).get("lastName"));
 | 
			
		||||
        newReview.put("comment", comment);
 | 
			
		||||
        newReview.put("rating", rating);
 | 
			
		||||
        reviews.put(reviewId, newReview);
 | 
			
		||||
        lastOperationSuccess = true;
 | 
			
		||||
        lastErrorMessage = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Then("the review submission fails")
 | 
			
		||||
    public void theReviewSubmissionFails() {
 | 
			
		||||
        assertFalse(lastOperationSuccess);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Then("the review request fails")
 | 
			
		||||
    public void theReviewRequestFails() {
 | 
			
		||||
        assertFalse(lastOperationSuccess);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Then("the review deletion fails")
 | 
			
		||||
    public void theReviewDeletionFails() {
 | 
			
		||||
        assertFalse(lastOperationSuccess);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @And("I receive a review error message containing {string}")
 | 
			
		||||
    public void iReceiveAReviewErrorMessageContaining(String msg) {
 | 
			
		||||
        assertNotNull(lastErrorMessage);
 | 
			
		||||
@@ -191,41 +151,4 @@ public class ReviewSteps {
 | 
			
		||||
    public void theReviewSystemStillHasReviews(int expected) {
 | 
			
		||||
        assertEquals(expected, reviews.size());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Then("the review request fails")
 | 
			
		||||
    public void theReviewRequestFails() {
 | 
			
		||||
        assertFalse(lastOperationSuccess);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Then("the review deletion fails")
 | 
			
		||||
    public void theReviewDeletionFails() {
 | 
			
		||||
        assertFalse(lastOperationSuccess);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I request all reviews for book {string}")
 | 
			
		||||
    public void iRequestAllReviewsForBook(String isbn) {
 | 
			
		||||
        if (!books.containsKey(isbn)) {
 | 
			
		||||
            lastReviewResult = null;
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "Book or customer not found";
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        lastReviewResult = reviews.values().stream()
 | 
			
		||||
            .filter(r -> r.get("bookId").equals(isbn))
 | 
			
		||||
            .toList();
 | 
			
		||||
        lastOperationSuccess = true;
 | 
			
		||||
        lastErrorMessage = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I try to delete the review with id {string}")
 | 
			
		||||
    public void iTryToDeleteTheReviewWithId(String reviewId) {
 | 
			
		||||
        if (!reviews.containsKey(reviewId)) {
 | 
			
		||||
            lastOperationSuccess = false;
 | 
			
		||||
            lastErrorMessage = "Review not found";
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        reviews.remove(reviewId);
 | 
			
		||||
        lastOperationSuccess = true;
 | 
			
		||||
        lastErrorMessage = null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,12 +6,70 @@ Feature: Manage reviews
 | 
			
		||||
    Given the review system has the following review customers:
 | 
			
		||||
      | id                                   | firstName | lastName | phoneNumber | loyaltyPoints |
 | 
			
		||||
      | 33333333-3333-3333-3333-333333333333 | Carol     | White    | 0600000003  | 50            |
 | 
			
		||||
    And the review system has the following books:
 | 
			
		||||
    And the review system has the following review books:
 | 
			
		||||
      | isbn      | title         | author      | publisher | publicationDate | price | quantity | language |
 | 
			
		||||
      | 978333333 | Book B        | Author B    | PubB      | 2020-01-01      | 18.0  | 5        | EN       |
 | 
			
		||||
      | 978444444 | Book C        | Author C    | PubC      | 2021-06-15      | 22.0  | 3        | FR       |
 | 
			
		||||
    And the review system has the following reviews:
 | 
			
		||||
      | reviewId                               | bookId    | customerName | comment      | rating |
 | 
			
		||||
      | rev-1                                  | 978333333 | Carol White  | Great book!  | 5      |
 | 
			
		||||
      | reviewId | bookId    | customerName | comment      | rating |
 | 
			
		||||
      | rev-1    | 978333333 | Carol White  | Great book!  | 5      |
 | 
			
		||||
 | 
			
		||||
  # ...existing scenarios...
 | 
			
		||||
  Scenario: Submit a new review
 | 
			
		||||
    When I submit a new review with the following information:
 | 
			
		||||
      | customerId                           | isbn      | rating | comment        |
 | 
			
		||||
      | 33333333-3333-3333-3333-333333333333 | 978444444 | 4      | Enjoyed a lot! |
 | 
			
		||||
    Then the review is created successfully
 | 
			
		||||
    And the review system now has 2 reviews
 | 
			
		||||
 | 
			
		||||
  Scenario: Get reviews by customer
 | 
			
		||||
    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      |
 | 
			
		||||
 | 
			
		||||
  Scenario: Attempt to submit a review for a book not purchased
 | 
			
		||||
    When I try to submit a new review with the following information:
 | 
			
		||||
      | customerId                             | isbn      | rating | comment        |
 | 
			
		||||
      | 33333333-3333-3333-3333-333333333333   | 978333333 | 5      | Not purchased  |
 | 
			
		||||
    Then the review submission fails
 | 
			
		||||
    And I receive a review error message containing "customer hasn't purchased the book"
 | 
			
		||||
    And the review system still has 1 review
 | 
			
		||||
 | 
			
		||||
  Scenario: Attempt to submit a review with invalid details
 | 
			
		||||
    When I try to submit a new review with the following information:
 | 
			
		||||
      | customerId | isbn | rating | comment |
 | 
			
		||||
      |            |      |        |         |
 | 
			
		||||
    Then the review submission fails
 | 
			
		||||
    And I receive a review error message containing "Invalid review details"
 | 
			
		||||
    And the review system still has 1 review
 | 
			
		||||
 | 
			
		||||
  Scenario: Attempt to submit a review for unknown book or customer
 | 
			
		||||
    When I try to submit a new review with the following information:
 | 
			
		||||
      | customerId                             | isbn      | rating | comment        |
 | 
			
		||||
      | 99999999-9999-9999-9999-999999999999   | 999999999 | 5      | Unknown book   |
 | 
			
		||||
    Then the review submission fails
 | 
			
		||||
    And I receive a review error message containing "Book or customer not found"
 | 
			
		||||
    And the review system still has 1 review
 | 
			
		||||
 | 
			
		||||
  Scenario: Attempt to submit a duplicate review
 | 
			
		||||
    When I try to submit a new review with the following information:
 | 
			
		||||
      | customerId                             | isbn      | rating | comment        |
 | 
			
		||||
      | 33333333-3333-3333-3333-333333333333   | 978333333 | 5      | Another review |
 | 
			
		||||
    Then the review submission fails
 | 
			
		||||
    And I receive a review error message containing "Review already exists"
 | 
			
		||||
    And the review system still has 1 review
 | 
			
		||||
 | 
			
		||||
  Scenario: Attempt to get reviews by unknown customer
 | 
			
		||||
    When I request all reviews by customer "99999999-9999-9999-9999-999999999999"
 | 
			
		||||
    Then the review request fails
 | 
			
		||||
    And I receive a review error message containing "Book or customer not found"
 | 
			
		||||
 | 
			
		||||
  Scenario: Attempt to get reviews by unknown book
 | 
			
		||||
    When I request all reviews for book "999999999"
 | 
			
		||||
    Then the review request fails
 | 
			
		||||
    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"
 | 
			
		||||
    Then the review deletion fails
 | 
			
		||||
    And I receive a review error message containing "Review not found"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user