forked from pierront/mylibrary-template
Update ReviewSteps.java
This commit is contained in:
@@ -12,93 +12,102 @@ import java.util.*;
|
||||
|
||||
public class ReviewSteps {
|
||||
|
||||
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>> customers = new HashMap<>();
|
||||
private final Map<Long, Map<String, String>> books = 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 boolean lastOperationSuccess = false;
|
||||
private String lastErrorMessage = null;
|
||||
private String lastReviewError;
|
||||
private boolean lastReviewSuccess;
|
||||
private List<Map<String, String>> lastReviewsFetched;
|
||||
|
||||
@Given("the review system has the following review customers:")
|
||||
public void theReviewSystemHasTheFollowingReviewCustomers(DataTable dataTable) {
|
||||
reviewCustomers.clear();
|
||||
customers.clear();
|
||||
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) {
|
||||
reviewBooks.clear();
|
||||
books.clear();
|
||||
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) {
|
||||
reviews.clear();
|
||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
||||
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:")
|
||||
public void iSubmitANewReviewWithTheFollowingInformation(DataTable dataTable) {
|
||||
Map<String, String> reviewInfo = dataTable.asMaps(String.class, String.class).getFirst();
|
||||
String reviewId = "rev-" + (reviews.size() + 1);
|
||||
reviews.put(reviewId, new HashMap<>(reviewInfo));
|
||||
lastReviewId = reviewId;
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
}
|
||||
Map<String, String> info = dataTable.asMaps(String.class, String.class).getFirst();
|
||||
String customerId = info.get("customerId");
|
||||
String isbnStr = info.get("isbn");
|
||||
String rating = info.get("rating");
|
||||
String comment = info.get("comment");
|
||||
|
||||
@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;
|
||||
if (customerId == null || customerId.isBlank() ||
|
||||
isbnStr == null || isbnStr.isBlank() ||
|
||||
rating == null || rating.isBlank()) {
|
||||
lastReviewSuccess = false;
|
||||
lastReviewError = "Invalid review details";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
if (!customers.containsKey(customerId) || !books.containsKey(Long.parseLong(isbnStr))) {
|
||||
lastReviewSuccess = false;
|
||||
lastReviewError = "Book or customer not found";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@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";
|
||||
for (Map<String, String> review : reviews.values()) {
|
||||
if (review.get("customerName").equals(customers.get(customerId).get("firstName") + " " + customers.get(customerId).get("lastName"))
|
||||
&& review.get("bookId").equals(isbnStr)) {
|
||||
lastReviewSuccess = false;
|
||||
lastReviewError = "Review already exists";
|
||||
return;
|
||||
}
|
||||
}
|
||||
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")
|
||||
public void theReviewIsCreatedSuccessfully() {
|
||||
assertTrue(lastOperationSuccess);
|
||||
assertTrue(lastReviewSuccess);
|
||||
assertNotNull(lastReviewId);
|
||||
}
|
||||
|
||||
@And("the review system now has {int} reviews")
|
||||
@@ -106,40 +115,52 @@ public class ReviewSteps {
|
||||
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:")
|
||||
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);
|
||||
assertEquals(expected.size(), lastReviewsFetched.size());
|
||||
for (int i = 0; i < expected.size(); i++) {
|
||||
Map<String, String> exp = expected.get(i);
|
||||
Map<String, String> act = lastReviewsFetched.get(i);
|
||||
for (String key : exp.keySet()) {
|
||||
assertEquals(exp.get(key), act.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@When("I try to submit a new review with the following information:")
|
||||
public void iTryToSubmitANewReviewWithTheFollowingInformation(DataTable dataTable) {
|
||||
iSubmitANewReviewWithTheFollowingInformation(dataTable);
|
||||
}
|
||||
|
||||
@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);
|
||||
assertFalse(lastReviewSuccess);
|
||||
assertNotNull(lastReviewError);
|
||||
}
|
||||
|
||||
@And("I receive a review error message containing {string}")
|
||||
public void iReceiveAReviewErrorMessageContaining(String msg) {
|
||||
assertNotNull(lastErrorMessage);
|
||||
assertTrue(lastErrorMessage.contains(msg));
|
||||
assertNotNull(lastReviewError);
|
||||
assertTrue(lastReviewError.contains(msg), "Expected error message to contain: \"" + msg + "\" but was: \"" + lastReviewError + "\"");
|
||||
}
|
||||
|
||||
@And("the review system still has {int} review")
|
||||
@@ -147,8 +168,44 @@ public class ReviewSteps {
|
||||
assertEquals(expected, reviews.size());
|
||||
}
|
||||
|
||||
@And("the review system still has {int} reviews")
|
||||
public void theReviewSystemStillHasReviews(int expected) {
|
||||
assertEquals(expected, reviews.size());
|
||||
@When("I request all reviews for book {string}")
|
||||
public void iRequestAllReviewsForBook(String isbnStr) {
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user