step book

This commit is contained in:
2025-06-12 22:01:51 +02:00
parent 76f2145629
commit a27dba9ad2
2 changed files with 186 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
package fr.iut_fbleau.but3.dev62.mylibrary.features.client; package fr.iut_fbleau.but3.dev62.mylibrary.features.book;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -19,4 +19,180 @@ import java.util.UUID;
public class BookSteps { public class BookSteps {
private final BookRepository bookRepository = new BookRepository();
private final BookUseCase bookUseCase = new BookUseCase(bookRepository);
private final Map<Long, BookDto> existingBooks = new HashMap<>();
private BookDto registeredBook;
private ExceptionDto errorResponse;
// ----------------------------Background---------------------------------
@Given("the system has the following books:")
public void theSystemHasTheFollowingBooks(DataTable dataTable) {
bookRepository.deleteAll();
List<Map<String, String>> books = dataTable.asMaps(String.class, String.class);
for (Map<String, String> row : books) {
BookDto book = new BookDto(
Long.parseLong(row.get("isbn")),
row.get("title"),
row.get("author"),
row.get("publisher"),
LocalDate.parse(row.get("publicationDate")),
Double.parseDouble(row.get("price")),
Integer.parseInt(row.get("quantity")),
List.of("FICTION"),
null,
row.get("language")
);
bookRepository.save(book);
existingBooks.put(book.getIsbn(), book);
}
assertEquals(books.size(), bookRepository.findAll().size());
}
// -------------------------Scénario 1-----------------------
@When("I register a new book with the following information:")
public void iRegisterANewBook(DataTable dataTable) {
Map<String, String> data = dataTable.asMaps().getFirst();
BookDto book = new BookDto(
Long.parseLong(data.get("isbn")),
data.get("title"),
data.get("author"),
data.get("publisher"),
LocalDate.parse(data.get("publicationDate")),
Double.parseDouble(data.get("price")),
Integer.parseInt(data.get("quantity")),
List.of("FICTION"),
null,
data.get("language")
);
registeredBook = bookUseCase.registerBook(book);
}
@Then("the book is successfully registered")
public void theBookIsSuccessfullyRegistered() {
assertNotNull(registeredBook);
assertTrue(bookRepository.existsByIsbn(registeredBook.getIsbn()));
}
@And("the system now has {int} books")
public void theSystemNowHasBooks(int expected) {
assertEquals(expected, bookRepository.findAll().size());
}
// -------------------------Scénario 2----------------------
@When("I request all books")
public void iRequestAllBooks() {
List<BookDto> allBooks = bookUseCase.getAllBooks();
existingBooks.clear();
for (BookDto book : allBooks) {
existingBooks.put(book.getIsbn(), book);
}
}
@Then("I receive the following books:")
public void iReceiveTheFollowingBooks(DataTable dataTable) {
List<Map<String, String>> rows = dataTable.asMaps();
for (Map<String, String> row : rows) {
long isbn = Long.parseLong(row.get("isbn"));
assertTrue(existingBooks.containsKey(isbn));
}
}
// -------------------------Scénario 3---------------------
@When("I request the book with id {long}")
public void iRequestTheBookWithId(long isbn) {
try {
registeredBook = bookUseCase.getBookByIsbn(isbn);
} catch (BookNotFoundException e) {
errorResponse = new ExceptionDto("BookNotFoundException", e.getMessage());
}
}
@Then("I receive the following book information:")
public void iReceiveTheFollowingBookInformation(DataTable dataTable) {
Map<String, String> expected = dataTable.asMaps().getFirst();
assertEquals(expected.get("isbn"), String.valueOf(registeredBook.getIsbn()));
assertEquals(expected.get("title"), registeredBook.getTitle());
assertEquals(expected.get("author"), registeredBook.getAuthor());
assertEquals(expected.get("publisher"), registeredBook.getPublisher());
assertEquals(expected.get("publicationDate"), registeredBook.getPublicationDate().toString());
assertEquals(expected.get("price"), String.valueOf(registeredBook.getPrice()));
assertEquals(expected.get("quantity"), String.valueOf(registeredBook.getQuantity()));
assertEquals(expected.get("language"), registeredBook.getLanguage());
}
// -------------------------Scénario 4--------------------
@When("I try to register a new book with the following information:")
public void iTryToRegisterANewBookWithInvalidData(DataTable dataTable) {
try {
Map<String, String> data = dataTable.asMaps().getFirst();
BookDto book = new BookDto(
data.get("isbn") != null && !data.get("isbn").isEmpty() ? Long.parseLong(data.get("isbn")) : null,
data.get("title"),
data.get("author"),
data.get("publisher"),
data.get("publicationDate") != null && !data.get("publicationDate").isEmpty() ? LocalDate.parse(data.get("publicationDate")) : null,
data.get("price") != null && !data.get("price").isEmpty() ? Double.parseDouble(data.get("price")) : null,
data.get("quantity") != null && !data.get("quantity").isEmpty() ? Integer.parseInt(data.get("quantity")) : null,
List.of(), null, data.get("language")
);
bookUseCase.registerBook(book);
} catch (InvalidBookDataException e) {
errorResponse = new ExceptionDto("InvalidBookDataException", e.getMessage());
}
}
@Then("the registration fails")
public void theRegistrationFails() {
assertNotNull(errorResponse);
}
// -------------------------Scénario 5--------------------
@When("I try to register a new book with an existing ISBN")
public void iTryToRegisterABookWithExistingIsbn(DataTable dataTable) {
try {
Map<String, String> data = dataTable.asMaps().getFirst();
BookDto book = new BookDto(
Long.parseLong(data.get("isbn")),
data.get("title"),
data.get("author"),
data.get("publisher"),
LocalDate.parse(data.get("publicationDate")),
Double.parseDouble(data.get("price")),
Integer.parseInt(data.get("quantity")),
List.of("FICTION"), null, data.get("language")
);
bookUseCase.registerBook(book);
} catch (BookAlreadyExistsException e) {
errorResponse = new ExceptionDto("BookAlreadyExistsException", e.getMessage());
}
}
// ------------------------Scénario 6----------------------
@When("I request the book with id {int}")
public void iRequestBookWithUnknownId(int isbn) {
try {
bookUseCase.getBookByIsbn((long) isbn);
} catch (BookNotFoundException e) {
errorResponse = new ExceptionDto("BookNotFoundException", e.getMessage());
}
}
// -------------------------Scénario 7--------------------
@When("I request all books with title {string}")
public void iRequestAllBooksWithTitle(String title) {
List<BookDto> filtered = bookUseCase.searchBooksByTitle(title);
assertTrue(filtered.isEmpty());
}
// -------------------------Scénario 8--------------------
@Then("I receive an empty list of books")
public void iReceiveAnEmptyListOfBooks() {
assertTrue(bookUseCase.getLastSearchResults().isEmpty());
}
// -----------Utilser dans plusieurs scénarios----------------
@And("the system still has {int} books")
public void theSystemStillHasBooks(int count) {
assertEquals(count, bookRepository.findAll().size());
}
@And("I receive an error message containing {string}")
public void iReceiveAnErrorMessageContaining(String errorMessage) {
assertTrue(errorResponse.getErrorMessage().contains(errorMessage));
}
} }

View File

@@ -2,12 +2,15 @@
Feature: Manage books Feature: Manage books
# ----------------------------Background---------------------------------
Background: Background:
Given the system has the following books: Given the system has the following books:
| isbn | title | author | publisher | publicationDate | price | quantity | language | | isbn | title | author | publisher | publicationDate | price | quantity | language |
| 978123456 | The Odyssey | Homer | Penguin | 2000-01-01 | 10.0 | 5 | EN | | 978123456 | The Odyssey | Homer | Penguin | 2000-01-01 | 10.0 | 5 | EN |
| 978654321 | War and Peace | Leo Tolstoy | Vintage | 2005-05-10 | 15.0 | 2 | EN | | 978654321 | War and Peace | Leo Tolstoy | Vintage | 2005-05-10 | 15.0 | 2 | EN |
# ----------------------------Scénario 1---------------------------------
Scenario: Register a new book Scenario: Register a new book
When I register a new book with the following information: When I register a new book with the following information:
| isbn | title | author | publisher | publicationDate | price | quantity | language | | isbn | title | author | publisher | publicationDate | price | quantity | language |
@@ -15,6 +18,7 @@ Feature: Manage books
Then the book is successfully registered Then the book is successfully registered
And the system now has 3 books And the system now has 3 books
# ----------------------------Scénario 2---------------------------------
Scenario: Get all books Scenario: Get all books
When I request all books When I request all books
Then I receive the following books: Then I receive the following books:
@@ -22,12 +26,14 @@ Feature: Manage books
| 978123456 | The Odyssey | Homer | Penguin | 2000-01-01 | 10.0 | 5 | EN | | 978123456 | The Odyssey | Homer | Penguin | 2000-01-01 | 10.0 | 5 | EN |
| 978654321 | War and Peace | Leo Tolstoy | Vintage | 2005-05-10 | 15.0 | 2 | EN | | 978654321 | War and Peace | Leo Tolstoy | Vintage | 2005-05-10 | 15.0 | 2 | EN |
# ----------------------------Scénario 3---------------------------------
Scenario: Get a book by ID Scenario: Get a book by ID
When I request the book with id 978123456 When I request the book with id 978123456
Then I receive the following book information: Then I receive the following book information:
| isbn | title | author | publisher | publicationDate | price | quantity | language | | isbn | title | author | publisher | publicationDate | price | quantity | language |
| 978123456 | The Odyssey | Homer | Penguin | 2000-01-01 | 10.0 | 5 | EN | | 978123456 | The Odyssey | Homer | Penguin | 2000-01-01 | 10.0 | 5 | EN |
# ----------------------------Scénario 4---------------------------------
Scenario: Attempt to register a book with invalid data Scenario: Attempt to register a book with invalid data
When I try to register a new book with the following information: When I try to register a new book with the following information:
| isbn | title | author | publisher | publicationDate | price | quantity | language | | isbn | title | author | publisher | publicationDate | price | quantity | language |
@@ -36,6 +42,7 @@ Feature: Manage books
And I receive an error message containing "Invalid book data provided" And I receive an error message containing "Invalid book data provided"
And the system still has 2 books And the system still has 2 books
# ----------------------------Scénario 5---------------------------------
Scenario: Attempt to register a book with an existing ISBN Scenario: Attempt to register a book with an existing ISBN
When I try to register a new book with the following information: When I try to register a new book with the following information:
| isbn | title | author | publisher | publicationDate | price | quantity | language | | isbn | title | author | publisher | publicationDate | price | quantity | language |
@@ -44,11 +51,13 @@ Feature: Manage books
And I receive an error message containing "Conflict with existing book in database" And I receive an error message containing "Conflict with existing book in database"
And the system still has 2 books And the system still has 2 books
# ----------------------------Scénario 6---------------------------------
Scenario: Attempt to get a book with unknown ID Scenario: Attempt to get a book with unknown ID
When I request the book with id 999999999 When I request the book with id 999999999
Then the request fails Then the request fails
And I receive an error message containing "Book not found" And I receive an error message containing "Book not found"
# ----------------------------Scénario 7---------------------------------
Scenario: Attempt to get all books with a filter that matches nothing Scenario: Attempt to get all books with a filter that matches nothing
When I request all books with title "Nonexistent Book" When I request all books with title "Nonexistent Book"
Then I receive an empty list of books Then I receive an empty list of books