From 4815d2d0bad44cc826b52e88622cdf4ea0f03830 Mon Sep 17 00:00:00 2001 From: AISSI-JUDE-CHRIST Date: Fri, 17 Apr 2026 17:23:02 +0200 Subject: [PATCH] Suite des tests pour passer les commandes, correction des tests --- .../book/exception/BookNotFoundException.java | 13 +++-- .../mylibrary/order/usecase/OrderUseCase.java | 5 +- .../order/usecase/OrderUseCaseTest.java | 54 ++++++++++++++++++- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java index d62cb3d..6dac031 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java @@ -1,14 +1,13 @@ package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; import java.text.MessageFormat; -import java.util.UUID; -public class BookNotFoundException { +public class BookNotFoundException extends Exception { - public static final String THE_BOOK_WITH_ID_DOES_NOT_EXISTS = "The book with id {0} does not exist"; + public static final String THE_BOOK_WITH_ISBN_DOES_NOT_EXIST = + "The book with isbn {0} does not exist"; - public BookNotFoundException(UUID uuid) { - super(); //super(MessageFormat.format(THE_BOOK_WITH_ID_DOES_NOT_EXISTS, uuid)); à corriger - } - + public BookNotFoundException(String isbn) { + super(MessageFormat.format(THE_BOOK_WITH_ISBN_DOES_NOT_EXIST, isbn)); + } } \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/usecase/OrderUseCase.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/usecase/OrderUseCase.java index b703f0b..bda1575 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/usecase/OrderUseCase.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/usecase/OrderUseCase.java @@ -1,6 +1,7 @@ package fr.iut_fbleau.but3.dev62.mylibrary.order.usecase; import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book; +import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.BookNotFoundException; import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository; import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer; import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException; @@ -22,14 +23,14 @@ public final class OrderUseCase { } public OrderDTO placeOrder(OrderInfo orderInfo) - throws CustomerNotFoundException, InsufficientStockException { + throws CustomerNotFoundException, InsufficientStockException, BookNotFoundException { Customer customer = customerRepository.findById(orderInfo.customerId()) .orElseThrow(() -> new CustomerNotFoundException(orderInfo.customerId())); double totalAmount = 0; for (OrderLineInfo orderLine : orderInfo.orderLines()) { Book book = bookRepository.findByIsbn(orderLine.bookIsbn()) - .orElseThrow(() -> new IllegalArgumentException("Book not found")); + .orElseThrow(() -> new BookNotFoundException(orderLine.bookIsbn())); if (orderLine.quantity() > book.getStock()) { throw new InsufficientStockException(book.getIsbn(), orderLine.quantity(), book.getStock()); } diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/usecase/OrderUseCaseTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/usecase/OrderUseCaseTest.java index ab79a01..4878c6b 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/usecase/OrderUseCaseTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/usecase/OrderUseCaseTest.java @@ -10,6 +10,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book; +import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.BookNotFoundException; import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository; import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer; import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException; @@ -45,7 +46,7 @@ class OrderUseCaseTest { @Test @DisplayName("Should place order and return expected amount and loyalty points") void shouldPlaceOrderAndReturnExpectedAmountAndLoyaltyPoints() - throws CustomerNotFoundException, InsufficientStockException { + throws CustomerNotFoundException, InsufficientStockException, BookNotFoundException { UUID customerId = UUID.randomUUID(); Customer customer = Customer.builder() .id(customerId) @@ -94,7 +95,7 @@ class OrderUseCaseTest { @Test @DisplayName("Should throw when requested quantity is greater than available stock") - void shouldThrowWhenRequestedQuantityIsGreaterThanAvailableStock() { + void shouldThrowWhenRequestedQuantityIsGreaterThanAvailableStock() throws BookNotFoundException { UUID customerId = UUID.randomUUID(); Customer customer = Customer.builder() .id(customerId) @@ -132,4 +133,53 @@ class OrderUseCaseTest { verify(bookRepository, never()).save(any(Book.class)); verify(customerRepository, never()).save(any(Customer.class)); } + + @Test + @DisplayName("Should throw when customer does not exist") + void shouldThrowWhenCustomerDoesNotExist() { + UUID customerId = UUID.randomUUID(); + OrderInfo orderInfo = new OrderInfo( + customerId, + List.of(new OrderLineInfo("9780132350884", 1)), + new OrderAddressInfo("1 rue de Paris", "Paris", "75000", "France"), + PaymentMode.CB + ); + + when(customerRepository.findById(customerId)).thenReturn(Optional.empty()); + + assertThrows(CustomerNotFoundException.class, () -> orderUseCase.placeOrder(orderInfo)); + verify(customerRepository, times(1)).findById(customerId); + verify(bookRepository, never()).findByIsbn(any(String.class)); + verify(bookRepository, never()).save(any(Book.class)); + verify(customerRepository, never()).save(any(Customer.class)); + } + + @Test + @DisplayName("Should throw when ordered book does not exist") + void shouldThrowWhenOrderedBookDoesNotExist() { + UUID customerId = UUID.randomUUID(); + Customer customer = Customer.builder() + .id(customerId) + .firstName("Jane") + .lastName("Doe") + .phoneNumber("0612345678") + .loyaltyPoints(10) + .build(); + String unknownIsbn = "9999999999999"; + OrderInfo orderInfo = new OrderInfo( + customerId, + List.of(new OrderLineInfo(unknownIsbn, 1)), + new OrderAddressInfo("1 rue de Paris", "Paris", "75000", "France"), + PaymentMode.CB + ); + + when(customerRepository.findById(customerId)).thenReturn(Optional.of(customer)); + when(bookRepository.findByIsbn(unknownIsbn)).thenReturn(Optional.empty()); + + assertThrows(BookNotFoundException.class, () -> orderUseCase.placeOrder(orderInfo)); + verify(customerRepository, times(1)).findById(customerId); + verify(bookRepository, times(1)).findByIsbn(unknownIsbn); + verify(bookRepository, never()).save(any(Book.class)); + verify(customerRepository, never()).save(any(Customer.class)); + } }