forked from pierront/mylibrary-template
book end
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -8,5 +8,5 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" project-jdk-name="ms-21" project-jdk-type="JavaSDK" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21_PREVIEW" project-jdk-name="ms-21" project-jdk-type="JavaSDK" />
|
||||
</project>
|
@@ -22,8 +22,6 @@ public class Book {
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
// Getters (setters optionnels si besoin)
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
@@ -1,43 +1,54 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.error;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.*;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.util.*;
|
||||
|
||||
@DisplayName("Book error and failure scenarios")
|
||||
public class bookErrorTest {
|
||||
|
||||
private String lastErrorMessage;
|
||||
private boolean lastOperationSuccess;
|
||||
private List<Map<String, String>> lastBookResult;
|
||||
private BookManagement bookManagement;
|
||||
|
||||
@Test
|
||||
@DisplayName("Registration fails when data is invalid")
|
||||
void testRegistrationFails() {
|
||||
lastOperationSuccess = false;
|
||||
assertFalse(lastOperationSuccess);
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
bookManagement = new BookManagement();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Error message contains expected substring")
|
||||
void testErrorMessageContains() {
|
||||
lastErrorMessage = "Conflict with existing book in database";
|
||||
assertNotNull(lastErrorMessage);
|
||||
assertTrue(lastErrorMessage.contains("Conflict"));
|
||||
@DisplayName("Registration fails when book data is invalid")
|
||||
void testRegistrationFailsInvalidData() {
|
||||
Book invalidBook = new Book("", "", "", "", "", "", "", "");
|
||||
InvalidBookDataException exception = assertThrows(InvalidBookDataException.class, () -> {
|
||||
bookManagement.registerBook(invalidBook);
|
||||
});
|
||||
assertTrue(exception.getMessage().contains("Invalid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Receiving an empty list of books")
|
||||
@DisplayName("Registration fails when duplicate book ISBN")
|
||||
void testRegistrationFailsDuplicateBook() throws Exception {
|
||||
Book book = new Book("123", "Title", "Author", "Publisher", "2024-01-01", "10", "5", "FR");
|
||||
bookManagement.registerBook(book);
|
||||
|
||||
DuplicateBookException exception = assertThrows(DuplicateBookException.class, () -> {
|
||||
bookManagement.registerBook(book);
|
||||
});
|
||||
assertTrue(exception.getMessage().contains("Conflict"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Get book by ISBN throws BookNotFoundException if not found")
|
||||
void testGetBookByIsbnNotFound() {
|
||||
BookNotFoundException exception = assertThrows(BookNotFoundException.class, () -> {
|
||||
bookManagement.getBookByIsbn("non-existent-isbn");
|
||||
});
|
||||
assertTrue(exception.getMessage().contains("not found"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty book list when no books registered")
|
||||
void testEmptyBookList() {
|
||||
lastBookResult = new ArrayList<>();
|
||||
assertNotNull(lastBookResult);
|
||||
assertTrue(lastBookResult.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Request fails")
|
||||
void testRequestFails() {
|
||||
lastOperationSuccess = false;
|
||||
assertFalse(lastOperationSuccess);
|
||||
assertTrue(bookManagement.getAllBooks().isEmpty());
|
||||
}
|
||||
}
|
||||
|
@@ -1,123 +1,130 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.*;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.*;
|
||||
import io.cucumber.datatable.DataTable;
|
||||
import org.junit.jupiter.api.*;
|
||||
import java.util.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@DisplayName("Book retrieval scenarios and data registration")
|
||||
public class bookFunctionTest {
|
||||
|
||||
private Map<String, Map<String, String>> books;
|
||||
private List<Map<String, String>> lastBookResult;
|
||||
private Map<String, String> lastSingleBookResult;
|
||||
private BookManagement bookManagement;
|
||||
private boolean lastOperationSuccess;
|
||||
private String lastErrorMessage;
|
||||
private List<Book> lastBookResult;
|
||||
private Book lastSingleBookResult;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
books = new HashMap<>();
|
||||
lastBookResult = new ArrayList<>();
|
||||
lastSingleBookResult = null;
|
||||
bookManagement = new BookManagement();
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = null;
|
||||
lastBookResult = null;
|
||||
lastSingleBookResult = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Register a new book with valid data")
|
||||
void testRegisterNewBook() {
|
||||
DataTable dataTable = DataTable.create(
|
||||
List.of(
|
||||
List.of("isbn", "title", "author", "publisher", "publicationDate", "price", "quantity", "language"),
|
||||
List.of("999", "La vie de Bob", "Boby Bob", "Bob", "2025-01-01", "29.99", "10", "FR")
|
||||
)
|
||||
void testRegisterNewBook() throws Exception {
|
||||
Book newBook = new Book(
|
||||
"999",
|
||||
"La vie de Bob",
|
||||
"Boby Bob",
|
||||
"Bob",
|
||||
"2025-01-01",
|
||||
"29.99",
|
||||
"10",
|
||||
"FR"
|
||||
);
|
||||
iRegisterANewBookWithTheFollowingInformation(dataTable);
|
||||
assertTrue(lastOperationSuccess);
|
||||
}
|
||||
|
||||
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));
|
||||
try {
|
||||
bookManagement.registerBook(newBook);
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
} catch (InvalidBookDataException | DuplicateBookException e) {
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = e.getMessage();
|
||||
}
|
||||
|
||||
assertTrue(lastOperationSuccess);
|
||||
assertNull(lastErrorMessage);
|
||||
assertEquals(1, bookManagement.getBookCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Request all books")
|
||||
void testRequestAllBooks() {
|
||||
books.put("1", Map.of("title", "Livre A"));
|
||||
books.put("2", Map.of("title", "Livre B"));
|
||||
void testRequestAllBooks() throws Exception {
|
||||
bookManagement.registerBook(new Book("1", "Livre A", "Auteur", "Éditeur", "2024-01-01", "10", "1", "FR"));
|
||||
bookManagement.registerBook(new Book("2", "Livre B", "Auteur", "Éditeur", "2024-01-01", "12", "1", "FR"));
|
||||
|
||||
iRequestAllBooks();
|
||||
lastBookResult = bookManagement.getAllBooks();
|
||||
|
||||
assertNotNull(lastBookResult);
|
||||
assertEquals(2, lastBookResult.size());
|
||||
}
|
||||
|
||||
public void iRequestAllBooks() {
|
||||
lastBookResult = new ArrayList<>(books.values());
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Request single book by ID")
|
||||
void testRequestBookById() {
|
||||
books.put("123", Map.of("isbn", "123", "title", "blabla"));
|
||||
iRequestTheBookWithId("123");
|
||||
@DisplayName("Request single book by ISBN")
|
||||
void testRequestBookByIsbn() throws Exception {
|
||||
Book book = new Book("123", "blabla", "Auteur", "Éditeur", "2024-01-01", "15", "1", "FR");
|
||||
bookManagement.registerBook(book);
|
||||
|
||||
lastSingleBookResult = bookManagement.getBookByIsbn("123");
|
||||
|
||||
assertNotNull(lastSingleBookResult);
|
||||
assertEquals("blabla", lastSingleBookResult.get("title"));
|
||||
}
|
||||
|
||||
public void iRequestTheBookWithId(String isbn) {
|
||||
if (!books.containsKey(isbn)) {
|
||||
lastSingleBookResult = null;
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = "Book not found";
|
||||
return;
|
||||
}
|
||||
lastSingleBookResult = books.get(isbn);
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
assertEquals("blabla", lastSingleBookResult.getTitle());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Attempt to register a book that should fail")
|
||||
void testRegisterBookWithExpectedFailure() {
|
||||
DataTable dataTable = DataTable.create(
|
||||
List.of(
|
||||
List.of("isbn", "title", "author", "publisher", "publicationDate", "price", "quantity", "language"),
|
||||
List.of("666", "La vie de Boby", "Boby Bob", "bob", "2025-01-01", "25.00", "10", "FR")
|
||||
)
|
||||
@DisplayName("Attempt to register a duplicate book")
|
||||
void testRegisterDuplicateBook() {
|
||||
Map<String, String> bookData = Map.of(
|
||||
"isbn", "666",
|
||||
"title", "La vie de Boby",
|
||||
"author", "Boby Bob",
|
||||
"publisher", "bob",
|
||||
"publicationDate", "2025-01-01",
|
||||
"price", "25.00",
|
||||
"quantity", "10",
|
||||
"language", "FR"
|
||||
);
|
||||
iTryToRegisterANewBookWithTheFollowingInformation(dataTable);
|
||||
|
||||
iRegisterANewBookWithTheFollowingInformation(bookData);
|
||||
assertTrue(lastOperationSuccess);
|
||||
|
||||
iTryToRegisterANewBookWithTheFollowingInformation(bookData);
|
||||
assertFalse(lastOperationSuccess);
|
||||
assertNotNull(lastErrorMessage);
|
||||
assertTrue(lastErrorMessage.contains("Conflict"));
|
||||
}
|
||||
|
||||
public void iTryToRegisterANewBookWithTheFollowingInformation(DataTable dataTable) {
|
||||
iRegisterANewBookWithTheFollowingInformation(dataTable);
|
||||
public void iRegisterANewBookWithTheFollowingInformation(Map<String, String> row) {
|
||||
Book newBook = 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")
|
||||
);
|
||||
|
||||
try {
|
||||
bookManagement.registerBook(newBook);
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
} catch (InvalidBookDataException | DuplicateBookException e) {
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
void iTryToRegisterANewBookWithTheFollowingInformation(Map<String, String> row) {
|
||||
iRegisterANewBookWithTheFollowingInformation(row);
|
||||
if (lastOperationSuccess) {
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = "Expected failure but succeeded";
|
||||
@@ -126,20 +133,17 @@ public class bookFunctionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Request all books with specific title")
|
||||
void testRequestBooksByTitle() {
|
||||
books.put("1", Map.of("title", "Wouah"));
|
||||
books.put("2", Map.of("title", "Troop"));
|
||||
books.put("3", Map.of("title", "Bien"));
|
||||
void testRequestBooksByTitle() throws Exception {
|
||||
bookManagement.registerBook(new Book("1", "Bien", "X", "Y", "2020", "1", "1", "FR"));
|
||||
bookManagement.registerBook(new Book("2", "Troop", "X", "Y", "2020", "1", "1", "FR"));
|
||||
bookManagement.registerBook(new Book("3", "Bien", "X", "Y", "2020", "1", "1", "FR"));
|
||||
|
||||
iRequestAllBooksWithTitle("Bien");
|
||||
lastBookResult = bookManagement.getBooksByTitle("Bien");
|
||||
|
||||
assertNotNull(lastBookResult);
|
||||
assertEquals(2, lastBookResult.size());
|
||||
for (Book b : lastBookResult) {
|
||||
assertEquals("Bien", b.getTitle());
|
||||
}
|
||||
|
||||
public void iRequestAllBooksWithTitle(String titleFilter) {
|
||||
lastBookResult = books.values().stream()
|
||||
.filter(book -> book.get("title").equalsIgnoreCase(titleFilter))
|
||||
.toList();
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
}
|
||||
}
|
||||
|
@@ -1,81 +1,61 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.result;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.Book;
|
||||
import org.junit.jupiter.api.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.util.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@DisplayName("Book result success scenarios")
|
||||
public class bookResultTest {
|
||||
private Map<String, String> lastSingleBookResult;
|
||||
private Map<String, Map<String, String>> books;
|
||||
|
||||
private Map<String, Book> books;
|
||||
private Book sampleBook;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
books = new HashMap<>();
|
||||
books.put("123", Map.of("titre", "Livre A"));
|
||||
books.put("456", Map.of("title", "Livre B"));
|
||||
books.put("123", new Book("123", "Livre A", "Auteur", "Éditeur", "2024-01-01", "10", "1", "FR"));
|
||||
books.put("456", new Book("456", "Livre B", "Auteur", "Éditeur", "2024-01-01", "12", "2", "FR"));
|
||||
|
||||
lastSingleBookResult = Map.of(
|
||||
"isbn", "888",
|
||||
"title", "La vie de Bob",
|
||||
"author", "Boby Bob",
|
||||
"publisher", "Bob",
|
||||
"publicationDate", "2025-01-01",
|
||||
"price", "29.99",
|
||||
"quantity", "10",
|
||||
"language", "Fr"
|
||||
sampleBook = new Book(
|
||||
"888",
|
||||
"La vie de Bob",
|
||||
"Boby Bob",
|
||||
"Bob",
|
||||
"2025-01-01",
|
||||
"29.99",
|
||||
"10",
|
||||
"FR"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Book is successfully registered")
|
||||
void testBookRegistrationSuccess() {
|
||||
boolean lastOperationSuccess = true;
|
||||
assertTrue(lastOperationSuccess);
|
||||
@DisplayName("Book is successfully created with correct data")
|
||||
void testBookCreation() {
|
||||
assertEquals("888", sampleBook.getIsbn());
|
||||
assertEquals("La vie de Bob", sampleBook.getTitle());
|
||||
assertEquals("Boby Bob", sampleBook.getAuthor());
|
||||
assertEquals("Bob", sampleBook.getPublisher());
|
||||
assertEquals("2025-01-01", sampleBook.getPublicationDate());
|
||||
assertEquals("29.99", sampleBook.getPrice());
|
||||
assertEquals("10", sampleBook.getQuantity());
|
||||
assertEquals("FR", sampleBook.getLanguage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Receive detailed information of a single book")
|
||||
void testReceiveSingleBookInformation() {
|
||||
lastSingleBookResult = Map.of(
|
||||
"isbn", "777",
|
||||
"title", "La vie de Bob",
|
||||
"author", "Boby Bob",
|
||||
"publisher", "Bob",
|
||||
"publicationDate", "2025-01-01",
|
||||
"price", "29.99",
|
||||
"quantity", "10",
|
||||
"language", "Fr"
|
||||
);
|
||||
|
||||
List<Map<String, String>> expectedList = List.of(Map.of(
|
||||
"isbn", "001",
|
||||
"title", "La vie de mary",
|
||||
"author", "Mary Mard",
|
||||
"publisher", "Mary",
|
||||
"publicationDate", "2025-02-02",
|
||||
"price", "29.99",
|
||||
"quantity", "10",
|
||||
"language", "Fr"
|
||||
));
|
||||
|
||||
Map<String, String> expectedBook = expectedList.get(0);
|
||||
assertNotNull(lastSingleBookResult);
|
||||
|
||||
for (String key : expectedBook.keySet()) {
|
||||
assertEquals(expectedBook.get(key), lastSingleBookResult.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("System has expected number of books after registration")
|
||||
void testSystemHasBooks() {
|
||||
@DisplayName("Books map contains expected number of books")
|
||||
void testBooksMapSize() {
|
||||
assertEquals(2, books.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("System still has expected number of books")
|
||||
void testSystemStillHasBooks() {
|
||||
assertEquals(2, books.size());
|
||||
@DisplayName("Books map contains expected books")
|
||||
void testBooksMapContent() {
|
||||
assertTrue(books.containsKey("123"));
|
||||
assertTrue(books.containsKey("456"));
|
||||
assertEquals("Livre A", books.get("123").getTitle());
|
||||
assertEquals("Livre B", books.get("456").getTitle());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user