forked from pierront/mylibrary-template
stock insufisant & tests à corriger
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.order.exception;
|
||||||
|
|
||||||
|
public class InsufficientStockException extends Exception {
|
||||||
|
|
||||||
|
public InsufficientStockException(String bookIsbn, int requested, int available) {
|
||||||
|
super("Insufficient stock for book " + bookIsbn + ": requested=" + requested
|
||||||
|
+ ", available=" + available);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository
|
|||||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderDTO;
|
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.OrderInfo;
|
||||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderLineInfo;
|
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderLineInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.InsufficientStockException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public final class OrderUseCase {
|
public final class OrderUseCase {
|
||||||
@@ -20,7 +21,8 @@ public final class OrderUseCase {
|
|||||||
this.bookRepository = bookRepository;
|
this.bookRepository = bookRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OrderDTO placeOrder(OrderInfo orderInfo) throws CustomerNotFoundException {
|
public OrderDTO placeOrder(OrderInfo orderInfo)
|
||||||
|
throws CustomerNotFoundException, InsufficientStockException {
|
||||||
Customer customer = customerRepository.findById(orderInfo.customerId())
|
Customer customer = customerRepository.findById(orderInfo.customerId())
|
||||||
.orElseThrow(() -> new CustomerNotFoundException(orderInfo.customerId()));
|
.orElseThrow(() -> new CustomerNotFoundException(orderInfo.customerId()));
|
||||||
|
|
||||||
@@ -28,6 +30,9 @@ public final class OrderUseCase {
|
|||||||
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 IllegalArgumentException("Book not found"));
|
||||||
|
if (orderLine.quantity() > book.getStock()) {
|
||||||
|
throw new InsufficientStockException(book.getIsbn(), orderLine.quantity(), book.getStock());
|
||||||
|
}
|
||||||
totalAmount += book.getPrice() * orderLine.quantity();
|
totalAmount += book.getPrice() * orderLine.quantity();
|
||||||
Book updatedBook = Book.builder()
|
Book updatedBook = Book.builder()
|
||||||
.isbn(book.getIsbn())
|
.isbn(book.getIsbn())
|
||||||
|
|||||||
+46
-1
@@ -2,7 +2,9 @@ package fr.iut_fbleau.but3.dev62.mylibrary.order.usecase;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.mockito.Mockito.any;
|
import static org.mockito.Mockito.any;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
@@ -17,6 +19,7 @@ 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.OrderInfo;
|
||||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderLineInfo;
|
import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderLineInfo;
|
||||||
import fr.iut_fbleau.but3.dev62.mylibrary.order.PaymentMode;
|
import fr.iut_fbleau.but3.dev62.mylibrary.order.PaymentMode;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.InsufficientStockException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@@ -41,7 +44,8 @@ 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() throws CustomerNotFoundException {
|
void shouldPlaceOrderAndReturnExpectedAmountAndLoyaltyPoints()
|
||||||
|
throws CustomerNotFoundException, InsufficientStockException {
|
||||||
UUID customerId = UUID.randomUUID();
|
UUID customerId = UUID.randomUUID();
|
||||||
Customer customer = Customer.builder()
|
Customer customer = Customer.builder()
|
||||||
.id(customerId)
|
.id(customerId)
|
||||||
@@ -87,4 +91,45 @@ class OrderUseCaseTest {
|
|||||||
verify(bookRepository, times(1)).save(any(Book.class));
|
verify(bookRepository, times(1)).save(any(Book.class));
|
||||||
verify(customerRepository, times(1)).save(customer);
|
verify(customerRepository, times(1)).save(customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw when requested quantity is greater than available stock")
|
||||||
|
void shouldThrowWhenRequestedQuantityIsGreaterThanAvailableStock() {
|
||||||
|
UUID customerId = UUID.randomUUID();
|
||||||
|
Customer customer = Customer.builder()
|
||||||
|
.id(customerId)
|
||||||
|
.firstName("Jane")
|
||||||
|
.lastName("Doe")
|
||||||
|
.phoneNumber("0612345678")
|
||||||
|
.loyaltyPoints(10)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Book cleanCode = Book.builder()
|
||||||
|
.isbn("9780132350884")
|
||||||
|
.title("Clean Code")
|
||||||
|
.author("Robert C. Martin")
|
||||||
|
.publisher("Prentice Hall")
|
||||||
|
.categories(List.of("Programming"))
|
||||||
|
.description("A handbook of agile software craftsmanship")
|
||||||
|
.langue("fr")
|
||||||
|
.price(40.0)
|
||||||
|
.stock(1)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
OrderInfo orderInfo = new OrderInfo(
|
||||||
|
customerId,
|
||||||
|
List.of(new OrderLineInfo("9780132350884", 2)),
|
||||||
|
new OrderAddressInfo("1 rue de Paris", "Paris", "75000", "France"),
|
||||||
|
PaymentMode.CB
|
||||||
|
);
|
||||||
|
|
||||||
|
when(customerRepository.findById(customerId)).thenReturn(Optional.of(customer));
|
||||||
|
when(bookRepository.findByIsbn("9780132350884")).thenReturn(Optional.of(cleanCode));
|
||||||
|
|
||||||
|
assertThrows(InsufficientStockException.class, () -> orderUseCase.placeOrder(orderInfo));
|
||||||
|
verify(customerRepository, times(1)).findById(customerId);
|
||||||
|
verify(bookRepository, times(1)).findByIsbn("9780132350884");
|
||||||
|
verify(bookRepository, never()).save(any(Book.class));
|
||||||
|
verify(customerRepository, never()).save(any(Customer.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user