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 static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import io.cucumber.datatable.DataTable;
|
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.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class ReviewSteps {
|
public class ReviewSteps {
|
||||||
|
|
||||||
// Simulations des repositories en mémoire
|
private final Map<String, Map<String, String>> reviewCustomers = new HashMap<>();
|
||||||
private final Map<String, Map<String, String>> customers = new HashMap<>();
|
private final Map<String, Map<String, String>> reviewBooks = new HashMap<>();
|
||||||
private final Map<String, Map<String, String>> books = new HashMap<>();
|
private final Map<String, Map<String, String>> reviews = new HashMap<>();
|
||||||
private final Map<String, Map<String, String>> reviews = new LinkedHashMap<>();
|
private String lastReviewId;
|
||||||
|
private boolean lastOperationSuccess = false;
|
||||||
// Pour stocker les résultats/intermédiaires
|
private String lastErrorMessage = null;
|
||||||
private List<Map<String, String>> lastReviewResult;
|
|
||||||
private String lastErrorMessage;
|
|
||||||
private boolean lastOperationSuccess;
|
|
||||||
|
|
||||||
@Given("the review system has the following review customers:")
|
@Given("the review system has the following review customers:")
|
||||||
public void theReviewSystemHasTheFollowingReviewCustomers(DataTable dataTable) {
|
public void theReviewSystemHasTheFollowingReviewCustomers(DataTable dataTable) {
|
||||||
customers.clear();
|
reviewCustomers.clear();
|
||||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
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:")
|
@Given("the review system has the following review books:")
|
||||||
public void theReviewSystemHasTheFollowingBooks(DataTable dataTable) {
|
public void theReviewSystemHasTheFollowingReviewBooks(DataTable dataTable) {
|
||||||
books.clear();
|
reviewBooks.clear();
|
||||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
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) {
|
public void theReviewSystemHasTheFollowingReviews(DataTable dataTable) {
|
||||||
reviews.clear();
|
reviews.clear();
|
||||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
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));
|
reviews.put(row.get("reviewId"), new HashMap<>(row));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@When("I submit a new review with the following information:")
|
@When("I submit a new review with the following information:")
|
||||||
public void iSubmitANewReviewWithTheFollowingInformation(DataTable dataTable) {
|
public void iSubmitANewReviewWithTheFollowingInformation(DataTable dataTable) {
|
||||||
Map<String, String> review = dataTable.asMaps(String.class, String.class).getFirst();
|
Map<String, String> reviewInfo = 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
|
|
||||||
String reviewId = "rev-" + (reviews.size() + 1);
|
String reviewId = "rev-" + (reviews.size() + 1);
|
||||||
Map<String, String> newReview = new HashMap<>();
|
reviews.put(reviewId, new HashMap<>(reviewInfo));
|
||||||
newReview.put("reviewId", reviewId);
|
lastReviewId = 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;
|
lastOperationSuccess = true;
|
||||||
lastErrorMessage = null;
|
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")
|
@Then("the review is created successfully")
|
||||||
public void theReviewIsCreatedSuccessfully() {
|
public void theReviewIsCreatedSuccessfully() {
|
||||||
assertTrue(lastOperationSuccess);
|
assertTrue(lastOperationSuccess);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Then("the review system now has {int} reviews")
|
@And("the review system now has {int} reviews")
|
||||||
public void theReviewSystemNowHasReviews(int expected) {
|
public void theReviewSystemNowHasReviews(int expected) {
|
||||||
assertEquals(expected, reviews.size());
|
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:")
|
@Then("I receive the following reviews:")
|
||||||
public void iReceiveTheFollowingReviews(DataTable expectedTable) {
|
public void iReceiveTheFollowingReviews(DataTable dataTable) {
|
||||||
List<Map<String, String>> expected = expectedTable.asMaps(String.class, String.class);
|
List<Map<String, String>> expected = dataTable.asMaps(String.class, String.class);
|
||||||
assertNotNull(lastReviewResult);
|
for (Map<String, String> exp : expected) {
|
||||||
assertEquals(expected.size(), lastReviewResult.size());
|
boolean match = reviews.values().stream().anyMatch(r ->
|
||||||
for (int i = 0; i < expected.size(); i++) {
|
Objects.equals(exp.get("reviewId"), r.get("reviewId")) &&
|
||||||
Map<String, String> exp = expected.get(i);
|
Objects.equals(exp.get("bookId"), r.get("bookId")) &&
|
||||||
Map<String, String> actual = lastReviewResult.get(i);
|
Objects.equals(exp.get("customerName"), r.get("customerName")) &&
|
||||||
for (String key : exp.keySet()) {
|
Objects.equals(exp.get("comment"), r.get("comment")) &&
|
||||||
assertEquals(exp.get(key), actual.get(key));
|
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")
|
@Then("the review submission fails")
|
||||||
public void theReviewSubmissionFails() {
|
public void theReviewSubmissionFails() {
|
||||||
assertFalse(lastOperationSuccess);
|
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}")
|
@And("I receive a review error message containing {string}")
|
||||||
public void iReceiveAReviewErrorMessageContaining(String msg) {
|
public void iReceiveAReviewErrorMessageContaining(String msg) {
|
||||||
assertNotNull(lastErrorMessage);
|
assertNotNull(lastErrorMessage);
|
||||||
@@ -191,41 +151,4 @@ public class ReviewSteps {
|
|||||||
public void theReviewSystemStillHasReviews(int expected) {
|
public void theReviewSystemStillHasReviews(int expected) {
|
||||||
assertEquals(expected, reviews.size());
|
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,7 +6,7 @@ Feature: Manage reviews
|
|||||||
Given the review system has the following review customers:
|
Given the review system has the following review customers:
|
||||||
| id | firstName | lastName | phoneNumber | loyaltyPoints |
|
| id | firstName | lastName | phoneNumber | loyaltyPoints |
|
||||||
| 33333333-3333-3333-3333-333333333333 | Carol | White | 0600000003 | 50 |
|
| 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 |
|
| isbn | title | author | publisher | publicationDate | price | quantity | language |
|
||||||
| 978333333 | Book B | Author B | PubB | 2020-01-01 | 18.0 | 5 | EN |
|
| 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 |
|
| 978444444 | Book C | Author C | PubC | 2021-06-15 | 22.0 | 3 | FR |
|
||||||
@@ -14,4 +14,62 @@ Feature: Manage reviews
|
|||||||
| reviewId | bookId | customerName | comment | rating |
|
| reviewId | bookId | customerName | comment | rating |
|
||||||
| rev-1 | 978333333 | Carol White | Great book! | 5 |
|
| 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