forked from pierront/mylibrary-template
Review step et feature
This commit is contained in:
@@ -1,22 +1,220 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.features.client;
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.features.review;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import io.cucumber.datatable.DataTable;
|
||||
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.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import io.cucumber.java.en.*;
|
||||
|
||||
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;
|
||||
|
||||
@Given("the system has the following customers:")
|
||||
public void theSystemHasTheFollowingCustomers(DataTable dataTable) {
|
||||
customers.clear();
|
||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
||||
customers.put(row.get("id"), row);
|
||||
}
|
||||
}
|
||||
|
||||
@Given("the system has the following books:")
|
||||
public void theSystemHasTheFollowingBooks(DataTable dataTable) {
|
||||
books.clear();
|
||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
||||
books.put(row.get("isbn"), row);
|
||||
}
|
||||
}
|
||||
|
||||
@Given("the system has the following reviews:")
|
||||
public void theSystemHasTheFollowingReviews(DataTable dataTable) {
|
||||
reviews.clear();
|
||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
||||
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).get(0);
|
||||
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);
|
||||
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 is created successfully")
|
||||
public void theReviewIsCreatedSuccessfully() {
|
||||
assertTrue(lastOperationSuccess);
|
||||
}
|
||||
|
||||
@And("the system now has {int} reviews")
|
||||
public void theSystemNowHasReviews(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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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).get(0);
|
||||
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 submission fails")
|
||||
public void theSubmissionFails() {
|
||||
assertFalse(lastOperationSuccess);
|
||||
}
|
||||
|
||||
@And("I receive an error message containing {string}")
|
||||
public void iReceiveAnErrorMessageContaining(String msg) {
|
||||
assertNotNull(lastErrorMessage);
|
||||
assertTrue(lastErrorMessage.contains(msg));
|
||||
}
|
||||
|
||||
@And("the system still has {int} review")
|
||||
public void theSystemStillHasReview(int expected) {
|
||||
assertEquals(expected, reviews.size());
|
||||
}
|
||||
|
||||
@And("the system still has {int} reviews")
|
||||
public void theSystemStillHasReviewsPlural(int expected) {
|
||||
assertEquals(expected, reviews.size());
|
||||
}
|
||||
|
||||
@Then("the request fails")
|
||||
public void theRequestFails() {
|
||||
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;
|
||||
}
|
||||
|
||||
@Then("the deletion fails")
|
||||
public void theDeletionFails() {
|
||||
assertFalse(lastOperationSuccess);
|
||||
}
|
||||
}
|
@@ -9,14 +9,15 @@ Feature: Manage reviews
|
||||
And the system has the following 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 system has the following reviews:
|
||||
| reviewId | bookId | customerName | comment | rating |
|
||||
| rev-1 | 978333333 | Carol White | Great book! | 5 |
|
||||
|
||||
Scenario: Submit a new review
|
||||
When I submit a new review with the following information:
|
||||
| customerId | isbn | rating | comment |
|
||||
| 33333333-3333-3333-3333-333333333333 | 978333333 | 4 | Enjoyed a lot! |
|
||||
| customerId | isbn | rating | comment |
|
||||
| 33333333-3333-3333-3333-333333333333 | 978444444 | 4 | Enjoyed a lot! |
|
||||
Then the review is created successfully
|
||||
And the system now has 2 reviews
|
||||
|
||||
|
Reference in New Issue
Block a user