From 276b6e4fc6f5d0d6d51c19059b627acb4af5b857 Mon Sep 17 00:00:00 2001 From: Tom Moguljak Date: Wed, 11 Jun 2025 00:15:23 +0200 Subject: [PATCH] =?UTF-8?q?Sauvegarde=20de=20l'avanc=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mylibrary/features/order/OrderSteps.java | 250 ++++++++++++++++++ src/test/resources/features/oder.feature | 153 +++++++++++ 2 files changed, 403 insertions(+) create mode 100644 src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/features/order/OrderSteps.java create mode 100644 src/test/resources/features/oder.feature diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/features/order/OrderSteps.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/features/order/OrderSteps.java new file mode 100644 index 0000000..10b0eb0 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/features/order/OrderSteps.java @@ -0,0 +1,250 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.features.order; + +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 fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; +import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderRepository; +import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderDTO; +import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.OrderNotFoundException; +import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.IllegalOrderPointException; +import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException; +import fr.iut_fbleau.but3.dev62.mylibrary.order.usecase.OrderUseCase; +import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer; +import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository; +import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book; +import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository; +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.*; +import java.text.ParseException; +import java.text.SimpleDateFormat; + + +public class OrderSteps { + private final OrderRepository orderRepository = new OrderRepository(); + private final OrderUseCase orderUseCase = new OrderUseCase(orderRepository); + private NotValidOrderException notValidOrderException; + private IllegalOrderPointException illegalOrderPointException; + private OrderNotFoundException orderNotFoundException; + + private Optional orderById; + private UUID orderRegistration; + + private final CustomerRepository customerRepository = new CustomerRepository(); + private final Map customerPhoneUUID = new HashMap<>(); + private final BookRepository bookRepository = new BookRepository(); + private final Map BookISBN = new HashMap<>(); + private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); + + private ArrayList listOfStrings(String arg) { + return new ArrayList(Arrays.asList(arg.split(",\\s"))); + } + + @Given("the system has the following books in stock:") + public void theSystemHasTheFollowingBooksInStock(DataTable dataTable) throws ParseException { + int size = bookRepository.findAll().size(); + + if (size > 0) { + bookRepository.deleteAll(); + } + + List> books = dataTable.asMaps(String.class, String.class); + + for (Map book : books) { + String ISBN = book.get("ISBN"); + Book newBook = Book.builder() + .isbn(ISBN) + .title(book.get("titre")) + .author(book.get("auteur")) + .publisher(book.get("editeur")) + .date(formatter.parse(book.get("datePublication"))) + .price(Double.parseDouble(book.get("prix"))) + .initialStock(Integer.parseInt(book.get("stockInitial"))) + .categories(listOfStrings(book.get("categories"))) + .description(book.get("description")) + .language(book.get("langue")) + .build(); + Book save = bookRepository.save(newBook); + BookISBN.put(ISBN, save.getIsbn()); + } + + assertEquals(books.size(), bookRepository.findAll().size()); + } + + @Given("the system has the following customers:") + public void theSystemHasTheFollowingCustomers(DataTable dataTable) { + int size = customerRepository.findAll().size(); + + if (size > 0) { + customerRepository.deleteAll(); + } + + List> customers = dataTable.asMaps(String.class, String.class); + + for (Map customer : customers) { + String numeroTelephone = customer.get("numeroTelephone"); + Customer newCustomer = Customer.builder() + .firstName(customer.get("prenom")) + .lastName(customer.get("nom")) + .phoneNumber(numeroTelephone) + .loyaltyPoints(Integer.parseInt(customer.get("pointsFidelite"))) + .build(); + Customer save = customerRepository.save(newCustomer); + customerPhoneUUID.put(numeroTelephone, save.getId()); + } + + assertEquals(customers.size(), customerRepository.findAll().size()); + } + + @When("I create a new order with the following information:") + public void iCreateANewOrderWithTheFollowingInformation(DataTable dataTable) throws NotValidOrderException { + List> rows = dataTable.asMaps(String.class, String.class); + + // Extract the first row of data + Map orderData = rows.getFirst(); + + // Create a new OrderInfo object with the correct keys + OrderInfo newOrder = new OrderInfo( + UUID.fromString(orderData.get("customerId")), + orderData.get("paymentMethod"), + Integer.parseInt(orderData.get("loyaltyPoints")) + ); + + // Register the order + orderRegistration = orderUseCase.registerOrder(newOrder); + } + + @And("the order includes the following books:") + public void theOrderIncludesTheFollowingBooks(DataTable dataTable) { + List> books = dataTable.asMaps(String.class, String.class); + for (Map book : books) { + String isbn = book.get("isbn"); + int quantity = Integer.parseInt(book.get("quantity")); + + Book bookEntity = bookRepository.findByISBN(BookISBN.get(isbn)) + .orElseThrow(() -> new IllegalArgumentException("Book not found: " + isbn)); + + double total = bookEntity.getPrice() * quantity; + orderUseCase.addBookToOrder(orderRegistration, bookEntity, quantity, total); + } + } + + @And("the delivery address is:") + public void theDeliveryAddressIs(DataTable dataTable) { + List> addressData = dataTable.asMaps(String.class, String.class); + if (addressData.isEmpty()) { + throw new IllegalArgumentException("Address data cannot be empty"); + } + Map address = addressData.get(0); + String street = address.get("street"); + String city = address.get("city"); + String postalCode = address.get("postalCode"); + String country = address.get("country"); + + orderUseCase.setDeliveryAddress(orderRegistration, street, city, postalCode, country); + } + + @Then("a new order is created") + public void aNewOrderIsCreated() { + assertNotNull(orderRegistration); + } + + @And("the total price is {double}") + public void theTotalPriceIs(double price) { + Order order = orderRepository.findById(orderRegistration); + assertEquals(price, order.getTotalPrice()); + } + + @And("the loyalty points remain unchanged at {int}") + public void theLoyaltyPointsRemainUnchanged(int loyaltyPoints){ + Order order = orderRepository.findById(orderRegistration); + Customer customer = customerRepository.findById(order.getCustomerId()) + .orElseThrow(() -> new IllegalArgumentException("Customer not found")); + assertEquals(loyaltyPoints, customer.getLoyaltyPoints()); + } + + @And("{int} loyalty points are deducted") + public void loyaltyPointsAreDeducted(int loyaltyPoints) { + Order order = orderRepository.findById(orderRegistration); + Customer customer = customerRepository.findById(order.getCustomerId()) + .orElseThrow(() -> new IllegalArgumentException("Customer not found")); + assertEquals(customer.getLoyaltyPoints(), order.getLoyaltyPoints() - loyaltyPoints); + } + + @And("I receive an error for validation order message containing {string}") + public void iReceiveAnErrorForValidationOrderMessageContaining(String errorMessage) { + assertEquals(errorMessage, notValidOrderException.getMessage()); + } + + @When("I create a new order with an unknown customer:") + public void iCreateANewOrderWithAnUnknownCustomer() { + + } + + @And("I receive an error for not found exception message containing {string}") + public void iReceiveAnErrorForNotFoundExceptionMessageContaining(String errorMessage) { + assertEquals(errorMessage, orderNotFoundException.getMessage()); + } + + @When("I create a new order with insufficient loyalty points:") + public void iCreateANewOrderWithInsufficientLoyaltyPoints() { + } + + @And("I receive an error for illegal order exception message containing {string}") + public void iReceiveAnErrorForIllegalOrderExceptionMessageContaining(String errorMessage) { + assertEquals(errorMessage, illegalOrderPointException.getMessage()); + } + + @When("I create a new order with an invalid payment method:") + public void iCreateANewOrderWithAnInvalidPaymentMethod() { + } + + @And("the order includes no books") + public void theOrderIncludesNoBooks() { + } + + @Given("an order with ID {string} exists for customer {string}") + public void anOrderWithIDExistsForCustomer(String arg0, String arg1) { + } + + @When("I retrieve the order by ID {string}") + public void iRetrieveTheOrderByID(String arg0) { + } + + @When("I request all orders for customer {string}") + public void iRequestAllOrdersForCustomer(String arg0) { + } + + @Then("the retrieval fails") + public void theRetrievalFails() { + } + + @And("I try to set the delivery address to:") + public void iTryToSetTheDeliveryAddressTo() { + } + + @And("I try to order more books than available stock:") + public void iTryToOrderMoreBooksThanAvailableStock() { + } + + @And("I try to order a book that does not exist:") + public void iTryToOrderABookThatDoesNotExist() { + } + + @Then("I receive the order details") + public void iReceiveTheOrderDetails() { + } + + @Then("I receive a list of orders") + public void iReceiveAListOfOrders() { + } +} diff --git a/src/test/resources/features/oder.feature b/src/test/resources/features/oder.feature new file mode 100644 index 0000000..31dabd3 --- /dev/null +++ b/src/test/resources/features/oder.feature @@ -0,0 +1,153 @@ +# language: en + +Feature: Manage customer orders + Background: + Given the system has the following books in stock: + | isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue | + | 1234567890123 | The Pragmatic Programmer | Andy Hunt | Addison-Wesley | 2025-06-10 | 39.99 | 10 | FICTION,THRILLER | A practical guide to becoming a better and more efficient software developer. | EN | + | 9876543210123 | Clean Code | Robert Martin | Prentice Hall | 2024-01-15 | 49.99 | 5 | FICTION | A handbook of best practices for writing readable, maintainable, and clean code in Java. | EN | + And the system has the following customers: + | id | firstName | lastName | phoneNumber | loyaltyPoints | + | 11111111-1111-1111-1111-111111111111 | Alice | Smith | 0612345678 | 100 | + | 22222222-2222-2222-2222-222222222222 | Bob | Martin | 0698765432 | 10 | + + # Create orders + + Scenario: Create an order using credit card + When I create a new order with the following information: + | customerId | paymentMethod | + | 11111111-1111-1111-1111-111111111111 | CREDIT_CARD | + And the order includes the following books: + | bookId | quantity | + | 1234567890123 | 2 | + And the delivery address is: + | street | city | postalCode | country | + | 12 Main St. | Paris | 75000 | France | + Then a new order is created + And the total price is 79.98 + And customer "11111111-1111-1111-1111-111111111111" now has 100 loyalty points + + Scenario: Create an order using loyalty points + When I create a new order with the following information: + | customerId | paymentMethod | + | 11111111-1111-1111-1111-111111111111 | LOYALTY_POINTS | + And the order includes the following books: + | bookId | quantity | + | 9876543210123 | 1 | + And the delivery address is: + | street | city | postalCode | country | + | 42 Book Street | Lyon | 69000 | France | + Then a new order is created + And the total price is 49.99 + And 49 loyalty points are deducted + And customer "11111111-1111-1111-1111-111111111111" now has 51 loyalty points + + Scenario: Attempt to create an order with invalid address + When I create a new order with the following information: + | customerId | paymentMethod | + | 11111111-1111-1111-1111-111111111111 | CREDIT_CARD | + And the order includes the following books: + | bookId | quantity | + | 1234567890123 | 1 | + And I try to set the delivery address to: + | street | city | postalCode | country | + | | | | | + Then the creation fails + And I receive an error for validation order message containing "Address fields are required" + + Scenario: Attempt to create an order with unknown customer + When I create a new order with an unknown customer: + | customerId | paymentMethod | + | 00000000-0000-0000-0000-000000000000 | CREDIT_CARD | + And the order includes the following books: + | bookId | quantity | + | 1234567890123 | 1 | + And the delivery address is: + | street | city | postalCode | country | + | 12 Main St. | Paris | 75000 | France | + Then the creation fails + And I receive an error for not found exception message containing "Customer not found" + + Scenario: Attempt to create an order with insufficient loyalty points + When I create a new order with insufficient loyalty points: + | customerId | paymentMethod | + | 22222222-2222-2222-2222-222222222222 | LOYALTY_POINTS | + And the order includes the following books: + | bookId | quantity | + | 9876543210123 | 1 | + And the delivery address is: + | street | city | postalCode | country | + | 42 Book Street | Lyon | 69000 | France | + Then the creation fails + And I receive an error for illegal order exception message containing "Not enough loyalty points" + + Scenario: Attempt to order more books than available stock + When I create a new order with the following information: + | customerId | paymentMethod | + | 11111111-1111-1111-1111-111111111111 | CREDIT_CARD | + And I try to order more books than available stock: + | bookId | quantity | + | 1234567890123 | 50 | + And the delivery address is: + | street | city | postalCode | country | + | 12 Main St. | Paris | 75000 | France | + Then the creation fails + And I receive an error for illegal order exception message containing "Insufficient book stock" + + Scenario: Attempt to create an order with invalid payment method + When I create a new order with an invalid payment method: + | customerId | paymentMethod | + | 11111111-1111-1111-1111-111111111111 | UNKNOWN | + And the order includes the following books: + | bookId | quantity | + | 1234567890123 | 1 | + And the delivery address is: + | street | city | postalCode | country | + | 12 Main St. | Paris | 75000 | France | + Then the creation fails + And I receive an error for validation order message containing "Payment method is not valid" + + Scenario: Attempt to create an order with unknown book + When I create a new order with the following information: + | customerId | paymentMethod | + | 11111111-1111-1111-1111-111111111111 | CREDIT_CARD | + And I try to order a book that does not exist: + | bookId | quantity | + | unknownBookId | 1 | + And the delivery address is: + | street | city | postalCode | country | + | 12 Main St. | Paris | 75000 | France | + Then the creation fails + And I receive an error for not found exception message containing "Book not found" + + Scenario: Attempt to create an order with empty book list + When I create a new order with the following information: + | customerId | paymentMethod | + | 11111111-1111-1111-1111-111111111111 | CREDIT_CARD | + And the order includes no books + And the delivery address is: + | street | city | postalCode | country | + | 12 Main St. | Paris | 75000 | France | + Then the creation fails + And I receive an error for validation order message containing "Book list cannot be empty" + + #Get orders + + Scenario: Retrieve an order by ID + Given an order with ID "abcd1234-5678-90ef-1234-567890abcdef" exists for customer "11111111-1111-1111-1111-111111111111" + When I retrieve the order by ID "abcd1234-5678-90ef-1234-567890abcdef" + Then I receive the order details + + Scenario: Retrieve all orders for a customer + When I request all orders for customer "11111111-1111-1111-1111-111111111111" + Then I receive a list of orders + + Scenario: Attempt to retrieve an unknown order + When I retrieve the order by ID "unknown-order-id" + Then the retrieval fails + And I receive an error for not found exception message containing "Order not found" + + Scenario: Attempt to retrieve orders for an unknown customer + When I request all orders for customer "00000000-0000-0000-0000-000000000000" + Then the retrieval fails + And I receive an error for not found exception message containing "Customer not found"