book Exeception

This commit is contained in:
2025-06-13 22:10:29 +02:00
parent d18a9074a3
commit 459d03b36b
5 changed files with 115 additions and 98 deletions

View File

@@ -1,15 +1,18 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.*;
import java.util.*;
public class BookManagement {
private final Map<String, Book> books = new LinkedHashMap<>();
public boolean registerBook(Book book) {
public boolean registerBook(Book book) throws InvalidBookDataException, DuplicateBookException {
if (!isValid(book)) {
throw new IllegalArgumentException("Invalid book data provided");
throw new InvalidBookDataException("Invalid book data provided");
}
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);
return true;
@@ -19,16 +22,16 @@ public class BookManagement {
return new ArrayList<>(books.values());
}
public Book getBookByIsbn(String isbn) {
public Book getBookByIsbn(String isbn) throws BookNotFoundException {
if (!books.containsKey(isbn)) {
throw new NoSuchElementException("Book not found");
throw new BookNotFoundException("Book not found");
}
return books.get(isbn);
}
public List<Book> getBooksByTitle(String title) {
return books.values().stream()
.filter(Book -> Book.getTitle().equalsIgnoreCase(title))
.filter(book -> book.getTitle().equalsIgnoreCase(title))
.toList();
}

View File

@@ -0,0 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
public class BookNotFoundException extends Exception {
public BookNotFoundException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
public class DuplicateBookException extends Exception {
public DuplicateBookException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
public class InvalidBookDataException extends Exception {
public InvalidBookDataException(String message) {
super(message);
}
}

View File

@@ -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--------------------------------
@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()) {
lastOperationSuccess = false;
lastErrorMessage = "Invalid book data provided";
return;
}
if (books.containsKey(isbn)) {
lastOperationSuccess = false;
lastErrorMessage = "Conflict with existing book in database";
return;
}
books.put(isbn, new HashMap<>(newBook));
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 = e.getMessage();
}
}
@Then("the book is successfully registered")
@@ -63,15 +68,27 @@ public class BookSteps {
assertTrue(lastOperationSuccess);
}
@Then("the registration fails")
public void theRegistrationFails() {
assertFalse(lastOperationSuccess);
}
@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")
public void theSystemNowHasBooks(int expectedCount) {
assertEquals(expectedCount, books.size());
@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---------------------------------
@When("I request the book with id {string}")
public void iRequestTheBookWithId(String isbn) {
if (!books.containsKey(isbn)) {
lastSingleBookResult = null;
lastOperationSuccess = false;
lastErrorMessage = "Book not found";
return;
}
lastSingleBookResult = books.get(isbn);
try {
lastSingleBookResult = bookManagement.getBookByIsbn(isbn);
lastOperationSuccess = true;
lastErrorMessage = null;
} catch (BookNotFoundException e) {
lastOperationSuccess = false;
lastErrorMessage = e.getMessage();
}
}
@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));
}
}
// ----------------------------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);
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 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));
}
}