diff --git a/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/BookTest.java b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/BookTest.java new file mode 100644 index 0000000..91b012d --- /dev/null +++ b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/BookTest.java @@ -0,0 +1,75 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.book.entity; + +import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookQuantityException; +import java.time.LocalDate; +import java.util.List; +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.*; + +class BookTest { + + @Test + @DisplayName("Builder should create a valid Book instance") + void testBookBuilder() { + long isbn = 9780321125217L; + Book book = Book.builder() + .isbn(isbn) + .title("Domain-Driven Design") + .author("Eric Evans") + .publisher("Addison-Wesley") + .publicationDate(LocalDate.of(2003, 8, 22)) + .price(54.99) + .quantity(10) + .categories(List.of(Category.SCIENCE, Category.REFERENCE)) + .description("Tackling complexity in the heart of software") + .language("EN") + .build(); + + assertEquals(isbn, book.getIsbn()); + assertEquals("Domain-Driven Design", book.getTitle()); + assertEquals("Eric Evans", book.getAuthor()); + assertEquals("Addison-Wesley", book.getPublisher()); + assertEquals(LocalDate.of(2003, 8, 22), book.getPublicationDate()); + assertEquals(54.99, book.getPrice()); + assertEquals(10, book.getQuantity()); + assertEquals(List.of(Category.SCIENCE, Category.REFERENCE), book.getCategories()); + assertEquals("EN", book.getLanguage()); + } + + @Nested + @DisplayName("Stock management") + class StockTests { + + @Test + @DisplayName("addStock should increase the quantity") + void testAddStock() { + Book book = Book.builder().quantity(5).build(); + book.addStock(7); + assertEquals(12, book.getQuantity()); + } + + @Test + @DisplayName("removeStock should decrease the quantity") + void testRemoveStock() throws IllegalBookQuantityException { + Book book = Book.builder().quantity(5).build(); + book.removeStock(3); + assertEquals(2, book.getQuantity()); + } + + @Test + @DisplayName("removeStock should throw when removing more than available") + void testRemoveTooMuchStock() { + Book book = Book.builder().quantity(5).build(); + IllegalBookQuantityException exception = assertThrows( + IllegalBookQuantityException.class, + () -> book.removeStock(10) + ); + assertEquals(5, book.getQuantity()); + assertTrue(exception.getMessage().contains("10")); + assertTrue(exception.getMessage().contains("5")); + } + } +}