forked from pierront/mylibrary-template
		
	subscription step
This commit is contained in:
		@@ -0,0 +1,56 @@
 | 
			
		||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDate;
 | 
			
		||||
 | 
			
		||||
public class Subscription {
 | 
			
		||||
    private String subscriptionId;
 | 
			
		||||
    private String customerId;
 | 
			
		||||
    private int durationInMonths;
 | 
			
		||||
    private LocalDate startDate;
 | 
			
		||||
    private LocalDate endDate;
 | 
			
		||||
 | 
			
		||||
    public Subscription(String subscriptionId, String customerId, int durationInMonths, LocalDate startDate) {
 | 
			
		||||
        this.subscriptionId = subscriptionId;
 | 
			
		||||
        this.customerId = customerId;
 | 
			
		||||
        this.durationInMonths = durationInMonths;
 | 
			
		||||
        this.startDate = startDate;
 | 
			
		||||
        this.endDate = startDate.plusMonths(durationInMonths).minusDays(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getSubscriptionId() {
 | 
			
		||||
        return subscriptionId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getCustomerId() {
 | 
			
		||||
        return customerId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getDurationInMonths() {
 | 
			
		||||
        return durationInMonths;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public LocalDate getStartDate() {
 | 
			
		||||
        return startDate;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public LocalDate getEndDate() {
 | 
			
		||||
        return endDate;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setSubscriptionId(String subscriptionId) {
 | 
			
		||||
        this.subscriptionId = subscriptionId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setCustomerId(String customerId) {
 | 
			
		||||
        this.customerId = customerId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setDurationInMonths(int durationInMonths) {
 | 
			
		||||
        this.durationInMonths = durationInMonths;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setStartDate(LocalDate startDate) {
 | 
			
		||||
        this.startDate = startDate;
 | 
			
		||||
        this.endDate = startDate.plusMonths(durationInMonths).minusDays(1);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,48 @@
 | 
			
		||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDate;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
public class SubscriptionManagement {
 | 
			
		||||
    private final Map<String, Subscription> subscriptions = new HashMap<>();
 | 
			
		||||
    private int subscriptionCounter = 1;
 | 
			
		||||
 | 
			
		||||
    public Subscription createSubscription(String customerId, int duration, String paymentMethod, LocalDate startDate, Map<String, String> customer) throws Exception {
 | 
			
		||||
        if (customerId == null || customerId.isBlank() ||
 | 
			
		||||
                paymentMethod == null || paymentMethod.isBlank() ||
 | 
			
		||||
                startDate == null) {
 | 
			
		||||
            throw new IllegalArgumentException("Invalid subscription details or payment method");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (customer == null) {
 | 
			
		||||
            throw new IllegalArgumentException("Customer not found");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (paymentMethod.equals("LOYALTY_POINTS")) {
 | 
			
		||||
            int points = Integer.parseInt(customer.getOrDefault("loyaltyPoints", "0"));
 | 
			
		||||
            if (points < duration * 10) {
 | 
			
		||||
                throw new IllegalArgumentException("Not enough loyalty points");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        String newId = "sub-" + (subscriptions.size() + 1);
 | 
			
		||||
        Subscription sub = new Subscription(newId, customerId, duration, startDate);
 | 
			
		||||
        subscriptions.put(newId, sub);
 | 
			
		||||
        return sub;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Subscription getSubscriptionForCustomer(String customerId) {
 | 
			
		||||
        for (Subscription s : subscriptions.values()) {
 | 
			
		||||
            if (s.getCustomerId().equals(customerId)) return s;
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getSubscriptionCount() {
 | 
			
		||||
        return subscriptions.size();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Collection<Subscription> getAllSubscriptions() {
 | 
			
		||||
        return subscriptions.values();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -126,7 +126,7 @@ public class SubscriptionSteps {
 | 
			
		||||
    @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
 | 
			
		||||
        lastRequestSuccess = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Then("the subscription request fails")
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,24 @@
 | 
			
		||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.error;
 | 
			
		||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.function.SubscriptionFunctionTest;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import io.cucumber.datatable.DataTable;
 | 
			
		||||
import io.cucumber.java.en.And;sr
 | 
			
		||||
import io.cucumber.java.en.Then;
 | 
			
		||||
 | 
			
		||||
public class SubscriptionErrorTest extends SubscriptionFunctionTest {
 | 
			
		||||
 | 
			
		||||
    List<Map<String, String>> rows = DataTable.asMaps();
 | 
			
		||||
    private final Map<String, Map<String, String>> subscriptions = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
    @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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,112 @@
 | 
			
		||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.function;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
import java.time.LocalDate;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import io.cucumber.datatable.DataTable;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import io.cucumber.java.en.And;
 | 
			
		||||
import io.cucumber.java.en.When;
 | 
			
		||||
import io.cucumber.java.en.Given;
 | 
			
		||||
 | 
			
		||||
public class SubscriptionFunctionTest {
 | 
			
		||||
 | 
			
		||||
    List<Map<String, String>> rows = DataTable.asMaps(String.class, String.class);
 | 
			
		||||
    private final Map<String, Map<String, String>> subscriptions = new HashMap<>();
 | 
			
		||||
    private final Map<String, Map<String, String>> customers = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
    protected boolean lastRequestSuccess;
 | 
			
		||||
    protected String lastErrorMessage;
 | 
			
		||||
    protected Map<String, String> lastSubscriptionCreated;
 | 
			
		||||
    protected 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().getFirst();
 | 
			
		||||
        String customerId = info.get("customerId");
 | 
			
		||||
        String duration = info.get("durationInMonths");
 | 
			
		||||
        String paymentMethod = info.get("paymentMethod");
 | 
			
		||||
        String requestedStartDate = info.get("requestedStartDate");
 | 
			
		||||
 | 
			
		||||
        // (logique copiée, à remplacer ensuite par SubscriptionManagement)
 | 
			
		||||
        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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @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";
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @When("I try to request a new subscription with the following information:")
 | 
			
		||||
    public void iTryToRequestANewSubscriptionWithTheFollowingInformation(DataTable dataTable) {
 | 
			
		||||
        iRequestANewSubscriptionWithTheFollowingInformation(dataTable);
 | 
			
		||||
        lastRequestSuccess = false;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,32 @@
 | 
			
		||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.result;
 | 
			
		||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.function.SubscriptionFunctionTest;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
 | 
			
		||||
import io.cucumber.cienvironment.internal.com.eclipsesource.json.JsonArray;
 | 
			
		||||
import io.cucumber.datatable.DataTable;
 | 
			
		||||
import io.cucumber.java.en.And;
 | 
			
		||||
import io.cucumber.java.en.Then;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
public class SubscriptionResultTest extends SubscriptionFunctionTest {
 | 
			
		||||
    private final Map<String, Map<String, String>> subscriptions = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
    @And("the subscription system still has {int} subscription")
 | 
			
		||||
    public void theSubscriptionSystemStillHasSubscription(int expected) {
 | 
			
		||||
        assertEquals(expected, subscriptions.size());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @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));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @And("the subscription system now has {int} subscriptions")
 | 
			
		||||
    public void theSubscriptionSystemNowHasSubscriptions(int expected) {
 | 
			
		||||
        assertEquals(expected, subscriptions.size());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user