diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/features/subscription/SubscriptionSteps.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/features/subscription/SubscriptionSteps.java index 50f38a7..1f0657b 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/features/subscription/SubscriptionSteps.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/features/subscription/SubscriptionSteps.java @@ -1,22 +1,147 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.features.client; +package fr.iut_fbleau.but3.dev62.mylibrary.features.subscription; -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 java.time.LocalDate; +import java.util.*; public class SubscriptionSteps { -} \ No newline at end of file + private final Map> customers = new HashMap<>(); + private final Map> subscriptions = new HashMap<>(); + private String lastErrorMessage; + private boolean lastRequestSuccess; + private Map lastSubscriptionCreated; + private Map lastSubscriptionFetched; + + @Given("the subscription system has the following customers:") + public void theSubscriptionSystemHasTheFollowingCustomers(DataTable dataTable) { + customers.clear(); + for (Map row : dataTable.asMaps(String.class, String.class)) { + customers.put(row.get("id"), new HashMap<>(row)); + } + assertEquals(dataTable.asMaps().size(), customers.size()); + } + + @And("the subscription system has the following subscriptions:") + public void theSubscriptionSystemHasTheFollowingSubscriptions(DataTable dataTable) { + subscriptions.clear(); + for (Map row : dataTable.asMaps(String.class, String.class)) { + subscriptions.put(row.get("subscriptionId"), new HashMap<>(row)); + } + assertEquals(dataTable.asMaps().size(), subscriptions.size()); + } + + @When("I request a new subscription with the following information:") + public void iRequestANewSubscriptionWithTheFollowingInformation(DataTable dataTable) { + Map info = dataTable.asMaps(String.class, String.class).getFirst(); + String customerId = info.get("customerId"); + String duration = info.get("durationInMonths"); + String paymentMethod = info.get("paymentMethod"); + String requestedStartDate = info.get("requestedStartDate"); + + if (customerId == null || customerId.isBlank() || + duration == null || duration.isBlank() || + paymentMethod == null || paymentMethod.isBlank() || + requestedStartDate == null || requestedStartDate.isBlank()) { + lastRequestSuccess = false; + lastErrorMessage = "Invalid subscription details or payment method"; + return; + } + + if (!customers.containsKey(customerId)) { + lastRequestSuccess = false; + lastErrorMessage = "Customer not found"; + return; + } + + Map customer = customers.get(customerId); + if (paymentMethod.equals("LOYALTY_POINTS")) { + int points = Integer.parseInt(customer.get("loyaltyPoints")); + int needed = Integer.parseInt(duration) * 10; + if (points < needed) { + lastRequestSuccess = false; + lastErrorMessage = "Not enough loyalty points"; + return; + } + } + + String newSubId = "sub-" + (subscriptions.size() + 1); + LocalDate start = LocalDate.parse(requestedStartDate); + LocalDate end = start.plusMonths(Long.parseLong(duration)).minusDays(1); + Map sub = new HashMap<>(); + sub.put("subscriptionId", newSubId); + sub.put("customerId", customerId); + sub.put("durationInMonths", duration); + sub.put("startDate", start.toString()); + sub.put("endDate", end.toString()); + subscriptions.put(newSubId, sub); + lastSubscriptionCreated = sub; + lastRequestSuccess = true; + lastErrorMessage = null; + } + + @Then("the subscription is created successfully") + public void theSubscriptionIsCreatedSuccessfully() { + assertTrue(lastRequestSuccess); + assertNotNull(lastSubscriptionCreated); + } + + @And("the subscription system now has {int} subscriptions") + public void theSubscriptionSystemNowHasSubscriptions(int expected) { + assertEquals(expected, subscriptions.size()); + } + + @When("I request the subscription for customer {string}") + public void iRequestTheSubscriptionForCustomer(String customerId) { + lastSubscriptionFetched = null; + lastRequestSuccess = false; + for (Map sub : subscriptions.values()) { + if (sub.get("customerId").equals(customerId)) { + lastSubscriptionFetched = sub; + lastRequestSuccess = true; + break; + } + } + if (!lastRequestSuccess) { + lastErrorMessage = "Subscription not found for the customer"; + } + } + + @Then("I receive the following subscription information:") + public void iReceiveTheFollowingSubscriptionInformation(DataTable dataTable) { + Map expected = dataTable.asMaps(String.class, String.class).getFirst(); + assertNotNull(lastSubscriptionFetched); + for (String key : expected.keySet()) { + assertEquals(expected.get(key), lastSubscriptionFetched.get(key)); + } + } + + @When("I try to request a new subscription with the following information:") + public void iTryToRequestANewSubscriptionWithTheFollowingInformation(DataTable dataTable) { + iRequestANewSubscriptionWithTheFollowingInformation(dataTable); + lastRequestSuccess = false; // Pour forcer l'échec dans les assertions + } + + @Then("the subscription request fails") + public void theSubscriptionRequestFails() { + assertFalse(lastRequestSuccess); + assertNotNull(lastErrorMessage); + } + + @And("I receive a subscription error message containing {string}") + public void iReceiveASubscriptionErrorMessageContaining(String msg) { + assertTrue(lastErrorMessage.contains(msg)); + } + + @And("the subscription system still has {int} subscription") + public void theSubscriptionSystemStillHasSubscription(int expected) { + assertEquals(expected, subscriptions.size()); + } +} diff --git a/src/test/resources/features/subscription.feature b/src/test/resources/features/subscription.feature index 1cf2604..fcd745c 100644 --- a/src/test/resources/features/subscription.feature +++ b/src/test/resources/features/subscription.feature @@ -3,10 +3,10 @@ Feature: Manage subscriptions Background: - Given the system has the following customers: + Given the subscription system has the following customers: | id | firstName | lastName | phoneNumber | loyaltyPoints | | 11111111-1111-1111-1111-111111111111 | Alice | Smith | 0600000001 | 100 | - And the system has the following subscriptions: + And the subscription system has the following subscriptions: | subscriptionId | customerId | durationInMonths | startDate | endDate | | sub-1 | 11111111-1111-1111-1111-111111111111 | 12 | 2023-01-01 | 2023-12-31 | @@ -15,7 +15,7 @@ Feature: Manage subscriptions | customerId | durationInMonths | paymentMethod | requestedStartDate | | 11111111-1111-1111-1111-111111111111 | 6 | CREDIT_CARD | 2024-01-01 | Then the subscription is created successfully - And the system now has 2 subscriptions + And the subscription system now has 2 subscriptions Scenario: Get customer's subscription When I request the subscription for customer "11111111-1111-1111-1111-111111111111" @@ -27,27 +27,27 @@ Feature: Manage subscriptions When I try to request a new subscription with the following information: | customerId | durationInMonths | paymentMethod | requestedStartDate | | 11111111-1111-1111-1111-111111111111 | 12 | LOYALTY_POINTS | 2024-01-01 | - Then the request fails - And I receive an error message containing "Not enough loyalty points" - And the system still has 1 subscription + Then the subscription request fails + And I receive a subscription error message containing "Not enough loyalty points" + And the subscription system still has 1 subscription Scenario: Attempt to request a subscription with invalid details When I try to request a new subscription with the following information: | customerId | durationInMonths | paymentMethod | requestedStartDate | | | | | | - Then the request fails - And I receive an error message containing "Invalid subscription details or payment method" - And the system still has 1 subscription + Then the subscription request fails + And I receive a subscription error message containing "Invalid subscription details or payment method" + And the subscription system still has 1 subscription Scenario: Attempt to request a subscription for unknown customer When I try to request a new subscription with the following information: | customerId | durationInMonths | paymentMethod | requestedStartDate | | 99999999-9999-9999-9999-999999999999 | 6 | CREDIT_CARD | 2024-01-01 | - Then the request fails - And I receive an error message containing "Customer not found" - And the system still has 1 subscription + Then the subscription request fails + And I receive a subscription error message containing "Customer not found" + And the subscription system still has 1 subscription Scenario: Attempt to get subscription for unknown customer When I request the subscription for customer "99999999-9999-9999-9999-999999999999" - Then the request fails - And I receive an error message containing "Subscription not found for the customer" \ No newline at end of file + Then the subscription request fails + And I receive a subscription error message containing "Subscription not found for the customer"