forked from pierront/mylibrary-template
book Exeception
This commit is contained in:
@@ -1,15 +1,18 @@
|
|||||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.*;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class BookManagement {
|
public class BookManagement {
|
||||||
private final Map<String, Book> books = new LinkedHashMap<>();
|
private final Map<String, Book> books = new LinkedHashMap<>();
|
||||||
|
|
||||||
public boolean registerBook(Book book) {
|
public boolean registerBook(Book book) throws InvalidBookDataException, DuplicateBookException {
|
||||||
if (!isValid(book)) {
|
if (!isValid(book)) {
|
||||||
throw new IllegalArgumentException("Invalid book data provided");
|
throw new InvalidBookDataException("Invalid book data provided");
|
||||||
}
|
}
|
||||||
if (books.containsKey(book.getIsbn())) {
|
if (books.containsKey(book.getIsbn())) {
|
||||||
throw new IllegalStateException("Conflict with existing book in database");
|
throw new DuplicateBookException("Conflict with existing book in database");
|
||||||
}
|
}
|
||||||
books.put(book.getIsbn(), book);
|
books.put(book.getIsbn(), book);
|
||||||
return true;
|
return true;
|
||||||
@@ -19,16 +22,16 @@ public class BookManagement {
|
|||||||
return new ArrayList<>(books.values());
|
return new ArrayList<>(books.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Book getBookByIsbn(String isbn) {
|
public Book getBookByIsbn(String isbn) throws BookNotFoundException {
|
||||||
if (!books.containsKey(isbn)) {
|
if (!books.containsKey(isbn)) {
|
||||||
throw new NoSuchElementException("Book not found");
|
throw new BookNotFoundException("Book not found");
|
||||||
}
|
}
|
||||||
return books.get(isbn);
|
return books.get(isbn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Book> getBooksByTitle(String title) {
|
public List<Book> getBooksByTitle(String title) {
|
||||||
return books.values().stream()
|
return books.values().stream()
|
||||||
.filter(Book -> Book.getTitle().equalsIgnoreCase(title))
|
.filter(book -> book.getTitle().equalsIgnoreCase(title))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
public class BookNotFoundException extends Exception {
|
||||||
|
public BookNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
public class DuplicateBookException extends Exception {
|
||||||
|
public DuplicateBookException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
public class InvalidBookDataException extends Exception {
|
||||||
|
public InvalidBookDataException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
package fr.iut_fbleau.but3.dev62.mylibrary.features.book;
|
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.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
@@ -13,49 +12,55 @@ import java.util.*;
|
|||||||
|
|
||||||
public class BookSteps {
|
public class BookSteps {
|
||||||
|
|
||||||
private final Map<String, Map<String, String>> books = new LinkedHashMap<>();
|
private final BookManagement bookManagement = new BookManagement();
|
||||||
|
private List<Book> lastBookResult;
|
||||||
private List<Map<String, String>> lastBookResult;
|
private Book lastSingleBookResult;
|
||||||
private Map<String, String> lastSingleBookResult;
|
|
||||||
private String lastErrorMessage;
|
private String lastErrorMessage;
|
||||||
private boolean lastOperationSuccess;
|
private boolean lastOperationSuccess;
|
||||||
|
|
||||||
// ----------------------------Background--------------------------------
|
// ----------------------------Background--------------------------------
|
||||||
@Given("the system has the following books:")
|
@Given("the system has the following books:")
|
||||||
public void theSystemHasTheFollowingBooks(DataTable dataTable) {
|
public void theSystemHasTheFollowingBooks(DataTable dataTable) {
|
||||||
books.clear();
|
|
||||||
for (Map<String, String> row : dataTable.asMaps(String.class, String.class)) {
|
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 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) {
|
public void iRegisterANewBookWithTheFollowingInformation(DataTable dataTable) {
|
||||||
Map<String, String> newBook = dataTable.asMaps(String.class, String.class).get(0);
|
Map<String, String> row = dataTable.asMaps(String.class, String.class).get(0);
|
||||||
|
try {
|
||||||
String isbn = newBook.get("isbn");
|
Book book = new Book(
|
||||||
if (isbn == null || isbn.isBlank()
|
row.get("isbn"),
|
||||||
|| newBook.get("title") == null || newBook.get("title").isBlank()
|
row.get("title"),
|
||||||
|| newBook.get("author") == null || newBook.get("author").isBlank()
|
row.get("author"),
|
||||||
|| newBook.get("publisher") == null || newBook.get("publisher").isBlank()
|
row.get("publisher"),
|
||||||
|| newBook.get("publicationDate") == null || newBook.get("publicationDate").isBlank()
|
row.get("publicationDate"),
|
||||||
|| newBook.get("price") == null || newBook.get("price").isBlank()
|
row.get("price"),
|
||||||
|| newBook.get("quantity") == null || newBook.get("quantity").isBlank()
|
row.get("quantity"),
|
||||||
|| newBook.get("language") == null || newBook.get("language").isBlank()) {
|
row.get("language")
|
||||||
|
);
|
||||||
|
bookManagement.registerBook(book);
|
||||||
|
lastOperationSuccess = true;
|
||||||
|
lastErrorMessage = null;
|
||||||
|
} catch (InvalidBookDataException | DuplicateBookException e) {
|
||||||
lastOperationSuccess = false;
|
lastOperationSuccess = false;
|
||||||
lastErrorMessage = "Invalid book data provided";
|
lastErrorMessage = e.getMessage();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
@Then("the book is successfully registered")
|
||||||
@@ -63,15 +68,27 @@ public class BookSteps {
|
|||||||
assertTrue(lastOperationSuccess);
|
assertTrue(lastOperationSuccess);
|
||||||
}
|
}
|
||||||
|
|
||||||
@And("the system now has {int} books")
|
@Then("the registration fails")
|
||||||
public void theSystemNowHasBooks(int expectedCount) {
|
public void theRegistrationFails() {
|
||||||
assertEquals(expectedCount, books.size());
|
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")
|
@When("I request all books")
|
||||||
public void iRequestAllBooks() {
|
public void iRequestAllBooks() {
|
||||||
lastBookResult = new ArrayList<>(books.values());
|
lastBookResult = bookManagement.getAllBooks();
|
||||||
lastOperationSuccess = true;
|
lastOperationSuccess = true;
|
||||||
lastErrorMessage = null;
|
lastErrorMessage = null;
|
||||||
}
|
}
|
||||||
@@ -83,67 +100,50 @@ public class BookSteps {
|
|||||||
assertEquals(expected.size(), lastBookResult.size());
|
assertEquals(expected.size(), lastBookResult.size());
|
||||||
|
|
||||||
for (int i = 0; i < expected.size(); i++) {
|
for (int i = 0; i < expected.size(); i++) {
|
||||||
|
Book actual = lastBookResult.get(i);
|
||||||
Map<String, String> expectedBook = expected.get(i);
|
Map<String, String> expectedBook = expected.get(i);
|
||||||
Map<String, String> actualBook = lastBookResult.get(i);
|
assertEquals(expectedBook.get("isbn"), actual.getIsbn());
|
||||||
for (String key : expectedBook.keySet()) {
|
assertEquals(expectedBook.get("title"), actual.getTitle());
|
||||||
assertEquals(expectedBook.get(key), actualBook.get(key));
|
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}")
|
@When("I request the book with id {string}")
|
||||||
public void iRequestTheBookWithId(String isbn) {
|
public void iRequestTheBookWithId(String isbn) {
|
||||||
if (!books.containsKey(isbn)) {
|
try {
|
||||||
lastSingleBookResult = null;
|
lastSingleBookResult = bookManagement.getBookByIsbn(isbn);
|
||||||
|
lastOperationSuccess = true;
|
||||||
|
lastErrorMessage = null;
|
||||||
|
} catch (BookNotFoundException e) {
|
||||||
lastOperationSuccess = false;
|
lastOperationSuccess = false;
|
||||||
lastErrorMessage = "Book not found";
|
lastErrorMessage = e.getMessage();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
lastSingleBookResult = books.get(isbn);
|
|
||||||
lastOperationSuccess = true;
|
|
||||||
lastErrorMessage = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Then("I receive the following book information:")
|
@Then("I receive the following book information:")
|
||||||
public void iReceiveTheFollowingBookInformation(DataTable expectedTable) {
|
public void iReceiveTheFollowingBookInformation(DataTable expectedTable) {
|
||||||
List<Map<String, String>> expectedList = expectedTable.asMaps(String.class, String.class);
|
Map<String, String> expectedBook = expectedTable.asMaps(String.class, String.class).get(0);
|
||||||
assertEquals(1, expectedList.size());
|
|
||||||
Map<String, String> expectedBook = expectedList.get(0);
|
|
||||||
assertNotNull(lastSingleBookResult);
|
assertNotNull(lastSingleBookResult);
|
||||||
|
assertEquals(expectedBook.get("isbn"), lastSingleBookResult.getIsbn());
|
||||||
for (String key : expectedBook.keySet()) {
|
assertEquals(expectedBook.get("title"), lastSingleBookResult.getTitle());
|
||||||
assertEquals(expectedBook.get(key), lastSingleBookResult.get(key));
|
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---------------------------------
|
// ----------------------------Scénario 6---------------------------------
|
||||||
@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---------------------------------
|
|
||||||
@When("I request all books with title {string}")
|
@When("I request all books with title {string}")
|
||||||
public void iRequestAllBooksWithTitle(String titleFilter) {
|
public void iRequestAllBooksWithTitle(String titleFilter) {
|
||||||
lastBookResult = books.values().stream()
|
lastBookResult = bookManagement.getBooksByTitle(titleFilter);
|
||||||
.filter(book -> book.get("title").equalsIgnoreCase(titleFilter))
|
|
||||||
.toList();
|
|
||||||
lastOperationSuccess = true;
|
lastOperationSuccess = true;
|
||||||
lastErrorMessage = null;
|
lastErrorMessage = null;
|
||||||
}
|
}
|
||||||
@@ -154,15 +154,8 @@ public class BookSteps {
|
|||||||
assertTrue(lastBookResult.isEmpty());
|
assertTrue(lastBookResult.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------Dans plusieurs scénario-----------------------------
|
@When("the request fails")
|
||||||
@Then("the registration fails")
|
public void theRequestFails() {
|
||||||
public void theRegistrationFails() {
|
|
||||||
assertFalse(lastOperationSuccess);
|
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