Update OrderSteps.java

This commit is contained in:
Kroccmou
2025-06-14 00:08:47 +02:00
parent af8d086d01
commit d07de49cc9

View File

@@ -12,5 +12,167 @@ import java.util.*;
public class OrderSteps { public class OrderSteps {
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>> orders = new HashMap<>();
private String lastOrderId;
private String lastOrderError;
private boolean lastOrderSuccess;
private Map<String, String> lastOrderFetched;
@Given("the order system has the following customers:")
public void theOrderSystemHasTheFollowingCustomers(DataTable dataTable) {
customers.clear();
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
customers.put(row.get("id"), new HashMap<>(row));
}
assertEquals(dataTable.asMaps().size(), customers.size());
}
@And("the order system has the following books:")
public void theOrderSystemHasTheFollowingBooks(DataTable dataTable) {
books.clear();
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
books.put(Long.parseLong(row.get("isbn")), new HashMap<>(row));
}
assertEquals(dataTable.asMaps().size(), books.size());
}
@And("the order system has the following orders:")
public void theOrderSystemHasTheFollowingOrders(DataTable dataTable) {
orders.clear();
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
orders.put(row.get("id"), new HashMap<>(row));
}
assertEquals(dataTable.asMaps().size(), orders.size());
}
@When("I create a new order with the following information:")
public void iCreateANewOrderWithTheFollowingInformation(DataTable dataTable) {
Map<String, String> info = dataTable.asMaps(String.class, String.class).getFirst();
String customerId = info.get("customerId");
String paymentMethod = info.get("paymentMethod");
String orderLineDtos = info.get("orderLineDtos");
String addressStreet = info.get("addressStreet");
String addressCity = info.get("addressCity");
String addressPostalCode = info.get("addressPostalCode");
String addressCountry = info.get("addressCountry");
// Validation simple
if (customerId == null || customerId.isBlank() ||
paymentMethod == null || paymentMethod.isBlank() ||
orderLineDtos == null || orderLineDtos.isBlank() ||
addressStreet == null || addressStreet.isBlank() ||
addressCity == null || addressCity.isBlank() ||
addressPostalCode == null || addressPostalCode.isBlank() ||
addressCountry == null || addressCountry.isBlank()) {
lastOrderSuccess = false;
lastOrderError = "Invalid order details or address";
return;
}
if (!customers.containsKey(customerId)) {
lastOrderSuccess = false;
lastOrderError = "Customer not found";
return;
}
// Parse orderLineDtos (attendu: [{ "bookId":978222222, "quantity":2 }])
try {
String content = orderLineDtos.replace("[", "").replace("]", "").replace("{", "").replace("}", "");
String[] pairs = content.split(",");
Long bookId = null;
int quantity = 0;
for (String pair : pairs) {
String[] kv = pair.trim().replace("\"", "").split(":");
if (kv.length == 2) {
if (kv[0].trim().equals("bookId")) bookId = Long.parseLong(kv[1].trim());
if (kv[0].trim().equals("quantity")) quantity = Integer.parseInt(kv[1].trim());
}
}
if (bookId == null || !books.containsKey(bookId)) {
lastOrderSuccess = false;
lastOrderError = "Invalid order details or address";
return;
}
int stock = Integer.parseInt(books.get(bookId).get("quantity"));
if (quantity > stock) {
lastOrderSuccess = false;
lastOrderError = "book quantity insufficient";
return;
}
// Décrémente le stock fictivement
books.get(bookId).put("quantity", String.valueOf(stock - quantity));
// Création de la commande
String newOrderId = "ord-" + (orders.size() + 1);
Map<String, String> order = new HashMap<>();
order.put("id", newOrderId);
order.put("customerId", customerId);
order.put("totalPrice", String.valueOf(
Double.parseDouble(books.get(bookId).get("price")) * quantity
));
order.put("paymentMethod", paymentMethod);
orders.put(newOrderId, order);
lastOrderId = newOrderId;
lastOrderSuccess = true;
lastOrderError = null;
} catch (Exception e) {
lastOrderSuccess = false;
lastOrderError = "Invalid order details or address";
}
}
@Then("the order is created successfully")
public void theOrderIsCreatedSuccessfully() {
assertTrue(lastOrderSuccess);
assertNotNull(lastOrderId);
}
@And("the order system now has {int} orders")
public void theOrderSystemNowHasOrders(int expected) {
assertEquals(expected, orders.size());
}
@When("I request the order with id {string}")
public void iRequestTheOrderWithId(String id) {
lastOrderFetched = orders.get(id);
lastOrderSuccess = lastOrderFetched != null;
if (!lastOrderSuccess) lastOrderError = "Order not found";
}
@Then("I receive the following order information:")
public void iReceiveTheFollowingOrderInformation(DataTable dataTable) {
Map<String, String> expected = dataTable.asMaps(String.class, String.class).getFirst();
assertNotNull(lastOrderFetched);
for (String key : expected.keySet()) {
assertEquals(expected.get(key), lastOrderFetched.get(key));
}
}
@When("I try to create a new order with the following information:")
public void iTryToCreateANewOrderWithTheFollowingInformation(DataTable dataTable) {
iCreateANewOrderWithTheFollowingInformation(dataTable);
}
@Then("the order creation fails")
public void theOrderCreationFails() {
assertFalse(lastOrderSuccess);
assertNotNull(lastOrderError);
}
@And("I receive an order error message containing {string}")
public void iReceiveAnOrderErrorMessageContaining(String msg) {
assertNotNull(lastOrderError);
assertTrue(lastOrderError.contains(msg), "Expected error message to contain: \"" + msg + "\" but was: \"" + lastOrderError + "\"");
}
@And("the order system still has {int} order")
public void theOrderSystemStillHasOrder(int expected) {
assertEquals(expected, orders.size());
}
@Then("the order request fails")
public void theOrderRequestFails() {
assertFalse(lastOrderSuccess);
assertNotNull(lastOrderError);
}
} }