forked from pierront/mylibrary-template
		
	Fini
This commit is contained in:
		@@ -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; }
 | 
			
		||||
}
 | 
			
		||||
@@ -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; }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,4 @@
 | 
			
		||||
package fr.iut_fbleau.but3.dev62.mylibrary.order.converter;
 | 
			
		||||
 | 
			
		||||
public class OrderConverterTest {
 | 
			
		||||
}
 | 
			
		||||
@@ -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; }
 | 
			
		||||
}
 | 
			
		||||
@@ -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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user