forked from pierront/mylibrary-template
		
	correction subscriptions feature et steps
This commit is contained in:
		@@ -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.*;
 | 
				
			||||||
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 io.cucumber.datatable.DataTable;
 | 
					import io.cucumber.datatable.DataTable;
 | 
				
			||||||
import io.cucumber.java.en.And;
 | 
					import io.cucumber.java.en.And;
 | 
				
			||||||
import io.cucumber.java.en.Given;
 | 
					import io.cucumber.java.en.Given;
 | 
				
			||||||
import io.cucumber.java.en.Then;
 | 
					import io.cucumber.java.en.Then;
 | 
				
			||||||
import io.cucumber.java.en.When;
 | 
					import io.cucumber.java.en.When;
 | 
				
			||||||
import java.util.HashMap;
 | 
					
 | 
				
			||||||
import java.util.List;
 | 
					import java.time.LocalDate;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.*;
 | 
				
			||||||
import java.util.Optional;
 | 
					 | 
				
			||||||
import java.util.UUID;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class SubscriptionSteps {
 | 
					public class SubscriptionSteps {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					    private final Map<String, Map<String, String>> customers = new HashMap<>();
 | 
				
			||||||
 | 
					    private final Map<String, Map<String, String>> subscriptions = new HashMap<>();
 | 
				
			||||||
 | 
					    private String lastErrorMessage;
 | 
				
			||||||
 | 
					    private boolean lastRequestSuccess;
 | 
				
			||||||
 | 
					    private Map<String, String> lastSubscriptionCreated;
 | 
				
			||||||
 | 
					    private Map<String, String> lastSubscriptionFetched;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Given("the subscription system has the following customers:")
 | 
				
			||||||
 | 
					    public void theSubscriptionSystemHasTheFollowingCustomers(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 subscription system has the following subscriptions:")
 | 
				
			||||||
 | 
					    public void theSubscriptionSystemHasTheFollowingSubscriptions(DataTable dataTable) {
 | 
				
			||||||
 | 
					        subscriptions.clear();
 | 
				
			||||||
 | 
					        for (Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,10 +3,10 @@
 | 
				
			|||||||
Feature: Manage subscriptions
 | 
					Feature: Manage subscriptions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Background:
 | 
					  Background:
 | 
				
			||||||
    Given the system has the following customers:
 | 
					    Given the subscription system has the following customers:
 | 
				
			||||||
      | id                                   | firstName | lastName | phoneNumber | loyaltyPoints |
 | 
					      | id                                   | firstName | lastName | phoneNumber | loyaltyPoints |
 | 
				
			||||||
      | 11111111-1111-1111-1111-111111111111 | Alice     | Smith    | 0600000001  | 100           |
 | 
					      | 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     |
 | 
					      | subscriptionId | customerId                             | durationInMonths | startDate   | endDate     |
 | 
				
			||||||
      | sub-1          | 11111111-1111-1111-1111-111111111111   | 12               | 2023-01-01  | 2023-12-31  |
 | 
					      | 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 |
 | 
					      | customerId                             | durationInMonths | paymentMethod  | requestedStartDate |
 | 
				
			||||||
      | 11111111-1111-1111-1111-111111111111   | 6                | CREDIT_CARD    | 2024-01-01        |
 | 
					      | 11111111-1111-1111-1111-111111111111   | 6                | CREDIT_CARD    | 2024-01-01        |
 | 
				
			||||||
    Then the subscription is created successfully
 | 
					    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
 | 
					  Scenario: Get customer's subscription
 | 
				
			||||||
    When I request the subscription for customer "11111111-1111-1111-1111-111111111111"
 | 
					    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:
 | 
					    When I try to request a new subscription with the following information:
 | 
				
			||||||
      | customerId                             | durationInMonths | paymentMethod   | requestedStartDate |
 | 
					      | customerId                             | durationInMonths | paymentMethod   | requestedStartDate |
 | 
				
			||||||
      | 11111111-1111-1111-1111-111111111111   | 12               | LOYALTY_POINTS  | 2024-01-01        |
 | 
					      | 11111111-1111-1111-1111-111111111111   | 12               | LOYALTY_POINTS  | 2024-01-01        |
 | 
				
			||||||
    Then the request fails
 | 
					    Then the subscription request fails
 | 
				
			||||||
    And I receive an error message containing "Not enough loyalty points"
 | 
					    And I receive a subscription error message containing "Not enough loyalty points"
 | 
				
			||||||
    And the system still has 1 subscription
 | 
					    And the subscription system still has 1 subscription
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Scenario: Attempt to request a subscription with invalid details
 | 
					  Scenario: Attempt to request a subscription with invalid details
 | 
				
			||||||
    When I try to request a new subscription with the following information:
 | 
					    When I try to request a new subscription with the following information:
 | 
				
			||||||
      | customerId | durationInMonths | paymentMethod | requestedStartDate |
 | 
					      | customerId | durationInMonths | paymentMethod | requestedStartDate |
 | 
				
			||||||
      |            |                  |               |                   |
 | 
					      |            |                  |               |                   |
 | 
				
			||||||
    Then the request fails
 | 
					    Then the subscription request fails
 | 
				
			||||||
    And I receive an error message containing "Invalid subscription details or payment method"
 | 
					    And I receive a subscription error message containing "Invalid subscription details or payment method"
 | 
				
			||||||
    And the system still has 1 subscription
 | 
					    And the subscription system still has 1 subscription
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Scenario: Attempt to request a subscription for unknown customer
 | 
					  Scenario: Attempt to request a subscription for unknown customer
 | 
				
			||||||
    When I try to request a new subscription with the following information:
 | 
					    When I try to request a new subscription with the following information:
 | 
				
			||||||
      | customerId                             | durationInMonths | paymentMethod  | requestedStartDate |
 | 
					      | customerId                             | durationInMonths | paymentMethod  | requestedStartDate |
 | 
				
			||||||
      | 99999999-9999-9999-9999-999999999999   | 6                | CREDIT_CARD    | 2024-01-01        |
 | 
					      | 99999999-9999-9999-9999-999999999999   | 6                | CREDIT_CARD    | 2024-01-01        |
 | 
				
			||||||
    Then the request fails
 | 
					    Then the subscription request fails
 | 
				
			||||||
    And I receive an error message containing "Customer not found"
 | 
					    And I receive a subscription error message containing "Customer not found"
 | 
				
			||||||
    And the system still has 1 subscription
 | 
					    And the subscription system still has 1 subscription
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Scenario: Attempt to get subscription for unknown customer
 | 
					  Scenario: Attempt to get subscription for unknown customer
 | 
				
			||||||
    When I request the subscription for customer "99999999-9999-9999-9999-999999999999"
 | 
					    When I request the subscription for customer "99999999-9999-9999-9999-999999999999"
 | 
				
			||||||
    Then the request fails
 | 
					    Then the subscription request fails
 | 
				
			||||||
    And I receive an error message containing "Subscription not found for the customer"
 | 
					    And I receive a subscription error message containing "Subscription not found for the customer"
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user