This commit is contained in:
2026-04-11 23:40:02 +02:00
parent dab8a636d0
commit c7fd2e8311
5 changed files with 536 additions and 0 deletions
@@ -0,0 +1,24 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
import lombok.Builder;
import lombok.Getter;
@Builder
@Getter
public class BookDTO {
private final UUID id;
private final String isbn;
private final String title;
private final String author;
private final String publisher;
private final LocalDate publicationDate;
private final BigDecimal price;
private final int stock;
private final List<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,230 @@
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 java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class 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,212 @@
package fr.iut_fbleau.but3.dev62.mylibrary.features.book;
import static org.junit.jupiter.api.Assertions.*;
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 fr.iut_fbleau.but3.dev62.mylibrary.book.usecase.BookUseCase;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.Before;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public class BookSteps {
private final BookRepository bookRepository = new BookRepository();
private final BookUseCase bookUseCase = new BookUseCase(bookRepository);
private final Map<String, UUID> bookIsbnUUID = new HashMap<>();
private String bookRegistration;
private Optional<BookDTO> bookByIsbn;
private Book updatedBook;
private NotValidBookException notValidBookException;
@Before
public void setUp() {
bookRepository.deleteAll();
bookIsbnUUID.clear();
bookRegistration = null;
bookByIsbn = null;
updatedBook = null;
notValidBookException = null;
}
@Given("the catalog has the following books:")
public void theCatalogHasTheFollowingBooks(DataTable dataTable) {
List<Map<String, String>> books = dataTable.asMaps(String.class, String.class);
for (Map<String, String> book : books) {
String isbn = book.get("isbn");
Book newBook = Book.builder()
.isbn(isbn)
.title(book.get("titre"))
.author(book.get("auteur"))
.publisher(book.get("editeur"))
.publicationDate(LocalDate.parse(book.get("datePublication")))
.price(new BigDecimal(book.get("prix")))
.stock(Integer.parseInt(book.get("stockInitial")))
.categories(List.of(book.get("categories").split(";")))
.description(book.get("description"))
.language(book.get("langue"))
.build();
Book save = bookRepository.save(newBook);
bookIsbnUUID.put(isbn, save.getId());
}
assertEquals(books.size(), bookRepository.findAll().size());
}
@When("I register a new book with the following information:")
public void iRegisterANewBookWithTheFollowingInformation(DataTable dataTable) throws NotValidBookException {
Map<String, String> bookInfo = dataTable.asMaps(String.class, String.class).getFirst();
BookInfo newBook = new BookInfo(
bookInfo.get("isbn"),
bookInfo.get("titre"),
bookInfo.get("auteur"),
bookInfo.get("editeur"),
LocalDate.parse(bookInfo.get("datePublication")),
new BigDecimal(bookInfo.get("prix")),
Integer.parseInt(bookInfo.get("stockInitial")),
List.of(bookInfo.get("categories").split(";")),
bookInfo.get("description"),
bookInfo.get("langue")
);
bookRegistration = bookUseCase.registerBook(newBook);
}
@Then("a new book is created")
public void aNewBookIsCreated() {
assertNotNull(bookRegistration);
}
@And("the catalog now has {int} books")
public void theCatalogNowHasBooks(int numberOfBooks) {
assertEquals(numberOfBooks, bookRepository.findAll().size());
}
@And("the book {string} has a stock of {int}")
public void theBookHasAStockOf(String isbn, int stock) {
Book book = bookRepository.findByIsbn(isbn).orElseThrow();
assertEquals(stock, book.getStock());
}
@When("I request the book with ISBN {string}")
public void iRequestTheBookWithISBN(String isbn) {
bookByIsbn = bookUseCase.findBookByIsbn(isbn);
}
@Then("I receive the following book information:")
public void iReceiveTheFollowingBookInformation(DataTable dataTable) {
Map<String, String> bookInfo = dataTable.asMaps(String.class, String.class).getFirst();
assertTrue(bookByIsbn.isPresent());
BookDTO bookDTO = bookByIsbn.get();
assertEquals(bookInfo.get("isbn"), bookDTO.getIsbn());
assertEquals(bookInfo.get("titre"), bookDTO.getTitle());
assertEquals(bookInfo.get("auteur"), bookDTO.getAuthor());
assertEquals(bookInfo.get("editeur"), bookDTO.getPublisher());
assertEquals(LocalDate.parse(bookInfo.get("datePublication")), bookDTO.getPublicationDate());
assertEquals(new BigDecimal(bookInfo.get("prix")), bookDTO.getPrice());
assertEquals(Integer.parseInt(bookInfo.get("stockInitial")), bookDTO.getStock());
assertEquals(bookInfo.get("description"), bookDTO.getDescription());
assertEquals(bookInfo.get("langue"), bookDTO.getLanguage());
}
@When("I update book {string} with the following information:")
public void iUpdateBookWithTheFollowingInformation(String isbn, DataTable dataTable)
throws BookNotFoundException, NotValidBookException {
Map<String, String> bookData = dataTable.asMaps(String.class, String.class).getFirst();
BookInfo bookInfo = new BookInfo(
bookData.get("isbn"),
bookData.get("titre"),
bookData.get("auteur"),
bookData.get("editeur"),
LocalDate.parse(bookData.get("datePublication")),
new BigDecimal(bookData.get("prix")),
Integer.parseInt(bookData.get("stockInitial")),
List.of(bookData.get("categories").split(";")),
bookData.get("description"),
bookData.get("langue")
);
UUID uuid = bookIsbnUUID.get(isbn);
bookUseCase.updateBook(uuid, bookInfo);
}
@Then("the book {string} has the following updated information:")
public void theBookHasTheFollowingUpdatedInformation(String previousIsbn, DataTable dataTable) {
Map<String, String> updatedData = dataTable.asMaps(String.class, String.class).getFirst();
UUID uuid = bookIsbnUUID.get(previousIsbn);
updatedBook = bookRepository.findById(uuid).orElseThrow();
assertEquals(updatedData.get("isbn"), updatedBook.getIsbn());
assertEquals(updatedData.get("titre"), updatedBook.getTitle());
assertEquals(updatedData.get("auteur"), updatedBook.getAuthor());
assertEquals(updatedData.get("editeur"), updatedBook.getPublisher());
assertEquals(LocalDate.parse(updatedData.get("datePublication")), updatedBook.getPublicationDate());
assertEquals(new BigDecimal(updatedData.get("prix")), updatedBook.getPrice());
assertEquals(updatedData.get("description"), updatedBook.getDescription());
assertEquals(updatedData.get("langue"), updatedBook.getLanguage());
}
@And("the stock remains unchanged at {int}")
public void theStockRemainsUnchangedAt(int expectedStock) {
assertEquals(expectedStock, updatedBook.getStock());
}
@When("I delete the book with ISBN {string}")
public void iDeleteTheBookWithISBN(String isbn) throws BookNotFoundException {
UUID uuid = bookIsbnUUID.get(isbn);
bookUseCase.deleteBook(uuid);
}
@Then("the book {string} is removed from the catalog")
public void theBookIsRemovedFromTheCatalog(String isbn) {
UUID uuid = bookIsbnUUID.get(isbn);
assertFalse(bookRepository.existsById(uuid));
}
@When("I try to register a new book with the following information:")
public void iTryToRegisterANewBookWithTheFollowingInformation(DataTable dataTable) {
Map<String, String> bookInfo = dataTable.asMaps(String.class, String.class).getFirst();
BookInfo newBook = new BookInfo(
bookInfo.get("isbn"),
bookInfo.get("titre"),
bookInfo.get("auteur"),
bookInfo.get("editeur"),
LocalDate.parse(bookInfo.get("datePublication")),
new BigDecimal(bookInfo.get("prix")),
Integer.parseInt(bookInfo.get("stockInitial")),
List.of(bookInfo.get("categories").split(";")),
bookInfo.get("description"),
bookInfo.get("langue")
);
notValidBookException = assertThrows(NotValidBookException.class,
() -> bookUseCase.registerBook(newBook));
}
@Then("the book registration fails")
public void theBookRegistrationFails() {
assertNotNull(notValidBookException);
}
@And("I receive a validation book error message containing {string}")
public void iReceiveAValidationBookErrorMessageContaining(String errorMessage) {
assertEquals(errorMessage, notValidBookException.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"