Compare commits

...

3 Commits

Author SHA1 Message Date
Kroccmou
554f6f1661 Update OrderSteps.java 2025-06-14 00:21:43 +02:00
Kroccmou
696b1566f6 Update ReviewSteps.java 2025-06-14 00:21:40 +02:00
Kroccmou
0df33e9e32 Update review.feature 2025-06-14 00:21:38 +02:00
3 changed files with 148 additions and 87 deletions

View File

@@ -76,7 +76,6 @@ public class OrderSteps {
return; return;
} }
// Parse orderLineDtos (attendu: [{ "bookId":978222222, "quantity":2 }])
try { try {
String content = orderLineDtos.replace("[", "").replace("]", "").replace("{", "").replace("}", ""); String content = orderLineDtos.replace("[", "").replace("]", "").replace("{", "").replace("}", "");
String[] pairs = content.split(","); String[] pairs = content.split(",");

View File

@@ -12,93 +12,102 @@ import java.util.*;
public class ReviewSteps { public class ReviewSteps {
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<Long, 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 HashMap<>();
private final Set<String> purchases = new HashSet<>(); // Ajout pour les achats
private String lastReviewId; private String lastReviewId;
private boolean lastOperationSuccess = false; private String lastReviewError;
private String lastErrorMessage = null; private boolean lastReviewSuccess;
private List<Map<String, String>> lastReviewsFetched;
@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) {
reviewCustomers.clear(); customers.clear();
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) { for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
reviewCustomers.put(row.get("id"), row); customers.put(row.get("id"), new HashMap<>(row));
} }
assertEquals(dataTable.asMaps().size(), customers.size());
} }
@Given("the review system has the following review books:") @And("the review system has the following review books:")
public void theReviewSystemHasTheFollowingReviewBooks(DataTable dataTable) { public void theReviewSystemHasTheFollowingReviewBooks(DataTable dataTable) {
reviewBooks.clear(); books.clear();
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) { for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
reviewBooks.put(row.get("isbn"), row); books.put(Long.parseLong(row.get("isbn")), new HashMap<>(row));
} }
assertEquals(dataTable.asMaps().size(), books.size());
} }
@Given("the review system has the following reviews:") @And("the review system has the following reviews:")
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)) {
reviews.put(row.get("reviewId"), new HashMap<>(row)); reviews.put(row.get("reviewId"), new HashMap<>(row));
} }
assertEquals(dataTable.asMaps().size(), reviews.size());
}
@And("the review system has the following purchases:")
public void theReviewSystemHasTheFollowingPurchases(DataTable dataTable) {
purchases.clear();
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
String customerId = row.get("customerId");
String bookId = row.get("bookId");
purchases.add(customerId + ":" + bookId);
}
} }
@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> reviewInfo = dataTable.asMaps(String.class, String.class).getFirst(); Map<String, String> info = dataTable.asMaps(String.class, String.class).getFirst();
String reviewId = "rev-" + (reviews.size() + 1); String customerId = info.get("customerId");
reviews.put(reviewId, new HashMap<>(reviewInfo)); String isbnStr = info.get("isbn");
lastReviewId = reviewId; String rating = info.get("rating");
lastOperationSuccess = true; String comment = info.get("comment");
lastErrorMessage = null;
}
@When("I try to submit a new review with the following information:") if (customerId == null || customerId.isBlank() ||
public void iTryToSubmitANewReviewWithTheFollowingInformation(DataTable dataTable) { isbnStr == null || isbnStr.isBlank() ||
// Simule un échec de soumission rating == null || rating.isBlank()) {
lastOperationSuccess = false; lastReviewSuccess = false;
lastErrorMessage = "Simulated review submission error"; lastReviewError = "Invalid review details";
} return;
@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;
} }
} if (!customers.containsKey(customerId) || !books.containsKey(Long.parseLong(isbnStr))) {
lastReviewSuccess = false;
@When("I request all reviews for book {string}") lastReviewError = "Book or customer not found";
public void iRequestAllReviewsForBook(String isbn) { return;
// 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;
} }
} for (Map<String, String> review : reviews.values()) {
if (review.get("customerName").equals(customers.get(customerId).get("firstName") + " " + customers.get(customerId).get("lastName"))
@When("I try to delete the review with id {string}") && review.get("bookId").equals(isbnStr)) {
public void iTryToDeleteTheReviewWithId(String reviewId) { lastReviewSuccess = false;
if (reviews.containsKey(reviewId)) { lastReviewError = "Review already exists";
reviews.remove(reviewId); return;
lastOperationSuccess = true; }
lastErrorMessage = null;
} else {
lastOperationSuccess = false;
lastErrorMessage = "Review not found";
} }
if (!purchases.contains(customerId + ":" + isbnStr)) {
lastReviewSuccess = false;
lastReviewError = "customer hasn't purchased the book";
return;
}
String newReviewId = "rev-" + (reviews.size() + 1);
Map<String, String> review = new HashMap<>();
review.put("reviewId", newReviewId);
review.put("bookId", isbnStr);
review.put("customerName", customers.get(customerId).get("firstName") + " " + customers.get(customerId).get("lastName"));
review.put("comment", comment);
review.put("rating", rating);
reviews.put(newReviewId, review);
lastReviewId = newReviewId;
lastReviewSuccess = true;
lastReviewError = null;
} }
@Then("the review is created successfully") @Then("the review is created successfully")
public void theReviewIsCreatedSuccessfully() { public void theReviewIsCreatedSuccessfully() {
assertTrue(lastOperationSuccess); assertTrue(lastReviewSuccess);
assertNotNull(lastReviewId);
} }
@And("the review system now has {int} reviews") @And("the review system now has {int} reviews")
@@ -106,40 +115,52 @@ public class ReviewSteps {
assertEquals(expected, reviews.size()); assertEquals(expected, reviews.size());
} }
@When("I request all reviews by customer {string}")
public void iRequestAllReviewsByCustomer(String customerId) {
lastReviewsFetched = new ArrayList<>();
if (!customers.containsKey(customerId)) {
lastReviewSuccess = false;
lastReviewError = "Book or customer not found";
return;
}
String customerName = customers.get(customerId).get("firstName") + " " + customers.get(customerId).get("lastName");
for (Map<String, String> review : reviews.values()) {
if (review.get("customerName").equals(customerName)) {
lastReviewsFetched.add(review);
}
}
lastReviewSuccess = true;
lastReviewError = null;
}
@Then("I receive the following reviews:") @Then("I receive the following reviews:")
public void iReceiveTheFollowingReviews(DataTable dataTable) { public void iReceiveTheFollowingReviews(DataTable dataTable) {
List<Map<String, String>> expected = dataTable.asMaps(String.class, String.class); List<Map<String, String>> expected = dataTable.asMaps(String.class, String.class);
for (Map<String, String> exp : expected) { assertEquals(expected.size(), lastReviewsFetched.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> act = lastReviewsFetched.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), act.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) {
iSubmitANewReviewWithTheFollowingInformation(dataTable);
}
@Then("the review submission fails") @Then("the review submission fails")
public void theReviewSubmissionFails() { public void theReviewSubmissionFails() {
assertFalse(lastOperationSuccess); assertFalse(lastReviewSuccess);
} assertNotNull(lastReviewError);
@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(lastReviewError);
assertTrue(lastErrorMessage.contains(msg)); assertTrue(lastReviewError.contains(msg), "Expected error message to contain: \"" + msg + "\" but was: \"" + lastReviewError + "\"");
} }
@And("the review system still has {int} review") @And("the review system still has {int} review")
@@ -147,8 +168,44 @@ public class ReviewSteps {
assertEquals(expected, reviews.size()); assertEquals(expected, reviews.size());
} }
@And("the review system still has {int} reviews") @When("I request all reviews for book {string}")
public void theReviewSystemStillHasReviews(int expected) { public void iRequestAllReviewsForBook(String isbnStr) {
assertEquals(expected, reviews.size()); lastReviewsFetched = new ArrayList<>();
if (!books.containsKey(Long.parseLong(isbnStr))) {
lastReviewSuccess = false;
lastReviewError = "Book or customer not found";
return;
}
for (Map<String, String> review : reviews.values()) {
if (review.get("bookId").equals(isbnStr)) {
lastReviewsFetched.add(review);
}
}
lastReviewSuccess = true;
lastReviewError = null;
} }
}
@Then("the review request fails")
public void theReviewRequestFails() {
assertFalse(lastReviewSuccess);
assertNotNull(lastReviewError);
}
@When("I try to delete the review with id {string}")
public void iTryToDeleteTheReviewWithId(String reviewId) {
if (!reviews.containsKey(reviewId)) {
lastReviewSuccess = false;
lastReviewError = "Review not found";
return;
}
reviews.remove(reviewId);
lastReviewSuccess = true;
lastReviewError = null;
}
@Then("the review deletion fails")
public void theReviewDeletionFails() {
assertFalse(lastReviewSuccess);
assertNotNull(lastReviewError);
}
}

View File

@@ -7,12 +7,17 @@ Feature: Manage reviews
| 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 review 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 |
| 978555555 | Book D | Author D | PubD | 2022-03-10 | 25.0 | 2 | DE |
And the review system has the following reviews: And the review system has the following 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 |
And the review system has the following purchases:
| customerId | bookId | purchaseDate |
| 33333333-3333-3333-3333-333333333333 | 978333333 | 2021-01-01 |
| 33333333-3333-3333-3333-333333333333 | 978444444 | 2021-06-15 |
Scenario: Submit a new review Scenario: Submit a new review
When I submit a new review with the following information: When I submit a new review with the following information:
@@ -30,7 +35,7 @@ Feature: Manage reviews
Scenario: Attempt to submit a review for a book not purchased Scenario: Attempt to submit a review for a book not purchased
When I try to submit a new review with the following information: When I try to submit a new review with the following information:
| customerId | isbn | rating | comment | | customerId | isbn | rating | comment |
| 33333333-3333-3333-3333-333333333333 | 978333333 | 5 | Not purchased | | 33333333-3333-3333-3333-333333333333 | 978555555 | 5 | Not purchased |
Then the review submission fails Then the review submission fails
And I receive a review error message containing "customer hasn't purchased the book" And I receive a review error message containing "customer hasn't purchased the book"
And the review system still has 1 review And the review system still has 1 review