forked from pierront/mylibrary-template
book Exeception
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.features.book;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.error.bookErrorTest;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.function.bookFunctionTest;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.result.bookResultTest;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.*;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -13,49 +12,55 @@ import java.util.*;
|
||||
|
||||
public class BookSteps {
|
||||
|
||||
private final Map<String, Map<String, String>> books = new LinkedHashMap<>();
|
||||
|
||||
private List<Map<String, String>> lastBookResult;
|
||||
private Map<String, String> lastSingleBookResult;
|
||||
private final BookManagement bookManagement = new BookManagement();
|
||||
private List<Book> lastBookResult;
|
||||
private Book lastSingleBookResult;
|
||||
private String lastErrorMessage;
|
||||
private boolean lastOperationSuccess;
|
||||
|
||||
// ----------------------------Background--------------------------------
|
||||
// ----------------------------Background--------------------------------
|
||||
@Given("the system has the following books:")
|
||||
public void theSystemHasTheFollowingBooks(DataTable dataTable) {
|
||||
books.clear();
|
||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
||||
books.put(row.get("isbn"), new HashMap<>(row));
|
||||
try {
|
||||
Book book = new Book(
|
||||
row.get("isbn"),
|
||||
row.get("title"),
|
||||
row.get("author"),
|
||||
row.get("publisher"),
|
||||
row.get("publicationDate"),
|
||||
row.get("price"),
|
||||
row.get("quantity"),
|
||||
row.get("language")
|
||||
);
|
||||
bookManagement.registerBook(book);
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
}
|
||||
// ----------------------------Scénario 1---------------------------------
|
||||
|
||||
// ----------------------------Scénario 1 et 4 (succès ou échec)---------------------------------
|
||||
@When("I register a new book with the following information:")
|
||||
@When("I try to register a new book with the following information:")
|
||||
public void iRegisterANewBookWithTheFollowingInformation(DataTable dataTable) {
|
||||
Map<String, String> newBook = dataTable.asMaps(String.class, String.class).get(0);
|
||||
|
||||
String isbn = newBook.get("isbn");
|
||||
if (isbn == null || isbn.isBlank()
|
||||
|| newBook.get("title") == null || newBook.get("title").isBlank()
|
||||
|| newBook.get("author") == null || newBook.get("author").isBlank()
|
||||
|| newBook.get("publisher") == null || newBook.get("publisher").isBlank()
|
||||
|| newBook.get("publicationDate") == null || newBook.get("publicationDate").isBlank()
|
||||
|| newBook.get("price") == null || newBook.get("price").isBlank()
|
||||
|| newBook.get("quantity") == null || newBook.get("quantity").isBlank()
|
||||
|| newBook.get("language") == null || newBook.get("language").isBlank()) {
|
||||
Map<String, String> row = dataTable.asMaps(String.class, String.class).get(0);
|
||||
try {
|
||||
Book book = new Book(
|
||||
row.get("isbn"),
|
||||
row.get("title"),
|
||||
row.get("author"),
|
||||
row.get("publisher"),
|
||||
row.get("publicationDate"),
|
||||
row.get("price"),
|
||||
row.get("quantity"),
|
||||
row.get("language")
|
||||
);
|
||||
bookManagement.registerBook(book);
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
} catch (InvalidBookDataException | DuplicateBookException e) {
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = "Invalid book data provided";
|
||||
return;
|
||||
lastErrorMessage = e.getMessage();
|
||||
}
|
||||
|
||||
if (books.containsKey(isbn)) {
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = "Conflict with existing book in database";
|
||||
return;
|
||||
}
|
||||
|
||||
books.put(isbn, new HashMap<>(newBook));
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
}
|
||||
|
||||
@Then("the book is successfully registered")
|
||||
@@ -63,15 +68,27 @@ public class BookSteps {
|
||||
assertTrue(lastOperationSuccess);
|
||||
}
|
||||
|
||||
@And("the system now has {int} books")
|
||||
public void theSystemNowHasBooks(int expectedCount) {
|
||||
assertEquals(expectedCount, books.size());
|
||||
@Then("the registration fails")
|
||||
public void theRegistrationFails() {
|
||||
assertFalse(lastOperationSuccess);
|
||||
}
|
||||
|
||||
// ----------------------------Scénario 2---------------------------------
|
||||
@And("I receive an error message containing {string}")
|
||||
public void iReceiveAnErrorMessageContaining(String msg) {
|
||||
assertNotNull(lastErrorMessage);
|
||||
assertTrue(lastErrorMessage.contains(msg));
|
||||
}
|
||||
|
||||
@And("the system now has {int} books")
|
||||
@And("the system still has {int} books")
|
||||
public void theSystemHasBooks(int expectedCount) {
|
||||
assertEquals(expectedCount, bookManagement.getBookCount());
|
||||
}
|
||||
|
||||
// ----------------------------Scénario 2---------------------------------
|
||||
@When("I request all books")
|
||||
public void iRequestAllBooks() {
|
||||
lastBookResult = new ArrayList<>(books.values());
|
||||
lastBookResult = bookManagement.getAllBooks();
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
}
|
||||
@@ -83,67 +100,50 @@ public class BookSteps {
|
||||
assertEquals(expected.size(), lastBookResult.size());
|
||||
|
||||
for (int i = 0; i < expected.size(); i++) {
|
||||
Book actual = lastBookResult.get(i);
|
||||
Map<String, String> expectedBook = expected.get(i);
|
||||
Map<String, String> actualBook = lastBookResult.get(i);
|
||||
for (String key : expectedBook.keySet()) {
|
||||
assertEquals(expectedBook.get(key), actualBook.get(key));
|
||||
}
|
||||
assertEquals(expectedBook.get("isbn"), actual.getIsbn());
|
||||
assertEquals(expectedBook.get("title"), actual.getTitle());
|
||||
assertEquals(expectedBook.get("author"), actual.getAuthor());
|
||||
assertEquals(expectedBook.get("publisher"), actual.getPublisher());
|
||||
assertEquals(expectedBook.get("publicationDate"), actual.getPublicationDate());
|
||||
assertEquals(expectedBook.get("price"), actual.getPrice());
|
||||
assertEquals(expectedBook.get("quantity"), actual.getQuantity());
|
||||
assertEquals(expectedBook.get("language"), actual.getLanguage());
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------Scénario 3---------------------------------
|
||||
// ----------------------------Scénario 3---------------------------------
|
||||
@When("I request the book with id {string}")
|
||||
public void iRequestTheBookWithId(String isbn) {
|
||||
if (!books.containsKey(isbn)) {
|
||||
lastSingleBookResult = null;
|
||||
try {
|
||||
lastSingleBookResult = bookManagement.getBookByIsbn(isbn);
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
} catch (BookNotFoundException e) {
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = "Book not found";
|
||||
return;
|
||||
lastErrorMessage = e.getMessage();
|
||||
}
|
||||
lastSingleBookResult = books.get(isbn);
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
}
|
||||
|
||||
@Then("I receive the following book information:")
|
||||
public void iReceiveTheFollowingBookInformation(DataTable expectedTable) {
|
||||
List<Map<String, String>> expectedList = expectedTable.asMaps(String.class, String.class);
|
||||
assertEquals(1, expectedList.size());
|
||||
Map<String, String> expectedBook = expectedList.get(0);
|
||||
Map<String, String> expectedBook = expectedTable.asMaps(String.class, String.class).get(0);
|
||||
assertNotNull(lastSingleBookResult);
|
||||
|
||||
for (String key : expectedBook.keySet()) {
|
||||
assertEquals(expectedBook.get(key), lastSingleBookResult.get(key));
|
||||
}
|
||||
assertEquals(expectedBook.get("isbn"), lastSingleBookResult.getIsbn());
|
||||
assertEquals(expectedBook.get("title"), lastSingleBookResult.getTitle());
|
||||
assertEquals(expectedBook.get("author"), lastSingleBookResult.getAuthor());
|
||||
assertEquals(expectedBook.get("publisher"), lastSingleBookResult.getPublisher());
|
||||
assertEquals(expectedBook.get("publicationDate"), lastSingleBookResult.getPublicationDate());
|
||||
assertEquals(expectedBook.get("price"), lastSingleBookResult.getPrice());
|
||||
assertEquals(expectedBook.get("quantity"), lastSingleBookResult.getQuantity());
|
||||
assertEquals(expectedBook.get("language"), lastSingleBookResult.getLanguage());
|
||||
}
|
||||
|
||||
// ----------------------------Scénario 4---------------------------------
|
||||
@When("I try to register a new book with the following information:")
|
||||
public void iTryToRegisterANewBookWithTheFollowingInformation(DataTable dataTable) {
|
||||
iRegisterANewBookWithTheFollowingInformation(dataTable);
|
||||
if (lastOperationSuccess) {
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = "Expected failure but succeeded";
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------Scénario 5---------------------------------
|
||||
@And("the system still has {int} books")
|
||||
public void theSystemStillHasBooks(int expectedCount) {
|
||||
assertEquals(expectedCount, books.size());
|
||||
}
|
||||
|
||||
@When("the request fails")
|
||||
public void theRequestFails() {
|
||||
assertFalse(lastOperationSuccess);
|
||||
}
|
||||
|
||||
// ----------------------------Scénario 6---------------------------------
|
||||
// ----------------------------Scénario 6---------------------------------
|
||||
@When("I request all books with title {string}")
|
||||
public void iRequestAllBooksWithTitle(String titleFilter) {
|
||||
lastBookResult = books.values().stream()
|
||||
.filter(book -> book.get("title").equalsIgnoreCase(titleFilter))
|
||||
.toList();
|
||||
lastBookResult = bookManagement.getBooksByTitle(titleFilter);
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
}
|
||||
@@ -154,15 +154,8 @@ public class BookSteps {
|
||||
assertTrue(lastBookResult.isEmpty());
|
||||
}
|
||||
|
||||
// ----------------------Dans plusieurs scénario-----------------------------
|
||||
@Then("the registration fails")
|
||||
public void theRegistrationFails() {
|
||||
@When("the request fails")
|
||||
public void theRequestFails() {
|
||||
assertFalse(lastOperationSuccess);
|
||||
}
|
||||
|
||||
@And("I receive an error message containing {string}")
|
||||
public void iReceiveAnErrorMessageContaining(String msg) {
|
||||
assertNotNull(lastErrorMessage);
|
||||
assertTrue(lastErrorMessage.contains(msg));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user