ajout du scenario cucumber et du README
This commit is contained in:
+106
@@ -0,0 +1,106 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.features.book;
|
||||
|
||||
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 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.Category;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.BookAlreadyExistsException;
|
||||
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.en.And;
|
||||
import io.cucumber.java.en.Given;
|
||||
import io.cucumber.java.en.Then;
|
||||
import io.cucumber.java.en.When;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BookSteps {
|
||||
|
||||
private final BookRepository bookRepository = new BookRepository();
|
||||
private final BookUseCase bookUseCase = new BookUseCase(bookRepository);
|
||||
|
||||
private long lastRegisteredIsbn;
|
||||
private BookDTO retrievedBook;
|
||||
private List<BookDTO> allBooks;
|
||||
private Exception lastException;
|
||||
|
||||
@Given("the catalog has the following books:")
|
||||
public void theCatalogHasTheFollowingBooks(DataTable dataTable) throws NotValidBookException, BookAlreadyExistsException {
|
||||
bookRepository.deleteAll();
|
||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
||||
bookUseCase.registerBook(toBookInfo(row));
|
||||
}
|
||||
}
|
||||
|
||||
@When("I register a new book with the following information:")
|
||||
public void iRegisterANewBook(DataTable dataTable) throws NotValidBookException, BookAlreadyExistsException {
|
||||
Map<String, String> row = dataTable.asMaps(String.class, String.class).getFirst();
|
||||
lastRegisteredIsbn = bookUseCase.registerBook(toBookInfo(row));
|
||||
}
|
||||
|
||||
@When("I try to register a new book with the following information:")
|
||||
public void iTryToRegisterANewBook(DataTable dataTable) {
|
||||
Map<String, String> row = dataTable.asMaps(String.class, String.class).getFirst();
|
||||
lastException = assertThrows(Exception.class, () -> bookUseCase.registerBook(toBookInfo(row)));
|
||||
}
|
||||
|
||||
@When("I request the book with isbn {long}")
|
||||
public void iRequestTheBook(long isbn) throws BookNotFoundException {
|
||||
retrievedBook = bookUseCase.getBookByIsbn(isbn);
|
||||
}
|
||||
|
||||
@When("I list all books")
|
||||
public void iListAllBooks() {
|
||||
allBooks = bookUseCase.getAllBooks();
|
||||
}
|
||||
|
||||
@Then("the book is created")
|
||||
public void theBookIsCreated() {
|
||||
assertNotNull(lastRegisteredIsbn);
|
||||
}
|
||||
|
||||
@And("the catalog now has {int} book(s)")
|
||||
public void theCatalogNowHasNBooks(int expected) {
|
||||
assertEquals(expected, bookRepository.findAll().size());
|
||||
}
|
||||
|
||||
@Then("the registration fails with a {string}")
|
||||
public void theRegistrationFailsWith(String exceptionSimpleName) {
|
||||
assertNotNull(lastException);
|
||||
assertEquals(exceptionSimpleName, lastException.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
@Then("I receive a book whose title is {string}")
|
||||
public void iReceiveABookWhoseTitleIs(String title) {
|
||||
assertNotNull(retrievedBook);
|
||||
assertEquals(title, retrievedBook.getTitle());
|
||||
}
|
||||
|
||||
@Then("I receive {int} book(s)")
|
||||
public void iReceiveNBooks(int expected) {
|
||||
assertNotNull(allBooks);
|
||||
assertEquals(expected, allBooks.size());
|
||||
}
|
||||
|
||||
private static BookInfo toBookInfo(Map<String, String> row) {
|
||||
return new BookInfo(
|
||||
Long.parseLong(row.get("isbn")),
|
||||
row.get("titre"),
|
||||
row.get("auteur"),
|
||||
row.get("editeur"),
|
||||
LocalDate.parse(row.get("datePublication")),
|
||||
Double.parseDouble(row.get("prix")),
|
||||
Integer.parseInt(row.get("stock")),
|
||||
List.of(Category.valueOf(row.get("categorie"))),
|
||||
"",
|
||||
row.get("langue")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
# language: en
|
||||
|
||||
Feature: Manage book catalog
|
||||
|
||||
Scenario: Register a new book in the catalog
|
||||
When I register a new book with the following information:
|
||||
| isbn | titre | auteur | editeur | datePublication | prix | stock | categorie | langue |
|
||||
| 9780321125217 | DDD | Evans | AW | 2003-08-22 | 54.99 | 10 | SCIENCE | EN |
|
||||
Then the book is created
|
||||
And the catalog now has 1 book
|
||||
|
||||
Scenario: Reject duplicate ISBN
|
||||
Given the catalog has the following books:
|
||||
| isbn | titre | auteur | editeur | datePublication | prix | stock | categorie | langue |
|
||||
| 9780321125217 | DDD | Evans | AW | 2003-08-22 | 54.99 | 10 | SCIENCE | EN |
|
||||
When I try to register a new book with the following information:
|
||||
| isbn | titre | auteur | editeur | datePublication | prix | stock | categorie | langue |
|
||||
| 9780321125217 | DDD copy | Evans | AW | 2003-08-22 | 54.99 | 10 | SCIENCE | EN |
|
||||
Then the registration fails with a "BookAlreadyExistsException"
|
||||
|
||||
Scenario: Reject invalid book information
|
||||
When I try to register a new book with the following information:
|
||||
| isbn | titre | auteur | editeur | datePublication | prix | stock | categorie | langue |
|
||||
| 9780321125217 | | Evans | AW | 2003-08-22 | 0 | -1 | SCIENCE | EN |
|
||||
Then the registration fails with a "NotValidBookException"
|
||||
|
||||
Scenario: Retrieve a book by its ISBN
|
||||
Given the catalog has the following books:
|
||||
| isbn | titre | auteur | editeur | datePublication | prix | stock | categorie | langue |
|
||||
| 9780321125217 | DDD | Evans | AW | 2003-08-22 | 54.99 | 10 | SCIENCE | EN |
|
||||
When I request the book with isbn 9780321125217
|
||||
Then I receive a book whose title is "DDD"
|
||||
|
||||
Scenario: List all books in the catalog
|
||||
Given the catalog has the following books:
|
||||
| isbn | titre | auteur | editeur | datePublication | prix | stock | categorie | langue |
|
||||
| 9780321125217 | DDD | Evans | AW | 2003-08-22 | 54.99 | 10 | SCIENCE | EN |
|
||||
| 9780132350884 | Clean | Martin | PH | 2008-08-01 | 30.00 | 5 | SCIENCE | EN |
|
||||
When I list all books
|
||||
Then I receive 2 books
|
||||
Reference in New Issue
Block a user