diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/BookDTO.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/BookDTO.java index b1e23ca..83807e6 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/BookDTO.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/BookDTO.java @@ -1,24 +1,24 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.book; - -import java.math.BigDecimal; -import java.time.LocalDate; -import java.util.List; -import java.util.UUID; -import lombok.Builder; -import lombok.Getter; - -@Builder -@Getter -public class BookDTO { - private final UUID id; - private final String isbn; - private final String title; - private final String author; - private final String publisher; - private final LocalDate publicationDate; - private final BigDecimal price; - private final int stock; - private final List categories; - private final String description; - private final String language; -} +package fr.iut_fbleau.but3.dev62.mylibrary.book; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.List; +import java.util.UUID; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class BookDTO { + private final UUID id; + private final String isbn; + private final String title; + private final String author; + private final String publisher; + private final LocalDate publicationDate; + private final BigDecimal price; + private final int stock; + private final List categories; + private final String description; + private final String language; +} diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/BookInfo.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/BookInfo.java index 6ce2f8c..76201b1 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/BookInfo.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/BookInfo.java @@ -1,19 +1,19 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.book; - -import java.math.BigDecimal; -import java.time.LocalDate; -import java.util.List; - -public record BookInfo( - String isbn, - String title, - String author, - String publisher, - LocalDate publicationDate, - BigDecimal price, - int initialStock, - List categories, - String description, - String language -) { -} +package fr.iut_fbleau.but3.dev62.mylibrary.book; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.List; + +public record BookInfo( + String isbn, + String title, + String author, + String publisher, + LocalDate publicationDate, + BigDecimal price, + int initialStock, + List categories, + String description, + String language +) { +} diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/converter/BookConverter.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/converter/BookConverter.java index 7cd1df9..24682fa 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/converter/BookConverter.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/converter/BookConverter.java @@ -1,42 +1,42 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.book.converter; - -import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO; -import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book; - -public final class BookConverter { - - private BookConverter() { - } - - public static Book toDomain(BookInfo bookInfo) { - return Book.builder() - .isbn(bookInfo.isbn()) - .title(bookInfo.title()) - .author(bookInfo.author()) - .publisher(bookInfo.publisher()) - .publicationDate(bookInfo.publicationDate()) - .price(bookInfo.price()) - .stock(bookInfo.initialStock()) - .categories(bookInfo.categories()) - .description(bookInfo.description()) - .language(bookInfo.language()) - .build(); - } - - public static BookDTO toDTO(Book book) { - return BookDTO.builder() - .id(book.getId()) - .isbn(book.getIsbn()) - .title(book.getTitle()) - .author(book.getAuthor()) - .publisher(book.getPublisher()) - .publicationDate(book.getPublicationDate()) - .price(book.getPrice()) - .stock(book.getStock()) - .categories(book.getCategories()) - .description(book.getDescription()) - .language(book.getLanguage()) - .build(); - } +package fr.iut_fbleau.but3.dev62.mylibrary.book.converter; + +import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO; +import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book; + +public final class BookConverter { + + private BookConverter() { + } + + public static Book toDomain(BookInfo bookInfo) { + return Book.builder() + .isbn(bookInfo.isbn()) + .title(bookInfo.title()) + .author(bookInfo.author()) + .publisher(bookInfo.publisher()) + .publicationDate(bookInfo.publicationDate()) + .price(bookInfo.price()) + .stock(bookInfo.initialStock()) + .categories(bookInfo.categories()) + .description(bookInfo.description()) + .language(bookInfo.language()) + .build(); + } + + public static BookDTO toDTO(Book book) { + return BookDTO.builder() + .id(book.getId()) + .isbn(book.getIsbn()) + .title(book.getTitle()) + .author(book.getAuthor()) + .publisher(book.getPublisher()) + .publicationDate(book.getPublicationDate()) + .price(book.getPrice()) + .stock(book.getStock()) + .categories(book.getCategories()) + .description(book.getDescription()) + .language(book.getLanguage()) + .build(); + } } \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/Book.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/Book.java index 86f306e..80e901a 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/Book.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/Book.java @@ -1,30 +1,30 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.book.entity; - -import java.math.BigDecimal; -import java.time.LocalDate; -import java.util.List; -import java.util.UUID; -import lombok.Builder; -import lombok.Getter; - -@Builder -@Getter -public class Book { - - private UUID id; - private String isbn; - private String title; - private String author; - private String publisher; - private LocalDate publicationDate; - private BigDecimal price; - private int stock; - private List categories; - private String description; - private String language; - - public void setRandomUUID() { - this.id = UUID.randomUUID(); - } -} +package fr.iut_fbleau.but3.dev62.mylibrary.book.entity; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.List; +import java.util.UUID; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class Book { + + private UUID id; + private String isbn; + private String title; + private String author; + private String publisher; + private LocalDate publicationDate; + private BigDecimal price; + private int stock; + private List categories; + private String description; + private String language; + + public void setRandomUUID() { + this.id = UUID.randomUUID(); + } +} \ No newline at end of file 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 5d59c9c..091fde1 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,14 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; - -import java.text.MessageFormat; -import java.util.UUID; - -public class BookNotFoundException extends Exception { - - public static final String THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The book with id {0} does not exist"; - - public BookNotFoundException(UUID uuid) { - super(MessageFormat.format(THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid)); - } -} +package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; + +import java.text.MessageFormat; +import java.util.UUID; + +public class BookNotFoundException extends Exception { + + public static final String THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The book with id {0} does not exist"; + + public BookNotFoundException(UUID uuid) { + super(MessageFormat.format(THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid)); + } +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookException.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookException.java index aad876f..017e965 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookException.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookException.java @@ -1,9 +1,9 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; - -public class NotValidBookException extends Exception { - - public NotValidBookException(String message) { - super(message); - } -} +package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; + +public class NotValidBookException extends Exception { + + public NotValidBookException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/repository/BookRepository.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/repository/BookRepository.java index 1c80d77..e96f55b 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/repository/BookRepository.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/repository/BookRepository.java @@ -1,51 +1,51 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.book.repository; - -import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -public final class BookRepository { - - private final List books = new ArrayList<>(); - - public List findAll() { - return books; - } - - public void deleteAll() { - books.clear(); - } - - public Book save(Book newBook) { - Optional optionalBookWithSameId = this.findById(newBook.getId()); - optionalBookWithSameId.ifPresentOrElse(books::remove, newBook::setRandomUUID); - this.books.add(newBook); - return newBook; - } - - public Optional findById(UUID uuid) { - return this.books.stream() - .filter(book -> book.getId().equals(uuid)) - .findFirst(); - } - - public boolean existsById(UUID uuid) { - return this.books.stream() - .anyMatch(book -> book.getId().equals(uuid)); - } - - public Optional findByIsbn(String isbn) { - return this.books.stream() - .filter(book -> book.getIsbn().equals(isbn)) - .findFirst(); - } - - public void delete(Book book) { - this.books.remove(book); - } -} +package fr.iut_fbleau.but3.dev62.mylibrary.book.repository; + +import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +public final class BookRepository { + + private final List books = new ArrayList<>(); + + public List findAll() { + return books; + } + + public void deleteAll() { + books.clear(); + } + + public Book save(Book newBook) { + Optional optionalBookWithSameId = this.findById(newBook.getId()); + optionalBookWithSameId.ifPresentOrElse(books::remove, newBook::setRandomUUID); + this.books.add(newBook); + return newBook; + } + + public Optional findById(UUID uuid) { + return this.books.stream() + .filter(book -> book.getId().equals(uuid)) + .findFirst(); + } + + public boolean existsById(UUID uuid) { + return this.books.stream() + .anyMatch(book -> book.getId().equals(uuid)); + } + + public Optional findByIsbn(String isbn) { + return this.books.stream() + .filter(book -> book.getIsbn().equals(isbn)) + .findFirst(); + } + + public void delete(Book book) { + this.books.remove(book); + } +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/usecase/BookUseCase.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/usecase/BookUseCase.java index 662b721..723960c 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/usecase/BookUseCase.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/usecase/BookUseCase.java @@ -1,67 +1,67 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.book.usecase; - -import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO; -import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.book.converter.BookConverter; -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.exception.NotValidBookException; -import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository; -import fr.iut_fbleau.but3.dev62.mylibrary.book.validator.BookValidator; -import java.util.Optional; -import java.util.UUID; - -public final class BookUseCase { - - private final BookRepository bookRepository; - - public BookUseCase(BookRepository bookRepository) { - this.bookRepository = bookRepository; - } - - public String registerBook(BookInfo bookInfo) throws NotValidBookException { - BookValidator.validate(bookInfo); - Book bookToRegister = BookConverter.toDomain(bookInfo); - Book registeredBook = bookRepository.save(bookToRegister); - return registeredBook.getIsbn(); - } - - public Optional findBookByIsbn(String isbn) { - Optional optionalBook = bookRepository.findByIsbn(isbn); - return optionalBook.map(BookConverter::toDTO); - } - - public BookDTO updateBook(UUID uuid, BookInfo bookInfo) throws BookNotFoundException, NotValidBookException { - BookValidator.validate(bookInfo); - Book existingBook = getBookIfNotFoundThrowException(uuid); - Book updatedBook = Book.builder() - .id(uuid) - .isbn(bookInfo.isbn()) - .title(bookInfo.title()) - .author(bookInfo.author()) - .publisher(bookInfo.publisher()) - .publicationDate(bookInfo.publicationDate()) - .price(bookInfo.price()) - .stock(existingBook.getStock()) - .categories(bookInfo.categories()) - .description(bookInfo.description()) - .language(bookInfo.language()) - .build(); - Book saved = bookRepository.save(updatedBook); - return BookConverter.toDTO(saved); - } - - public void deleteBook(UUID uuid) throws BookNotFoundException { - Book bookToDelete = getBookIfNotFoundThrowException(uuid); - bookRepository.delete(bookToDelete); - } - - private Book getBookIfNotFoundThrowException(UUID uuid) throws BookNotFoundException { - Optional optionalBook = bookRepository.findById(uuid); - if (optionalBook.isEmpty()) { - throw new BookNotFoundException(uuid); - } - return optionalBook.get(); - } -} +package fr.iut_fbleau.but3.dev62.mylibrary.book.usecase; + +import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO; +import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.book.converter.BookConverter; +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.exception.NotValidBookException; +import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository; +import fr.iut_fbleau.but3.dev62.mylibrary.book.validator.BookValidator; +import java.util.Optional; +import java.util.UUID; + +public final class BookUseCase { + + private final BookRepository bookRepository; + + public BookUseCase(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + public String registerBook(BookInfo bookInfo) throws NotValidBookException { + BookValidator.validate(bookInfo); + Book bookToRegister = BookConverter.toDomain(bookInfo); + Book registeredBook = bookRepository.save(bookToRegister); + return registeredBook.getIsbn(); + } + + public Optional findBookByIsbn(String isbn) { + Optional optionalBook = bookRepository.findByIsbn(isbn); + return optionalBook.map(BookConverter::toDTO); + } + + public BookDTO updateBook(UUID uuid, BookInfo bookInfo) throws BookNotFoundException, NotValidBookException { + BookValidator.validate(bookInfo); + Book existingBook = getBookIfNotFoundThrowException(uuid); + Book updatedBook = Book.builder() + .id(uuid) + .isbn(bookInfo.isbn()) + .title(bookInfo.title()) + .author(bookInfo.author()) + .publisher(bookInfo.publisher()) + .publicationDate(bookInfo.publicationDate()) + .price(bookInfo.price()) + .stock(existingBook.getStock()) + .categories(bookInfo.categories()) + .description(bookInfo.description()) + .language(bookInfo.language()) + .build(); + Book saved = bookRepository.save(updatedBook); + return BookConverter.toDTO(saved); + } + + public void deleteBook(UUID uuid) throws BookNotFoundException { + Book bookToDelete = getBookIfNotFoundThrowException(uuid); + bookRepository.delete(bookToDelete); + } + + private Book getBookIfNotFoundThrowException(UUID uuid) throws BookNotFoundException { + Optional optionalBook = bookRepository.findById(uuid); + if (optionalBook.isEmpty()) { + throw new BookNotFoundException(uuid); + } + return optionalBook.get(); + } +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/validator/BookValidator.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/validator/BookValidator.java index e0cce37..76325f2 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/validator/BookValidator.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/validator/BookValidator.java @@ -1,59 +1,59 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.book.validator; - -import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException; -import java.math.BigDecimal; - -public final class BookValidator { - - public static final String ISBN_IS_NOT_VALID = "ISBN is not valid"; - public static final String PRICE_MUST_BE_POSITIVE = "Price must be positive"; - public static final String TITLE_CANNOT_BE_BLANK = "Title cannot be blank"; - public static final String AUTHOR_CANNOT_BE_BLANK = "Author cannot be blank"; - public static final String PUBLISHER_CANNOT_BE_BLANK = "Publisher cannot be blank"; - public static final String ISBN_REGEX = "\\d{13}"; - - private BookValidator() { - } - - public static void validate(BookInfo bookInfo) throws NotValidBookException { - validateIsbn(bookInfo); - validateTitle(bookInfo); - validateAuthor(bookInfo); - validatePublisher(bookInfo); - validatePrice(bookInfo); - } - - private static void validateIsbn(BookInfo bookInfo) throws NotValidBookException { - if (bookInfo.isbn() == null || bookInfo.isbn().isBlank()) { - throw new NotValidBookException(ISBN_IS_NOT_VALID); - } - if (!bookInfo.isbn().matches(ISBN_REGEX)) { - throw new NotValidBookException(ISBN_IS_NOT_VALID); - } - } - - private static void validateTitle(BookInfo bookInfo) throws NotValidBookException { - if (bookInfo.title() == null || bookInfo.title().isBlank()) { - throw new NotValidBookException(TITLE_CANNOT_BE_BLANK); - } - } - - private static void validateAuthor(BookInfo bookInfo) throws NotValidBookException { - if (bookInfo.author() == null || bookInfo.author().isBlank()) { - throw new NotValidBookException(AUTHOR_CANNOT_BE_BLANK); - } - } - - private static void validatePublisher(BookInfo bookInfo) throws NotValidBookException { - if (bookInfo.publisher() == null || bookInfo.publisher().isBlank()) { - throw new NotValidBookException(PUBLISHER_CANNOT_BE_BLANK); - } - } - - private static void validatePrice(BookInfo bookInfo) throws NotValidBookException { - if (bookInfo.price() == null || bookInfo.price().compareTo(BigDecimal.ZERO) <= 0) { - throw new NotValidBookException(PRICE_MUST_BE_POSITIVE); - } - } +package fr.iut_fbleau.but3.dev62.mylibrary.book.validator; + +import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException; +import java.math.BigDecimal; + +public final class BookValidator { + + public static final String ISBN_IS_NOT_VALID = "ISBN is not valid"; + public static final String PRICE_MUST_BE_POSITIVE = "Price must be positive"; + public static final String TITLE_CANNOT_BE_BLANK = "Title cannot be blank"; + public static final String AUTHOR_CANNOT_BE_BLANK = "Author cannot be blank"; + public static final String PUBLISHER_CANNOT_BE_BLANK = "Publisher cannot be blank"; + public static final String ISBN_REGEX = "\\d{13}"; + + private BookValidator() { + } + + public static void validate(BookInfo bookInfo) throws NotValidBookException { + validateIsbn(bookInfo); + validateTitle(bookInfo); + validateAuthor(bookInfo); + validatePublisher(bookInfo); + validatePrice(bookInfo); + } + + private static void validateIsbn(BookInfo bookInfo) throws NotValidBookException { + if (bookInfo.isbn() == null || bookInfo.isbn().isBlank()) { + throw new NotValidBookException(ISBN_IS_NOT_VALID); + } + if (!bookInfo.isbn().matches(ISBN_REGEX)) { + throw new NotValidBookException(ISBN_IS_NOT_VALID); + } + } + + private static void validateTitle(BookInfo bookInfo) throws NotValidBookException { + if (bookInfo.title() == null || bookInfo.title().isBlank()) { + throw new NotValidBookException(TITLE_CANNOT_BE_BLANK); + } + } + + private static void validateAuthor(BookInfo bookInfo) throws NotValidBookException { + if (bookInfo.author() == null || bookInfo.author().isBlank()) { + throw new NotValidBookException(AUTHOR_CANNOT_BE_BLANK); + } + } + + private static void validatePublisher(BookInfo bookInfo) throws NotValidBookException { + if (bookInfo.publisher() == null || bookInfo.publisher().isBlank()) { + throw new NotValidBookException(PUBLISHER_CANNOT_BE_BLANK); + } + } + + private static void validatePrice(BookInfo bookInfo) throws NotValidBookException { + if (bookInfo.price() == null || bookInfo.price().compareTo(BigDecimal.ZERO) <= 0) { + throw new NotValidBookException(PRICE_MUST_BE_POSITIVE); + } + } } \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/AdresseLivraison.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/AdresseLivraison.java index 815aed9..1227699 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/AdresseLivraison.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/AdresseLivraison.java @@ -1,5 +1,5 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order; - -public record AdresseLivraison(String rue, String ville, String codePostal, String pays) { -} +package fr.iut_fbleau.but3.dev62.mylibrary.order; + +public record AdresseLivraison(String rue, String ville, String codePostal, String pays) { +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/LigneCommandeInfo.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/LigneCommandeInfo.java index d5b2542..25bad52 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/LigneCommandeInfo.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/LigneCommandeInfo.java @@ -1,7 +1,7 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order; - -import java.util.UUID; - -public record LigneCommandeInfo(UUID livreId, int quantite) { -} +package fr.iut_fbleau.but3.dev62.mylibrary.order; + +import java.util.UUID; + +public record LigneCommandeInfo(UUID livreId, int quantite) { +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/ModePaiement.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/ModePaiement.java index 6c0fbb0..ba49374 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/ModePaiement.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/ModePaiement.java @@ -1,8 +1,8 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order; - -public enum ModePaiement { - CB, - PAYPAL, - POINTS_FIDELITE -} +package fr.iut_fbleau.but3.dev62.mylibrary.order; + +public enum ModePaiement { + CB, + PAYPAL, + POINTS_FIDELITE +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/OrderDTO.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/OrderDTO.java index 2ae9d6a..451d8cd 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/OrderDTO.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/OrderDTO.java @@ -1,15 +1,15 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order; - -import java.math.BigDecimal; -import java.util.UUID; -import lombok.Builder; -import lombok.Getter; - -@Builder -@Getter -public class OrderDTO { - private final UUID commandeId; - private final BigDecimal montantTotal; - private final int pointsFideliteGagnes; -} +package fr.iut_fbleau.but3.dev62.mylibrary.order; + +import java.math.BigDecimal; +import java.util.UUID; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class OrderDTO { + private final UUID commandeId; + private final BigDecimal montantTotal; + private final int pointsFideliteGagnes; +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/OrderInfo.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/OrderInfo.java index e1969f9..fa082e2 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/OrderInfo.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/OrderInfo.java @@ -1,13 +1,13 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order; - -import java.util.List; -import java.util.UUID; - -public record OrderInfo( - UUID clientId, - List lignesCommande, - AdresseLivraison adresseLivraison, - ModePaiement modePaiement -) { -} +package fr.iut_fbleau.but3.dev62.mylibrary.order; + +import java.util.List; +import java.util.UUID; + +public record OrderInfo( + UUID clientId, + List lignesCommande, + AdresseLivraison adresseLivraison, + ModePaiement modePaiement +) { +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/converter/OrderConverter.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/converter/OrderConverter.java index 908fc6a..abd7442 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/converter/OrderConverter.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/converter/OrderConverter.java @@ -1,34 +1,34 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.converter; - -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.entity.LigneCommande; -import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; -import java.math.BigDecimal; -import java.util.List; - -public final class OrderConverter { - - private OrderConverter() { - } - - public static Order toDomain(OrderInfo orderInfo, List lignes, - BigDecimal montantTotal, int pointsFideliteGagnes) { - return Order.builder() - .clientId(orderInfo.clientId()) - .lignesCommande(lignes) - .adresseLivraison(orderInfo.adresseLivraison()) - .modePaiement(orderInfo.modePaiement()) - .montantTotal(montantTotal) - .pointsFideliteGagnes(pointsFideliteGagnes) - .build(); - } - - public static OrderDTO toDTO(Order order) { - return OrderDTO.builder() - .commandeId(order.getId()) - .montantTotal(order.getMontantTotal()) - .pointsFideliteGagnes(order.getPointsFideliteGagnes()) - .build(); - } +package fr.iut_fbleau.but3.dev62.mylibrary.order.converter; + +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.entity.LigneCommande; +import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; +import java.math.BigDecimal; +import java.util.List; + +public final class OrderConverter { + + private OrderConverter() { + } + + public static Order toDomain(OrderInfo orderInfo, List lignes, + BigDecimal montantTotal, int pointsFideliteGagnes) { + return Order.builder() + .clientId(orderInfo.clientId()) + .lignesCommande(lignes) + .adresseLivraison(orderInfo.adresseLivraison()) + .modePaiement(orderInfo.modePaiement()) + .montantTotal(montantTotal) + .pointsFideliteGagnes(pointsFideliteGagnes) + .build(); + } + + public static OrderDTO toDTO(Order order) { + return OrderDTO.builder() + .commandeId(order.getId()) + .montantTotal(order.getMontantTotal()) + .pointsFideliteGagnes(order.getPointsFideliteGagnes()) + .build(); + } } \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/LigneCommande.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/LigneCommande.java index 7636c6c..9a4d536 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/LigneCommande.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/LigneCommande.java @@ -1,15 +1,15 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.entity; - -import java.math.BigDecimal; -import java.util.UUID; -import lombok.Builder; -import lombok.Getter; - -@Builder -@Getter -public class LigneCommande { - private UUID livreId; - private int quantite; - private BigDecimal prixUnitaire; -} +package fr.iut_fbleau.but3.dev62.mylibrary.order.entity; + +import java.math.BigDecimal; +import java.util.UUID; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class LigneCommande { + private UUID livreId; + private int quantite; + private BigDecimal prixUnitaire; +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/Order.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/Order.java index cfc00af..96612b8 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/Order.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/Order.java @@ -1,26 +1,26 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.entity; - -import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; -import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; -import java.math.BigDecimal; -import java.util.List; -import java.util.UUID; -import lombok.Builder; -import lombok.Getter; - -@Builder -@Getter -public class Order { - private UUID id; - private UUID clientId; - private List lignesCommande; - private AdresseLivraison adresseLivraison; - private ModePaiement modePaiement; - private BigDecimal montantTotal; - private int pointsFideliteGagnes; - - public void setRandomUUID() { - this.id = UUID.randomUUID(); - } -} +package fr.iut_fbleau.but3.dev62.mylibrary.order.entity; + +import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; +import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; +import java.math.BigDecimal; +import java.util.List; +import java.util.UUID; +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class Order { + private UUID id; + private UUID clientId; + private List lignesCommande; + private AdresseLivraison adresseLivraison; + private ModePaiement modePaiement; + private BigDecimal montantTotal; + private int pointsFideliteGagnes; + + public void setRandomUUID() { + this.id = UUID.randomUUID(); + } +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/NotValidOrderException.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/NotValidOrderException.java index 8cc83f6..9d782c1 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/NotValidOrderException.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/NotValidOrderException.java @@ -1,9 +1,9 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.exception; - -public class NotValidOrderException extends Exception { - - public NotValidOrderException(String message) { - super(message); - } -} +package fr.iut_fbleau.but3.dev62.mylibrary.order.exception; + +public class NotValidOrderException extends Exception { + + public NotValidOrderException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/OrderNotFoundException.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/OrderNotFoundException.java index 30c9339..a8d62ca 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/OrderNotFoundException.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/OrderNotFoundException.java @@ -1,14 +1,14 @@ -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)); - } -} +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)); + } +} \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/repository/OrderRepository.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/repository/OrderRepository.java index a6e5a3e..127bc6e 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/repository/OrderRepository.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/repository/OrderRepository.java @@ -1,50 +1,50 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.repository; - -import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -public final class OrderRepository { - - private final List orders = new ArrayList<>(); - - public List findAll() { - return orders; - } - - public void deleteAll() { - orders.clear(); - } - - public Order save(Order newOrder) { - Optional existing = this.findById(newOrder.getId()); - existing.ifPresentOrElse(orders::remove, newOrder::setRandomUUID); - this.orders.add(newOrder); - return newOrder; - } - - public Optional findById(UUID uuid) { - return this.orders.stream() - .filter(order -> order.getId().equals(uuid)) - .findFirst(); - } - - public boolean existsById(UUID uuid) { - return this.orders.stream() - .anyMatch(order -> order.getId().equals(uuid)); - } - - public List findByClientId(UUID clientId) { - return this.orders.stream() - .filter(order -> order.getClientId().equals(clientId)) - .toList(); - } - - public void delete(Order order) { - this.orders.remove(order); - } +package fr.iut_fbleau.but3.dev62.mylibrary.order.repository; + +import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +public final class OrderRepository { + + private final List orders = new ArrayList<>(); + + public List findAll() { + return orders; + } + + public void deleteAll() { + orders.clear(); + } + + public Order save(Order newOrder) { + Optional existing = this.findById(newOrder.getId()); + existing.ifPresentOrElse(orders::remove, newOrder::setRandomUUID); + this.orders.add(newOrder); + return newOrder; + } + + public Optional findById(UUID uuid) { + return this.orders.stream() + .filter(order -> order.getId().equals(uuid)) + .findFirst(); + } + + public boolean existsById(UUID uuid) { + return this.orders.stream() + .anyMatch(order -> order.getId().equals(uuid)); + } + + public List findByClientId(UUID clientId) { + return this.orders.stream() + .filter(order -> order.getClientId().equals(clientId)) + .toList(); + } + + public void delete(Order order) { + this.orders.remove(order); + } } \ 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 5aad7ce..d6a6a45 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,79 +1,79 @@ -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.repository.BookRepository; -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.repository.CustomerRepository; -import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; -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.converter.OrderConverter; -import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.LigneCommande; -import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; -import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException; -import fr.iut_fbleau.but3.dev62.mylibrary.order.repository.OrderRepository; -import fr.iut_fbleau.but3.dev62.mylibrary.order.validator.OrderValidator; -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.UUID; - -public final class OrderUseCase { - - private final OrderRepository orderRepository; - private final CustomerRepository customerRepository; - private final BookRepository bookRepository; - - public OrderUseCase(OrderRepository orderRepository, CustomerRepository customerRepository, - BookRepository bookRepository) { - this.orderRepository = orderRepository; - this.customerRepository = customerRepository; - this.bookRepository = bookRepository; - } - - public OrderDTO passerCommande(OrderInfo orderInfo) - throws NotValidOrderException, CustomerNotFoundException { - OrderValidator.validate(orderInfo); - - Customer customer = customerRepository.findById(orderInfo.clientId()) - .orElseThrow(() -> new CustomerNotFoundException(orderInfo.clientId())); - - List lignes = new ArrayList<>(); - BigDecimal montantTotal = BigDecimal.ZERO; - - for (LigneCommandeInfo ligneInfo : orderInfo.lignesCommande()) { - Book book = bookRepository.findById(ligneInfo.livreId()) - .orElseThrow(() -> new NotValidOrderException("Book not found: " + ligneInfo.livreId())); - - BigDecimal prixLigne = book.getPrice().multiply(BigDecimal.valueOf(ligneInfo.quantite())); - montantTotal = montantTotal.add(prixLigne); - - lignes.add(LigneCommande.builder() - .livreId(ligneInfo.livreId()) - .quantite(ligneInfo.quantite()) - .prixUnitaire(book.getPrice()) - .build()); - } - - int pointsGagnes = montantTotal.intValue(); - - if (orderInfo.modePaiement() == ModePaiement.POINTS_FIDELITE) { - customer.addLoyaltyPoints(-montantTotal.intValue()); - } else { - customer.addLoyaltyPoints(pointsGagnes); - } - customerRepository.save(customer); - - Order order = OrderConverter.toDomain(orderInfo, lignes, montantTotal, pointsGagnes); - Order savedOrder = orderRepository.save(order); - - return OrderConverter.toDTO(savedOrder); - } - - public Optional findOrderById(UUID uuid) { - return orderRepository.findById(uuid).map(OrderConverter::toDTO); - } +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.repository.BookRepository; +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.repository.CustomerRepository; +import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; +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.converter.OrderConverter; +import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.LigneCommande; +import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; +import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException; +import fr.iut_fbleau.but3.dev62.mylibrary.order.repository.OrderRepository; +import fr.iut_fbleau.but3.dev62.mylibrary.order.validator.OrderValidator; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public final class OrderUseCase { + + private final OrderRepository orderRepository; + private final CustomerRepository customerRepository; + private final BookRepository bookRepository; + + public OrderUseCase(OrderRepository orderRepository, CustomerRepository customerRepository, + BookRepository bookRepository) { + this.orderRepository = orderRepository; + this.customerRepository = customerRepository; + this.bookRepository = bookRepository; + } + + public OrderDTO passerCommande(OrderInfo orderInfo) + throws NotValidOrderException, CustomerNotFoundException { + OrderValidator.validate(orderInfo); + + Customer customer = customerRepository.findById(orderInfo.clientId()) + .orElseThrow(() -> new CustomerNotFoundException(orderInfo.clientId())); + + List lignes = new ArrayList<>(); + BigDecimal montantTotal = BigDecimal.ZERO; + + for (LigneCommandeInfo ligneInfo : orderInfo.lignesCommande()) { + Book book = bookRepository.findById(ligneInfo.livreId()) + .orElseThrow(() -> new NotValidOrderException("Book not found: " + ligneInfo.livreId())); + + BigDecimal prixLigne = book.getPrice().multiply(BigDecimal.valueOf(ligneInfo.quantite())); + montantTotal = montantTotal.add(prixLigne); + + lignes.add(LigneCommande.builder() + .livreId(ligneInfo.livreId()) + .quantite(ligneInfo.quantite()) + .prixUnitaire(book.getPrice()) + .build()); + } + + int pointsGagnes = montantTotal.intValue(); + + if (orderInfo.modePaiement() == ModePaiement.POINTS_FIDELITE) { + customer.addLoyaltyPoints(-montantTotal.intValue()); + } else { + customer.addLoyaltyPoints(pointsGagnes); + } + customerRepository.save(customer); + + Order order = OrderConverter.toDomain(orderInfo, lignes, montantTotal, pointsGagnes); + Order savedOrder = orderRepository.save(order); + + return OrderConverter.toDTO(savedOrder); + } + + public Optional findOrderById(UUID uuid) { + return orderRepository.findById(uuid).map(OrderConverter::toDTO); + } } \ No newline at end of file diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/validator/OrderValidator.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/validator/OrderValidator.java index 0cc9746..b7b1b04 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/validator/OrderValidator.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/order/validator/OrderValidator.java @@ -1,53 +1,53 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.validator; - -import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException; - -public final class OrderValidator { - - public static final String CLIENT_ID_CANNOT_BE_NULL = "Client id cannot be null"; - public static final String LIGNES_COMMANDE_CANNOT_BE_EMPTY = "Order must have at least one item"; - public static final String QUANTITE_MUST_BE_POSITIVE = "Quantity must be positive"; - public static final String MODE_PAIEMENT_CANNOT_BE_NULL = "Payment method cannot be null"; - public static final String ADRESSE_LIVRAISON_CANNOT_BE_NULL = "Delivery address cannot be null"; - - private OrderValidator() { - } - - public static void validate(OrderInfo orderInfo) throws NotValidOrderException { - validateClientId(orderInfo); - validateLignesCommande(orderInfo); - validateAdresseLivraison(orderInfo); - validateModePaiement(orderInfo); - } - - private static void validateClientId(OrderInfo orderInfo) throws NotValidOrderException { - if (orderInfo.clientId() == null) { - throw new NotValidOrderException(CLIENT_ID_CANNOT_BE_NULL); - } - } - - private static void validateLignesCommande(OrderInfo orderInfo) throws NotValidOrderException { - if (orderInfo.lignesCommande() == null || orderInfo.lignesCommande().isEmpty()) { - throw new NotValidOrderException(LIGNES_COMMANDE_CANNOT_BE_EMPTY); - } - for (LigneCommandeInfo ligne : orderInfo.lignesCommande()) { - if (ligne.quantite() <= 0) { - throw new NotValidOrderException(QUANTITE_MUST_BE_POSITIVE); - } - } - } - - private static void validateAdresseLivraison(OrderInfo orderInfo) throws NotValidOrderException { - if (orderInfo.adresseLivraison() == null) { - throw new NotValidOrderException(ADRESSE_LIVRAISON_CANNOT_BE_NULL); - } - } - - private static void validateModePaiement(OrderInfo orderInfo) throws NotValidOrderException { - if (orderInfo.modePaiement() == null) { - throw new NotValidOrderException(MODE_PAIEMENT_CANNOT_BE_NULL); - } - } +package fr.iut_fbleau.but3.dev62.mylibrary.order.validator; + +import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException; + +public final class OrderValidator { + + public static final String CLIENT_ID_CANNOT_BE_NULL = "Client id cannot be null"; + public static final String LIGNES_COMMANDE_CANNOT_BE_EMPTY = "Order must have at least one item"; + public static final String QUANTITE_MUST_BE_POSITIVE = "Quantity must be positive"; + public static final String MODE_PAIEMENT_CANNOT_BE_NULL = "Payment method cannot be null"; + public static final String ADRESSE_LIVRAISON_CANNOT_BE_NULL = "Delivery address cannot be null"; + + private OrderValidator() { + } + + public static void validate(OrderInfo orderInfo) throws NotValidOrderException { + validateClientId(orderInfo); + validateLignesCommande(orderInfo); + validateAdresseLivraison(orderInfo); + validateModePaiement(orderInfo); + } + + private static void validateClientId(OrderInfo orderInfo) throws NotValidOrderException { + if (orderInfo.clientId() == null) { + throw new NotValidOrderException(CLIENT_ID_CANNOT_BE_NULL); + } + } + + private static void validateLignesCommande(OrderInfo orderInfo) throws NotValidOrderException { + if (orderInfo.lignesCommande() == null || orderInfo.lignesCommande().isEmpty()) { + throw new NotValidOrderException(LIGNES_COMMANDE_CANNOT_BE_EMPTY); + } + for (LigneCommandeInfo ligne : orderInfo.lignesCommande()) { + if (ligne.quantite() <= 0) { + throw new NotValidOrderException(QUANTITE_MUST_BE_POSITIVE); + } + } + } + + private static void validateAdresseLivraison(OrderInfo orderInfo) throws NotValidOrderException { + if (orderInfo.adresseLivraison() == null) { + throw new NotValidOrderException(ADRESSE_LIVRAISON_CANNOT_BE_NULL); + } + } + + private static void validateModePaiement(OrderInfo orderInfo) throws NotValidOrderException { + if (orderInfo.modePaiement() == null) { + throw new NotValidOrderException(MODE_PAIEMENT_CANNOT_BE_NULL); + } + } } \ No newline at end of file diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/validator/BookValidatorTest.java:Zone.Identifier b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/validator/BookValidatorTest.java:Zone.Identifier deleted file mode 100644 index abbff80..0000000 Binary files a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/validator/BookValidatorTest.java:Zone.Identifier and /dev/null differ 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 index ee4c44e..d5405c4 100644 --- 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 @@ -1,223 +1,223 @@ -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.book.entity.Book; -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; -import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository; -import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; -import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; -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.NotValidOrderException; -import fr.iut_fbleau.but3.dev62.mylibrary.order.repository.OrderRepository; -import fr.iut_fbleau.but3.dev62.mylibrary.order.usecase.OrderUseCase; -import io.cucumber.datatable.DataTable; -import io.cucumber.java.Before; -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.math.BigDecimal; -import java.time.LocalDate; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -public class OrderSteps { - - private final CustomerRepository customerRepository = new CustomerRepository(); - private final BookRepository bookRepository = new BookRepository(); - private final OrderRepository orderRepository = new OrderRepository(); - private final OrderUseCase orderUseCase = new OrderUseCase(orderRepository, customerRepository, bookRepository); - - private final Map customerPhoneUUID = new HashMap<>(); - private final Map bookIsbnUUID = new HashMap<>(); - - private OrderDTO orderResult; - private NotValidOrderException notValidOrderException; - private boolean customerNotFoundThrown = false; - - @Before - public void setUp() { - customerRepository.deleteAll(); - bookRepository.deleteAll(); - orderRepository.deleteAll(); - customerPhoneUUID.clear(); - bookIsbnUUID.clear(); - orderResult = null; - notValidOrderException = null; - customerNotFoundThrown = false; - } - - @Given("the system has the following customers for order:") - public void theSystemHasTheFollowingCustomersForOrder(DataTable dataTable) { - List> customers = dataTable.asMaps(String.class, String.class); - - for (Map customer : customers) { - String phone = customer.get("numeroTelephone"); - Customer newCustomer = Customer.builder() - .firstName(customer.get("prenom")) - .lastName(customer.get("nom")) - .phoneNumber(phone) - .loyaltyPoints(Integer.parseInt(customer.get("pointsFidelite"))) - .build(); - Customer saved = customerRepository.save(newCustomer); - customerPhoneUUID.put(phone, saved.getId()); - } - - assertEquals(customers.size(), customerRepository.findAll().size()); - } - - @And("the catalog has the following books for order:") - public void theCatalogHasTheFollowingBooksForOrder(DataTable dataTable) { - 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")) - .publicationDate(LocalDate.parse(book.get("datePublication"))) - .price(new BigDecimal(book.get("prix"))) - .stock(Integer.parseInt(book.get("stockInitial"))) - .categories(List.of(book.get("categories").split(";"))) - .description(book.get("description")) - .language(book.get("langue")) - .build(); - Book saved = bookRepository.save(newBook); - bookIsbnUUID.put(isbn, saved.getId()); - } - - assertEquals(books.size(), bookRepository.findAll().size()); - } - - @When("I place an order for customer {string} with:") - public void iPlaceAnOrderForCustomerWith(String phoneNumber, DataTable dataTable) - throws NotValidOrderException, CustomerNotFoundException { - Map row = dataTable.asMaps(String.class, String.class).getFirst(); - - UUID customerId = customerPhoneUUID.get(phoneNumber); - UUID livreId = bookIsbnUUID.get(row.get("livreIsbn")); - - AdresseLivraison adresse = new AdresseLivraison( - row.get("rue"), row.get("ville"), row.get("codePostal"), row.get("pays") - ); - - OrderInfo orderInfo = new OrderInfo( - customerId, - List.of(new LigneCommandeInfo(livreId, Integer.parseInt(row.get("quantite")))), - adresse, - ModePaiement.valueOf(row.get("modePaiement")) - ); - - orderResult = orderUseCase.passerCommande(orderInfo); - } - - @Then("a new order is created") - public void aNewOrderIsCreated() { - assertNotNull(orderResult); - assertNotNull(orderResult.getCommandeId()); - } - - @And("the order total is {double}") - public void theOrderTotalIs(double expectedTotal) { - assertEquals(0, new BigDecimal(String.valueOf(expectedTotal)).compareTo(orderResult.getMontantTotal())); - } - - @And("the customer {string} now has {int} loyalty points") - public void theCustomerNowHasLoyaltyPoints(String phoneNumber, int expectedPoints) { - UUID customerId = customerPhoneUUID.get(phoneNumber); - Customer customer = customerRepository.findById(customerId).orElseThrow(); - assertEquals(expectedPoints, customer.getLoyaltyPoints()); - } - - @When("I try to place an order for customer {string} with no items:") - public void iTryToPlaceAnOrderForCustomerWithNoItems(String phoneNumber, DataTable dataTable) { - Map row = dataTable.asMaps(String.class, String.class).getFirst(); - UUID customerId = customerPhoneUUID.get(phoneNumber); - - AdresseLivraison adresse = new AdresseLivraison( - row.get("rue"), row.get("ville"), row.get("codePostal"), row.get("pays") - ); - - OrderInfo orderInfo = new OrderInfo( - customerId, List.of(), adresse, ModePaiement.valueOf(row.get("modePaiement")) - ); - - notValidOrderException = assertThrows(NotValidOrderException.class, - () -> orderUseCase.passerCommande(orderInfo)); - } - - @When("I try to place an order for customer {string} with invalid quantity:") - public void iTryToPlaceAnOrderForCustomerWithInvalidQuantity(String phoneNumber, DataTable dataTable) { - Map row = dataTable.asMaps(String.class, String.class).getFirst(); - UUID customerId = customerPhoneUUID.get(phoneNumber); - UUID livreId = bookIsbnUUID.get(row.get("livreIsbn")); - - AdresseLivraison adresse = new AdresseLivraison( - row.get("rue"), row.get("ville"), row.get("codePostal"), row.get("pays") - ); - - OrderInfo orderInfo = new OrderInfo( - customerId, - List.of(new LigneCommandeInfo(livreId, Integer.parseInt(row.get("quantite")))), - adresse, - ModePaiement.valueOf(row.get("modePaiement")) - ); - - notValidOrderException = assertThrows(NotValidOrderException.class, - () -> orderUseCase.passerCommande(orderInfo)); - } - - @When("I try to place an order for an unknown customer with:") - public void iTryToPlaceAnOrderForAnUnknownCustomerWith(DataTable dataTable) { - Map row = dataTable.asMaps(String.class, String.class).getFirst(); - UUID unknownId = UUID.randomUUID(); - UUID livreId = bookIsbnUUID.get(row.get("livreIsbn")); - - AdresseLivraison adresse = new AdresseLivraison( - row.get("rue"), row.get("ville"), row.get("codePostal"), row.get("pays") - ); - - OrderInfo orderInfo = new OrderInfo( - unknownId, - List.of(new LigneCommandeInfo(livreId, Integer.parseInt(row.get("quantite")))), - adresse, - ModePaiement.valueOf(row.get("modePaiement")) - ); - - try { - orderUseCase.passerCommande(orderInfo); - } catch (CustomerNotFoundException e) { - customerNotFoundThrown = true; - } catch (NotValidOrderException e) { - notValidOrderException = e; - } - } - - @Then("the order placement fails") - public void theOrderPlacementFails() { - assertNotNull(notValidOrderException); - } - - @Then("the order placement fails with customer not found") - public void theOrderPlacementFailsWithCustomerNotFound() { - assertTrue(customerNotFoundThrown); - } - - @And("I receive a validation order error containing {string}") - public void iReceiveAValidationOrderErrorContaining(String errorMessage) { - assertEquals(errorMessage, notValidOrderException.getMessage()); - } +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.book.entity.Book; +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; +import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository; +import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; +import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; +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.NotValidOrderException; +import fr.iut_fbleau.but3.dev62.mylibrary.order.repository.OrderRepository; +import fr.iut_fbleau.but3.dev62.mylibrary.order.usecase.OrderUseCase; +import io.cucumber.datatable.DataTable; +import io.cucumber.java.Before; +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.math.BigDecimal; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class OrderSteps { + + private final CustomerRepository customerRepository = new CustomerRepository(); + private final BookRepository bookRepository = new BookRepository(); + private final OrderRepository orderRepository = new OrderRepository(); + private final OrderUseCase orderUseCase = new OrderUseCase(orderRepository, customerRepository, bookRepository); + + private final Map customerPhoneUUID = new HashMap<>(); + private final Map bookIsbnUUID = new HashMap<>(); + + private OrderDTO orderResult; + private NotValidOrderException notValidOrderException; + private boolean customerNotFoundThrown = false; + + @Before + public void setUp() { + customerRepository.deleteAll(); + bookRepository.deleteAll(); + orderRepository.deleteAll(); + customerPhoneUUID.clear(); + bookIsbnUUID.clear(); + orderResult = null; + notValidOrderException = null; + customerNotFoundThrown = false; + } + + @Given("the system has the following customers for order:") + public void theSystemHasTheFollowingCustomersForOrder(DataTable dataTable) { + List> customers = dataTable.asMaps(String.class, String.class); + + for (Map customer : customers) { + String phone = customer.get("numeroTelephone"); + Customer newCustomer = Customer.builder() + .firstName(customer.get("prenom")) + .lastName(customer.get("nom")) + .phoneNumber(phone) + .loyaltyPoints(Integer.parseInt(customer.get("pointsFidelite"))) + .build(); + Customer saved = customerRepository.save(newCustomer); + customerPhoneUUID.put(phone, saved.getId()); + } + + assertEquals(customers.size(), customerRepository.findAll().size()); + } + + @And("the catalog has the following books for order:") + public void theCatalogHasTheFollowingBooksForOrder(DataTable dataTable) { + 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")) + .publicationDate(LocalDate.parse(book.get("datePublication"))) + .price(new BigDecimal(book.get("prix"))) + .stock(Integer.parseInt(book.get("stockInitial"))) + .categories(List.of(book.get("categories").split(";"))) + .description(book.get("description")) + .language(book.get("langue")) + .build(); + Book saved = bookRepository.save(newBook); + bookIsbnUUID.put(isbn, saved.getId()); + } + + assertEquals(books.size(), bookRepository.findAll().size()); + } + + @When("I place an order for customer {string} with:") + public void iPlaceAnOrderForCustomerWith(String phoneNumber, DataTable dataTable) + throws NotValidOrderException, CustomerNotFoundException { + Map row = dataTable.asMaps(String.class, String.class).getFirst(); + + UUID customerId = customerPhoneUUID.get(phoneNumber); + UUID livreId = bookIsbnUUID.get(row.get("livreIsbn")); + + AdresseLivraison adresse = new AdresseLivraison( + row.get("rue"), row.get("ville"), row.get("codePostal"), row.get("pays") + ); + + OrderInfo orderInfo = new OrderInfo( + customerId, + List.of(new LigneCommandeInfo(livreId, Integer.parseInt(row.get("quantite")))), + adresse, + ModePaiement.valueOf(row.get("modePaiement")) + ); + + orderResult = orderUseCase.passerCommande(orderInfo); + } + + @Then("a new order is created") + public void aNewOrderIsCreated() { + assertNotNull(orderResult); + assertNotNull(orderResult.getCommandeId()); + } + + @And("the order total is {double}") + public void theOrderTotalIs(double expectedTotal) { + assertEquals(0, new BigDecimal(String.valueOf(expectedTotal)).compareTo(orderResult.getMontantTotal())); + } + + @And("the customer {string} now has {int} loyalty points") + public void theCustomerNowHasLoyaltyPoints(String phoneNumber, int expectedPoints) { + UUID customerId = customerPhoneUUID.get(phoneNumber); + Customer customer = customerRepository.findById(customerId).orElseThrow(); + assertEquals(expectedPoints, customer.getLoyaltyPoints()); + } + + @When("I try to place an order for customer {string} with no items:") + public void iTryToPlaceAnOrderForCustomerWithNoItems(String phoneNumber, DataTable dataTable) { + Map row = dataTable.asMaps(String.class, String.class).getFirst(); + UUID customerId = customerPhoneUUID.get(phoneNumber); + + AdresseLivraison adresse = new AdresseLivraison( + row.get("rue"), row.get("ville"), row.get("codePostal"), row.get("pays") + ); + + OrderInfo orderInfo = new OrderInfo( + customerId, List.of(), adresse, ModePaiement.valueOf(row.get("modePaiement")) + ); + + notValidOrderException = assertThrows(NotValidOrderException.class, + () -> orderUseCase.passerCommande(orderInfo)); + } + + @When("I try to place an order for customer {string} with invalid quantity:") + public void iTryToPlaceAnOrderForCustomerWithInvalidQuantity(String phoneNumber, DataTable dataTable) { + Map row = dataTable.asMaps(String.class, String.class).getFirst(); + UUID customerId = customerPhoneUUID.get(phoneNumber); + UUID livreId = bookIsbnUUID.get(row.get("livreIsbn")); + + AdresseLivraison adresse = new AdresseLivraison( + row.get("rue"), row.get("ville"), row.get("codePostal"), row.get("pays") + ); + + OrderInfo orderInfo = new OrderInfo( + customerId, + List.of(new LigneCommandeInfo(livreId, Integer.parseInt(row.get("quantite")))), + adresse, + ModePaiement.valueOf(row.get("modePaiement")) + ); + + notValidOrderException = assertThrows(NotValidOrderException.class, + () -> orderUseCase.passerCommande(orderInfo)); + } + + @When("I try to place an order for an unknown customer with:") + public void iTryToPlaceAnOrderForAnUnknownCustomerWith(DataTable dataTable) { + Map row = dataTable.asMaps(String.class, String.class).getFirst(); + UUID unknownId = UUID.randomUUID(); + UUID livreId = bookIsbnUUID.get(row.get("livreIsbn")); + + AdresseLivraison adresse = new AdresseLivraison( + row.get("rue"), row.get("ville"), row.get("codePostal"), row.get("pays") + ); + + OrderInfo orderInfo = new OrderInfo( + unknownId, + List.of(new LigneCommandeInfo(livreId, Integer.parseInt(row.get("quantite")))), + adresse, + ModePaiement.valueOf(row.get("modePaiement")) + ); + + try { + orderUseCase.passerCommande(orderInfo); + } catch (CustomerNotFoundException e) { + customerNotFoundThrown = true; + } catch (NotValidOrderException e) { + notValidOrderException = e; + } + } + + @Then("the order placement fails") + public void theOrderPlacementFails() { + assertNotNull(notValidOrderException); + } + + @Then("the order placement fails with customer not found") + public void theOrderPlacementFailsWithCustomerNotFound() { + assertTrue(customerNotFoundThrown); + } + + @And("I receive a validation order error containing {string}") + public void iReceiveAValidationOrderErrorContaining(String errorMessage) { + assertEquals(errorMessage, notValidOrderException.getMessage()); + } } \ No newline at end of file diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/converter/OrderConverterTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/converter/OrderConverterTest.java index d031d14..830cfa5 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/converter/OrderConverterTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/converter/OrderConverterTest.java @@ -1,94 +1,94 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.converter; - -import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; -import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; -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.entity.LigneCommande; -import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; - -import java.math.BigDecimal; -import java.util.List; -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.*; - -@DisplayName("OrderConverter Unit Tests") -class OrderConverterTest { - - private final UUID clientId = UUID.randomUUID(); - private final UUID livreId = UUID.randomUUID(); - private final AdresseLivraison adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); - - @Nested - @DisplayName("toDomain() method tests") - class ToDomainTests { - - @Test - @DisplayName("Should convert OrderInfo to Order domain object") - void shouldConvertOrderInfoToDomain() { - OrderInfo orderInfo = new OrderInfo( - clientId, - List.of(new LigneCommandeInfo(livreId, 2)), - adresse, - ModePaiement.CB - ); - - List lignes = List.of( - LigneCommande.builder() - .livreId(livreId) - .quantite(2) - .prixUnitaire(new BigDecimal("12.90")) - .build() - ); - - Order result = OrderConverter.toDomain(orderInfo, lignes, new BigDecimal("25.80"), 25); - - assertNotNull(result); - assertEquals(clientId, result.getClientId()); - assertEquals(adresse, result.getAdresseLivraison()); - assertEquals(ModePaiement.CB, result.getModePaiement()); - assertEquals(new BigDecimal("25.80"), result.getMontantTotal()); - assertEquals(25, result.getPointsFideliteGagnes()); - assertEquals(1, result.getLignesCommande().size()); - } - - @Test - @DisplayName("Should have null ID after toDomain (set by repository)") - void shouldHaveNullIdAfterToDomain() { - OrderInfo orderInfo = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.CB); - - Order result = OrderConverter.toDomain(orderInfo, List.of(), BigDecimal.ZERO, 0); - - assertNull(result.getId()); - } - } - - @Nested - @DisplayName("toDTO() method tests") - class ToDTOTests { - - @Test - @DisplayName("Should convert Order domain object to OrderDTO with all fields mapped correctly") - void shouldConvertOrderToDTO() { - UUID orderId = UUID.randomUUID(); - Order order = Order.builder() - .id(orderId) - .clientId(clientId) - .montantTotal(new BigDecimal("25.80")) - .pointsFideliteGagnes(25) - .build(); - - OrderDTO result = OrderConverter.toDTO(order); - - assertNotNull(result); - assertEquals(orderId, result.getCommandeId()); - assertEquals(new BigDecimal("25.80"), result.getMontantTotal()); - assertEquals(25, result.getPointsFideliteGagnes()); - } - } +package fr.iut_fbleau.but3.dev62.mylibrary.order.converter; + +import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; +import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; +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.entity.LigneCommande; +import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +@DisplayName("OrderConverter Unit Tests") +class OrderConverterTest { + + private final UUID clientId = UUID.randomUUID(); + private final UUID livreId = UUID.randomUUID(); + private final AdresseLivraison adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); + + @Nested + @DisplayName("toDomain() method tests") + class ToDomainTests { + + @Test + @DisplayName("Should convert OrderInfo to Order domain object") + void shouldConvertOrderInfoToDomain() { + OrderInfo orderInfo = new OrderInfo( + clientId, + List.of(new LigneCommandeInfo(livreId, 2)), + adresse, + ModePaiement.CB + ); + + List lignes = List.of( + LigneCommande.builder() + .livreId(livreId) + .quantite(2) + .prixUnitaire(new BigDecimal("12.90")) + .build() + ); + + Order result = OrderConverter.toDomain(orderInfo, lignes, new BigDecimal("25.80"), 25); + + assertNotNull(result); + assertEquals(clientId, result.getClientId()); + assertEquals(adresse, result.getAdresseLivraison()); + assertEquals(ModePaiement.CB, result.getModePaiement()); + assertEquals(new BigDecimal("25.80"), result.getMontantTotal()); + assertEquals(25, result.getPointsFideliteGagnes()); + assertEquals(1, result.getLignesCommande().size()); + } + + @Test + @DisplayName("Should have null ID after toDomain (set by repository)") + void shouldHaveNullIdAfterToDomain() { + OrderInfo orderInfo = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.CB); + + Order result = OrderConverter.toDomain(orderInfo, List.of(), BigDecimal.ZERO, 0); + + assertNull(result.getId()); + } + } + + @Nested + @DisplayName("toDTO() method tests") + class ToDTOTests { + + @Test + @DisplayName("Should convert Order domain object to OrderDTO with all fields mapped correctly") + void shouldConvertOrderToDTO() { + UUID orderId = UUID.randomUUID(); + Order order = Order.builder() + .id(orderId) + .clientId(clientId) + .montantTotal(new BigDecimal("25.80")) + .pointsFideliteGagnes(25) + .build(); + + OrderDTO result = OrderConverter.toDTO(order); + + assertNotNull(result); + assertEquals(orderId, result.getCommandeId()); + assertEquals(new BigDecimal("25.80"), result.getMontantTotal()); + assertEquals(25, result.getPointsFideliteGagnes()); + } + } } \ No newline at end of file diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/OrderTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/OrderTest.java index f5a5b10..5bd7468 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/OrderTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/entity/OrderTest.java @@ -1,81 +1,81 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.entity; - -import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; -import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.math.BigDecimal; -import java.util.List; -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.*; - -class OrderTest { - - @Test - @DisplayName("Builder should create a valid Order instance") - void testOrderBuilder() { - UUID id = UUID.randomUUID(); - UUID clientId = UUID.randomUUID(); - AdresseLivraison adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); - - Order order = Order.builder() - .id(id) - .clientId(clientId) - .adresseLivraison(adresse) - .modePaiement(ModePaiement.CB) - .montantTotal(new BigDecimal("25.80")) - .pointsFideliteGagnes(25) - .lignesCommande(List.of()) - .build(); - - assertEquals(id, order.getId()); - assertEquals(clientId, order.getClientId()); - assertEquals(adresse, order.getAdresseLivraison()); - assertEquals(ModePaiement.CB, order.getModePaiement()); - assertEquals(new BigDecimal("25.80"), order.getMontantTotal()); - assertEquals(25, order.getPointsFideliteGagnes()); - } - - @Test - @DisplayName("setRandomUUID should set a new non-null UUID") - void testSetRandomUUID() { - Order order = Order.builder().build(); - UUID originalId = order.getId(); - - order.setRandomUUID(); - - assertNotNull(order.getId()); - assertNotEquals(originalId, order.getId()); - } - - @Test - @DisplayName("Two setRandomUUID calls should produce different UUIDs") - void testSetRandomUUIDTwice() { - Order order = Order.builder().build(); - order.setRandomUUID(); - UUID firstId = order.getId(); - - order.setRandomUUID(); - UUID secondId = order.getId(); - - assertNotEquals(firstId, secondId); - } - - @Test - @DisplayName("Builder should create a valid LigneCommande instance") - void testLigneCommandeBuilder() { - UUID livreId = UUID.randomUUID(); - - LigneCommande ligne = LigneCommande.builder() - .livreId(livreId) - .quantite(3) - .prixUnitaire(new BigDecimal("12.90")) - .build(); - - assertEquals(livreId, ligne.getLivreId()); - assertEquals(3, ligne.getQuantite()); - assertEquals(new BigDecimal("12.90"), ligne.getPrixUnitaire()); - } +package fr.iut_fbleau.but3.dev62.mylibrary.order.entity; + +import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; +import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +class OrderTest { + + @Test + @DisplayName("Builder should create a valid Order instance") + void testOrderBuilder() { + UUID id = UUID.randomUUID(); + UUID clientId = UUID.randomUUID(); + AdresseLivraison adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); + + Order order = Order.builder() + .id(id) + .clientId(clientId) + .adresseLivraison(adresse) + .modePaiement(ModePaiement.CB) + .montantTotal(new BigDecimal("25.80")) + .pointsFideliteGagnes(25) + .lignesCommande(List.of()) + .build(); + + assertEquals(id, order.getId()); + assertEquals(clientId, order.getClientId()); + assertEquals(adresse, order.getAdresseLivraison()); + assertEquals(ModePaiement.CB, order.getModePaiement()); + assertEquals(new BigDecimal("25.80"), order.getMontantTotal()); + assertEquals(25, order.getPointsFideliteGagnes()); + } + + @Test + @DisplayName("setRandomUUID should set a new non-null UUID") + void testSetRandomUUID() { + Order order = Order.builder().build(); + UUID originalId = order.getId(); + + order.setRandomUUID(); + + assertNotNull(order.getId()); + assertNotEquals(originalId, order.getId()); + } + + @Test + @DisplayName("Two setRandomUUID calls should produce different UUIDs") + void testSetRandomUUIDTwice() { + Order order = Order.builder().build(); + order.setRandomUUID(); + UUID firstId = order.getId(); + + order.setRandomUUID(); + UUID secondId = order.getId(); + + assertNotEquals(firstId, secondId); + } + + @Test + @DisplayName("Builder should create a valid LigneCommande instance") + void testLigneCommandeBuilder() { + UUID livreId = UUID.randomUUID(); + + LigneCommande ligne = LigneCommande.builder() + .livreId(livreId) + .quantite(3) + .prixUnitaire(new BigDecimal("12.90")) + .build(); + + assertEquals(livreId, ligne.getLivreId()); + assertEquals(3, ligne.getQuantite()); + assertEquals(new BigDecimal("12.90"), ligne.getPrixUnitaire()); + } } \ No newline at end of file diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/NotValidOrderExceptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/NotValidOrderExceptionTest.java index ce25e15..9025e0a 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/NotValidOrderExceptionTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/NotValidOrderExceptionTest.java @@ -1,61 +1,61 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.exception; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import static org.junit.jupiter.api.Assertions.*; - -class NotValidOrderExceptionTest { - - @Test - @DisplayName("Exception should be created with the provided message") - void testExceptionCreation() { - String errorMessage = "Order data is not valid"; - - NotValidOrderException exception = new NotValidOrderException(errorMessage); - - assertEquals(errorMessage, exception.getMessage()); - } - - @ParameterizedTest - @ValueSource(strings = { - "Client id cannot be null", - "Order must have at least one item", - "Quantity must be positive", - "Payment method cannot be null", - "Delivery address cannot be null" - }) - @DisplayName("Exception should handle different validation messages") - void testExceptionWithDifferentMessages(String errorMessage) { - NotValidOrderException exception = new NotValidOrderException(errorMessage); - - assertEquals(errorMessage, exception.getMessage()); - } - - @Test - @DisplayName("Exception should be properly thrown and caught") - void testExceptionCanBeThrownAndCaught() { - String errorMessage = "Order must have at least one item"; - - Exception exception = assertThrows(NotValidOrderException.class, () -> { - throw new NotValidOrderException(errorMessage); - }); - - assertEquals(errorMessage, exception.getMessage()); - } - - @Test - @DisplayName("Exception should be catchable as a general Exception") - void testExceptionInheritance() { - String errorMessage = "Quantity must be positive"; - - try { - throw new NotValidOrderException(errorMessage); - } catch (Exception e) { - assertEquals(NotValidOrderException.class, e.getClass()); - assertEquals(errorMessage, e.getMessage()); - } - } +package fr.iut_fbleau.but3.dev62.mylibrary.order.exception; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.*; + +class NotValidOrderExceptionTest { + + @Test + @DisplayName("Exception should be created with the provided message") + void testExceptionCreation() { + String errorMessage = "Order data is not valid"; + + NotValidOrderException exception = new NotValidOrderException(errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + } + + @ParameterizedTest + @ValueSource(strings = { + "Client id cannot be null", + "Order must have at least one item", + "Quantity must be positive", + "Payment method cannot be null", + "Delivery address cannot be null" + }) + @DisplayName("Exception should handle different validation messages") + void testExceptionWithDifferentMessages(String errorMessage) { + NotValidOrderException exception = new NotValidOrderException(errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should be properly thrown and caught") + void testExceptionCanBeThrownAndCaught() { + String errorMessage = "Order must have at least one item"; + + Exception exception = assertThrows(NotValidOrderException.class, () -> { + throw new NotValidOrderException(errorMessage); + }); + + assertEquals(errorMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should be catchable as a general Exception") + void testExceptionInheritance() { + String errorMessage = "Quantity must be positive"; + + try { + throw new NotValidOrderException(errorMessage); + } catch (Exception e) { + assertEquals(NotValidOrderException.class, e.getClass()); + assertEquals(errorMessage, e.getMessage()); + } + } } \ No newline at end of file diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/OrderNotFoundExceptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/OrderNotFoundExceptionTest.java index ae337a0..2c2198b 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/OrderNotFoundExceptionTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/exception/OrderNotFoundExceptionTest.java @@ -1,47 +1,47 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.exception; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.*; - -class OrderNotFoundExceptionTest { - - @Test - @DisplayName("Exception message should contain the UUID provided") - void testExceptionMessageContainsUUID() { - UUID uuid = UUID.randomUUID(); - - OrderNotFoundException exception = new OrderNotFoundException(uuid); - - String expectedMessage = String.format("The order with id %s does not exist", uuid); - assertEquals(expectedMessage, exception.getMessage()); - } - - @Test - @DisplayName("Exception should use the correct constant message format") - void testExceptionUsesConstantMessageFormat() { - UUID uuid = UUID.randomUUID(); - - OrderNotFoundException exception = new OrderNotFoundException(uuid); - - assertEquals("The order with id {0} does not exist", - OrderNotFoundException.THE_ORDER_WITH_ID_DOES_NOT_EXIST_MESSAGE); - assertTrue(exception.getMessage().contains(uuid.toString())); - } - - @Test - @DisplayName("Exception should be properly thrown and caught") - void testExceptionCanBeThrownAndCaught() { - UUID uuid = UUID.randomUUID(); - - try { - throw new OrderNotFoundException(uuid); - } catch (OrderNotFoundException e) { - String expectedMessage = String.format("The order with id %s does not exist", uuid); - assertEquals(expectedMessage, e.getMessage()); - } - } +package fr.iut_fbleau.but3.dev62.mylibrary.order.exception; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +class OrderNotFoundExceptionTest { + + @Test + @DisplayName("Exception message should contain the UUID provided") + void testExceptionMessageContainsUUID() { + UUID uuid = UUID.randomUUID(); + + OrderNotFoundException exception = new OrderNotFoundException(uuid); + + String expectedMessage = String.format("The order with id %s does not exist", uuid); + assertEquals(expectedMessage, exception.getMessage()); + } + + @Test + @DisplayName("Exception should use the correct constant message format") + void testExceptionUsesConstantMessageFormat() { + UUID uuid = UUID.randomUUID(); + + OrderNotFoundException exception = new OrderNotFoundException(uuid); + + assertEquals("The order with id {0} does not exist", + OrderNotFoundException.THE_ORDER_WITH_ID_DOES_NOT_EXIST_MESSAGE); + assertTrue(exception.getMessage().contains(uuid.toString())); + } + + @Test + @DisplayName("Exception should be properly thrown and caught") + void testExceptionCanBeThrownAndCaught() { + UUID uuid = UUID.randomUUID(); + + try { + throw new OrderNotFoundException(uuid); + } catch (OrderNotFoundException e) { + String expectedMessage = String.format("The order with id %s does not exist", uuid); + assertEquals(expectedMessage, e.getMessage()); + } + } } \ No newline at end of file diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/repository/OrderRepositoryTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/repository/OrderRepositoryTest.java index b97da94..73ec9fc 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/repository/OrderRepositoryTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/repository/OrderRepositoryTest.java @@ -1,202 +1,202 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.repository; - -import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; -import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; -import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; - -import java.math.BigDecimal; -import java.util.List; -import java.util.Optional; -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.*; - -class OrderRepositoryTest { - - private OrderRepository repository; - private Order order1; - private Order order2; - private UUID clientId1; - private UUID clientId2; - - @BeforeEach - void setUp() { - repository = new OrderRepository(); - clientId1 = UUID.randomUUID(); - clientId2 = UUID.randomUUID(); - - AdresseLivraison adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); - - order1 = Order.builder() - .clientId(clientId1) - .adresseLivraison(adresse) - .modePaiement(ModePaiement.CB) - .montantTotal(new BigDecimal("25.80")) - .pointsFideliteGagnes(25) - .lignesCommande(List.of()) - .build(); - order1.setRandomUUID(); - - order2 = Order.builder() - .clientId(clientId2) - .adresseLivraison(adresse) - .modePaiement(ModePaiement.PAYPAL) - .montantTotal(new BigDecimal("9.50")) - .pointsFideliteGagnes(9) - .lignesCommande(List.of()) - .build(); - order2.setRandomUUID(); - } - - @Test - @DisplayName("New repository should be empty") - void testNewRepositoryIsEmpty() { - assertTrue(repository.findAll().isEmpty()); - assertEquals(0, repository.findAll().size()); - } - - @Nested - @DisplayName("Save operations") - class SaveOperations { - - @Test - @DisplayName("Save should add a new order") - void testSaveNewOrder() { - Order saved = repository.save(order1); - - assertEquals(1, repository.findAll().size()); - assertEquals(order1.getId(), saved.getId()); - } - - @Test - @DisplayName("Save should update existing order with same ID") - void testSaveUpdatesExistingOrder() { - repository.save(order1); - UUID id = order1.getId(); - - Order updatedOrder = Order.builder() - .id(id) - .clientId(clientId1) - .adresseLivraison(new AdresseLivraison("2 rue de Lyon", "Lyon", "69001", "France")) - .modePaiement(ModePaiement.PAYPAL) - .montantTotal(new BigDecimal("50.00")) - .pointsFideliteGagnes(50) - .lignesCommande(List.of()) - .build(); - - Order saved = repository.save(updatedOrder); - - assertEquals(1, repository.findAll().size()); - assertEquals(id, saved.getId()); - assertEquals(new BigDecimal("50.00"), saved.getMontantTotal()); - } - - @Test - @DisplayName("Save multiple orders should add all of them") - void testSaveMultipleOrders() { - repository.save(order1); - repository.save(order2); - - assertEquals(2, repository.findAll().size()); - } - } - - @Nested - @DisplayName("Find operations") - class FindOperations { - - @BeforeEach - void setUpOrders() { - repository.save(order1); - repository.save(order2); - } - - @Test - @DisplayName("FindAll should return all orders") - void testFindAll() { - assertEquals(2, repository.findAll().size()); - } - - @Test - @DisplayName("FindById should return order with matching ID") - void testFindById() { - Optional found = repository.findById(order1.getId()); - - assertTrue(found.isPresent()); - assertEquals(order1.getId(), found.get().getId()); - } - - @Test - @DisplayName("FindById should return empty Optional when ID doesn't exist") - void testFindByIdNotFound() { - Optional found = repository.findById(UUID.randomUUID()); - - assertTrue(found.isEmpty()); - } - - @Test - @DisplayName("FindByClientId should return all orders for a client") - void testFindByClientId() { - List found = repository.findByClientId(clientId1); - - assertEquals(1, found.size()); - assertEquals(order1.getId(), found.getFirst().getId()); - } - - @Test - @DisplayName("ExistsById should return true when ID exists") - void testExistsByIdExists() { - assertTrue(repository.existsById(order1.getId())); - } - - @Test - @DisplayName("ExistsById should return false when ID doesn't exist") - void testExistsByIdNotExists() { - assertFalse(repository.existsById(UUID.randomUUID())); - } - } - - @Nested - @DisplayName("Delete operations") - class DeleteOperations { - - @BeforeEach - void setUpOrders() { - repository.save(order1); - repository.save(order2); - } - - @Test - @DisplayName("Delete should remove the specified order") - void testDelete() { - repository.delete(order1); - - assertEquals(1, repository.findAll().size()); - assertFalse(repository.findAll().contains(order1)); - assertTrue(repository.findAll().contains(order2)); - } - - @Test - @DisplayName("DeleteAll should remove all orders") - void testDeleteAll() { - repository.deleteAll(); - - assertTrue(repository.findAll().isEmpty()); - } - - @Test - @DisplayName("Delete should not throw exception when order doesn't exist") - void testDeleteNonExistentOrder() { - Order nonExistent = Order.builder().build(); - nonExistent.setRandomUUID(); - - assertDoesNotThrow(() -> repository.delete(nonExistent)); - assertEquals(2, repository.findAll().size()); - } - } -} +package fr.iut_fbleau.but3.dev62.mylibrary.order.repository; + +import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; +import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; +import fr.iut_fbleau.but3.dev62.mylibrary.order.entity.Order; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +class OrderRepositoryTest { + + private OrderRepository repository; + private Order order1; + private Order order2; + private UUID clientId1; + private UUID clientId2; + + @BeforeEach + void setUp() { + repository = new OrderRepository(); + clientId1 = UUID.randomUUID(); + clientId2 = UUID.randomUUID(); + + AdresseLivraison adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); + + order1 = Order.builder() + .clientId(clientId1) + .adresseLivraison(adresse) + .modePaiement(ModePaiement.CB) + .montantTotal(new BigDecimal("25.80")) + .pointsFideliteGagnes(25) + .lignesCommande(List.of()) + .build(); + order1.setRandomUUID(); + + order2 = Order.builder() + .clientId(clientId2) + .adresseLivraison(adresse) + .modePaiement(ModePaiement.PAYPAL) + .montantTotal(new BigDecimal("9.50")) + .pointsFideliteGagnes(9) + .lignesCommande(List.of()) + .build(); + order2.setRandomUUID(); + } + + @Test + @DisplayName("New repository should be empty") + void testNewRepositoryIsEmpty() { + assertTrue(repository.findAll().isEmpty()); + assertEquals(0, repository.findAll().size()); + } + + @Nested + @DisplayName("Save operations") + class SaveOperations { + + @Test + @DisplayName("Save should add a new order") + void testSaveNewOrder() { + Order saved = repository.save(order1); + + assertEquals(1, repository.findAll().size()); + assertEquals(order1.getId(), saved.getId()); + } + + @Test + @DisplayName("Save should update existing order with same ID") + void testSaveUpdatesExistingOrder() { + repository.save(order1); + UUID id = order1.getId(); + + Order updatedOrder = Order.builder() + .id(id) + .clientId(clientId1) + .adresseLivraison(new AdresseLivraison("2 rue de Lyon", "Lyon", "69001", "France")) + .modePaiement(ModePaiement.PAYPAL) + .montantTotal(new BigDecimal("50.00")) + .pointsFideliteGagnes(50) + .lignesCommande(List.of()) + .build(); + + Order saved = repository.save(updatedOrder); + + assertEquals(1, repository.findAll().size()); + assertEquals(id, saved.getId()); + assertEquals(new BigDecimal("50.00"), saved.getMontantTotal()); + } + + @Test + @DisplayName("Save multiple orders should add all of them") + void testSaveMultipleOrders() { + repository.save(order1); + repository.save(order2); + + assertEquals(2, repository.findAll().size()); + } + } + + @Nested + @DisplayName("Find operations") + class FindOperations { + + @BeforeEach + void setUpOrders() { + repository.save(order1); + repository.save(order2); + } + + @Test + @DisplayName("FindAll should return all orders") + void testFindAll() { + assertEquals(2, repository.findAll().size()); + } + + @Test + @DisplayName("FindById should return order with matching ID") + void testFindById() { + Optional found = repository.findById(order1.getId()); + + assertTrue(found.isPresent()); + assertEquals(order1.getId(), found.get().getId()); + } + + @Test + @DisplayName("FindById should return empty Optional when ID doesn't exist") + void testFindByIdNotFound() { + Optional found = repository.findById(UUID.randomUUID()); + + assertTrue(found.isEmpty()); + } + + @Test + @DisplayName("FindByClientId should return all orders for a client") + void testFindByClientId() { + List found = repository.findByClientId(clientId1); + + assertEquals(1, found.size()); + assertEquals(order1.getId(), found.getFirst().getId()); + } + + @Test + @DisplayName("ExistsById should return true when ID exists") + void testExistsByIdExists() { + assertTrue(repository.existsById(order1.getId())); + } + + @Test + @DisplayName("ExistsById should return false when ID doesn't exist") + void testExistsByIdNotExists() { + assertFalse(repository.existsById(UUID.randomUUID())); + } + } + + @Nested + @DisplayName("Delete operations") + class DeleteOperations { + + @BeforeEach + void setUpOrders() { + repository.save(order1); + repository.save(order2); + } + + @Test + @DisplayName("Delete should remove the specified order") + void testDelete() { + repository.delete(order1); + + assertEquals(1, repository.findAll().size()); + assertFalse(repository.findAll().contains(order1)); + assertTrue(repository.findAll().contains(order2)); + } + + @Test + @DisplayName("DeleteAll should remove all orders") + void testDeleteAll() { + repository.deleteAll(); + + assertTrue(repository.findAll().isEmpty()); + } + + @Test + @DisplayName("Delete should not throw exception when order doesn't exist") + void testDeleteNonExistentOrder() { + Order nonExistent = Order.builder().build(); + nonExistent.setRandomUUID(); + + assertDoesNotThrow(() -> repository.delete(nonExistent)); + assertEquals(2, repository.findAll().size()); + } + } +} \ No newline at end of file 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 6cb30c6..b7cf1f4 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 @@ -1,226 +1,226 @@ -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.repository.BookRepository; -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.repository.CustomerRepository; -import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; -import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; -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.entity.Order; -import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException; -import fr.iut_fbleau.but3.dev62.mylibrary.order.repository.OrderRepository; -import java.math.BigDecimal; -import java.time.LocalDate; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; - -@ExtendWith(MockitoExtension.class) -class OrderUseCaseTest { - - @Mock - private OrderRepository orderRepository; - - @Mock - private CustomerRepository customerRepository; - - @Mock - private BookRepository bookRepository; - - @InjectMocks - private OrderUseCase orderUseCase; - - private UUID customerId; - private UUID bookId; - private Customer testCustomer; - private Book testBook; - private AdresseLivraison adresse; - private OrderInfo validOrderInfo; - - @BeforeEach - void setUp() { - customerId = UUID.randomUUID(); - bookId = UUID.randomUUID(); - - testCustomer = Customer.builder() - .id(customerId) - .firstName("Marie") - .lastName("Dupont") - .phoneNumber("0612345678") - .loyaltyPoints(100) - .build(); - - testBook = Book.builder() - .id(bookId) - .isbn("9782016289308") - .title("Le Petit Prince") - .author("Antoine de Saint-Exupery") - .publisher("Gallimard") - .publicationDate(LocalDate.of(1943, 4, 6)) - .price(new BigDecimal("12.90")) - .stock(10) - .categories(List.of("Roman")) - .description("Un classique") - .language("FR") - .build(); - - adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); - - validOrderInfo = new OrderInfo( - customerId, - List.of(new LigneCommandeInfo(bookId, 2)), - adresse, - ModePaiement.CB - ); - } - - @Nested - @DisplayName("PasserCommande tests") - class PasserCommandeTests { - - @Test - @DisplayName("Should place order when valid data is provided") - void testPasserCommandeWithValidData() throws NotValidOrderException, CustomerNotFoundException { - when(customerRepository.findById(customerId)).thenReturn(Optional.of(testCustomer)); - when(bookRepository.findById(bookId)).thenReturn(Optional.of(testBook)); - when(customerRepository.save(any(Customer.class))).thenReturn(testCustomer); - - UUID orderId = UUID.randomUUID(); - Order savedOrder = Order.builder() - .id(orderId) - .clientId(customerId) - .montantTotal(new BigDecimal("25.80")) - .pointsFideliteGagnes(25) - .build(); - when(orderRepository.save(any(Order.class))).thenReturn(savedOrder); - - OrderDTO result = orderUseCase.passerCommande(validOrderInfo); - - assertNotNull(result); - assertEquals(orderId, result.getCommandeId()); - verify(orderRepository, times(1)).save(any(Order.class)); - verify(customerRepository, times(1)).save(any(Customer.class)); - } - - @Test - @DisplayName("Should throw exception when customer does not exist") - void testPasserCommandeWithUnknownCustomer() { - when(customerRepository.findById(customerId)).thenReturn(Optional.empty()); - - assertThrows(CustomerNotFoundException.class, - () -> orderUseCase.passerCommande(validOrderInfo)); - - verify(orderRepository, never()).save(any(Order.class)); - } - - @Test - @DisplayName("Should throw exception when order has no items") - void testPasserCommandeWithNoItems() { - OrderInfo emptyOrder = new OrderInfo(customerId, List.of(), adresse, ModePaiement.CB); - - assertThrows(NotValidOrderException.class, - () -> orderUseCase.passerCommande(emptyOrder)); - - verify(orderRepository, never()).save(any(Order.class)); - } - - @Test - @DisplayName("Should throw exception when quantity is zero or negative") - void testPasserCommandeWithInvalidQuantity() { - OrderInfo invalidQty = new OrderInfo( - customerId, - List.of(new LigneCommandeInfo(bookId, 0)), - adresse, - ModePaiement.CB - ); - - assertThrows(NotValidOrderException.class, - () -> orderUseCase.passerCommande(invalidQty)); - - verify(orderRepository, never()).save(any(Order.class)); - } - - @Test - @DisplayName("Should throw exception when clientId is null") - void testPasserCommandeWithNullClientId() { - OrderInfo nullClient = new OrderInfo( - null, - List.of(new LigneCommandeInfo(bookId, 1)), - adresse, - ModePaiement.CB - ); - - assertThrows(NotValidOrderException.class, - () -> orderUseCase.passerCommande(nullClient)); - - verify(orderRepository, never()).save(any(Order.class)); - } - - @Test - @DisplayName("Should throw exception when payment method is null") - void testPasserCommandeWithNullModePaiement() { - OrderInfo nullPayment = new OrderInfo( - customerId, - List.of(new LigneCommandeInfo(bookId, 1)), - adresse, - null - ); - - assertThrows(NotValidOrderException.class, - () -> orderUseCase.passerCommande(nullPayment)); - - verify(orderRepository, never()).save(any(Order.class)); - } - } - - @Nested - @DisplayName("FindOrder tests") - class FindOrderTests { - - @Test - @DisplayName("Should return order when ID exists") - void testFindOrderById() { - UUID orderId = UUID.randomUUID(); - Order order = Order.builder() - .id(orderId) - .clientId(customerId) - .montantTotal(new BigDecimal("25.80")) - .pointsFideliteGagnes(25) - .build(); - - when(orderRepository.findById(orderId)).thenReturn(Optional.of(order)); - - Optional result = orderUseCase.findOrderById(orderId); - - assertTrue(result.isPresent()); - assertEquals(orderId, result.get().getCommandeId()); - } - - @Test - @DisplayName("Should return empty Optional when ID does not exist") - void testFindOrderByIdNotFound() { - UUID nonExistentId = UUID.randomUUID(); - when(orderRepository.findById(nonExistentId)).thenReturn(Optional.empty()); - - Optional result = orderUseCase.findOrderById(nonExistentId); - - assertTrue(result.isEmpty()); - } - } +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.repository.BookRepository; +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.repository.CustomerRepository; +import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; +import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; +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.entity.Order; +import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException; +import fr.iut_fbleau.but3.dev62.mylibrary.order.repository.OrderRepository; +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class OrderUseCaseTest { + + @Mock + private OrderRepository orderRepository; + + @Mock + private CustomerRepository customerRepository; + + @Mock + private BookRepository bookRepository; + + @InjectMocks + private OrderUseCase orderUseCase; + + private UUID customerId; + private UUID bookId; + private Customer testCustomer; + private Book testBook; + private AdresseLivraison adresse; + private OrderInfo validOrderInfo; + + @BeforeEach + void setUp() { + customerId = UUID.randomUUID(); + bookId = UUID.randomUUID(); + + testCustomer = Customer.builder() + .id(customerId) + .firstName("Marie") + .lastName("Dupont") + .phoneNumber("0612345678") + .loyaltyPoints(100) + .build(); + + testBook = Book.builder() + .id(bookId) + .isbn("9782016289308") + .title("Le Petit Prince") + .author("Antoine de Saint-Exupery") + .publisher("Gallimard") + .publicationDate(LocalDate.of(1943, 4, 6)) + .price(new BigDecimal("12.90")) + .stock(10) + .categories(List.of("Roman")) + .description("Un classique") + .language("FR") + .build(); + + adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); + + validOrderInfo = new OrderInfo( + customerId, + List.of(new LigneCommandeInfo(bookId, 2)), + adresse, + ModePaiement.CB + ); + } + + @Nested + @DisplayName("PasserCommande tests") + class PasserCommandeTests { + + @Test + @DisplayName("Should place order when valid data is provided") + void testPasserCommandeWithValidData() throws NotValidOrderException, CustomerNotFoundException { + when(customerRepository.findById(customerId)).thenReturn(Optional.of(testCustomer)); + when(bookRepository.findById(bookId)).thenReturn(Optional.of(testBook)); + when(customerRepository.save(any(Customer.class))).thenReturn(testCustomer); + + UUID orderId = UUID.randomUUID(); + Order savedOrder = Order.builder() + .id(orderId) + .clientId(customerId) + .montantTotal(new BigDecimal("25.80")) + .pointsFideliteGagnes(25) + .build(); + when(orderRepository.save(any(Order.class))).thenReturn(savedOrder); + + OrderDTO result = orderUseCase.passerCommande(validOrderInfo); + + assertNotNull(result); + assertEquals(orderId, result.getCommandeId()); + verify(orderRepository, times(1)).save(any(Order.class)); + verify(customerRepository, times(1)).save(any(Customer.class)); + } + + @Test + @DisplayName("Should throw exception when customer does not exist") + void testPasserCommandeWithUnknownCustomer() { + when(customerRepository.findById(customerId)).thenReturn(Optional.empty()); + + assertThrows(CustomerNotFoundException.class, + () -> orderUseCase.passerCommande(validOrderInfo)); + + verify(orderRepository, never()).save(any(Order.class)); + } + + @Test + @DisplayName("Should throw exception when order has no items") + void testPasserCommandeWithNoItems() { + OrderInfo emptyOrder = new OrderInfo(customerId, List.of(), adresse, ModePaiement.CB); + + assertThrows(NotValidOrderException.class, + () -> orderUseCase.passerCommande(emptyOrder)); + + verify(orderRepository, never()).save(any(Order.class)); + } + + @Test + @DisplayName("Should throw exception when quantity is zero or negative") + void testPasserCommandeWithInvalidQuantity() { + OrderInfo invalidQty = new OrderInfo( + customerId, + List.of(new LigneCommandeInfo(bookId, 0)), + adresse, + ModePaiement.CB + ); + + assertThrows(NotValidOrderException.class, + () -> orderUseCase.passerCommande(invalidQty)); + + verify(orderRepository, never()).save(any(Order.class)); + } + + @Test + @DisplayName("Should throw exception when clientId is null") + void testPasserCommandeWithNullClientId() { + OrderInfo nullClient = new OrderInfo( + null, + List.of(new LigneCommandeInfo(bookId, 1)), + adresse, + ModePaiement.CB + ); + + assertThrows(NotValidOrderException.class, + () -> orderUseCase.passerCommande(nullClient)); + + verify(orderRepository, never()).save(any(Order.class)); + } + + @Test + @DisplayName("Should throw exception when payment method is null") + void testPasserCommandeWithNullModePaiement() { + OrderInfo nullPayment = new OrderInfo( + customerId, + List.of(new LigneCommandeInfo(bookId, 1)), + adresse, + null + ); + + assertThrows(NotValidOrderException.class, + () -> orderUseCase.passerCommande(nullPayment)); + + verify(orderRepository, never()).save(any(Order.class)); + } + } + + @Nested + @DisplayName("FindOrder tests") + class FindOrderTests { + + @Test + @DisplayName("Should return order when ID exists") + void testFindOrderById() { + UUID orderId = UUID.randomUUID(); + Order order = Order.builder() + .id(orderId) + .clientId(customerId) + .montantTotal(new BigDecimal("25.80")) + .pointsFideliteGagnes(25) + .build(); + + when(orderRepository.findById(orderId)).thenReturn(Optional.of(order)); + + Optional result = orderUseCase.findOrderById(orderId); + + assertTrue(result.isPresent()); + assertEquals(orderId, result.get().getCommandeId()); + } + + @Test + @DisplayName("Should return empty Optional when ID does not exist") + void testFindOrderByIdNotFound() { + UUID nonExistentId = UUID.randomUUID(); + when(orderRepository.findById(nonExistentId)).thenReturn(Optional.empty()); + + Optional result = orderUseCase.findOrderById(nonExistentId); + + assertTrue(result.isEmpty()); + } + } } \ No newline at end of file diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/validator/OrderValidatorTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/validator/OrderValidatorTest.java index f619356..ee928db 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/validator/OrderValidatorTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/order/validator/OrderValidatorTest.java @@ -1,155 +1,155 @@ -package fr.iut_fbleau.but3.dev62.mylibrary.order.validator; - -import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; -import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; -import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo; -import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; - -import java.util.List; -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.*; - -class OrderValidatorTest { - - private final UUID clientId = UUID.randomUUID(); - private final UUID livreId = UUID.randomUUID(); - private final AdresseLivraison adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); - - private OrderInfo validOrder() { - return new OrderInfo( - clientId, - List.of(new LigneCommandeInfo(livreId, 1)), - adresse, - ModePaiement.CB - ); - } - - @Test - @DisplayName("Should validate order with valid data") - void testValidateValidOrder() { - assertDoesNotThrow(() -> OrderValidator.validate(validOrder())); - } - - @Nested - @DisplayName("ClientId validation tests") - class ClientIdValidationTests { - - @Test - @DisplayName("Should throw exception when clientId is null") - void testValidateNullClientId() { - OrderInfo order = new OrderInfo(null, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.CB); - - NotValidOrderException exception = assertThrows(NotValidOrderException.class, - () -> OrderValidator.validate(order)); - - assertEquals(OrderValidator.CLIENT_ID_CANNOT_BE_NULL, exception.getMessage()); - } - } - - @Nested - @DisplayName("LignesCommande validation tests") - class LignesCommandeValidationTests { - - @Test - @DisplayName("Should throw exception when lignesCommande is empty") - void testValidateEmptyLignesCommande() { - OrderInfo order = new OrderInfo(clientId, List.of(), adresse, ModePaiement.CB); - - NotValidOrderException exception = assertThrows(NotValidOrderException.class, - () -> OrderValidator.validate(order)); - - assertEquals(OrderValidator.LIGNES_COMMANDE_CANNOT_BE_EMPTY, exception.getMessage()); - } - - @Test - @DisplayName("Should throw exception when lignesCommande is null") - void testValidateNullLignesCommande() { - OrderInfo order = new OrderInfo(clientId, null, adresse, ModePaiement.CB); - - NotValidOrderException exception = assertThrows(NotValidOrderException.class, - () -> OrderValidator.validate(order)); - - assertEquals(OrderValidator.LIGNES_COMMANDE_CANNOT_BE_EMPTY, exception.getMessage()); - } - - @Test - @DisplayName("Should throw exception when quantite is zero") - void testValidateZeroQuantite() { - OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 0)), adresse, ModePaiement.CB); - - NotValidOrderException exception = assertThrows(NotValidOrderException.class, - () -> OrderValidator.validate(order)); - - assertEquals(OrderValidator.QUANTITE_MUST_BE_POSITIVE, exception.getMessage()); - } - - @Test - @DisplayName("Should throw exception when quantite is negative") - void testValidateNegativeQuantite() { - OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, -1)), adresse, ModePaiement.CB); - - NotValidOrderException exception = assertThrows(NotValidOrderException.class, - () -> OrderValidator.validate(order)); - - assertEquals(OrderValidator.QUANTITE_MUST_BE_POSITIVE, exception.getMessage()); - } - } - - @Nested - @DisplayName("AdresseLivraison validation tests") - class AdresseLivraisonValidationTests { - - @Test - @DisplayName("Should throw exception when adresseLivraison is null") - void testValidateNullAdresseLivraison() { - OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), null, ModePaiement.CB); - - NotValidOrderException exception = assertThrows(NotValidOrderException.class, - () -> OrderValidator.validate(order)); - - assertEquals(OrderValidator.ADRESSE_LIVRAISON_CANNOT_BE_NULL, exception.getMessage()); - } - } - - @Nested - @DisplayName("ModePaiement validation tests") - class ModePaiementValidationTests { - - @Test - @DisplayName("Should throw exception when modePaiement is null") - void testValidateNullModePaiement() { - OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, null); - - NotValidOrderException exception = assertThrows(NotValidOrderException.class, - () -> OrderValidator.validate(order)); - - assertEquals(OrderValidator.MODE_PAIEMENT_CANNOT_BE_NULL, exception.getMessage()); - } - - @Test - @DisplayName("Should validate with CB payment method") - void testValidateWithCB() { - OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.CB); - assertDoesNotThrow(() -> OrderValidator.validate(order)); - } - - @Test - @DisplayName("Should validate with PAYPAL payment method") - void testValidateWithPaypal() { - OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.PAYPAL); - assertDoesNotThrow(() -> OrderValidator.validate(order)); - } - - @Test - @DisplayName("Should validate with POINTS_FIDELITE payment method") - void testValidateWithPointsFidelite() { - OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.POINTS_FIDELITE); - assertDoesNotThrow(() -> OrderValidator.validate(order)); - } - } +package fr.iut_fbleau.but3.dev62.mylibrary.order.validator; + +import fr.iut_fbleau.but3.dev62.mylibrary.order.AdresseLivraison; +import fr.iut_fbleau.but3.dev62.mylibrary.order.LigneCommandeInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.order.ModePaiement; +import fr.iut_fbleau.but3.dev62.mylibrary.order.OrderInfo; +import fr.iut_fbleau.but3.dev62.mylibrary.order.exception.NotValidOrderException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +class OrderValidatorTest { + + private final UUID clientId = UUID.randomUUID(); + private final UUID livreId = UUID.randomUUID(); + private final AdresseLivraison adresse = new AdresseLivraison("1 rue de Paris", "Paris", "75001", "France"); + + private OrderInfo validOrder() { + return new OrderInfo( + clientId, + List.of(new LigneCommandeInfo(livreId, 1)), + adresse, + ModePaiement.CB + ); + } + + @Test + @DisplayName("Should validate order with valid data") + void testValidateValidOrder() { + assertDoesNotThrow(() -> OrderValidator.validate(validOrder())); + } + + @Nested + @DisplayName("ClientId validation tests") + class ClientIdValidationTests { + + @Test + @DisplayName("Should throw exception when clientId is null") + void testValidateNullClientId() { + OrderInfo order = new OrderInfo(null, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.CB); + + NotValidOrderException exception = assertThrows(NotValidOrderException.class, + () -> OrderValidator.validate(order)); + + assertEquals(OrderValidator.CLIENT_ID_CANNOT_BE_NULL, exception.getMessage()); + } + } + + @Nested + @DisplayName("LignesCommande validation tests") + class LignesCommandeValidationTests { + + @Test + @DisplayName("Should throw exception when lignesCommande is empty") + void testValidateEmptyLignesCommande() { + OrderInfo order = new OrderInfo(clientId, List.of(), adresse, ModePaiement.CB); + + NotValidOrderException exception = assertThrows(NotValidOrderException.class, + () -> OrderValidator.validate(order)); + + assertEquals(OrderValidator.LIGNES_COMMANDE_CANNOT_BE_EMPTY, exception.getMessage()); + } + + @Test + @DisplayName("Should throw exception when lignesCommande is null") + void testValidateNullLignesCommande() { + OrderInfo order = new OrderInfo(clientId, null, adresse, ModePaiement.CB); + + NotValidOrderException exception = assertThrows(NotValidOrderException.class, + () -> OrderValidator.validate(order)); + + assertEquals(OrderValidator.LIGNES_COMMANDE_CANNOT_BE_EMPTY, exception.getMessage()); + } + + @Test + @DisplayName("Should throw exception when quantite is zero") + void testValidateZeroQuantite() { + OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 0)), adresse, ModePaiement.CB); + + NotValidOrderException exception = assertThrows(NotValidOrderException.class, + () -> OrderValidator.validate(order)); + + assertEquals(OrderValidator.QUANTITE_MUST_BE_POSITIVE, exception.getMessage()); + } + + @Test + @DisplayName("Should throw exception when quantite is negative") + void testValidateNegativeQuantite() { + OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, -1)), adresse, ModePaiement.CB); + + NotValidOrderException exception = assertThrows(NotValidOrderException.class, + () -> OrderValidator.validate(order)); + + assertEquals(OrderValidator.QUANTITE_MUST_BE_POSITIVE, exception.getMessage()); + } + } + + @Nested + @DisplayName("AdresseLivraison validation tests") + class AdresseLivraisonValidationTests { + + @Test + @DisplayName("Should throw exception when adresseLivraison is null") + void testValidateNullAdresseLivraison() { + OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), null, ModePaiement.CB); + + NotValidOrderException exception = assertThrows(NotValidOrderException.class, + () -> OrderValidator.validate(order)); + + assertEquals(OrderValidator.ADRESSE_LIVRAISON_CANNOT_BE_NULL, exception.getMessage()); + } + } + + @Nested + @DisplayName("ModePaiement validation tests") + class ModePaiementValidationTests { + + @Test + @DisplayName("Should throw exception when modePaiement is null") + void testValidateNullModePaiement() { + OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, null); + + NotValidOrderException exception = assertThrows(NotValidOrderException.class, + () -> OrderValidator.validate(order)); + + assertEquals(OrderValidator.MODE_PAIEMENT_CANNOT_BE_NULL, exception.getMessage()); + } + + @Test + @DisplayName("Should validate with CB payment method") + void testValidateWithCB() { + OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.CB); + assertDoesNotThrow(() -> OrderValidator.validate(order)); + } + + @Test + @DisplayName("Should validate with PAYPAL payment method") + void testValidateWithPaypal() { + OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.PAYPAL); + assertDoesNotThrow(() -> OrderValidator.validate(order)); + } + + @Test + @DisplayName("Should validate with POINTS_FIDELITE payment method") + void testValidateWithPointsFidelite() { + OrderInfo order = new OrderInfo(clientId, List.of(new LigneCommandeInfo(livreId, 1)), adresse, ModePaiement.POINTS_FIDELITE); + assertDoesNotThrow(() -> OrderValidator.validate(order)); + } + } } \ No newline at end of file diff --git a/src/test/resources/features/order.feature b/src/test/resources/features/order.feature index 800718d..8e5c7fb 100644 --- a/src/test/resources/features/order.feature +++ b/src/test/resources/features/order.feature @@ -1,49 +1,49 @@ -# language: en - -Feature: Place an order - - Background: - Given the system has the following customers for order: - | prenom | nom | numeroTelephone | pointsFidelite | - | Marie | Dupont | 0612345678 | 100 | - | Jean | Martin | 0687654321 | 50 | - And the catalog has the following books for order: - | isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue | - | 9782016289308 | Le Petit Prince | Antoine de Saint-Exupery | Gallimard | 1943-04-06 | 12.90 | 10 | Roman | Un classique | FR | - | 9782070409189 | L Etranger | Albert Camus | Gallimard | 1942-05-19 | 9.50 | 5 | Roman | Philosophique| FR | - - Scenario: Place an order with CB payment - When I place an order for customer "0612345678" with: - | livreIsbn | quantite | rue | ville | codePostal | pays | modePaiement | - | 9782016289308 | 2 | 1 rue de Paris | Paris | 75001 | France | CB | - Then a new order is created - And the order total is 25.80 - And the customer "0612345678" now has 125 loyalty points - - Scenario: Place an order with PAYPAL payment - When I place an order for customer "0687654321" with: - | livreIsbn | quantite | rue | ville | codePostal | pays | modePaiement | - | 9782070409189 | 1 | 5 rue de Lyon | Lyon | 69001 | France | PAYPAL | - Then a new order is created - And the order total is 9.50 - And the customer "0687654321" now has 59 loyalty points - - Scenario: Attempt to place an order with no items - When I try to place an order for customer "0612345678" with no items: - | rue | ville | codePostal | pays | modePaiement | - | 1 rue de Paris | Paris | 75001 | France | CB | - Then the order placement fails - And I receive a validation order error containing "Order must have at least one item" - - Scenario: Attempt to place an order with invalid quantity - When I try to place an order for customer "0612345678" with invalid quantity: - | livreIsbn | quantite | rue | ville | codePostal | pays | modePaiement | - | 9782016289308 | 0 | 1 rue de Paris | Paris | 75001 | France | CB | - Then the order placement fails - And I receive a validation order error containing "Quantity must be positive" - - Scenario: Attempt to place an order for unknown customer - When I try to place an order for an unknown customer with: - | livreIsbn | quantite | rue | ville | codePostal | pays | modePaiement | - | 9782016289308 | 1 | 1 rue de Paris | Paris | 75001 | France | CB | - Then the order placement fails with customer not found +# language: en + +Feature: Place an order + + Background: + Given the system has the following customers for order: + | prenom | nom | numeroTelephone | pointsFidelite | + | Marie | Dupont | 0612345678 | 100 | + | Jean | Martin | 0687654321 | 50 | + And the catalog has the following books for order: + | isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue | + | 9782016289308 | Le Petit Prince | Antoine de Saint-Exupery | Gallimard | 1943-04-06 | 12.90 | 10 | Roman | Un classique | FR | + | 9782070409189 | L Etranger | Albert Camus | Gallimard | 1942-05-19 | 9.50 | 5 | Roman | Philosophique| FR | + + Scenario: Place an order with CB payment + When I place an order for customer "0612345678" with: + | livreIsbn | quantite | rue | ville | codePostal | pays | modePaiement | + | 9782016289308 | 2 | 1 rue de Paris | Paris | 75001 | France | CB | + Then a new order is created + And the order total is 25.80 + And the customer "0612345678" now has 125 loyalty points + + Scenario: Place an order with PAYPAL payment + When I place an order for customer "0687654321" with: + | livreIsbn | quantite | rue | ville | codePostal | pays | modePaiement | + | 9782070409189 | 1 | 5 rue de Lyon | Lyon | 69001 | France | PAYPAL | + Then a new order is created + And the order total is 9.50 + And the customer "0687654321" now has 59 loyalty points + + Scenario: Attempt to place an order with no items + When I try to place an order for customer "0612345678" with no items: + | rue | ville | codePostal | pays | modePaiement | + | 1 rue de Paris | Paris | 75001 | France | CB | + Then the order placement fails + And I receive a validation order error containing "Order must have at least one item" + + Scenario: Attempt to place an order with invalid quantity + When I try to place an order for customer "0612345678" with invalid quantity: + | livreIsbn | quantite | rue | ville | codePostal | pays | modePaiement | + | 9782016289308 | 0 | 1 rue de Paris | Paris | 75001 | France | CB | + Then the order placement fails + And I receive a validation order error containing "Quantity must be positive" + + Scenario: Attempt to place an order for unknown customer + When I try to place an order for an unknown customer with: + | livreIsbn | quantite | rue | ville | codePostal | pays | modePaiement | + | 9782016289308 | 1 | 1 rue de Paris | Paris | 75001 | France | CB | + Then the order placement fails with customer not found