Order #6

Closed
Wilfried BRIGITTE wants to merge 5 commits from bridja:order into main
17 changed files with 1294 additions and 0 deletions
Showing only changes of commit 123aa44814 - Show all commits
@@ -0,0 +1,25 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book;
import lombok.Builder;
import lombok.Getter;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
@Builder
@Getter
public class BookDTO {
private final UUID id;
private final String isbn;
private final String title;
private final String author;
private final String publisher;
private final LocalDate publicationDate;
private final BigDecimal price;
private final int stock;
private final List<String> categories;
private final String description;
private final String language;
}
@@ -0,0 +1,19 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
public record BookInfo(
String isbn,
String title,
String author,
String publisher,
LocalDate publicationDate,
BigDecimal price,
int initialStock,
List<String> categories,
String description,
String language
) {
}
@@ -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.BookInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
public final class BookConverter {
private BookConverter() {
}
public static Book toDomain(BookInfo bookInfo) {
return Book.builder()
.isbn(bookInfo.isbn())
.title(bookInfo.title())
.author(bookInfo.author())
.publisher(bookInfo.publisher())
.publicationDate(bookInfo.publicationDate())
.price(bookInfo.price())
.stock(bookInfo.initialStock())
.categories(bookInfo.categories())
.description(bookInfo.description())
.language(bookInfo.language())
.build();
}
public static BookDTO toDTO(Book book) {
return BookDTO.builder()
.id(book.getId())
.isbn(book.getIsbn())
.title(book.getTitle())
.author(book.getAuthor())
.publisher(book.getPublisher())
.publicationDate(book.getPublicationDate())
.price(book.getPrice())
.stock(book.getStock())
.categories(book.getCategories())
.description(book.getDescription())
.language(book.getLanguage())
.build();
}
}
@@ -0,0 +1,31 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
import lombok.Builder;
import lombok.Getter;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
@Builder
@Getter
public class Book {
private UUID id;
private String isbn;
private String title;
private String author;
private String publisher;
private LocalDate publicationDate;
private BigDecimal price;
private int stock;
private List<String> categories;
private String description;
private String language;
public void setRandomUUID() {
this.id = UUID.randomUUID();
}
}
@@ -0,0 +1,14 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
import java.text.MessageFormat;
import java.util.UUID;
public class BookNotFoundException extends Exception {
public static final String THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The book with id {0} does not exist";
public BookNotFoundException(UUID uuid) {
super(MessageFormat.format(THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
}
}
@@ -0,0 +1,9 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
public class NotValidBookException extends Exception {
public NotValidBookException(String message) {
super(message);
}
}
@@ -0,0 +1,52 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@NoArgsConstructor
public final 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> optionalBookWithSameId = this.findById(newBook.getId());
optionalBookWithSameId.ifPresentOrElse(books::remove, newBook::setRandomUUID);
this.books.add(newBook);
return newBook;
}
public Optional<Book> findById(UUID uuid) {
return this.books.stream()
.filter(book -> book.getId().equals(uuid))
.findFirst();
}
public boolean existsById(UUID uuid) {
return this.books.stream()
.anyMatch(book -> book.getId().equals(uuid));
}
public Optional<Book> findByIsbn(String isbn) {
return this.books.stream()
.filter(book -> book.getIsbn().equals(isbn))
.findFirst();
}
public void delete(Book book) {
this.books.remove(book);
}
}
@@ -0,0 +1,68 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.book.converter.BookConverter;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.BookNotFoundException;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.book.validator.BookValidator;
import java.util.Optional;
import java.util.UUID;
public final class BookUseCase {
private final BookRepository bookRepository;
public BookUseCase(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
public String registerBook(BookInfo bookInfo) throws NotValidBookException {
BookValidator.validate(bookInfo);
Book bookToRegister = BookConverter.toDomain(bookInfo);
Book registeredBook = bookRepository.save(bookToRegister);
return registeredBook.getIsbn();
}
public Optional<BookDTO> findBookByIsbn(String isbn) {
Optional<Book> optionalBook = bookRepository.findByIsbn(isbn);
return optionalBook.map(BookConverter::toDTO);
}
public BookDTO updateBook(UUID uuid, BookInfo bookInfo) throws BookNotFoundException, NotValidBookException {
BookValidator.validate(bookInfo);
Book existingBook = getBookIfNotFoundThrowException(uuid);
Book updatedBook = Book.builder()
.id(uuid)
.isbn(bookInfo.isbn())
.title(bookInfo.title())
.author(bookInfo.author())
.publisher(bookInfo.publisher())
.publicationDate(bookInfo.publicationDate())
.price(bookInfo.price())
.stock(existingBook.getStock())
.categories(bookInfo.categories())
.description(bookInfo.description())
.language(bookInfo.language())
.build();
Book saved = bookRepository.save(updatedBook);
return BookConverter.toDTO(saved);
}
public void deleteBook(UUID uuid) throws BookNotFoundException {
Book bookToDelete = getBookIfNotFoundThrowException(uuid);
bookRepository.delete(bookToDelete);
}
private Book getBookIfNotFoundThrowException(UUID uuid) throws BookNotFoundException {
Optional<Book> optionalBook = bookRepository.findById(uuid);
if (optionalBook.isEmpty()) {
throw new BookNotFoundException(uuid);
}
return optionalBook.get();
}
}
@@ -0,0 +1,60 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.validator;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
import java.math.BigDecimal;
public final class BookValidator {
public static final String ISBN_IS_NOT_VALID = "ISBN is not valid";
public static final String PRICE_MUST_BE_POSITIVE = "Price must be positive";
public static final String TITLE_CANNOT_BE_BLANK = "Title cannot be blank";
public static final String AUTHOR_CANNOT_BE_BLANK = "Author cannot be blank";
public static final String PUBLISHER_CANNOT_BE_BLANK = "Publisher cannot be blank";
public static final String ISBN_REGEX = "\\d{13}";
private BookValidator() {
}
public static void validate(BookInfo bookInfo) throws NotValidBookException {
validateIsbn(bookInfo);
validateTitle(bookInfo);
validateAuthor(bookInfo);
validatePublisher(bookInfo);
validatePrice(bookInfo);
}
private static void validateIsbn(BookInfo bookInfo) throws NotValidBookException {
if (bookInfo.isbn() == null || bookInfo.isbn().isBlank()) {
throw new NotValidBookException(ISBN_IS_NOT_VALID);
}
if (!bookInfo.isbn().matches(ISBN_REGEX)) {
throw new NotValidBookException(ISBN_IS_NOT_VALID);
}
}
private static void validateTitle(BookInfo bookInfo) throws NotValidBookException {
if (bookInfo.title() == null || bookInfo.title().isBlank()) {
throw new NotValidBookException(TITLE_CANNOT_BE_BLANK);
}
}
private static void validateAuthor(BookInfo bookInfo) throws NotValidBookException {
if (bookInfo.author() == null || bookInfo.author().isBlank()) {
throw new NotValidBookException(AUTHOR_CANNOT_BE_BLANK);
}
}
private static void validatePublisher(BookInfo bookInfo) throws NotValidBookException {
if (bookInfo.publisher() == null || bookInfo.publisher().isBlank()) {
throw new NotValidBookException(PUBLISHER_CANNOT_BE_BLANK);
}
}
private static void validatePrice(BookInfo bookInfo) throws NotValidBookException {
if (bookInfo.price() == null || bookInfo.price().compareTo(BigDecimal.ZERO) <= 0) {
throw new NotValidBookException(PRICE_MUST_BE_POSITIVE);
}
}
}
@@ -0,0 +1,108 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.converter;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
@DisplayName("BookConverter Unit Tests")
class BookConverterTest {
@Nested
@DisplayName("toDomain() method tests")
class ToDomainTests {
@Test
@DisplayName("Should convert BookInfo to Book domain object")
void shouldConvertBookInfoToDomain() {
BookInfo bookInfo = new BookInfo(
"9782016289308",
"Le Petit Prince",
"Antoine de Saint-Exupéry",
"Gallimard",
LocalDate.of(1943, 4, 6),
new BigDecimal("12.90"),
10,
List.of("Roman", "Jeunesse"),
"Un classique",
"FR"
);
Book result = BookConverter.toDomain(bookInfo);
assertNotNull(result);
assertEquals(bookInfo.isbn(), result.getIsbn());
assertEquals(bookInfo.title(), result.getTitle());
assertEquals(bookInfo.author(), result.getAuthor());
assertEquals(bookInfo.publisher(), result.getPublisher());
assertEquals(bookInfo.publicationDate(), result.getPublicationDate());
assertEquals(bookInfo.price(), result.getPrice());
assertEquals(bookInfo.initialStock(), result.getStock());
assertEquals(bookInfo.categories(), result.getCategories());
assertEquals(bookInfo.description(), result.getDescription());
assertEquals(bookInfo.language(), result.getLanguage());
}
@Test
@DisplayName("Should have null ID after toDomain (set by repository)")
void shouldHaveNullIdAfterToDomain() {
BookInfo bookInfo = new BookInfo(
"9782016289308", "Titre", "Auteur", "Editeur",
LocalDate.now(), new BigDecimal("10.00"), 5,
List.of("Roman"), "Description", "FR"
);
Book result = BookConverter.toDomain(bookInfo);
assertNull(result.getId());
}
}
@Nested
@DisplayName("toDTO() method tests")
class ToDTOTests {
@Test
@DisplayName("Should convert Book domain object to BookDTO with all fields mapped correctly")
void shouldConvertBookToDTO() {
UUID id = UUID.randomUUID();
Book book = Book.builder()
.id(id)
.isbn("9782016289308")
.title("Le Petit Prince")
.author("Antoine de Saint-Exupéry")
.publisher("Gallimard")
.publicationDate(LocalDate.of(1943, 4, 6))
.price(new BigDecimal("12.90"))
.stock(10)
.categories(List.of("Roman", "Jeunesse"))
.description("Un classique")
.language("FR")
.build();
BookDTO result = BookConverter.toDTO(book);
assertNotNull(result);
assertEquals(book.getId(), result.getId());
assertEquals(book.getIsbn(), result.getIsbn());
assertEquals(book.getTitle(), result.getTitle());
assertEquals(book.getAuthor(), result.getAuthor());
assertEquals(book.getPublisher(), result.getPublisher());
assertEquals(book.getPublicationDate(), result.getPublicationDate());
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());
}
}
}
@@ -0,0 +1,71 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
class BookTest {
@Test
@DisplayName("Builder should create a valid Book instance")
void testBookBuilder() {
UUID id = UUID.randomUUID();
Book book = Book.builder()
.id(id)
.isbn("9782016289308")
.title("Le Petit Prince")
.author("Antoine de Saint-Exupéry")
.publisher("Gallimard")
.publicationDate(LocalDate.of(1943, 4, 6))
.price(new BigDecimal("12.90"))
.stock(10)
.categories(List.of("Roman", "Jeunesse"))
.description("Un classique")
.language("FR")
.build();
assertEquals(id, book.getId());
assertEquals("9782016289308", book.getIsbn());
assertEquals("Le Petit Prince", book.getTitle());
assertEquals("Antoine de Saint-Exupéry", book.getAuthor());
assertEquals("Gallimard", book.getPublisher());
assertEquals(LocalDate.of(1943, 4, 6), book.getPublicationDate());
assertEquals(new BigDecimal("12.90"), book.getPrice());
assertEquals(10, book.getStock());
assertEquals(List.of("Roman", "Jeunesse"), book.getCategories());
assertEquals("Un classique", book.getDescription());
assertEquals("FR", book.getLanguage());
}
@Test
@DisplayName("setRandomUUID should set a new non-null UUID")
void testSetRandomUUID() {
Book book = Book.builder().build();
UUID originalId = book.getId();
book.setRandomUUID();
assertNotNull(book.getId());
assertNotEquals(originalId, book.getId());
}
@Test
@DisplayName("Two setRandomUUID calls should produce different UUIDs")
void testSetRandomUUIDTwice() {
Book book = Book.builder().build();
book.setRandomUUID();
UUID firstId = book.getId();
book.setRandomUUID();
UUID secondId = book.getId();
assertNotEquals(firstId, secondId);
}
}
@@ -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 java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class BookNotFoundExceptionTest {
@Test
@DisplayName("Exception message should contain the UUID provided")
void testExceptionMessageContainsUUID() {
UUID uuid = UUID.randomUUID();
BookNotFoundException exception = new BookNotFoundException(uuid);
String expectedMessage = String.format("The book with id %s does not exist", uuid);
assertEquals(expectedMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should use the correct constant message format")
void testExceptionUsesConstantMessageFormat() {
UUID uuid = UUID.randomUUID();
BookNotFoundException exception = new BookNotFoundException(uuid);
assertEquals("The book with id {0} does not exist",
BookNotFoundException.THE_BOOK_WITH_ID_DOES_NOT_EXIST_MESSAGE);
assertTrue(exception.getMessage().contains(uuid.toString()));
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
UUID uuid = UUID.randomUUID();
try {
throw new BookNotFoundException(uuid);
} catch (BookNotFoundException e) {
String expectedMessage = String.format("The book with id %s does not exist", uuid);
assertEquals(expectedMessage, e.getMessage());
}
}
}
@@ -0,0 +1,62 @@
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;
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 not valid",
"Title cannot be blank",
"Author cannot be blank",
"Publisher cannot be blank",
"Price must be positive"
})
@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 = "ISBN is not valid";
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 = "Price must be positive";
try {
throw new NotValidBookException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidBookException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -0,0 +1,219 @@
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.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
class BookRepositoryTest {
private BookRepository repository;
private Book book1;
private Book book2;
@BeforeEach
void setUp() {
repository = new BookRepository();
book1 = Book.builder()
.isbn("9782016289308")
.title("Le Petit Prince")
.author("Antoine de Saint-Exupéry")
.publisher("Gallimard")
.publicationDate(LocalDate.of(1943, 4, 6))
.price(new BigDecimal("12.90"))
.stock(10)
.categories(List.of("Roman"))
.description("Un classique")
.language("FR")
.build();
book1.setRandomUUID();
book2 = Book.builder()
.isbn("9782070409189")
.title("L'Étranger")
.author("Albert Camus")
.publisher("Gallimard")
.publicationDate(LocalDate.of(1942, 5, 19))
.price(new BigDecimal("9.50"))
.stock(5)
.categories(List.of("Roman"))
.description("Roman philosophique")
.language("FR")
.build();
book2.setRandomUUID();
}
@Test
@DisplayName("New repository should be empty")
void testNewRepositoryIsEmpty() {
assertTrue(repository.findAll().isEmpty());
assertEquals(0, repository.findAll().size());
}
@Nested
@DisplayName("Save operations")
class SaveOperations {
@Test
@DisplayName("Save should add a new book")
void testSaveNewBook() {
Book saved = repository.save(book1);
assertEquals(1, repository.findAll().size());
assertEquals(book1.getId(), saved.getId());
assertEquals(book1.getIsbn(), saved.getIsbn());
}
@Test
@DisplayName("Save should update existing book with same ID")
void testSaveUpdatesExistingBook() {
repository.save(book1);
UUID id = book1.getId();
Book updatedBook = Book.builder()
.id(id)
.isbn("9782016289308")
.title("Le Petit Prince - Edition collector")
.author("Antoine de Saint-Exupéry")
.publisher("Gallimard")
.publicationDate(LocalDate.of(1943, 4, 6))
.price(new BigDecimal("19.90"))
.stock(3)
.categories(List.of("Roman"))
.description("Edition collector")
.language("FR")
.build();
Book saved = repository.save(updatedBook);
assertEquals(1, repository.findAll().size());
assertEquals(id, saved.getId());
assertEquals("Le Petit Prince - Edition collector", saved.getTitle());
assertEquals(new BigDecimal("19.90"), saved.getPrice());
}
@Test
@DisplayName("Save multiple books should add all of them")
void testSaveMultipleBooks() {
repository.save(book1);
repository.save(book2);
assertEquals(2, repository.findAll().size());
assertTrue(repository.findAll().contains(book1));
assertTrue(repository.findAll().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() {
assertEquals(2, repository.findAll().size());
}
@Test
@DisplayName("FindById should return book with matching ID")
void testFindById() {
Optional<Book> found = repository.findById(book1.getId());
assertTrue(found.isPresent());
assertEquals(book1.getIsbn(), found.get().getIsbn());
assertEquals(book1.getTitle(), found.get().getTitle());
}
@Test
@DisplayName("FindById should return empty Optional when ID doesn't exist")
void testFindByIdNotFound() {
Optional<Book> found = repository.findById(UUID.randomUUID());
assertTrue(found.isEmpty());
}
@Test
@DisplayName("FindByIsbn should return book with matching ISBN")
void testFindByIsbn() {
Optional<Book> found = repository.findByIsbn("9782016289308");
assertTrue(found.isPresent());
assertEquals(book1.getId(), found.get().getId());
}
@Test
@DisplayName("FindByIsbn should return empty Optional when ISBN doesn't exist")
void testFindByIsbnNotFound() {
Optional<Book> found = repository.findByIsbn("0000000000000");
assertTrue(found.isEmpty());
}
@Test
@DisplayName("ExistsById should return true when ID exists")
void testExistsByIdExists() {
assertTrue(repository.existsById(book1.getId()));
}
@Test
@DisplayName("ExistsById should return false when ID doesn't exist")
void testExistsByIdNotExists() {
assertFalse(repository.existsById(UUID.randomUUID()));
}
}
@Nested
@DisplayName("Delete operations")
class DeleteOperations {
@BeforeEach
void setUpBooks() {
repository.save(book1);
repository.save(book2);
}
@Test
@DisplayName("Delete should remove the specified book")
void testDelete() {
repository.delete(book1);
assertEquals(1, repository.findAll().size());
assertFalse(repository.findAll().contains(book1));
assertTrue(repository.findAll().contains(book2));
}
@Test
@DisplayName("DeleteAll should remove all books")
void testDeleteAll() {
repository.deleteAll();
assertTrue(repository.findAll().isEmpty());
}
@Test
@DisplayName("Delete should not throw exception when book doesn't exist")
void testDeleteNonExistentBook() {
Book nonExistent = Book.builder().isbn("0000000000000").build();
nonExistent.setRandomUUID();
assertDoesNotThrow(() -> repository.delete(nonExistent));
assertEquals(2, repository.findAll().size());
}
}
}
@@ -0,0 +1,231 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.BookNotFoundException;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository;
import 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.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class BookUseCaseTest {
@Mock
private BookRepository bookRepository;
@InjectMocks
private BookUseCase bookUseCase;
private UUID bookId;
private Book testBook;
private BookInfo validBookInfo;
@BeforeEach
void setUp() {
bookId = UUID.randomUUID();
testBook = Book.builder()
.id(bookId)
.isbn("9782016289308")
.title("Le Petit Prince")
.author("Antoine de Saint-Exupéry")
.publisher("Gallimard")
.publicationDate(LocalDate.of(1943, 4, 6))
.price(new BigDecimal("12.90"))
.stock(10)
.categories(List.of("Roman", "Jeunesse"))
.description("Un classique")
.language("FR")
.build();
validBookInfo = new BookInfo(
"9782016289308",
"Le Petit Prince",
"Antoine de Saint-Exupéry",
"Gallimard",
LocalDate.of(1943, 4, 6),
new BigDecimal("12.90"),
10,
List.of("Roman", "Jeunesse"),
"Un classique",
"FR"
);
}
@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);
assertNotNull(registeredIsbn);
assertEquals("9782016289308", 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(
"",
"",
"",
"",
null,
BigDecimal.ZERO,
-1,
null,
"",
""
);
assertThrows(NotValidBookException.class,
() -> bookUseCase.registerBook(invalidBookInfo));
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("9782016289308")).thenReturn(Optional.of(testBook));
Optional<BookDTO> foundBook = bookUseCase.findBookByIsbn("9782016289308");
assertTrue(foundBook.isPresent());
assertEquals(testBook.getId(), foundBook.get().getId());
assertEquals(testBook.getTitle(), foundBook.get().getTitle());
verify(bookRepository, times(1)).findByIsbn("9782016289308");
}
@Test
@DisplayName("Should return empty Optional when ISBN doesn't exist")
void testFindBookByIsbnNotFound() {
when(bookRepository.findByIsbn("9999999999999")).thenReturn(Optional.empty());
Optional<BookDTO> foundBook = bookUseCase.findBookByIsbn("9999999999999");
assertTrue(foundBook.isEmpty());
verify(bookRepository, times(1)).findByIsbn("9999999999999");
}
}
@Nested
@DisplayName("Update book tests")
class UpdateBookTests {
@Test
@DisplayName("Should update book when valid data is provided")
void testUpdateBookWithValidData() throws BookNotFoundException, NotValidBookException {
when(bookRepository.findById(bookId)).thenReturn(Optional.of(testBook));
Book updatedBook = Book.builder()
.id(bookId)
.isbn("9782070409189")
.title("L'Étranger")
.author("Albert Camus")
.publisher("Gallimard")
.publicationDate(LocalDate.of(1942, 5, 19))
.price(new BigDecimal("9.50"))
.stock(10)
.categories(List.of("Roman"))
.description("Roman philosophique")
.language("FR")
.build();
when(bookRepository.save(any(Book.class))).thenReturn(updatedBook);
BookInfo updateInfo = new BookInfo(
"9782070409189",
"L'Étranger",
"Albert Camus",
"Gallimard",
LocalDate.of(1942, 5, 19),
new BigDecimal("9.50"),
99,
List.of("Roman"),
"Roman philosophique",
"FR"
);
BookDTO result = bookUseCase.updateBook(bookId, updateInfo);
assertNotNull(result);
assertEquals(bookId, result.getId());
assertEquals("L'Étranger", result.getTitle());
assertEquals("9782070409189", result.getIsbn());
assertEquals(10, result.getStock());
verify(bookRepository, times(1)).findById(bookId);
verify(bookRepository, times(1)).save(any(Book.class));
}
@Test
@DisplayName("Should throw exception when book ID doesn't exist")
void testUpdateBookNotFound() {
UUID nonExistentId = UUID.randomUUID();
when(bookRepository.findById(nonExistentId)).thenReturn(Optional.empty());
assertThrows(BookNotFoundException.class,
() -> bookUseCase.updateBook(nonExistentId, validBookInfo));
verify(bookRepository, times(1)).findById(nonExistentId);
verify(bookRepository, never()).save(any(Book.class));
}
}
@Nested
@DisplayName("Delete book tests")
class DeleteBookTests {
@Test
@DisplayName("Should delete book when ID exists")
void testDeleteBook() throws BookNotFoundException {
when(bookRepository.findById(bookId)).thenReturn(Optional.of(testBook));
assertDoesNotThrow(() -> bookUseCase.deleteBook(bookId));
verify(bookRepository, times(1)).findById(bookId);
verify(bookRepository, times(1)).delete(testBook);
}
@Test
@DisplayName("Should throw exception when deleting unknown book")
void testDeleteBookNotFound() {
UUID nonExistentId = UUID.randomUUID();
when(bookRepository.findById(nonExistentId)).thenReturn(Optional.empty());
assertThrows(BookNotFoundException.class,
() -> bookUseCase.deleteBook(nonExistentId));
verify(bookRepository, times(1)).findById(nonExistentId);
verify(bookRepository, never()).delete(any(Book.class));
}
}
}
@@ -0,0 +1,184 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.validator;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
import 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.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class BookValidatorTest {
private BookInfo validBook() {
return new BookInfo(
"9782016289308",
"Le Petit Prince",
"Antoine de Saint-Exupéry",
"Gallimard",
LocalDate.of(1943, 4, 6),
new BigDecimal("12.90"),
10,
List.of("Roman"),
"Un classique",
"FR"
);
}
@Test
@DisplayName("Should validate book with valid data")
void testValidateValidBook() {
assertDoesNotThrow(() -> BookValidator.validate(validBook()));
}
@Nested
@DisplayName("ISBN validation tests")
class IsbnValidationTests {
@Test
@DisplayName("Should throw exception when ISBN is blank")
void testValidateBlankIsbn() {
BookInfo book = new BookInfo("", "Titre", "Auteur", "Editeur",
LocalDate.now(), new BigDecimal("10.00"), 1, List.of("Roman"), "Desc", "FR");
NotValidBookException exception = assertThrows(NotValidBookException.class,
() -> BookValidator.validate(book));
assertEquals(BookValidator.ISBN_IS_NOT_VALID, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {"123456", "978201628930", "97820162893088", "abcdefghijklm", "978-016289308"})
@DisplayName("Should throw exception when ISBN format is invalid")
void testValidateInvalidIsbnFormat(String invalidIsbn) {
BookInfo book = new BookInfo(invalidIsbn, "Titre", "Auteur", "Editeur",
LocalDate.now(), new BigDecimal("10.00"), 1, List.of("Roman"), "Desc", "FR");
NotValidBookException exception = assertThrows(NotValidBookException.class,
() -> BookValidator.validate(book));
assertEquals(BookValidator.ISBN_IS_NOT_VALID, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {"9782016289308", "9782070409189", "9782253006329"})
@DisplayName("Should validate when ISBN has exactly 13 digits")
void testValidateValidIsbn(String validIsbn) {
BookInfo book = new BookInfo(validIsbn, "Titre", "Auteur", "Editeur",
LocalDate.now(), new BigDecimal("10.00"), 1, List.of("Roman"), "Desc", "FR");
assertDoesNotThrow(() -> BookValidator.validate(book));
}
}
@Nested
@DisplayName("Title validation tests")
class TitleValidationTests {
@Test
@DisplayName("Should throw exception when title is blank")
void testValidateBlankTitle() {
BookInfo book = new BookInfo("9782016289308", "", "Auteur", "Editeur",
LocalDate.now(), new BigDecimal("10.00"), 1, List.of("Roman"), "Desc", "FR");
NotValidBookException exception = assertThrows(NotValidBookException.class,
() -> BookValidator.validate(book));
assertEquals(BookValidator.TITLE_CANNOT_BE_BLANK, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {" ", " ", "\t", "\n"})
@DisplayName("Should throw exception when title contains only whitespace")
void testValidateWhitespaceTitle(String whitespace) {
BookInfo book = new BookInfo("9782016289308", whitespace, "Auteur", "Editeur",
LocalDate.now(), new BigDecimal("10.00"), 1, List.of("Roman"), "Desc", "FR");
NotValidBookException exception = assertThrows(NotValidBookException.class,
() -> BookValidator.validate(book));
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() {
BookInfo book = new BookInfo("9782016289308", "Titre", "", "Editeur",
LocalDate.now(), new BigDecimal("10.00"), 1, List.of("Roman"), "Desc", "FR");
NotValidBookException exception = assertThrows(NotValidBookException.class,
() -> BookValidator.validate(book));
assertEquals(BookValidator.AUTHOR_CANNOT_BE_BLANK, exception.getMessage());
}
}
@Nested
@DisplayName("Publisher validation tests")
class PublisherValidationTests {
@Test
@DisplayName("Should throw exception when publisher is blank")
void testValidateBlankPublisher() {
BookInfo book = new BookInfo("9782016289308", "Titre", "Auteur", "",
LocalDate.now(), new BigDecimal("10.00"), 1, List.of("Roman"), "Desc", "FR");
NotValidBookException exception = assertThrows(NotValidBookException.class,
() -> BookValidator.validate(book));
assertEquals(BookValidator.PUBLISHER_CANNOT_BE_BLANK, exception.getMessage());
}
}
@Nested
@DisplayName("Price validation tests")
class PriceValidationTests {
@Test
@DisplayName("Should throw exception when price is negative")
void testValidateNegativePrice() {
BookInfo book = new BookInfo("9782016289308", "Titre", "Auteur", "Editeur",
LocalDate.now(), new BigDecimal("-5.00"), 1, List.of("Roman"), "Desc", "FR");
NotValidBookException exception = assertThrows(NotValidBookException.class,
() -> BookValidator.validate(book));
assertEquals(BookValidator.PRICE_MUST_BE_POSITIVE, exception.getMessage());
}
@Test
@DisplayName("Should throw exception when price is zero")
void testValidateZeroPrice() {
BookInfo book = new BookInfo("9782016289308", "Titre", "Auteur", "Editeur",
LocalDate.now(), BigDecimal.ZERO, 1, List.of("Roman"), "Desc", "FR");
NotValidBookException exception = assertThrows(NotValidBookException.class,
() -> BookValidator.validate(book));
assertEquals(BookValidator.PRICE_MUST_BE_POSITIVE, exception.getMessage());
}
@Test
@DisplayName("Should throw exception when price is null")
void testValidateNullPrice() {
BookInfo book = new BookInfo("9782016289308", "Titre", "Auteur", "Editeur",
LocalDate.now(), null, 1, List.of("Roman"), "Desc", "FR");
NotValidBookException exception = assertThrows(NotValidBookException.class,
() -> BookValidator.validate(book));
assertEquals(BookValidator.PRICE_MUST_BE_POSITIVE, exception.getMessage());
}
}
}
+51
View File
@@ -0,0 +1,51 @@
# language: en
Feature: Manage books in the catalog
Background:
Given the catalog has the following books:
| isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue |
| 9782016289308 | Le Petit Prince | Antoine de Saint-Exupéry | Gallimard | 1943-04-06 | 12.90 | 10 | Roman;Jeunesse | Un classique | FR |
| 9782070409189 | L'Étranger | Albert Camus | Gallimard | 1942-05-19 | 9.50 | 5 | Roman | Roman philosophique | FR |
Scenario: Register a new book
When I register a new book with the following information:
| isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue |
| 9782253006329 | Harry Potter à l'école des sorciers | J.K. Rowling | Pocket | 1998-10-09 | 8.20 | 20 | Fantaisie;Jeunesse | Premier tome de la saga | FR |
Then a new book is created
And the catalog now has 3 books
And the book "9782253006329" has a stock of 20
Scenario: Retrieve a book by ISBN
When I request the book with ISBN "9782016289308"
Then I receive the following book information:
| isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue |
| 9782016289308 | Le Petit Prince | Antoine de Saint-Exupéry | Gallimard | 1943-04-06 | 12.90 | 10 | Roman;Jeunesse | Un classique | FR |
Scenario: Update a book information
When I update book "9782070409189" with the following information:
| isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue |
| 9782070409189 | L'Étranger - Edition | Albert Camus | Gallimard | 1942-05-19 | 11.00 | 99 | Roman;Classique | Nouvelle édition commentée | FR |
Then the book "9782070409189" has the following updated information:
| isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue |
| 9782070409189 | L'Étranger - Edition | Albert Camus | Gallimard | 1942-05-19 | 11.00 | 99 | Roman;Classique | Nouvelle édition commentée | FR |
And the stock remains unchanged at 5
Scenario: Delete a book
When I delete the book with ISBN "9782016289308"
Then the book "9782016289308" is removed from the catalog
And the catalog now has 1 books
Scenario: Attempt to register a book with invalid ISBN
When I try to register a new book with the following information:
| isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue |
| 123456 | Livre test | Test Auteur | TestEdit | 2024-01-01 | 5.00 | 1 | Test | ISBN incorrect | FR |
Then the book registration fails
And I receive a validation book error message containing "ISBN is not valid"
Scenario: Attempt to register a book with invalid price
When I try to register a new book with the following information:
| isbn | titre | auteur | editeur | datePublication | prix | stockInitial | categories | description | langue |
| 9782016289308 | Livre test | Test Auteur | TestEdit | 2024-01-01 | -5.00 | 1 | Test | Prix incorrect | FR |
Then the book registration fails
And I receive a validation book error message containing "Price must be positive"