Suite des tests pour passer les commandes, correction des tests

This commit is contained in:
AISSI-JUDE-CHRIST
2026-04-17 17:23:02 +02:00
parent 7b4b8124a7
commit 4815d2d0ba
3 changed files with 61 additions and 11 deletions
@@ -1,14 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
import java.text.MessageFormat; 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) { public BookNotFoundException(String isbn) {
super(); //super(MessageFormat.format(THE_BOOK_WITH_ID_DOES_NOT_EXISTS, uuid)); à corriger super(MessageFormat.format(THE_BOOK_WITH_ISBN_DOES_NOT_EXIST, isbn));
} }
} }
@@ -1,6 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.order.usecase; 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.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.book.repository.BookRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer; import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException; import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException;
@@ -22,14 +23,14 @@ public final class OrderUseCase {
} }
public OrderDTO placeOrder(OrderInfo orderInfo) public OrderDTO placeOrder(OrderInfo orderInfo)
throws CustomerNotFoundException, InsufficientStockException { throws CustomerNotFoundException, InsufficientStockException, BookNotFoundException {
Customer customer = customerRepository.findById(orderInfo.customerId()) Customer customer = customerRepository.findById(orderInfo.customerId())
.orElseThrow(() -> new CustomerNotFoundException(orderInfo.customerId())); .orElseThrow(() -> new CustomerNotFoundException(orderInfo.customerId()));
double totalAmount = 0; double totalAmount = 0;
for (OrderLineInfo orderLine : orderInfo.orderLines()) { for (OrderLineInfo orderLine : orderInfo.orderLines()) {
Book book = bookRepository.findByIsbn(orderLine.bookIsbn()) Book book = bookRepository.findByIsbn(orderLine.bookIsbn())
.orElseThrow(() -> new IllegalArgumentException("Book not found")); .orElseThrow(() -> new BookNotFoundException(orderLine.bookIsbn()));
if (orderLine.quantity() > book.getStock()) { if (orderLine.quantity() > book.getStock()) {
throw new InsufficientStockException(book.getIsbn(), orderLine.quantity(), book.getStock()); throw new InsufficientStockException(book.getIsbn(), orderLine.quantity(), book.getStock());
} }
@@ -10,6 +10,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book; 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.book.repository.BookRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer; import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException; import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException;
@@ -45,7 +46,7 @@ class OrderUseCaseTest {
@Test @Test
@DisplayName("Should place order and return expected amount and loyalty points") @DisplayName("Should place order and return expected amount and loyalty points")
void shouldPlaceOrderAndReturnExpectedAmountAndLoyaltyPoints() void shouldPlaceOrderAndReturnExpectedAmountAndLoyaltyPoints()
throws CustomerNotFoundException, InsufficientStockException { throws CustomerNotFoundException, InsufficientStockException, BookNotFoundException {
UUID customerId = UUID.randomUUID(); UUID customerId = UUID.randomUUID();
Customer customer = Customer.builder() Customer customer = Customer.builder()
.id(customerId) .id(customerId)
@@ -94,7 +95,7 @@ class OrderUseCaseTest {
@Test @Test
@DisplayName("Should throw when requested quantity is greater than available stock") @DisplayName("Should throw when requested quantity is greater than available stock")
void shouldThrowWhenRequestedQuantityIsGreaterThanAvailableStock() { void shouldThrowWhenRequestedQuantityIsGreaterThanAvailableStock() throws BookNotFoundException {
UUID customerId = UUID.randomUUID(); UUID customerId = UUID.randomUUID();
Customer customer = Customer.builder() Customer customer = Customer.builder()
.id(customerId) .id(customerId)
@@ -132,4 +133,53 @@ class OrderUseCaseTest {
verify(bookRepository, never()).save(any(Book.class)); verify(bookRepository, never()).save(any(Book.class));
verify(customerRepository, never()).save(any(Customer.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));
}
} }