Compare commits
35 Commits
main
..
1d066a802f
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d066a802f | |||
| a4808a28c9 | |||
| 90939326dc | |||
| 5cc3986307 | |||
| 5705081bbe | |||
| e11c5b744f | |||
| 90929b7bdd | |||
| 62b4a30add | |||
| 6e2bf9aab9 | |||
| 07b9357829 | |||
| 20fafa1a1c | |||
| 77d79de12a | |||
| 0c87631c85 | |||
| 50dd02423d | |||
| 6e3750e275 | |||
| 58e4a53196 | |||
| ee299e1e19 | |||
| 5196f30fd8 | |||
| 4d47cdd9a0 | |||
| 099cb403ca | |||
| 4c79ddcf6a | |||
| d8ae07a032 | |||
| 7d11743c02 | |||
| 182fcc0259 | |||
| adb6a885ae | |||
| aca81b68db | |||
| 495343ddb8 | |||
| 2b29d34615 | |||
| 961b3b69ba | |||
| 958f391b05 | |||
| e8563d44b1 | |||
| 55e43fca89 | |||
| c767f47719 | |||
| 370d35078f | |||
| 80c2b82c98 |
@@ -0,0 +1 @@
|
|||||||
|
Notre groupe est constitué de Marvin Aubert, Maxime Lebreton et de Patrick Felix Vimalaratnam
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
"langue": "string"
|
"langue": "string"
|
||||||
},
|
},
|
||||||
"output": {
|
"output": {
|
||||||
|
"_comment": "c'est ce qui affiche quand tu crée avec la usecase (regarde register de customer)",
|
||||||
"isbn": "string(13)"
|
"isbn": "string(13)"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -117,6 +117,11 @@
|
|||||||
<version>${mockito.version}</version>
|
<version>${mockito.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public class BookDTO {
|
||||||
|
private String isbn;
|
||||||
|
private String title;
|
||||||
|
private String author;
|
||||||
|
private String editor;
|
||||||
|
private LocalDate date;
|
||||||
|
private double price;
|
||||||
|
private Integer stock;
|
||||||
|
private ArrayList<String> categories = new ArrayList<>();
|
||||||
|
private String description ;
|
||||||
|
private String language;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@Builder
|
||||||
|
@Getter
|
||||||
|
public class BookDetails {
|
||||||
|
private ArrayList<String> categories = new ArrayList<>();
|
||||||
|
private String description ;
|
||||||
|
private String language;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public record BookInfo(String isbn, String title, String author, String editor, LocalDate date) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Builder
|
||||||
|
@Getter
|
||||||
|
public class BookSalesInfo {
|
||||||
|
private double price;
|
||||||
|
private Integer stock;
|
||||||
|
}
|
||||||
@@ -0,0 +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.BookDetails;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||||
|
|
||||||
|
public final class BookConverter {
|
||||||
|
private BookConverter(){}
|
||||||
|
|
||||||
|
public static Book ToDomain(BookInfo bookinfo, BookSalesInfo booksalesinfo, BookDetails bookdetails){
|
||||||
|
return Book.builder()
|
||||||
|
.isbn(bookinfo.isbn())
|
||||||
|
.title(bookinfo.title())
|
||||||
|
.author(bookinfo.author())
|
||||||
|
.editor(bookinfo.editor())
|
||||||
|
.date(bookinfo.date())
|
||||||
|
.price(booksalesinfo.getPrice())
|
||||||
|
.stock(booksalesinfo.getStock())
|
||||||
|
.categories(bookdetails.getCategories())
|
||||||
|
.description(bookdetails.getDescription())
|
||||||
|
.language(bookdetails.getLanguage())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BookDTO ToDTO(Book book){
|
||||||
|
return BookDTO.builder()
|
||||||
|
.isbn(book.getIsbn())
|
||||||
|
.title(book.getTitle())
|
||||||
|
.author(book.getAuthor())
|
||||||
|
.editor(book.getEditor())
|
||||||
|
.date(book.getDate())
|
||||||
|
.price(book.getPrice())
|
||||||
|
.stock(book.getStock())
|
||||||
|
.categories(book.getCategories())
|
||||||
|
.description(book.getDescription())
|
||||||
|
.language(book.getLanguage())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public class Book {
|
||||||
|
private String isbn;
|
||||||
|
private String title;
|
||||||
|
private String author;
|
||||||
|
private String editor;
|
||||||
|
private LocalDate date;
|
||||||
|
private double price;
|
||||||
|
private Integer stock;
|
||||||
|
private ArrayList<String> categories = new ArrayList<>();
|
||||||
|
private String description ;
|
||||||
|
private String language;
|
||||||
|
|
||||||
|
public void addStock(Integer copyToAdd){
|
||||||
|
this.stock += copyToAdd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeStock(Integer copyToRemomve) throws IllegalBookStockException {
|
||||||
|
if (copyToRemomve > this.stock){
|
||||||
|
throw new IllegalBookStockException(copyToRemomve, this.stock);
|
||||||
|
}
|
||||||
|
this.stock -= copyToRemomve;
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
public class BookNotFoundException extends RuntimeException {
|
||||||
|
public static final String THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The book with isbn {0} does not exist";
|
||||||
|
|
||||||
|
public BookNotFoundException(String isbn) {
|
||||||
|
super(MessageFormat.format(THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE, isbn));
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
public class IllegalBookStockException extends Exception {
|
||||||
|
public static final String CANNOT_REMOVE_COPY = "Cannot remove {0} copy from {1} copy";
|
||||||
|
|
||||||
|
public IllegalBookStockException(Integer toremove, Integer actual ) {
|
||||||
|
|
||||||
|
super(MessageFormat.format(CANNOT_REMOVE_COPY, toremove,
|
||||||
|
actual));
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
public class NotValidBookException extends RuntimeException {
|
||||||
|
public NotValidBookException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
public class BookRepository {
|
||||||
|
|
||||||
|
private final List<Book> books = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<Book> findAll() {
|
||||||
|
return books;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAll() {
|
||||||
|
books.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book save(Book newBook) {
|
||||||
|
Optional<Book> optionalBookWithSameIsbn = this.findByIsbn(newBook.getIsbn());
|
||||||
|
optionalBookWithSameIsbn.ifPresent(books::remove);
|
||||||
|
this.books.add(newBook);
|
||||||
|
return newBook;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Book> findByIsbn(String isbn) {
|
||||||
|
return this.books.stream()
|
||||||
|
.filter(book -> book.getIsbn().equals(isbn))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsByIsbn(String isbn) {
|
||||||
|
return this.books.stream()
|
||||||
|
.anyMatch(book -> book.getIsbn().equals(isbn));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(Book book) {
|
||||||
|
|
||||||
|
this.books.remove(book);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
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.BookDetails;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo;
|
||||||
|
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.IllegalBookStockException;
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class BookUseCase {
|
||||||
|
|
||||||
|
private final BookRepository bookRepository;
|
||||||
|
|
||||||
|
public BookUseCase(BookRepository bookRepository) {
|
||||||
|
|
||||||
|
this.bookRepository = bookRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String registerBook(BookInfo newbook, BookSalesInfo newbooksalesinfo, BookDetails newbookdetails) throws NotValidBookException {
|
||||||
|
BookValidator.validate(newbook);
|
||||||
|
BookValidator.validate(newbooksalesinfo);
|
||||||
|
BookValidator.validate(newbookdetails);
|
||||||
|
Book bookToRegister = BookConverter.ToDomain(newbook, newbooksalesinfo, newbookdetails);
|
||||||
|
Book bookToRegistered = bookRepository.save(bookToRegister);
|
||||||
|
return bookToRegistered.getIsbn();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<BookDTO> findBookByIsbn(String isbn) {
|
||||||
|
Optional<Book> optionalBook = bookRepository.findByIsbn(isbn);
|
||||||
|
return optionalBook.map(BookConverter::ToDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BookDTO updateBook(String isbn, BookInfo bookinfo, BookSalesInfo booksalesinfo, BookDetails bookdetails)
|
||||||
|
throws BookNotFoundException, NotValidBookException {
|
||||||
|
BookValidator.validate(bookinfo);
|
||||||
|
BookValidator.validate(booksalesinfo);
|
||||||
|
BookValidator.validate(bookdetails);
|
||||||
|
Book bookByIsbn = getBookIfDoesNotExistThrowBookNotFoundException(isbn);
|
||||||
|
Book book = Book.builder()
|
||||||
|
.isbn(isbn)
|
||||||
|
.title(bookinfo.title())
|
||||||
|
.author(bookinfo.author())
|
||||||
|
.editor(bookinfo.editor())
|
||||||
|
.date(bookByIsbn.getDate())
|
||||||
|
.price(booksalesinfo.getPrice())
|
||||||
|
.stock(booksalesinfo.getStock())
|
||||||
|
.categories(bookdetails.getCategories())
|
||||||
|
.description(bookdetails.getDescription())
|
||||||
|
.language(bookByIsbn.getLanguage())
|
||||||
|
.build();
|
||||||
|
Book updatedBook = bookRepository.save(book);
|
||||||
|
return BookConverter.ToDTO(updatedBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteBook(String isbn) throws BookNotFoundException {
|
||||||
|
Book bookToDelete = getBookIfDoesNotExistThrowBookNotFoundException(isbn);
|
||||||
|
this.bookRepository.delete(bookToDelete);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int addStockCopies(String isbn, int stockCopiesToAdd) throws BookNotFoundException {
|
||||||
|
Book bookToAddStockCopies = getBookIfDoesNotExistThrowBookNotFoundException(isbn);
|
||||||
|
bookToAddStockCopies.addStock(stockCopiesToAdd);
|
||||||
|
bookRepository.save(bookToAddStockCopies);
|
||||||
|
return bookToAddStockCopies.getStock();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int subtractStockCopies(String isbn, int stockCopiesToRemove)
|
||||||
|
throws BookNotFoundException, IllegalBookStockException {
|
||||||
|
Book bookToSubtractStockCopies = getBookIfDoesNotExistThrowBookNotFoundException(isbn);
|
||||||
|
bookToSubtractStockCopies.removeStock(stockCopiesToRemove);
|
||||||
|
bookRepository.save(bookToSubtractStockCopies);
|
||||||
|
return bookToSubtractStockCopies.getStock();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Book getBookIfDoesNotExistThrowBookNotFoundException(String isbn)
|
||||||
|
throws BookNotFoundException {
|
||||||
|
Optional<Book> optionalBookByIsbn = bookRepository.findByIsbn(isbn);
|
||||||
|
if (optionalBookByIsbn.isEmpty()) {
|
||||||
|
throw new BookNotFoundException(isbn);
|
||||||
|
}
|
||||||
|
return optionalBookByIsbn.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDetails;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class BookValidator {
|
||||||
|
|
||||||
|
public static final String ISBN_IS_NOT_VALID = "Isbn is not valid";
|
||||||
|
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 EDITOR_CANNOT_BE_BLANK = "Editor cannot be blank";
|
||||||
|
public static final String DATE_IS_NOT_VALID = "Date is not valid";
|
||||||
|
public static final String PRICE_IS_NOT_VALID = "Price is not valid";
|
||||||
|
public static final String STOCK_IS_NOT_VALID = "Stock is not valid";
|
||||||
|
public static final String CATEGORIES_IS_NOT_VALID = "Categories is not valid";
|
||||||
|
public static final String LANGUAGE_CANNOT_BE_BLANK = "Language cannot be blank";
|
||||||
|
|
||||||
|
private BookValidator() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void validate(BookInfo newBook) throws NotValidBookException {
|
||||||
|
validateISBN(newBook);
|
||||||
|
validateTitle(newBook);
|
||||||
|
validateAuthor(newBook);
|
||||||
|
validateEditor(newBook);
|
||||||
|
validateDate(newBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void validate(BookSalesInfo newBook) throws NotValidBookException {
|
||||||
|
validatePrice(newBook);
|
||||||
|
validateStock(newBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void validate(BookDetails newBook) throws NotValidBookException {
|
||||||
|
validateCategories(newBook);
|
||||||
|
validateLanguage(newBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateISBN(BookInfo newBook) throws NotValidBookException {
|
||||||
|
if (newBook.isbn().isBlank()) {
|
||||||
|
throw new NotValidBookException(ISBN_IS_NOT_VALID);
|
||||||
|
} else if (newBook.isbn().length() != 13) {
|
||||||
|
throw new NotValidBookException(ISBN_IS_NOT_VALID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateTitle(BookInfo newBook) throws NotValidBookException {
|
||||||
|
if (newBook.title().isBlank()) {
|
||||||
|
throw new NotValidBookException(TITLE_CANNOT_BE_BLANK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateAuthor(BookInfo newBook) throws NotValidBookException {
|
||||||
|
if (newBook.author().isBlank()) {
|
||||||
|
throw new NotValidBookException(AUTHOR_CANNOT_BE_BLANK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateEditor(BookInfo newBook) throws NotValidBookException {
|
||||||
|
if (newBook.editor().isBlank()) {
|
||||||
|
throw new NotValidBookException(EDITOR_CANNOT_BE_BLANK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateDate(BookInfo newBook) throws NotValidBookException {
|
||||||
|
if (newBook.date().isAfter(LocalDate.now())) {
|
||||||
|
throw new NotValidBookException(DATE_IS_NOT_VALID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validatePrice(BookSalesInfo newBook) throws NotValidBookException {
|
||||||
|
if (newBook.getPrice() <= 0) {
|
||||||
|
throw new NotValidBookException(PRICE_IS_NOT_VALID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateStock(BookSalesInfo newBook) throws NotValidBookException {
|
||||||
|
if (newBook.getStock() < 0) {
|
||||||
|
throw new NotValidBookException(STOCK_IS_NOT_VALID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateCategories(BookDetails newBook) throws NotValidBookException {
|
||||||
|
if (newBook.getCategories().isEmpty()) {
|
||||||
|
throw new NotValidBookException(CATEGORIES_IS_NOT_VALID);
|
||||||
|
}else if (CategoriesNotBlank(newBook.getCategories())) {
|
||||||
|
throw new NotValidBookException(CATEGORIES_IS_NOT_VALID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean CategoriesNotBlank(ArrayList<String> categories){
|
||||||
|
for (String categorie : categories) {
|
||||||
|
if (categorie.isBlank()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateLanguage(BookDetails newBook) throws NotValidBookException {
|
||||||
|
if (newBook.getLanguage().isBlank()) {
|
||||||
|
throw new NotValidBookException(LANGUAGE_CANNOT_BE_BLANK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
|
||||||
|
public class ReviewDTO {
|
||||||
|
private UUID avisId;
|
||||||
|
private UUID customerId;
|
||||||
|
private UUID bookId;
|
||||||
|
private Integer note;
|
||||||
|
private String comment;
|
||||||
|
private LocalDate purchaseDate;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record ReviewInfo(Integer note, String comment, LocalDate purchaseDate) {
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
|
||||||
|
|
||||||
|
public class ReviewConverter {
|
||||||
|
private ReviewConverter() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Review toDomain(ReviewInfo newReview) {
|
||||||
|
return Review.builder()
|
||||||
|
.note(newReview.note())
|
||||||
|
.comment(newReview.comment())
|
||||||
|
.purchaseDate(newReview.purchaseDate())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ReviewDTO toDTO(Review review) {
|
||||||
|
return ReviewDTO.builder()
|
||||||
|
.customerId(review.getCustomerId())
|
||||||
|
.bookId(review.getBookId())
|
||||||
|
.note(review.getNote())
|
||||||
|
.comment(review.getComment())
|
||||||
|
.purchaseDate(review.getPurchaseDate())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.entity;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
|
||||||
|
public class Review {
|
||||||
|
private UUID avisId;
|
||||||
|
private UUID customerId;
|
||||||
|
private UUID bookId;
|
||||||
|
private Integer note;
|
||||||
|
private String comment;
|
||||||
|
private LocalDate purchaseDate;
|
||||||
|
|
||||||
|
public void setRandomUUID() {
|
||||||
|
this.avisId = UUID.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRandomUUIDCustomerAndBook() {
|
||||||
|
|
||||||
|
this.customerId = UUID.randomUUID();
|
||||||
|
this.bookId = UUID.randomUUID();
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
|
||||||
|
|
||||||
|
public class NotValidReviewException extends RuntimeException {
|
||||||
|
public NotValidReviewException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+78
@@ -0,0 +1,78 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class ReviewRepository {
|
||||||
|
|
||||||
|
private final List<Review> reviews = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<Review> findAll() {
|
||||||
|
|
||||||
|
return reviews;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAll() {
|
||||||
|
|
||||||
|
reviews.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Review save(Review newReview) {
|
||||||
|
Optional<Review> optionalReviewWithSameAvisId = this.findByAvisId(newReview.getAvisId());
|
||||||
|
optionalReviewWithSameAvisId.ifPresent(reviews::remove);
|
||||||
|
this.reviews.add(newReview);
|
||||||
|
return newReview;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Review> findByCustomerId(UUID customerUUID) {
|
||||||
|
return this.reviews.stream()
|
||||||
|
.filter(review -> review.getCustomerId().equals(customerUUID))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Review> findByBookId(UUID bookUUID) {
|
||||||
|
return this.reviews.stream()
|
||||||
|
.filter(review -> review.getBookId().equals(bookUUID))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Review> findByAvisId(UUID avisUUID) {
|
||||||
|
return this.reviews.stream()
|
||||||
|
.filter(review -> review.getAvisId().equals(avisUUID))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsByCustomerId(UUID customerUUID) {
|
||||||
|
return this.reviews.stream()
|
||||||
|
.anyMatch(review -> review.getCustomerId().equals(customerUUID));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsByBookId(UUID bookUUID) {
|
||||||
|
return this.reviews.stream()
|
||||||
|
.anyMatch(review -> review.getBookId().equals(bookUUID));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsByAvisId(UUID avisUUID) {
|
||||||
|
return this.reviews.stream()
|
||||||
|
.anyMatch(review -> review.getAvisId().equals(avisUUID));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteCustomerReviews(UUID customerUUID) {
|
||||||
|
|
||||||
|
this.reviews.removeIf(review -> review.getCustomerId().equals(customerUUID));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteBookReviews(UUID bookUUID) {
|
||||||
|
|
||||||
|
this.reviews.removeIf(review -> review.getBookId().equals(bookUUID));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(Review review) {
|
||||||
|
|
||||||
|
this.reviews.remove(review);
|
||||||
|
}
|
||||||
|
}
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public class ReviewValidator {
|
||||||
|
|
||||||
|
public static final String NOTE_CANNOT_BE_LOWER_THAN_1 = "Note is greater than or equal to 1";
|
||||||
|
public static final String NOTE_CANNOT_BE_UPPER_THAN_5 = "Note is less than or equal to 5";
|
||||||
|
public static final String COMMENT_CANNOT_BE_BLANK = "Comment cannot be blank";
|
||||||
|
public static final String PURCHASE_DATE_IS_NOT_VALID = "Date is not valid";
|
||||||
|
|
||||||
|
public ReviewValidator() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void validate(ReviewInfo newReview) throws NotValidReviewException {
|
||||||
|
validateNoteLower1(newReview);
|
||||||
|
validateNoteUpper5(newReview);
|
||||||
|
validateComment(newReview);
|
||||||
|
validatePurchaseDate(newReview);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateNoteLower1(ReviewInfo newReview)
|
||||||
|
throws NotValidReviewException {
|
||||||
|
if (newReview.note() <= 1) {
|
||||||
|
throw new NotValidReviewException(NOTE_CANNOT_BE_LOWER_THAN_1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateNoteUpper5(ReviewInfo newReview)
|
||||||
|
throws NotValidReviewException {
|
||||||
|
if (newReview.note() >= 5) {
|
||||||
|
throw new NotValidReviewException("Note is less than or equal to 5");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateComment(ReviewInfo newReview) throws NotValidReviewException {
|
||||||
|
if (newReview.comment().isBlank()) {
|
||||||
|
throw new NotValidReviewException(COMMENT_CANNOT_BE_BLANK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validatePurchaseDate(ReviewInfo newReview) throws NotValidReviewException {
|
||||||
|
if (newReview.purchaseDate().isAfter(LocalDate.now())) {
|
||||||
|
throw new NotValidReviewException(PURCHASE_DATE_IS_NOT_VALID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+118
@@ -0,0 +1,118 @@
|
|||||||
|
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.BookDetails;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
@DisplayName("BookConverterTest Unit Tests")
|
||||||
|
public class BookConverterTest {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDomain() method tests")
|
||||||
|
class ToDomainTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void ShouldConvertBookToDomain(){
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookinfo = new BookInfo("0000000000000","La vie de Maxime", "Marvin Aubert", "Kioon", date);
|
||||||
|
BookSalesInfo booksalesinfo = BookSalesInfo.builder()
|
||||||
|
.price(12)
|
||||||
|
.stock(10)
|
||||||
|
.build();
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
BookDetails bookdetails = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Book result = BookConverter.ToDomain(bookinfo, booksalesinfo, bookdetails);
|
||||||
|
|
||||||
|
assertEquals(bookinfo.isbn(), result.getIsbn());
|
||||||
|
assertEquals(bookinfo.title(), result.getTitle());
|
||||||
|
assertEquals(bookinfo.author(), result.getAuthor());
|
||||||
|
assertEquals(bookinfo.editor(), result.getEditor());
|
||||||
|
assertEquals(bookinfo.date(), result.getDate());
|
||||||
|
assertEquals(booksalesinfo.getPrice(), result.getPrice());
|
||||||
|
assertEquals(booksalesinfo.getStock(), result.getStock());
|
||||||
|
assertEquals(bookdetails.getCategories(), result.getCategories());
|
||||||
|
assertEquals(bookdetails.getDescription(), result.getDescription());
|
||||||
|
assertEquals(bookdetails.getLanguage(), result.getLanguage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDTO() method tests")
|
||||||
|
class ToDTOTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void ShouldConvertBookToDTO() {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
Book book = Book.builder()
|
||||||
|
.isbn("1234567891012")
|
||||||
|
.title("La vie de Maxime")
|
||||||
|
.author("Marvin Aubert")
|
||||||
|
.editor("Kioon")
|
||||||
|
.date(date)
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
BookDTO result = BookConverter.ToDTO(book);
|
||||||
|
|
||||||
|
assertEquals(book.getIsbn(), result.getIsbn());
|
||||||
|
assertEquals(book.getTitle(), result.getTitle());
|
||||||
|
assertEquals(book.getAuthor(), result.getAuthor());
|
||||||
|
assertEquals(book.getEditor(), result.getEditor());
|
||||||
|
assertEquals(book.getDate(), result.getDate());
|
||||||
|
assertEquals(book.getPrice(), result.getPrice());
|
||||||
|
assertEquals(book.getStock(), result.getStock());
|
||||||
|
assertEquals(book.getCategories(), result.getCategories());
|
||||||
|
assertEquals(book.getDescription(), result.getDescription());
|
||||||
|
assertEquals(book.getLanguage(), result.getLanguage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should preserve empty string values during conversion")
|
||||||
|
void shouldPreserveEmptyStrings() {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookinfo = new BookInfo("0000000000000","La vie de Maxime", "Marvin Aubert", "Kioon", date);
|
||||||
|
BookSalesInfo booksalesinfo = BookSalesInfo.builder()
|
||||||
|
.price(12)
|
||||||
|
.stock(10)
|
||||||
|
.build();
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
BookDetails bookdetails = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description("")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Book book = BookConverter.ToDomain(bookinfo, booksalesinfo, bookdetails);
|
||||||
|
BookDTO result = BookConverter.ToDTO(book);
|
||||||
|
|
||||||
|
assertEquals("", result.getDescription());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,168 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class BookTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Builder should create a valid Book instance")
|
||||||
|
void testBookBuilder() {
|
||||||
|
String isbn = "1234567891012";
|
||||||
|
String title = "La vie de Maxime";
|
||||||
|
String author = "Marvin Aubert";
|
||||||
|
String editor = "Kioon";
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
double price = 12.99;
|
||||||
|
Integer stock = 50;
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
String description = "C'était un brave partit trop tôt";
|
||||||
|
String language = "Français";
|
||||||
|
Book book = Book.builder()
|
||||||
|
.isbn(isbn)
|
||||||
|
.title(title)
|
||||||
|
.author(author)
|
||||||
|
.editor(editor)
|
||||||
|
.date(date)
|
||||||
|
.price(price)
|
||||||
|
.stock(stock)
|
||||||
|
.categories(categories)
|
||||||
|
.description(description)
|
||||||
|
.language(language)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals(isbn, book.getIsbn());
|
||||||
|
assertEquals(title, book.getTitle());
|
||||||
|
assertEquals(author, book.getAuthor());
|
||||||
|
assertEquals(editor, book.getEditor());
|
||||||
|
assertEquals(date, book.getDate());
|
||||||
|
assertEquals(price, book.getPrice());
|
||||||
|
assertEquals(stock, book.getStock());
|
||||||
|
assertEquals(categories, book.getCategories());
|
||||||
|
assertEquals(description, book.getDescription());
|
||||||
|
assertEquals(language, book.getLanguage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Stock Tests")
|
||||||
|
class StockTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("addCopy should correctly increment stocks")
|
||||||
|
void testAddCopy() {
|
||||||
|
Book book =Book.builder()
|
||||||
|
.stock(5)
|
||||||
|
.build();
|
||||||
|
Integer copyToAdd = 5;
|
||||||
|
Integer copyExpected = 10;
|
||||||
|
|
||||||
|
book.addStock(copyToAdd);
|
||||||
|
|
||||||
|
assertEquals(copyExpected, book.getStock());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("addCopy should correctly increment zero points correctly")
|
||||||
|
void testAddZeroToCopy() {
|
||||||
|
Book book =Book.builder()
|
||||||
|
.stock(5)
|
||||||
|
.build();
|
||||||
|
Integer copyToAdd = 0;
|
||||||
|
Integer copyExpected = 5;
|
||||||
|
|
||||||
|
book.addStock(copyToAdd);
|
||||||
|
|
||||||
|
assertEquals(copyExpected, book.getStock());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("removeLoyaltyPoints should correctly decrement loyalty points")
|
||||||
|
void testRemoveCopy() throws IllegalBookStockException {
|
||||||
|
Book book =Book.builder()
|
||||||
|
.stock(5)
|
||||||
|
.build();
|
||||||
|
Integer copyToRemove = 2;
|
||||||
|
Integer copyExpected = 3;
|
||||||
|
|
||||||
|
book.removeStock(copyToRemove);
|
||||||
|
|
||||||
|
assertEquals(copyExpected, book.getStock());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("removeLoyaltyPoints should correctly decrement loyalty points")
|
||||||
|
void testRemoveZeroToCopy() throws IllegalBookStockException {
|
||||||
|
Book book =Book.builder()
|
||||||
|
.stock(5)
|
||||||
|
.build();
|
||||||
|
Integer copyToRemove = 0;
|
||||||
|
Integer copyExpected = 5;
|
||||||
|
|
||||||
|
book.removeStock(copyToRemove);
|
||||||
|
|
||||||
|
assertEquals(copyExpected, book.getStock());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("removeLoyaltyPoints should correctly decrement loyalty points")
|
||||||
|
void testRemoveAllToCopy() throws IllegalBookStockException {
|
||||||
|
Book book =Book.builder()
|
||||||
|
.stock(5)
|
||||||
|
.build();
|
||||||
|
Integer copyToRemove = 5;
|
||||||
|
Integer copyExpected = 0;
|
||||||
|
|
||||||
|
book.removeStock(copyToRemove);
|
||||||
|
|
||||||
|
assertEquals(copyExpected, book.getStock());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("removeSTock should throw exception when trying to remove more copy than available")
|
||||||
|
void testRemoveTooManyCopy() {
|
||||||
|
Book book = Book.builder()
|
||||||
|
.stock(50)
|
||||||
|
.build();
|
||||||
|
int copyToRemove = 75;
|
||||||
|
|
||||||
|
IllegalBookStockException exception = assertThrows(
|
||||||
|
IllegalBookStockException.class,
|
||||||
|
() -> book.removeStock(copyToRemove)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(50, book.getStock());
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains(String.valueOf(copyToRemove)));
|
||||||
|
assertTrue(exception.getMessage().contains(String.valueOf(book.getStock())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Price Tests")
|
||||||
|
class PriceTests {
|
||||||
|
@Test
|
||||||
|
@DisplayName("addCopy should correctly increment stocks")
|
||||||
|
void testAddCopy() {
|
||||||
|
Book book =Book.builder()
|
||||||
|
.stock(5)
|
||||||
|
.build();
|
||||||
|
Integer copyToAdd = 5;
|
||||||
|
Integer copyExpected = 10;
|
||||||
|
|
||||||
|
book.addStock(copyToAdd);
|
||||||
|
|
||||||
|
assertEquals(copyExpected, book.getStock());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class BookNotFoundExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the isbn provided")
|
||||||
|
void testExceptionMessageContainsIsbn() {
|
||||||
|
String isbn = "1234567891012";
|
||||||
|
|
||||||
|
BookNotFoundException exception = new BookNotFoundException(isbn);
|
||||||
|
|
||||||
|
String expectedMessage = String.format("The book with isbn %s does not exist", isbn);
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should use the correct constant message format")
|
||||||
|
void testExceptionUsesConstantMessageFormat() {
|
||||||
|
String isbn = "1234567891012";
|
||||||
|
|
||||||
|
BookNotFoundException exception = new BookNotFoundException(isbn);
|
||||||
|
|
||||||
|
String expectedFormatWithPlaceholder = "The book with isbn {0} does not exist";
|
||||||
|
assertEquals(BookNotFoundException.THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE,
|
||||||
|
expectedFormatWithPlaceholder);
|
||||||
|
assertTrue(exception.getMessage().contains(isbn.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
String isbn = "1234567891012";
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new BookNotFoundException(isbn);
|
||||||
|
} catch (BookNotFoundException e) {
|
||||||
|
String expectedMessage = String.format("The book with isbn %s does not exist", isbn);
|
||||||
|
assertEquals(expectedMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+70
@@ -0,0 +1,70 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.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.CsvSource;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class IllegalBookStockExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the needed and actual stock")
|
||||||
|
void testExceptionMessageContainsStock() {
|
||||||
|
int neededStock = 100;
|
||||||
|
int actualStock = 50;
|
||||||
|
|
||||||
|
IllegalBookStockException exception = new IllegalBookStockException(neededStock, actualStock);
|
||||||
|
|
||||||
|
String expectedMessage = "Cannot remove 100 copy from 50 copy";
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({
|
||||||
|
"100, 50",
|
||||||
|
"75, 25",
|
||||||
|
"200, 150",
|
||||||
|
"1000, 750"
|
||||||
|
})
|
||||||
|
@DisplayName("Exception message should be formatted correctly for different stock values")
|
||||||
|
void testExceptionMessageForDifferentStockValues(int neededStock, int actualStock) {
|
||||||
|
IllegalBookStockException exception = new IllegalBookStockException(neededStock, actualStock);
|
||||||
|
|
||||||
|
String expectedMessage = MessageFormat.format(IllegalBookStockException.CANNOT_REMOVE_COPY, neededStock, actualStock);
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should use the correct constant message format")
|
||||||
|
void testExceptionUsesConstantMessageFormat() {
|
||||||
|
int neededStock = 100;
|
||||||
|
int actualStock = 50;
|
||||||
|
|
||||||
|
IllegalBookStockException exception = new IllegalBookStockException(neededStock, actualStock);
|
||||||
|
|
||||||
|
String expectedFormatWithPlaceholder = "Cannot remove {0} copy from {1} copy";
|
||||||
|
assertEquals(IllegalBookStockException.CANNOT_REMOVE_COPY,
|
||||||
|
expectedFormatWithPlaceholder);
|
||||||
|
assertTrue(exception.getMessage().contains(String.valueOf(neededStock)));
|
||||||
|
assertTrue(exception.getMessage().contains(String.valueOf(actualStock)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
int neededStock = 100;
|
||||||
|
int actualStock = 50;
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new IllegalBookStockException(neededStock, actualStock);
|
||||||
|
} catch (IllegalBookStockException e) {
|
||||||
|
String expectedMessage = String.format("Cannot remove %d copy from %d copy", neededStock, actualStock);
|
||||||
|
assertEquals(expectedMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+61
@@ -0,0 +1,61 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.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.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class NotValidBookExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be created with the provided message")
|
||||||
|
void testExceptionCreation() {
|
||||||
|
String errorMessage = "Book data is not valid";
|
||||||
|
|
||||||
|
NotValidBookException exception = new NotValidBookException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {
|
||||||
|
"Isbn is required",
|
||||||
|
"Title cannot be empty",
|
||||||
|
"Date format is invalid",
|
||||||
|
"Price must be above 0"
|
||||||
|
})
|
||||||
|
@DisplayName("Exception should handle different validation messages")
|
||||||
|
void testExceptionWithDifferentMessages(String errorMessage) {
|
||||||
|
NotValidBookException exception = new NotValidBookException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
String errorMessage = "Required field is missing";
|
||||||
|
|
||||||
|
Exception exception = assertThrows(NotValidBookException.class, () -> {
|
||||||
|
throw new NotValidBookException(errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be catchable as a general Exception")
|
||||||
|
void testExceptionInheritance() {
|
||||||
|
String errorMessage = "Invalid book data";
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new NotValidBookException(errorMessage);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertEquals(NotValidBookException.class, e.getClass());
|
||||||
|
assertEquals(errorMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+255
@@ -0,0 +1,255 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||||
|
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.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class BookRepositoryTest {
|
||||||
|
|
||||||
|
private BookRepository repository;
|
||||||
|
private Book book1;
|
||||||
|
private Book book2;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
repository = new BookRepository();
|
||||||
|
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
|
||||||
|
book1 = Book.builder()
|
||||||
|
.isbn("1234567891012")
|
||||||
|
.title("La vie de Maxime")
|
||||||
|
.author("Marvin Aubert")
|
||||||
|
.editor("Kioon")
|
||||||
|
.date(date)
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
book2 = Book.builder()
|
||||||
|
.isbn("1234567891015")
|
||||||
|
.title("La vie de Marvin")
|
||||||
|
.author("Maxime Lebreton")
|
||||||
|
.editor("Kioon")
|
||||||
|
.date(date)
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("New repository should be empty")
|
||||||
|
void testNewRepositoryIsEmpty() {
|
||||||
|
List<Book> books = repository.findAll();
|
||||||
|
|
||||||
|
assertTrue(books.isEmpty());
|
||||||
|
assertEquals(0, books.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Save operations")
|
||||||
|
class SaveOperations {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should add a new book")
|
||||||
|
void testSaveNewBook() {
|
||||||
|
Book savedBook = repository.save(book1);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(book1.getIsbn(), savedBook.getIsbn());
|
||||||
|
assertEquals(book1.getTitle(), savedBook.getTitle());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should update existing book with same isbn")
|
||||||
|
void testSaveUpdatesExistingBook() {
|
||||||
|
repository.save(book1);
|
||||||
|
|
||||||
|
String isbn = "1234567891012";
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
|
||||||
|
Book updatedBook = Book.builder()
|
||||||
|
.isbn(isbn)
|
||||||
|
.title("La vie de Maxime")
|
||||||
|
.author("Updated")
|
||||||
|
.editor("Kioon")
|
||||||
|
.date(date)
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt beaucoup trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Book savedBook = repository.save(updatedBook);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(isbn, savedBook.getIsbn());
|
||||||
|
assertEquals("La vie de Maxime", savedBook.getTitle());
|
||||||
|
assertEquals("Updated", savedBook.getAuthor());
|
||||||
|
assertEquals("Kioon", savedBook.getEditor());
|
||||||
|
assertEquals(date, savedBook.getDate());
|
||||||
|
assertEquals(12.99, savedBook.getPrice());
|
||||||
|
assertEquals(50, savedBook.getStock());
|
||||||
|
assertEquals(categories, savedBook.getCategories());
|
||||||
|
assertEquals("C'était un brave partit trop tôt beaucoup trop tôt", savedBook.getDescription());
|
||||||
|
assertEquals("Français", savedBook.getLanguage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save multiple books should add all of them")
|
||||||
|
void testSaveMultipleBooks() {
|
||||||
|
repository.save(book1);
|
||||||
|
repository.save(book2);
|
||||||
|
|
||||||
|
List<Book> books = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, books.size());
|
||||||
|
assertTrue(books.contains(book1));
|
||||||
|
assertTrue(books.contains(book2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find operations")
|
||||||
|
class FindOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpBooks() {
|
||||||
|
repository.save(book1);
|
||||||
|
repository.save(book2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindAll should return all books")
|
||||||
|
void testFindAll() {
|
||||||
|
List<Book> books = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, books.size());
|
||||||
|
assertTrue(books.contains(book1));
|
||||||
|
assertTrue(books.contains(book2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindByIsbn should return book with matching isbn")
|
||||||
|
void testFindByIsbn() {
|
||||||
|
Optional<Book> foundBook = repository.findByIsbn(book1.getIsbn());
|
||||||
|
|
||||||
|
assertTrue(foundBook.isPresent());
|
||||||
|
assertEquals(book1.getTitle(), foundBook.get().getTitle());
|
||||||
|
assertEquals(book1.getAuthor(), foundBook.get().getAuthor());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindByIsbn should return empty Optional when isbn doesn't exist")
|
||||||
|
void testFindByIsbnNotFound() {
|
||||||
|
String nonExistedisbn = "1515265522652";
|
||||||
|
|
||||||
|
Optional<Book> foundBook = repository.findByIsbn(nonExistedisbn);
|
||||||
|
|
||||||
|
assertTrue(foundBook.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsByIsbn should return true when isbn exists")
|
||||||
|
void testExistsByIsbnExists() {
|
||||||
|
boolean exists = repository.existsByIsbn(book1.getIsbn());
|
||||||
|
|
||||||
|
assertTrue(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsByIsbn should return false when isbn doesn't exist")
|
||||||
|
void testExistsByIsbnNotExists() {
|
||||||
|
String nonExistedisbn = "1515265522652";
|
||||||
|
|
||||||
|
boolean exists = repository.existsByIsbn(nonExistedisbn);
|
||||||
|
|
||||||
|
assertFalse(exists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Delete operations")
|
||||||
|
class DeleteOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpBooks() {
|
||||||
|
repository.save(book1);
|
||||||
|
repository.save(book2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should remove the specified book")
|
||||||
|
void testDelete() {
|
||||||
|
repository.delete(book1);
|
||||||
|
|
||||||
|
List<Book> books = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(1, books.size());
|
||||||
|
assertFalse(books.contains(book1));
|
||||||
|
assertTrue(books.contains(book2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("DeleteAll should remove all books")
|
||||||
|
void testDeleteAll() {
|
||||||
|
repository.deleteAll();
|
||||||
|
|
||||||
|
List<Book> books = repository.findAll();
|
||||||
|
|
||||||
|
assertTrue(books.isEmpty());
|
||||||
|
assertEquals(0, books.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should not throw exception when book doesn't exist")
|
||||||
|
void testDeleteNonExistentBook() {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
Book nonExistentBook = Book.builder()
|
||||||
|
.isbn("1515466461319")
|
||||||
|
.title("La vie de Patrick")
|
||||||
|
.author("Updated")
|
||||||
|
.editor("Kioon")
|
||||||
|
.date(date)
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> repository.delete(nonExistentBook));
|
||||||
|
|
||||||
|
assertEquals(2, repository.findAll().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,356 @@
|
|||||||
|
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.BookDetails;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo;
|
||||||
|
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.BookNotFoundExceptionTest;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository;
|
||||||
|
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 java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
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 static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class BookUseCaseTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private BookRepository bookRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private BookUseCase bookUseCase;
|
||||||
|
|
||||||
|
private String bookIsbn;
|
||||||
|
private ArrayList<String> categories;
|
||||||
|
private LocalDate date;
|
||||||
|
private Book testBook;
|
||||||
|
private BookInfo validBookInfo;
|
||||||
|
private BookDetails validBookDetails;
|
||||||
|
private BookSalesInfo validBookSalesInfo;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
bookIsbn = "1234567891012";
|
||||||
|
date = LocalDate.of(2026, 3, 24);
|
||||||
|
categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
|
||||||
|
testBook = Book.builder()
|
||||||
|
.isbn(bookIsbn)
|
||||||
|
.title("La vie de Maxime")
|
||||||
|
.author("Marvin AUbert")
|
||||||
|
.editor("Kioon")
|
||||||
|
.date(date)
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
validBookInfo = new BookInfo(bookIsbn,"La vie de Maxime", "Marvin AUbert", "Kioon", date);
|
||||||
|
validBookSalesInfo = BookSalesInfo.builder()
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.build();
|
||||||
|
validBookDetails = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Register Book tests")
|
||||||
|
class RegisterBookTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should register book when valid data is provided")
|
||||||
|
void testRegisterBookWithValidData() throws NotValidBookException {
|
||||||
|
when(bookRepository.save(any(Book.class))).thenReturn(testBook);
|
||||||
|
|
||||||
|
String registeredIsbn = bookUseCase.registerBook(validBookInfo, validBookSalesInfo, validBookDetails);
|
||||||
|
|
||||||
|
assertNotNull(registeredIsbn);
|
||||||
|
assertEquals(bookIsbn, registeredIsbn);
|
||||||
|
verify(bookRepository, times(1)).save(any(Book.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when book data is not valid")
|
||||||
|
void testRegisterBookWithInvalidData() {
|
||||||
|
BookInfo invalidBookInfo = new BookInfo(bookIsbn,"", "", "", date);
|
||||||
|
BookSalesInfo invalidUpdateSalesInfo = BookSalesInfo.builder()
|
||||||
|
.price(0)
|
||||||
|
.stock(-3)
|
||||||
|
.build();
|
||||||
|
BookDetails invalidUpdateDetails = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description("")
|
||||||
|
.language("")
|
||||||
|
.build();
|
||||||
|
assertThrows(NotValidBookException.class,
|
||||||
|
() -> bookUseCase.registerBook(invalidBookInfo, invalidUpdateSalesInfo, invalidUpdateDetails));
|
||||||
|
|
||||||
|
verify(bookRepository, never()).save(any(Book.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find book tests")
|
||||||
|
class FindBookTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return book when isbn exists")
|
||||||
|
void testFindBookByIsbn() {
|
||||||
|
when(bookRepository.findByIsbn("1234567891012")).thenReturn(Optional.of(testBook));
|
||||||
|
|
||||||
|
Optional<BookDTO> foundBook = bookUseCase.findBookByIsbn("1234567891012");
|
||||||
|
|
||||||
|
assertTrue(foundBook.isPresent());
|
||||||
|
assertEquals(testBook.getIsbn(), foundBook.get().getIsbn());
|
||||||
|
assertEquals(testBook.getTitle(), foundBook.get().getTitle());
|
||||||
|
verify(bookRepository, times(1)).findByIsbn("1234567891012");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty Optional when isbn doesn't exist")
|
||||||
|
void testFindBookByIsbnNotFound() {
|
||||||
|
when(bookRepository.findByIsbn("1656546262516")).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
Optional<BookDTO> foundBook = bookUseCase.findBookByIsbn("1656546262516");
|
||||||
|
|
||||||
|
assertTrue(foundBook.isEmpty());
|
||||||
|
verify(bookRepository, times(1)).findByIsbn("1656546262516");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Update book tests")
|
||||||
|
class UpdateBookTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should update book when valid data is provided")
|
||||||
|
void testUpdateBookWithValidData() throws BookNotFoundException, NotValidBookException {
|
||||||
|
when(bookRepository.findByIsbn(bookIsbn)).thenReturn(Optional.of(testBook));
|
||||||
|
|
||||||
|
Book updatedBook = Book.builder()
|
||||||
|
.isbn(bookIsbn)
|
||||||
|
.title("La vie de Maxime")
|
||||||
|
.author("Updated")
|
||||||
|
.editor("Kioon")
|
||||||
|
.date(date)
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(bookRepository.save(any(Book.class))).thenReturn(updatedBook);
|
||||||
|
|
||||||
|
BookInfo updateInfo = new BookInfo(bookIsbn, "La vie de Maxime", "Updated", "Kioon", date);
|
||||||
|
BookSalesInfo updateSalesInfo = BookSalesInfo.builder()
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.build();
|
||||||
|
BookDetails updateDetails = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt beaucoup trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
BookDTO result = bookUseCase.updateBook(bookIsbn, updateInfo, updateSalesInfo, updateDetails);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(bookIsbn, result.getIsbn());
|
||||||
|
assertEquals("La vie de Maxime", result.getTitle());
|
||||||
|
assertEquals("Updated", result.getAuthor());
|
||||||
|
assertEquals("Kioon", result.getEditor());
|
||||||
|
assertEquals(date, result.getDate());
|
||||||
|
assertEquals(12.99, result.getPrice());
|
||||||
|
assertEquals(50, result.getStock());
|
||||||
|
assertEquals(categories, result.getCategories());
|
||||||
|
assertEquals("C'était un brave partit trop tôt", result.getDescription());
|
||||||
|
assertEquals("Français", result.getLanguage());
|
||||||
|
verify(bookRepository, times(1)).findByIsbn(bookIsbn);
|
||||||
|
verify(bookRepository, times(1)).save(any(Book.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when book isbn doesn't exist")
|
||||||
|
void testUpdateBookNotFound() {
|
||||||
|
String nonExistentIsbn = "1656546262516";
|
||||||
|
when(bookRepository.findByIsbn(nonExistentIsbn)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
BookInfo updateInfo = new BookInfo(nonExistentIsbn, "La vie de Maxime", "Updated", "Kioon", date);
|
||||||
|
BookSalesInfo updateSalesInfo = BookSalesInfo.builder()
|
||||||
|
.price(12.99)
|
||||||
|
.stock(50)
|
||||||
|
.build();
|
||||||
|
BookDetails updateDetails = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertThrows(BookNotFoundException.class,
|
||||||
|
() -> bookUseCase.updateBook(nonExistentIsbn, updateInfo, updateSalesInfo, updateDetails));
|
||||||
|
|
||||||
|
verify(bookRepository, times(1)).findByIsbn(nonExistentIsbn);
|
||||||
|
verify(bookRepository, never()).save(any(Book.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when update data is not valid")
|
||||||
|
void testUpdateBookWithInvalidData() {
|
||||||
|
BookInfo invalidUpdateInfo = new BookInfo(bookIsbn,"", "", "", date);
|
||||||
|
BookSalesInfo invalidUpdateSalesInfo = BookSalesInfo.builder()
|
||||||
|
.price(0)
|
||||||
|
.stock(-3)
|
||||||
|
.build();
|
||||||
|
BookDetails invalidUpdateDetails = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description("")
|
||||||
|
.language("")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertThrows(NotValidBookException.class,
|
||||||
|
() -> bookUseCase.updateBook(bookIsbn, invalidUpdateInfo, invalidUpdateSalesInfo, invalidUpdateDetails));
|
||||||
|
|
||||||
|
verify(bookRepository, never()).findByIsbn(any(String.class));
|
||||||
|
verify(bookRepository, never()).save(any(Book.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Delete book tests")
|
||||||
|
class DeleteBookTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should delete book when isbn exists")
|
||||||
|
void testDeleteBook() throws BookNotFoundException {
|
||||||
|
when(bookRepository.findByIsbn(bookIsbn)).thenReturn(Optional.of(testBook));
|
||||||
|
doNothing().when(bookRepository).delete(testBook);
|
||||||
|
|
||||||
|
bookUseCase.deleteBook(bookIsbn);
|
||||||
|
|
||||||
|
verify(bookRepository, times(1)).findByIsbn(bookIsbn);
|
||||||
|
verify(bookRepository, times(1)).delete(testBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when book isbn doesn't exist")
|
||||||
|
void testDeleteBookNotFound() {
|
||||||
|
String nonExistentIsbn = "1656546262516";
|
||||||
|
when(bookRepository.findByIsbn(nonExistentIsbn)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(BookNotFoundException.class,
|
||||||
|
() -> bookUseCase.deleteBook(nonExistentIsbn));
|
||||||
|
|
||||||
|
verify(bookRepository, times(1)).findByIsbn(nonExistentIsbn);
|
||||||
|
verify(bookRepository, never()).delete(any(Book.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Stock copies tests")
|
||||||
|
class StockCopiesTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should add stock copies to book")
|
||||||
|
void testAddStockCopies() throws BookNotFoundException {
|
||||||
|
when(bookRepository.findByIsbn(bookIsbn)).thenReturn(Optional.of(testBook));
|
||||||
|
when(bookRepository.save(testBook)).thenReturn(testBook);
|
||||||
|
|
||||||
|
int initialCopies = testBook.getStock();
|
||||||
|
int copiesToAdd = 50;
|
||||||
|
int expectedCopies = initialCopies + copiesToAdd;
|
||||||
|
|
||||||
|
int newCopies = bookUseCase.addStockCopies(bookIsbn, copiesToAdd);
|
||||||
|
|
||||||
|
assertEquals(expectedCopies, newCopies);
|
||||||
|
assertEquals(expectedCopies, testBook.getStock());
|
||||||
|
verify(bookRepository, times(1)).findByIsbn(bookIsbn);
|
||||||
|
verify(bookRepository, times(1)).save(testBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when adding copies to non-existent book")
|
||||||
|
void testAddStockCopiesToNonExistentBook() {
|
||||||
|
String nonExistentIsbn = "1656546262516";
|
||||||
|
when(bookRepository.findByIsbn(nonExistentIsbn)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(BookNotFoundException.class,
|
||||||
|
() -> bookUseCase.addStockCopies(nonExistentIsbn, 50));
|
||||||
|
|
||||||
|
verify(bookRepository, times(1)).findByIsbn(nonExistentIsbn);
|
||||||
|
verify(bookRepository, never()).save(any(Book.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should subtract stock copies from book")
|
||||||
|
void testSubtractStockCopies() throws BookNotFoundException, IllegalBookStockException {
|
||||||
|
when(bookRepository.findByIsbn(bookIsbn)).thenReturn(Optional.of(testBook));
|
||||||
|
when(bookRepository.save(testBook)).thenReturn(testBook);
|
||||||
|
|
||||||
|
int initialCopies = testBook.getStock();
|
||||||
|
int copiesToRemove = 30;
|
||||||
|
int expectedCopies = initialCopies - copiesToRemove;
|
||||||
|
|
||||||
|
int newCopies = bookUseCase.subtractStockCopies(bookIsbn, copiesToRemove);
|
||||||
|
|
||||||
|
assertEquals(expectedCopies, newCopies);
|
||||||
|
assertEquals(expectedCopies, testBook.getStock());
|
||||||
|
verify(bookRepository, times(1)).findByIsbn(bookIsbn);
|
||||||
|
verify(bookRepository, times(1)).save(testBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when trying to remove more copies than available")
|
||||||
|
void testSubtractTooManyStockCopies() {
|
||||||
|
when(bookRepository.findByIsbn(bookIsbn)).thenReturn(Optional.of(testBook));
|
||||||
|
|
||||||
|
int copiesToRemove = 200;
|
||||||
|
|
||||||
|
assertThrows(IllegalBookStockException.class,
|
||||||
|
() -> bookUseCase.subtractStockCopies(bookIsbn, copiesToRemove));
|
||||||
|
|
||||||
|
verify(bookRepository, times(1)).findByIsbn(bookIsbn);
|
||||||
|
verify(bookRepository, never()).save(any(Book.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when subtracting copies from non-existent book")
|
||||||
|
void testSubtractStockCopiesFromNonExistentBook() {
|
||||||
|
String nonExistentIsbn = "1656546262516";
|
||||||
|
when(bookRepository.findByIsbn(nonExistentIsbn)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(BookNotFoundException.class,
|
||||||
|
() -> bookUseCase.subtractStockCopies(nonExistentIsbn, 50));
|
||||||
|
|
||||||
|
verify(bookRepository, times(1)).findByIsbn(nonExistentIsbn);
|
||||||
|
verify(bookRepository, never()).save(any(Book.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+379
@@ -0,0 +1,379 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDetails;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookSalesInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class BookValidatorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should validate book with valid data")
|
||||||
|
void testValidateValidBook() {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo validBook = new BookInfo("0000000000000","La vie de Maxime", "Marvin Aubert", "Kioon", date);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> BookValidator.validate(validBook));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should validate book details with valid data")
|
||||||
|
void testValidateValidBookDetails() {
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
BookDetails validBookDetails = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description("C'était un brave partit trop tôt")
|
||||||
|
.language("Français")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> BookValidator.validate(validBookDetails));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should validate book sales informations with valid data")
|
||||||
|
void testValidateValidBookSalesInfo() {
|
||||||
|
BookSalesInfo validBookSalesInfo = BookSalesInfo.builder()
|
||||||
|
.price(15)
|
||||||
|
.stock(10)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> BookValidator.validate(validBookSalesInfo));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("ISBN validation tests")
|
||||||
|
class ISBNValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when isbn is blank")
|
||||||
|
void testValidateBlankISBN() {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookWithBlankISBN = new BookInfo("", "La vie de Maxime", "Marvin Aubert", "Kioon", date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithBlankISBN)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.ISBN_IS_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
@DisplayName("Should throw exception when isbn contains only whitespace")
|
||||||
|
void testValidateWhitespaceISBN(String whitespace) {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookWithWhitespaceISBN = new BookInfo(whitespace, "La vie de Maxime", "Marvin Aubert", "Kioon", date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithWhitespaceISBN)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.ISBN_IS_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"00000", "0000000000", "0000000000000000"})
|
||||||
|
@DisplayName("Should throw exception when isbn contains only thriteen character")
|
||||||
|
void testValidateThirteenCharacterISBN(String number) {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookWithNotExactCharacterISBN = new BookInfo(number, "La vie de Maxime", "Marvin Aubert", "Kioon", date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithNotExactCharacterISBN)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.ISBN_IS_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Title validation tests")
|
||||||
|
class TitleValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when title is blank")
|
||||||
|
void testValidateBlankTitle() {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookWithBlankTitle = new BookInfo("0000000000000","", "Marvin Aubert", "Kioon", date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithBlankTitle)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.TITLE_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
@DisplayName("Should throw exception when title contains only whitespace")
|
||||||
|
void testValidateWhitespaceFirstName(String whitespace) {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookWithWhitespaceTitle = new BookInfo("0000000000000",whitespace, "Marvin Aubert", "Kioon", date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithWhitespaceTitle)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.TITLE_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("author validation tests")
|
||||||
|
class AuthorValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when author is blank")
|
||||||
|
void testValidateBlankAuthor() {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookWithBlankAuthor = new BookInfo("0000000000000","La vie de Maxime", "", "Kioon", date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithBlankAuthor)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.AUTHOR_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
@DisplayName("Should throw exception when author contains only whitespace")
|
||||||
|
void testValidateWhitespaceAuthor(String whitespace) {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookWithWhitespaceAuthor = new BookInfo("0000000000000","La vie de Maxime", whitespace, "Kioon", date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithWhitespaceAuthor)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.AUTHOR_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("editor validation tests")
|
||||||
|
class EditorValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when editor is blank")
|
||||||
|
void testValidateBlankEditor() {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookWithBlankEditor = new BookInfo("0000000000000","La vie de Maxime", "Marvin Aubert", "", date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithBlankEditor)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.EDITOR_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
@DisplayName("Should throw exception when editor contains only whitespace")
|
||||||
|
void testValidateWhitespaceEditor(String whitespace) {
|
||||||
|
LocalDate date = LocalDate.of(2026, 3, 24);
|
||||||
|
BookInfo bookWithWhitespaceEditor = new BookInfo("0000000000000","La vie de Maxime", "Marvin Aubert", whitespace, date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithWhitespaceEditor)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.EDITOR_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("date validation tests")
|
||||||
|
class DateValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when date is after the actual date")
|
||||||
|
void testValidateFuturDate() {
|
||||||
|
LocalDate date = LocalDate.of(2026, 5, 24);
|
||||||
|
BookInfo bookWithFuturDate = new BookInfo("0000000000000","La vie de Maxime", "Marvin Aubert", "Kioon", date);
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithFuturDate)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.DATE_IS_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("price validation tests")
|
||||||
|
class PriceValidationTests {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(doubles = {-3, -15, 0})
|
||||||
|
@DisplayName("Should throw exception when price is negative or equal to zero")
|
||||||
|
void testValidateNegativePrice(double invalidprice) {
|
||||||
|
BookSalesInfo bookWithNegativePrice = BookSalesInfo.builder()
|
||||||
|
.price(invalidprice)
|
||||||
|
.stock(10)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithNegativePrice)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.PRICE_IS_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("stock validation tests")
|
||||||
|
class StockValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when stock is negative")
|
||||||
|
void testValidateNegativeSTock() {
|
||||||
|
BookSalesInfo bookWithNegativeStock = BookSalesInfo.builder()
|
||||||
|
.price(3)
|
||||||
|
.stock(-3)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithNegativeStock)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.STOCK_IS_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("categories validation tests")
|
||||||
|
class CategoriesValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when categories is empty")
|
||||||
|
void testValidateBlankCategories() {
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
String description = "C'était un brave partit trop tôt";
|
||||||
|
String language = "Français";
|
||||||
|
BookDetails bookWithEmptyCategories = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description(description)
|
||||||
|
.language(language)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithEmptyCategories)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.CATEGORIES_IS_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("provideLists")
|
||||||
|
@DisplayName("Should throw exception when categories contains an whitespace")
|
||||||
|
void testValidateWhitespaceCategories(ArrayList<String> invalidlist) {
|
||||||
|
String description = "C'était un brave partit trop tôt";
|
||||||
|
String language = "Français";
|
||||||
|
BookDetails bookWithWhitespaceCategories = BookDetails.builder()
|
||||||
|
.categories(invalidlist)
|
||||||
|
.description(description)
|
||||||
|
.language(language)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithWhitespaceCategories)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.CATEGORIES_IS_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
static Stream<ArrayList<String>> provideLists() {
|
||||||
|
return Stream.of(
|
||||||
|
new ArrayList<String>(java.util.List.of("")),
|
||||||
|
new ArrayList<String>(java.util.List.of(" ")),
|
||||||
|
new ArrayList<String>(java.util.List.of(" ")),
|
||||||
|
new ArrayList<String>(java.util.List.of("\t")),
|
||||||
|
new ArrayList<String>(java.util.List.of("\n")),
|
||||||
|
new ArrayList<String>(java.util.List.of("A", "", "C"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("language validation tests")
|
||||||
|
class LanguageValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when language is blank")
|
||||||
|
void testValidateNegativeLanguage() {
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
String description = "C'était un brave partit trop tôt";
|
||||||
|
String language = "";
|
||||||
|
|
||||||
|
BookDetails bookWithBlankLanguage = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description(description)
|
||||||
|
.language(language)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithBlankLanguage)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.LANGUAGE_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
@DisplayName("Should throw exception when language contains only whitespace")
|
||||||
|
void testValidateWhitespaceLanguage(String whitespace) {
|
||||||
|
ArrayList<String> categories = new ArrayList<>();
|
||||||
|
categories.add("Thriller");
|
||||||
|
categories.add("Biographie");
|
||||||
|
String description = "C'était un brave partit trop tôt";
|
||||||
|
|
||||||
|
BookDetails bookWithWhitespaceLanguage = BookDetails.builder()
|
||||||
|
.categories(categories)
|
||||||
|
.description(description)
|
||||||
|
.language(whitespace)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
NotValidBookException exception = assertThrows(
|
||||||
|
NotValidBookException.class,
|
||||||
|
() -> BookValidator.validate(bookWithWhitespaceLanguage)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(BookValidator.LANGUAGE_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+68
@@ -0,0 +1,68 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.converter;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
@DisplayName("ReviewConverterTest Unit Tests")
|
||||||
|
public class ReviewConverterTest {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDomain() method tests")
|
||||||
|
class ToDomainTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should convert ReviewInfo to Review domain object")
|
||||||
|
void shouldConvertReviewInfoToDomain() {
|
||||||
|
// Given
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
ReviewInfo reviewInfo = new ReviewInfo(5, "tres bon livre", purchaseDate);
|
||||||
|
|
||||||
|
// When
|
||||||
|
Review result = ReviewConverter.toDomain(reviewInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(reviewInfo.note(), result.getNote());
|
||||||
|
assertEquals(reviewInfo.comment(), result.getComment());
|
||||||
|
assertEquals(reviewInfo.purchaseDate(), result.getPurchaseDate());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDTO() method tests")
|
||||||
|
class ToDTOTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should convert Review domain object to ReviewDTO with all fields mapped correctly")
|
||||||
|
void shouldConvertReviewToDTO() {
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
Review review = Review.builder()
|
||||||
|
.avisId(UUID.randomUUID())
|
||||||
|
.customerId(UUID.randomUUID())
|
||||||
|
.bookId(UUID.randomUUID())
|
||||||
|
.note(5)
|
||||||
|
.comment("très bon livre")
|
||||||
|
.purchaseDate(purchaseDate)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ReviewDTO result = ReviewConverter.toDTO(review);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(review.getCustomerId(), result.getCustomerId());
|
||||||
|
assertEquals(review.getBookId(), result.getBookId());
|
||||||
|
assertEquals(review.getNote(), result.getNote());
|
||||||
|
assertEquals(review.getComment(), result.getComment());
|
||||||
|
assertEquals(review.getPurchaseDate(), result.getPurchaseDate());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.entity;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class ReviewTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Builder should create a valid review instance")
|
||||||
|
void testReviewBuilder() {
|
||||||
|
UUID customerId = UUID.randomUUID();
|
||||||
|
UUID bookId = UUID.randomUUID();
|
||||||
|
Integer note = 5;
|
||||||
|
String comment = "très bon livre";
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
|
||||||
|
Review review = Review.builder()
|
||||||
|
.customerId(customerId)
|
||||||
|
.bookId(bookId)
|
||||||
|
.note(note)
|
||||||
|
.comment(comment)
|
||||||
|
.purchaseDate(purchaseDate)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals(customerId, review.getCustomerId());
|
||||||
|
assertEquals(bookId, review.getBookId());
|
||||||
|
assertEquals(note, review.getNote());
|
||||||
|
assertEquals(comment, review.getComment());
|
||||||
|
assertEquals(purchaseDate, review.getPurchaseDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("setRandomUUID should change the ID to a new random UUID")
|
||||||
|
void testSetRandomUUID() {
|
||||||
|
Review review = Review.builder().build();
|
||||||
|
UUID originalAvisId = review.getAvisId();
|
||||||
|
UUID originalCustomerId = review.getCustomerId();
|
||||||
|
UUID originalBookId = review.getCustomerId();
|
||||||
|
|
||||||
|
review.setRandomUUID();
|
||||||
|
review.setRandomUUIDCustomerAndBook();
|
||||||
|
|
||||||
|
assertNotNull(review.getAvisId());
|
||||||
|
assertNotNull(review.getCustomerId());
|
||||||
|
assertNotNull(review.getBookId());
|
||||||
|
assertNotEquals(originalAvisId, review.getAvisId());
|
||||||
|
assertNotEquals(originalCustomerId, review.getCustomerId());
|
||||||
|
assertNotEquals(originalBookId, review.getBookId());
|
||||||
|
}
|
||||||
|
}
|
||||||
+60
@@ -0,0 +1,60 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.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.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class NotValidReviewExcpetionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be created with the provided message")
|
||||||
|
void testExceptionCreation() {
|
||||||
|
String errorMessage = "Review data is not valid";
|
||||||
|
|
||||||
|
NotValidReviewException exception = new NotValidReviewException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {
|
||||||
|
"Note is greater than or equal to 1",
|
||||||
|
"Note is less than or equal to 1",
|
||||||
|
"Comment cannot be empty"
|
||||||
|
})
|
||||||
|
@DisplayName("Exception should handle different validation messages")
|
||||||
|
void testExceptionWithDifferentMessages(String errorMessage) {
|
||||||
|
NotValidReviewException exception = new NotValidReviewException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
String errorMessage = "Comment field is empty";
|
||||||
|
|
||||||
|
Exception exception = assertThrows(NotValidReviewException.class, () -> {
|
||||||
|
throw new NotValidReviewException(errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be catchable as a general Exception")
|
||||||
|
void testExceptionInheritance() {
|
||||||
|
String errorMessage = "Invalid review data";
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new NotValidReviewException(errorMessage);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertEquals(NotValidReviewException.class, e.getClass());
|
||||||
|
assertEquals(errorMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+100
@@ -0,0 +1,100 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class ReviewNotFoundExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the UUID provided for customer")
|
||||||
|
void testExceptionMessageContainsUUIDForCustomer() {
|
||||||
|
UUID customerUUID = UUID.randomUUID();
|
||||||
|
|
||||||
|
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||||
|
|
||||||
|
String expectedMessage = String.format("The reviews with the customer id %s does not exists", customerUUID);
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the UUID provided for book")
|
||||||
|
void testExceptionMessageContainsUUIDForBook() {
|
||||||
|
UUID bookUUID = UUID.randomUUID();
|
||||||
|
|
||||||
|
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID));
|
||||||
|
|
||||||
|
String expectedMessage = String.format("The reviews with book id %s does not exists", bookUUID);
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the UUID provided for customer and book")
|
||||||
|
void testExceptionMessageContainsUUIDForCustomerAndBook() {
|
||||||
|
UUID customerUUID = UUID.randomUUID();
|
||||||
|
UUID bookUUID = UUID.randomUUID();
|
||||||
|
|
||||||
|
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.of(bookUUID));
|
||||||
|
|
||||||
|
String expectedMessage = String.format("The review with customer id %s and book id %s does not exists", customerUUID, bookUUID);
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should use the correct constant message format for customer")
|
||||||
|
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||||
|
UUID customerUUID = UUID.randomUUID();
|
||||||
|
|
||||||
|
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||||
|
|
||||||
|
String expectedFormatWithPlaceholder = "The reviews with the customer id {0} does not exists";
|
||||||
|
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE,
|
||||||
|
expectedFormatWithPlaceholder);
|
||||||
|
assertTrue(exception.getMessage().contains(customerUUID.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should use the correct constant message format for book")
|
||||||
|
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||||
|
UUID bookUUID = UUID.randomUUID();
|
||||||
|
|
||||||
|
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID));
|
||||||
|
|
||||||
|
String expectedFormatWithPlaceholder = "The reviews with the book id {0} does not exists";
|
||||||
|
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE,
|
||||||
|
expectedFormatWithPlaceholder);
|
||||||
|
assertTrue(exception.getMessage().contains(bookUUID.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should use the correct constant message format for customer and book")
|
||||||
|
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||||
|
UUID customerUUID = UUID.randomUUID();
|
||||||
|
UUID bookUUID = UUID.randomUUID();
|
||||||
|
|
||||||
|
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.of(bookUUID));
|
||||||
|
|
||||||
|
String expectedFormatWithPlaceholder = "The reviews with the customer id {0} and the book id {1} does not exists";
|
||||||
|
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_CUSTOMER_ID_AND_BOOK_ID_DOES_NOT_EXIST_MESSAGE,
|
||||||
|
expectedFormatWithPlaceholder);
|
||||||
|
assertTrue(exception.getMessage().contains(customerUUID.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
UUID customerUUID = UUID.randomUUID();
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||||
|
} catch (ReviewNotFoundException e) {
|
||||||
|
String expectedMessage = String.format("The reviews with the customer id %s does not exists", customerUUID);
|
||||||
|
assertEquals(expectedMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+354
@@ -0,0 +1,354 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
|
||||||
|
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.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class ReviewRepositoryTest {
|
||||||
|
|
||||||
|
private ReviewRepository repository;
|
||||||
|
private Review review1;
|
||||||
|
private Review review2;
|
||||||
|
private Review review3;
|
||||||
|
private Review review4;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
repository = new ReviewRepository();
|
||||||
|
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
review1 = Review.builder()
|
||||||
|
.note(1)
|
||||||
|
.comment("nul")
|
||||||
|
.purchaseDate(purchaseDate)
|
||||||
|
.build();
|
||||||
|
review1.setRandomUUID();
|
||||||
|
review1.setRandomUUIDCustomerAndBook();
|
||||||
|
|
||||||
|
UUID customerId = UUID.randomUUID();
|
||||||
|
UUID bookId = UUID.randomUUID();
|
||||||
|
review2 = Review.builder()
|
||||||
|
.customerId(customerId)
|
||||||
|
.bookId(bookId)
|
||||||
|
.note(1)
|
||||||
|
.comment("nul")
|
||||||
|
.purchaseDate(purchaseDate)
|
||||||
|
.build();
|
||||||
|
review2.setRandomUUID();
|
||||||
|
|
||||||
|
UUID bookId3 = UUID.randomUUID();
|
||||||
|
review3 = Review.builder()
|
||||||
|
.customerId(customerId)
|
||||||
|
.bookId(bookId3)
|
||||||
|
.note(2)
|
||||||
|
.comment("ça passe")
|
||||||
|
.purchaseDate(purchaseDate)
|
||||||
|
.build();
|
||||||
|
review3.setRandomUUID();
|
||||||
|
|
||||||
|
UUID customerId4 = UUID.randomUUID();
|
||||||
|
review4 = Review.builder()
|
||||||
|
.customerId(customerId4)
|
||||||
|
.bookId(bookId)
|
||||||
|
.note(2)
|
||||||
|
.comment("ça passe")
|
||||||
|
.purchaseDate(purchaseDate)
|
||||||
|
.build();
|
||||||
|
review4.setRandomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("New repository should be empty")
|
||||||
|
void testNewRepositoryIsEmpty() {
|
||||||
|
List<Review> reviews = repository.findAll();
|
||||||
|
|
||||||
|
assertTrue(reviews.isEmpty());
|
||||||
|
assertEquals(0, reviews.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Save operations")
|
||||||
|
class SaveOperations {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should add a new review")
|
||||||
|
void testSaveNewReview() {
|
||||||
|
Review savedReview = repository.save(review1);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(review1.getCustomerId(), savedReview.getCustomerId());
|
||||||
|
assertEquals(review1.getBookId(), savedReview.getBookId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should update existing review with same customer and book ID")
|
||||||
|
void testSaveUpdatesExistingReview() {
|
||||||
|
repository.save(review1);
|
||||||
|
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 5, 24);
|
||||||
|
UUID avisId = review1.getAvisId();
|
||||||
|
UUID customerId = UUID.randomUUID();
|
||||||
|
UUID bookId = UUID.randomUUID();
|
||||||
|
Review updatedReview = Review.builder()
|
||||||
|
.avisId(avisId)
|
||||||
|
.customerId(customerId)
|
||||||
|
.bookId(bookId)
|
||||||
|
.note(4)
|
||||||
|
.comment("pas mal")
|
||||||
|
.purchaseDate(purchaseDate)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Review savedReview = repository.save(updatedReview);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(avisId, savedReview.getAvisId());
|
||||||
|
assertEquals(customerId, savedReview.getCustomerId());
|
||||||
|
assertEquals(bookId, savedReview.getBookId());
|
||||||
|
assertEquals(4, savedReview.getNote());
|
||||||
|
assertEquals("pas mal", savedReview.getComment());
|
||||||
|
assertEquals(purchaseDate, savedReview.getPurchaseDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save multiple review should add all of them")
|
||||||
|
void testSaveMultipleReviews() {
|
||||||
|
repository.save(review1);
|
||||||
|
repository.save(review2);
|
||||||
|
|
||||||
|
List<Review> reviews = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, reviews.size());
|
||||||
|
assertTrue(reviews.contains(review1));
|
||||||
|
assertTrue(reviews.contains(review2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find operations")
|
||||||
|
class FindOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpReviews() {
|
||||||
|
repository.save(review1);
|
||||||
|
repository.save(review2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindAll should return all reviews")
|
||||||
|
void testFindAll() {
|
||||||
|
List<Review> reviews = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, reviews.size());
|
||||||
|
assertTrue(reviews.contains(review1));
|
||||||
|
assertTrue(reviews.contains(review2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByCustomerId should return review with matching customer ID")
|
||||||
|
void testFindByCustomerId() {
|
||||||
|
Optional<Review> foundreview = repository.findByCustomerId(review1.getCustomerId());
|
||||||
|
|
||||||
|
assertTrue(foundreview.isPresent());
|
||||||
|
assertEquals(review1.getNote(), foundreview.get().getNote());
|
||||||
|
assertEquals(review1.getComment(), foundreview.get().getComment());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByCustomerId should return empty Optional when a review with customer ID doesn't exist")
|
||||||
|
void testFindByCustomerIdNotFound() {
|
||||||
|
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||||
|
|
||||||
|
Optional<Review> foundreview = repository.findByCustomerId(nonExistentCustomerId);
|
||||||
|
|
||||||
|
assertTrue(foundreview.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByBookId should return review with matching book ID")
|
||||||
|
void testFindByBookId() {
|
||||||
|
Optional<Review> foundreview = repository.findByBookId(review1.getBookId());
|
||||||
|
|
||||||
|
assertTrue(foundreview.isPresent());
|
||||||
|
assertEquals(review1.getNote(), foundreview.get().getNote());
|
||||||
|
assertEquals(review1.getComment(), foundreview.get().getComment());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByBookId should return empty Optional when a review with book ID doesn't exist")
|
||||||
|
void testFindByBookIdNotFound() {
|
||||||
|
UUID nonExistentBookId = UUID.randomUUID();
|
||||||
|
|
||||||
|
Optional<Review> foundreview = repository.findByBookId(nonExistentBookId);
|
||||||
|
|
||||||
|
assertTrue(foundreview.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByAvisId should return review with matching avis ID")
|
||||||
|
void testFindByAvisId() {
|
||||||
|
Optional<Review> foundreview = repository.findByAvisId(review1.getAvisId());
|
||||||
|
|
||||||
|
assertTrue(foundreview.isPresent());
|
||||||
|
assertEquals(review1.getNote(), foundreview.get().getNote());
|
||||||
|
assertEquals(review1.getComment(), foundreview.get().getComment());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByAvisId should return empty Optional when a review with avis ID doesn't exist")
|
||||||
|
void testFindByAvisIdNotFound() {
|
||||||
|
UUID nonExistentAvisId = UUID.randomUUID();
|
||||||
|
|
||||||
|
Optional<Review> foundreview = repository.findByAvisId(nonExistentAvisId);
|
||||||
|
|
||||||
|
assertTrue(foundreview.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("existsByCustomerId should return true when a review with customer ID exists")
|
||||||
|
void testExistsByCustomerIdExists() {
|
||||||
|
boolean exists = repository.existsByCustomerId(review1.getCustomerId());
|
||||||
|
|
||||||
|
assertTrue(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("existsByCustomerId should return false when a review with customer ID doesn't exist")
|
||||||
|
void testExistsByCustomerIdNotExists() {
|
||||||
|
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||||
|
|
||||||
|
boolean exists = repository.existsByCustomerId(nonExistentCustomerId);
|
||||||
|
|
||||||
|
assertFalse(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("existsByBookId should return true when a review with book ID exists")
|
||||||
|
void testExistsByBookIdExists() {
|
||||||
|
boolean exists = repository.existsByBookId(review1.getBookId());
|
||||||
|
|
||||||
|
assertTrue(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("existsByBookId should return false when a review with book ID doesn't exist")
|
||||||
|
void testExistsByBookIdNotExists() {
|
||||||
|
UUID nonExistentBookId = UUID.randomUUID();
|
||||||
|
|
||||||
|
boolean exists = repository.existsByBookId(nonExistentBookId);
|
||||||
|
|
||||||
|
assertFalse(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("existsByAvisId should return true when a review with avis ID exists")
|
||||||
|
void testExistsByAvisIdExists() {
|
||||||
|
boolean exists = repository.existsByAvisId(review1.getAvisId());
|
||||||
|
|
||||||
|
assertTrue(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("existsByAvisId should return false when avis ID doesn't exist")
|
||||||
|
void testExistsByAvisIdNotExists() {
|
||||||
|
UUID nonExistentAvisId = UUID.randomUUID();
|
||||||
|
|
||||||
|
boolean exists = repository.existsByAvisId(nonExistentAvisId);
|
||||||
|
|
||||||
|
assertFalse(exists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Delete operations")
|
||||||
|
class DeleteOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpReviews() {
|
||||||
|
repository.save(review1);
|
||||||
|
repository.save(review2);
|
||||||
|
repository.save(review3);
|
||||||
|
repository.save(review4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should remove all reviews of a customer")
|
||||||
|
void testDeleteCustomerReviews() {
|
||||||
|
repository.deleteCustomerReviews(review2.getCustomerId());
|
||||||
|
|
||||||
|
List<Review> reviews = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, reviews.size());
|
||||||
|
assertTrue(reviews.contains(review1));
|
||||||
|
assertFalse(reviews.contains(review2));
|
||||||
|
assertFalse(reviews.contains(review3));
|
||||||
|
assertTrue(reviews.contains(review4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should remove all reviews of a book")
|
||||||
|
void testDeleteBookReviews() {
|
||||||
|
repository.deleteBookReviews(review2.getBookId());
|
||||||
|
|
||||||
|
List<Review> reviews = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, reviews.size());
|
||||||
|
assertTrue(reviews.contains(review1));
|
||||||
|
assertFalse(reviews.contains(review2));
|
||||||
|
assertTrue(reviews.contains(review3));
|
||||||
|
assertFalse(reviews.contains(review4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should remove the specified review")
|
||||||
|
void testDelete() {
|
||||||
|
repository.delete(review1);
|
||||||
|
|
||||||
|
List<Review> reviews = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(3, reviews.size());
|
||||||
|
assertFalse(reviews.contains(review1));
|
||||||
|
assertTrue(reviews.contains(review2));
|
||||||
|
assertTrue(reviews.contains(review3));
|
||||||
|
assertTrue(reviews.contains(review4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("DeleteAll should remove all reviews")
|
||||||
|
void testDeleteAll() {
|
||||||
|
repository.deleteAll();
|
||||||
|
|
||||||
|
List<Review> reviews = repository.findAll();
|
||||||
|
|
||||||
|
assertTrue(reviews.isEmpty());
|
||||||
|
assertEquals(0, reviews.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should not throw exception when review doesn't exist")
|
||||||
|
void testDeleteNonExistentReview() {
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
Review nonExistentReview = Review.builder()
|
||||||
|
.note(1)
|
||||||
|
.comment("nul")
|
||||||
|
.purchaseDate(purchaseDate)
|
||||||
|
.build();
|
||||||
|
nonExistentReview.setRandomUUID();
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> repository.delete(nonExistentReview));
|
||||||
|
|
||||||
|
assertEquals(4, repository.findAll().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+310
@@ -0,0 +1,310 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository;
|
||||||
|
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.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
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 static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
|
||||||
|
public class ReviewUseCaseTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ReviewRepository reviewRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private ReviewUseCase reviewUseCase;
|
||||||
|
|
||||||
|
private UUID customerId;
|
||||||
|
private UUID bookId;
|
||||||
|
private LocalDate purchaseDate;
|
||||||
|
private Review testReview;
|
||||||
|
private ReviewInfo validReviewInfo;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
customerId = UUID.randomUUID();
|
||||||
|
bookId = UUID.randomUUID();
|
||||||
|
purchaseDate = LocalDate.of(2026, 5, 24);
|
||||||
|
testReview = Review.builder()
|
||||||
|
.customerId(customerId)
|
||||||
|
.bookId(bookId)
|
||||||
|
.note(2)
|
||||||
|
.comment("plutôt mauvais")
|
||||||
|
.purchaseDate(purchaseDate)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
validReviewInfo = new ReviewInfo(2, "plutôt mauvais", purchaseDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Register review tests")
|
||||||
|
class RegisterReviewTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should register review when valid data is provided")
|
||||||
|
void testRegisterReviewWithValidData() throws NotValidReviewException {
|
||||||
|
when(reviewRepository.save(any(Review.class))).thenReturn(testReview);
|
||||||
|
|
||||||
|
UUID registeredId = reviewUseCase.registerReview(validReviewInfo);
|
||||||
|
|
||||||
|
assertNotNull(registeredId);
|
||||||
|
assertEquals(customerId, registeredId);
|
||||||
|
verify(reviewRepository, times(1)).save(any(Review.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when review data is not valid")
|
||||||
|
void testRegisterReviewWithInvalidData() {
|
||||||
|
ReviewInfo invalidReviewInfo = new ReviewInfo(2, "plutôt mauvais", purchaseDate);
|
||||||
|
|
||||||
|
assertThrows(NotValidReviewException.class,
|
||||||
|
() -> reviewUseCase.registerCustomer(invalidReviewInfo));
|
||||||
|
|
||||||
|
verify(reviewRepository, never()).save(any(Review.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find review tests")
|
||||||
|
class FindReviewTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return review when customer ID exists")
|
||||||
|
void testFindReviewByCustomerId() {
|
||||||
|
when(reviewRepository.findByCustomerId(customerId)).thenReturn(Optional.of(testReview));
|
||||||
|
|
||||||
|
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerId(customerId);
|
||||||
|
|
||||||
|
assertTrue(foundReview.isPresent());
|
||||||
|
assertEquals(testReview.getBookId(), foundReview.get().getBookId());
|
||||||
|
assertEquals(testReview.getNote(), foundReview.get().getNote());
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerId(customerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty Optional when customer ID doesn't exist")
|
||||||
|
void testFindReviewByCustomerIdNotFound() {
|
||||||
|
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||||
|
when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerId(nonExistentCustomerId);
|
||||||
|
|
||||||
|
assertTrue(foundReview.isEmpty());
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerId(nonExistentCustomerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return review when book ID exists")
|
||||||
|
void testFindReviewByBookId() {
|
||||||
|
when(reviewRepository.findByBookId(bookId)).thenReturn(Optional.of(testReview));
|
||||||
|
|
||||||
|
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByBookId(bookId);
|
||||||
|
|
||||||
|
assertTrue(foundReview.isPresent());
|
||||||
|
assertEquals(testReview.getCustomerId(), foundReview.get().getCustomerId());
|
||||||
|
assertEquals(testReview.getNote(), foundReview.get().getNote());
|
||||||
|
verify(reviewRepository, times(1)).findByBookId(bookId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty Optional when book ID doesn't exist")
|
||||||
|
void testFindReviewByBookIdNotFound() {
|
||||||
|
UUID nonExistentBookId = UUID.randomUUID();
|
||||||
|
when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByBookId(nonExistentBookId);
|
||||||
|
|
||||||
|
assertTrue(foundReview.isEmpty());
|
||||||
|
verify(reviewRepository, times(1)).findByBookId(nonExistentBookId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return review when customer and book ID exists")
|
||||||
|
void testFindReviewByCustomerAndBookId() {
|
||||||
|
when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview));
|
||||||
|
|
||||||
|
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerAndBookId(customerId, bookId);
|
||||||
|
|
||||||
|
assertTrue(foundReview.isPresent());
|
||||||
|
assertEquals(testReview.getBookId(), foundReview.get().getBookId());
|
||||||
|
assertEquals(testReview.getNote(), foundReview.get().getNote());
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty Optional when customer ID doesn't exist")
|
||||||
|
void testFindReviewByCustomerIdNotFound() {
|
||||||
|
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||||
|
UUID nonExistentBookId = UUID.randomUUID();
|
||||||
|
when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||||
|
|
||||||
|
assertTrue(foundReview.isEmpty());
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Update review tests")
|
||||||
|
class UpdateReviewTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should update review when valid data is provided")
|
||||||
|
void testUpdateReviewWithValidData() throws ReviewNotFoundException, NotValidReviewException {
|
||||||
|
when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview));
|
||||||
|
|
||||||
|
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 30);
|
||||||
|
Review updatedReview = Review.builder()
|
||||||
|
.customerId(customerId)
|
||||||
|
.bookId(bookId)
|
||||||
|
.note(4)
|
||||||
|
.comment("en fait c'est bien")
|
||||||
|
.purchaseDate(updatePurchaseDate)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(reviewRepository.save(any(Review.class))).thenReturn(updatedReview);
|
||||||
|
|
||||||
|
ReviewInfo updateInfo = new ReviewInfo(4, "en fait c'est bien", updatePurchaseDate);
|
||||||
|
|
||||||
|
ReviewDTO result = reviewUseCase.updateReview(customerId, bookId, updateInfo);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(customerId, result.getCustomerId());
|
||||||
|
assertEquals(4, result.getNote());
|
||||||
|
assertEquals("en fait c'est bien", result.getComment());
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId);
|
||||||
|
verify(reviewRepository, times(1)).save(any(Review.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when customer and book ID doesn't exist")
|
||||||
|
void testUpdateReviewNotFound() {
|
||||||
|
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||||
|
UUID nonExistentBookId = UUID.randomUUID();
|
||||||
|
when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24);
|
||||||
|
ReviewInfo updateInfo = new ReviewInfo(3, "moyen", updatePurchaseDate);
|
||||||
|
|
||||||
|
assertThrows(ReviewNotFoundException.class,
|
||||||
|
() -> reviewUseCase.updateReview(nonExistentCustomerId, nonExistentBookId, updateInfo));
|
||||||
|
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||||
|
verify(reviewRepository, never()).save(any(Review.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when update data is not valid")
|
||||||
|
void testUpdateReviewWithInvalidData() {
|
||||||
|
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24);
|
||||||
|
ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate);
|
||||||
|
|
||||||
|
assertThrows(NotValidCustomerException.class,
|
||||||
|
() -> reviewUseCase.updateCustomer(customerId, invalidUpdateInfo));
|
||||||
|
|
||||||
|
verify(reviewRepository, never()).findByCustomerAndBookId(any(UUID.class), any(UUID.class));
|
||||||
|
verify(reviewRepository, never()).save(any(Review.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Delete review tests")
|
||||||
|
class DeleteReviewTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should delete reviews when customer ID exists")
|
||||||
|
void testDeleteCustomerReviews() throws ReviewNotFoundException {
|
||||||
|
when(reviewRepository.findByCustomerId(customerId)).thenReturn(Optional.of(testReview));
|
||||||
|
doNothing().when(reviewRepository).delete(testReview);
|
||||||
|
|
||||||
|
reviewUseCase.deleteCustomerReviews(customerId);
|
||||||
|
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerId(customerId);
|
||||||
|
verify(reviewRepository, times(1)).delete(testReview);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when customer ID doesn't exist")
|
||||||
|
void testDeleteCustomerReviewsNotFound() {
|
||||||
|
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||||
|
when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(ReviewNotFoundException.class,
|
||||||
|
() -> reviewUseCase.deleteCustomerReviews(nonExistentCustomerId));
|
||||||
|
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerId(nonExistentCustomerId);
|
||||||
|
verify(reviewRepository, never()).delete(any(Review.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should delete reviews when book ID exists")
|
||||||
|
void testDeleteBookReviews() throws ReviewNotFoundException {
|
||||||
|
when(reviewRepository.findByBookId(bookId)).thenReturn(Optional.of(testReview));
|
||||||
|
doNothing().when(reviewRepository).delete(testReview);
|
||||||
|
|
||||||
|
reviewUseCase.deleteBookReviews(bookId);
|
||||||
|
|
||||||
|
verify(reviewRepository, times(1)).findByBookId(bookId);
|
||||||
|
verify(reviewRepository, times(1)).delete(testReview);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when book ID doesn't exist")
|
||||||
|
void testDeleteBookReviewsNotFound() {
|
||||||
|
UUID nonExistentBookId = UUID.randomUUID();
|
||||||
|
when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(ReviewNotFoundException.class,
|
||||||
|
() -> reviewUseCase.deleteBookReviews(nonExistentBookId));
|
||||||
|
|
||||||
|
verify(reviewRepository, times(1)).findByBookId(nonExistentBookId);
|
||||||
|
verify(reviewRepository, never()).delete(any(Review.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should delete review when customer and book ID exists")
|
||||||
|
void testDeleteReview() throws ReviewNotFoundException {
|
||||||
|
when(reviewRepository.findByCustomerAndBookId(customerId, bookId)).thenReturn(Optional.of(testReview));
|
||||||
|
doNothing().when(reviewRepository).delete(testReview);
|
||||||
|
|
||||||
|
reviewUseCase.deleteReview(customerId, bookId);
|
||||||
|
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerAndBookId(customerId, bookId);
|
||||||
|
verify(reviewRepository, times(1)).delete(testReview);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when customer and book ID doesn't exist")
|
||||||
|
void testDeleteReviewNotFound() {
|
||||||
|
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||||
|
UUID nonExistentBookId = UUID.randomUUID();
|
||||||
|
when(reviewRepository.findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(ReviewNotFoundException.class,
|
||||||
|
() -> reviewUseCase.deleteReviews(nonExistentCustomerId, nonExistentBookId));
|
||||||
|
|
||||||
|
verify(reviewRepository, times(1)).findByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||||
|
verify(reviewRepository, never()).delete(any(Review.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+113
@@ -0,0 +1,113 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class ReviewValidatorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should validate review with valid data")
|
||||||
|
void testValidateValidReview() {
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
ReviewInfo validReview = new ReviewInfo(3, "Bof", purchaseDate);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> ReviewValidator.validate(validReview));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Note validation tests")
|
||||||
|
class NoteValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when note is lower than 1")
|
||||||
|
void testValidateNoteLower1() {
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
ReviewInfo reviewWithNoteLower1 = new ReviewInfo(0, "Bof", purchaseDate);
|
||||||
|
|
||||||
|
NotValidReviewException exception = assertThrows(
|
||||||
|
NotValidReviewException.class,
|
||||||
|
() -> ReviewValidator.validate(reviewWithNoteLower1)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(ReviewValidator.NOTE_CANNOT_BE_LOWER_THAN_1, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when note is upper than 5")
|
||||||
|
void testValidateNoteUpper5() {
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
ReviewInfo reviewWithNoteUpper5 = new ReviewInfo(6, "Bof", purchaseDate);
|
||||||
|
|
||||||
|
NotValidReviewException exception = assertThrows(
|
||||||
|
NotValidReviewException.class,
|
||||||
|
() -> ReviewValidator.validate(reviewWithNoteUpper5)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(ReviewValidator.NOTE_CANNOT_BE_UPPER_THAN_5, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Comment validation tests")
|
||||||
|
class CommentValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when comment is blank")
|
||||||
|
void testValidateBlankComment() {
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
ReviewInfo reviewWithBlankComment = new ReviewInfo(3, "", purchaseDate);
|
||||||
|
|
||||||
|
NotValidReviewException exception = assertThrows(
|
||||||
|
NotValidReviewException.class,
|
||||||
|
() -> ReviewValidator.validate(reviewWithBlankComment)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
@DisplayName("Should throw exception when last name contains only whitespace")
|
||||||
|
void testValidateWhitespaceLastName(String whitespace) {
|
||||||
|
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||||
|
ReviewInfo reviewWithBlankComment = new ReviewInfo(3, whitespace, purchaseDate);
|
||||||
|
|
||||||
|
NotValidReviewException exception = assertThrows(
|
||||||
|
NotValidReviewException.class,
|
||||||
|
() -> ReviewValidator.validate(reviewWithBlankComment)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Purchase date validation tests")
|
||||||
|
class PurchaseDateValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when purchase date is after the actual date")
|
||||||
|
void testValidateFuturPurchaseDate() {
|
||||||
|
LocalDate futurepurchaseDate = LocalDate.of(2026, 6, 24);
|
||||||
|
ReviewInfo reviewWithFuturPurchaseDate = new ReviewInfo(2, "Bof", futurepurchaseDate);
|
||||||
|
|
||||||
|
NotValidReviewException exception = assertThrows(
|
||||||
|
NotValidReviewException.class,
|
||||||
|
() -> ReviewValidator.validate(reviewWithFuturPurchaseDate)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(ReviewValidator.PURCHASE_DATE_IS_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user