Compare commits

...

5 Commits

Author SHA1 Message Date
Kroccmou
2b092b62c1 Fini 2025-06-14 15:21:46 +02:00
Kroccmou
2d2bdfbcd6 Update SubscriptionFunctionTest.java 2025-06-14 01:56:15 +02:00
Kroccmou
c3140715e4 Update SubscriptionErrorTest.java 2025-06-14 01:56:11 +02:00
6820863c8e Merge branch 'main' of grond.iut-fbleau.fr:daniel/mylibrary 2025-06-14 01:50:46 +02:00
d8f63d1bdd subscription step 2025-06-14 01:49:19 +02:00
24 changed files with 836 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order;
import java.util.List;
import java.util.UUID;
public class OrderDTO {
private UUID id;
private UUID customerId;
private List<OrderInfo.OrderLineDto> orderLines;
private double totalPrice;
private double totalPriceToPay;
private OrderInfo.Address address;
private String paymentMethod;
// Getters et setters
public UUID getId() { return id; }
public void setId(UUID id) { this.id = id; }
public UUID getCustomerId() { return customerId; }
public void setCustomerId(UUID customerId) { this.customerId = customerId; }
public List<OrderInfo.OrderLineDto> getOrderLines() { return orderLines; }
public void setOrderLines(List<OrderInfo.OrderLineDto> orderLines) { this.orderLines = orderLines; }
public double getTotalPrice() { return totalPrice; }
public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; }
public double getTotalPriceToPay() { return totalPriceToPay; }
public void setTotalPriceToPay(double totalPriceToPay) { this.totalPriceToPay = totalPriceToPay; }
public OrderInfo.Address getAddress() { return address; }
public void setAddress(OrderInfo.Address address) { this.address = address; }
public String getPaymentMethod() { return paymentMethod; }
public void setPaymentMethod(String paymentMethod) { this.paymentMethod = paymentMethod; }
}

View File

@@ -0,0 +1,72 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order;
import java.util.List;
import java.util.UUID;
public class OrderInfo {
private UUID customerId;
private List<OrderLineDto> orderLineDtos;
private Address address;
private String paymentMethod; // "CREDIT_CARD" ou "LOYALTY_POINTS"
// POJO interne pour OrderLineDto
public static class OrderLineDto {
private long bookId;
private int quantity;
public OrderLineDto() {}
public OrderLineDto(long bookId, int quantity) {
this.bookId = bookId;
this.quantity = quantity;
}
public long getBookId() { return bookId; }
public void setBookId(long bookId) { this.bookId = bookId; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
}
// POJO interne pour Address
public static class Address {
private String street;
private String city;
private String postalCode;
private String country;
public Address() {}
public Address(String street, String city, String postalCode, String country) {
this.street = street;
this.city = city;
this.postalCode = postalCode;
this.country = country;
}
public String getStreet() { return street; }
public void setStreet(String street) { this.street = street; }
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
public String getPostalCode() { return postalCode; }
public void setPostalCode(String postalCode) { this.postalCode = postalCode; }
public String getCountry() { return country; }
public void setCountry(String country) { this.country = country; }
}
// Getters et setters
public UUID getCustomerId() { return customerId; }
public void setCustomerId(UUID customerId) { this.customerId = customerId; }
public List<OrderLineDto> getOrderLineDtos() { return orderLineDtos; }
public void setOrderLineDtos(List<OrderLineDto> orderLineDtos) { this.orderLineDtos = orderLineDtos; }
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
public String getPaymentMethod() { return paymentMethod; }
public void setPaymentMethod(String paymentMethod) { this.paymentMethod = paymentMethod; }
}

View File

@@ -0,0 +1,4 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.converter;
public class OrderConverterTest {
}

View File

@@ -0,0 +1,34 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.entity;
import java.util.List;
import java.util.UUID;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
public class Order {
private UUID id;
private UUID customerId;
private List<OrderInfo.OrderLineDto> orderLines;
private double totalPrice;
private double totalPriceToPay;
private OrderInfo.Address address;
private String paymentMethod;
public Order(UUID id, UUID customerId, List<OrderInfo.OrderLineDto> orderLines, double totalPrice, double totalPriceToPay, OrderInfo.Address address, String paymentMethod) {
this.id = id;
this.customerId = customerId;
this.orderLines = orderLines;
this.totalPrice = totalPrice;
this.totalPriceToPay = totalPriceToPay;
this.address = address;
this.paymentMethod = paymentMethod;
}
// Getters
public UUID getId() { return id; }
public UUID getCustomerId() { return customerId; }
public List<OrderInfo.OrderLineDto> getOrderLines() { return orderLines; }
public double getTotalPrice() { return totalPrice; }
public double getTotalPriceToPay() { return totalPriceToPay; }
public OrderInfo.Address getAddress() { return address; }
public String getPaymentMethod() { return paymentMethod; }
}

View File

@@ -0,0 +1,12 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.exception;
import java.text.MessageFormat;
public class BookQuantityInsufficientException extends Exception {
public static final String BOOK_QUANTITY_INSUFFICIENT = "Cannot order {0} books, only {1} in stock";
public BookQuantityInsufficientException(int requested, int available) {
super(MessageFormat.format(BOOK_QUANTITY_INSUFFICIENT, requested, available));
}
}

View File

@@ -0,0 +1,16 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.exception;
import java.text.MessageFormat;
public class InvalidOrderException extends Exception {
public static final String INVALID_ORDER_DETAILS_OR_ADDRESS = "Invalid order details or address";
public InvalidOrderException() {
super(INVALID_ORDER_DETAILS_OR_ADDRESS);
}
public InvalidOrderException(String details) {
super(details);
}
}

View File

@@ -0,0 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.exception;
import java.text.MessageFormat;
import java.util.UUID;
public class OrderNotFoundException extends Exception {
public static final String THE_ORDER_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The order with id {0} does not exist";
public OrderNotFoundException(UUID uuid) {
super(MessageFormat.format(THE_ORDER_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
}
}

View File

@@ -0,0 +1,30 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order;
import java.util.*;
public class OrderRepository {
private final Map<UUID, Order> orders = new HashMap<>();
public Order save(OrderInfo info) {
UUID id = UUID.randomUUID();
Order order = new Order(
id,
info.getCustomerId(),
info.getOrderLineDtos(),
0.0, // totalPrice à calculer selon la logique métier
0.0, // totalPriceToPay à calculer
info.getAddress(),
info.getPaymentMethod()
);
orders.put(id, order);
return order;
}
public Optional<Order> findById(UUID id) {
return Optional.ofNullable(orders.get(id));
}
}

View File

@@ -0,0 +1,45 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order;
import fr.iut_fbleau.but3.dev62.mylibrary.order.repository.OrderRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.order.validator.OrderValidator;
import java.util.Optional;
import java.util.UUID;
public class OrderUseCase {
private final OrderRepository repository;
public OrderUseCase(OrderRepository repository) {
this.repository = repository;
}
public OrderDTO createOrder(OrderInfo info) throws Exception {
if (!OrderValidator.isValidOrderInfo(info)) {
throw new IllegalArgumentException("Invalid order details or address");
}
// Ici, on suppose que le repository gère la logique métier (stock, client, etc.)
Order order = repository.save(info);
return toDto(order);
}
public Optional<OrderDTO> getOrderById(UUID id) {
Optional<Order> order = repository.findById(id);
return order.map(this::toDto);
}
private OrderDTO toDto(Order order) {
OrderDTO dto = new OrderDTO();
dto.setId(order.getId());
dto.setCustomerId(order.getCustomerId());
dto.setOrderLines(order.getOrderLines());
dto.setTotalPrice(order.getTotalPrice());
dto.setTotalPriceToPay(order.getTotalPriceToPay());
dto.setAddress(order.getAddress());
dto.setPaymentMethod(order.getPaymentMethod());
return dto;
}
}

View File

@@ -0,0 +1,47 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.validator;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
import java.util.List;
import java.util.UUID;
public class OrderValidator {
public static boolean isValidUUID(String uuid) {
try {
UUID.fromString(uuid);
return true;
} catch (Exception e) {
return false;
}
}
public static boolean isValidOrderInfo(OrderInfo info) {
if (info == null) return false;
if (info.getCustomerId() == null) return false;
if (info.getOrderLineDtos() == null || info.getOrderLineDtos().isEmpty()) return false;
if (info.getPaymentMethod() == null || info.getPaymentMethod().isBlank()) return false;
// Vérifie chaque ligne de commande
for (OrderInfo.OrderLineDto line : info.getOrderLineDtos()) {
if (line == null) return false;
if (line.getBookId() <= 0) return false;
if (line.getQuantity() <= 0) return false;
}
// Vérifie l'adresse
if (info.getAddress() == null) return false;
OrderInfo.Address addr = info.getAddress();
if (addr.getStreet() == null || addr.getStreet().isBlank()) return false;
if (addr.getCity() == null || addr.getCity().isBlank()) return false;
if (addr.getPostalCode() == null || addr.getPostalCode().isBlank()) return false;
if (addr.getCountry() == null || addr.getCountry().isBlank()) return false;
// Vérifie le mode de paiement
if (!info.getPaymentMethod().equals("CREDIT_CARD") && !info.getPaymentMethod().equals("LOYALTY_POINTS")) {
return false;
}
return true;
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -126,7 +126,7 @@ public class SubscriptionSteps {
@When("I try to request a new subscription with the following information:") @When("I try to request a new subscription with the following information:")
public void iTryToRequestANewSubscriptionWithTheFollowingInformation(DataTable dataTable) { public void iTryToRequestANewSubscriptionWithTheFollowingInformation(DataTable dataTable) {
iRequestANewSubscriptionWithTheFollowingInformation(dataTable); iRequestANewSubscriptionWithTheFollowingInformation(dataTable);
lastRequestSuccess = false; // Pour forcer l'échec dans les assertions lastRequestSuccess = false;
} }
@Then("the subscription request fails") @Then("the subscription request fails")

View File

@@ -0,0 +1,38 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.converter;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
import java.util.*;
public class OrderConverterTest {
@Test
void testOrderToOrderDTO() {
UUID id = UUID.randomUUID();
UUID customerId = UUID.randomUUID();
List<OrderInfo.OrderLineDto> lines = List.of(new OrderInfo.OrderLineDto(1234567890123L, 1));
OrderInfo.Address address = new OrderInfo.Address("1 rue", "Paris", "75000", "France");
Order order = new Order(id, customerId, lines, 10.0, 10.0, address, "CREDIT_CARD");
OrderDTO dto = new OrderDTO();
dto.setId(order.getId());
dto.setCustomerId(order.getCustomerId());
dto.setOrderLines(order.getOrderLines());
dto.setTotalPrice(order.getTotalPrice());
dto.setTotalPriceToPay(order.getTotalPriceToPay());
dto.setAddress(order.getAddress());
dto.setPaymentMethod(order.getPaymentMethod());
assertEquals(order.getId(), dto.getId());
assertEquals(order.getCustomerId(), dto.getCustomerId());
assertEquals(order.getOrderLines(), dto.getOrderLines());
assertEquals(order.getTotalPrice(), dto.getTotalPrice());
assertEquals(order.getTotalPriceToPay(), dto.getTotalPriceToPay());
assertEquals(order.getAddress(), dto.getAddress());
assertEquals(order.getPaymentMethod(), dto.getPaymentMethod());
}
}

View File

@@ -0,0 +1,31 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.entity;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
public class OrderTest {
@Test
void testOrderConstructorAndGetters() {
UUID id = UUID.randomUUID();
UUID customerId = UUID.randomUUID();
List<OrderInfo.OrderLineDto> orderLines = new ArrayList<>();
orderLines.add(new OrderInfo.OrderLineDto(1234567890123L, 2));
double totalPrice = 50.0;
double totalPriceToPay = 45.0;
OrderInfo.Address address = new OrderInfo.Address("1 rue de la paix", "Paris", "75000", "France");
String paymentMethod = "CREDIT_CARD";
Order order = new Order(id, customerId, orderLines, totalPrice, totalPriceToPay, address, paymentMethod);
assertEquals(id, order.getId());
assertEquals(customerId, order.getCustomerId());
assertEquals(orderLines, order.getOrderLines());
assertEquals(totalPrice, order.getTotalPrice());
assertEquals(totalPriceToPay, order.getTotalPriceToPay());
assertEquals(address, order.getAddress());
assertEquals(paymentMethod, order.getPaymentMethod());
}
}

View File

@@ -0,0 +1,12 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.exception;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class BookQuantityInsufficientExceptionTest {
@Test
void testMessage() {
BookQuantityInsufficientException ex = new BookQuantityInsufficientException(5, 2);
assertEquals("Cannot order 5 books, only 2 in stock", ex.getMessage());
}
}

View File

@@ -0,0 +1,12 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.exception;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class InvalidOrderExceptionTest {
@Test
void testMessage() {
InvalidOrderException ex = new InvalidOrderException();
assertEquals("Invalid order details or address", ex.getMessage());
}
}

View File

@@ -0,0 +1,14 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.exception;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.UUID;
public class OrderNotFoundExceptionTest {
@Test
void testMessage() {
UUID uuid = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
OrderNotFoundException ex = new OrderNotFoundException(uuid);
assertEquals("The order with id 123e4567-e89b-12d3-a456-426614174000 does not exist", ex.getMessage());
}
}

View File

@@ -0,0 +1,28 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.repository;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order;
import java.util.*;
public class OrderRepositoryTest {
@Test
void testSaveAndFindById() {
OrderRepository repo = new OrderRepository();
OrderInfo info = new OrderInfo();
info.setCustomerId(UUID.randomUUID());
info.setOrderLineDtos(List.of(new OrderInfo.OrderLineDto(1234567890123L, 1)));
info.setAddress(new OrderInfo.Address("1 rue", "Paris", "75000", "France"));
info.setPaymentMethod("CREDIT_CARD");
Order saved = repo.save(info);
assertNotNull(saved.getId());
Optional<Order> found = repo.findById(saved.getId());
assertTrue(found.isPresent());
assertEquals(saved.getId(), found.get().getId());
}
}

View File

@@ -0,0 +1,44 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.usecase;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import fr.iut_fbleau.but3.dev62.mylibrary.order.repository.OrderRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderDTO;
import java.util.*;
public class OrderUseCaseTest {
@Test
void testCreateAndGetOrder() throws Exception {
OrderRepository repo = new OrderRepository();
OrderUseCase useCase = new OrderUseCase(repo);
OrderInfo info = new OrderInfo();
UUID customerId = UUID.randomUUID();
info.setCustomerId(customerId);
info.setOrderLineDtos(List.of(new OrderInfo.OrderLineDto(1234567890123L, 1)));
info.setAddress(new OrderInfo.Address("1 rue", "Paris", "75000", "France"));
info.setPaymentMethod("CREDIT_CARD");
OrderDTO dto = useCase.createOrder(info);
assertNotNull(dto.getId());
assertEquals(customerId, dto.getCustomerId());
Optional<OrderDTO> found = useCase.getOrderById(dto.getId());
assertTrue(found.isPresent());
assertEquals(dto.getId(), found.get().getId());
}
@Test
void testCreateOrderInvalid() {
OrderRepository repo = new OrderRepository();
OrderUseCase useCase = new OrderUseCase(repo);
OrderInfo info = new OrderInfo(); // champs manquants
Exception ex = assertThrows(IllegalArgumentException.class, () -> useCase.createOrder(info));
assertEquals("Invalid order details or address", ex.getMessage());
}
}

View File

@@ -0,0 +1,81 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.validator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo;
import java.util.*;
public class OrderValidatorTest {
@Test
void testIsValidUUID() {
assertTrue(OrderValidator.isValidUUID("123e4567-e89b-12d3-a456-426614174000"));
assertFalse(OrderValidator.isValidUUID("not-a-uuid"));
assertFalse(OrderValidator.isValidUUID(""));
assertFalse(OrderValidator.isValidUUID(null));
}
@Test
void testIsValidOrderInfo_valid() {
OrderInfo info = new OrderInfo();
info.setCustomerId(UUID.randomUUID());
List<OrderInfo.OrderLineDto> lines = new ArrayList<>();
lines.add(new OrderInfo.OrderLineDto(1234567890123L, 2));
info.setOrderLineDtos(lines);
OrderInfo.Address address = new OrderInfo.Address("1 rue", "Paris", "75000", "France");
info.setAddress(address);
info.setPaymentMethod("CREDIT_CARD");
assertTrue(OrderValidator.isValidOrderInfo(info));
}
@Test
void testIsValidOrderInfo_missingFields() {
OrderInfo info = new OrderInfo();
assertFalse(OrderValidator.isValidOrderInfo(info));
info.setCustomerId(UUID.randomUUID());
assertFalse(OrderValidator.isValidOrderInfo(info));
info.setOrderLineDtos(new ArrayList<>());
assertFalse(OrderValidator.isValidOrderInfo(info));
List<OrderInfo.OrderLineDto> lines = new ArrayList<>();
lines.add(new OrderInfo.OrderLineDto(0, 2)); // bookId <= 0
info.setOrderLineDtos(lines);
assertFalse(OrderValidator.isValidOrderInfo(info));
lines.clear();
lines.add(new OrderInfo.OrderLineDto(1234567890123L, 0)); // quantity <= 0
info.setOrderLineDtos(lines);
assertFalse(OrderValidator.isValidOrderInfo(info));
lines.clear();
lines.add(new OrderInfo.OrderLineDto(1234567890123L, 2));
info.setOrderLineDtos(lines);
info.setPaymentMethod("CREDIT_CARD");
assertFalse(OrderValidator.isValidOrderInfo(info));
OrderInfo.Address address = new OrderInfo.Address("", "Paris", "75000", "France");
info.setAddress(address);
assertFalse(OrderValidator.isValidOrderInfo(info));
address = new OrderInfo.Address("1 rue", "", "75000", "France");
info.setAddress(address);
assertFalse(OrderValidator.isValidOrderInfo(info));
address = new OrderInfo.Address("1 rue", "Paris", "", "France");
info.setAddress(address);
assertFalse(OrderValidator.isValidOrderInfo(info));
address = new OrderInfo.Address("1 rue", "Paris", "75000", "");
info.setAddress(address);
assertFalse(OrderValidator.isValidOrderInfo(info));
address = new OrderInfo.Address("1 rue", "Paris", "75000", "France");
info.setAddress(address);
info.setPaymentMethod("UNKNOWN");
assertFalse(OrderValidator.isValidOrderInfo(info));
}
}

View File

@@ -0,0 +1,19 @@
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 io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
public class SubscriptionErrorTest extends SubscriptionFunctionTest {
@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));
}
}

View File

@@ -0,0 +1,111 @@
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 {
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;
}
}

View File

@@ -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());
}
}