This commit is contained in:
2025-06-13 23:52:34 +02:00
parent 52994a20a3
commit 9dd1336ff4
5 changed files with 170 additions and 177 deletions

2
.idea/misc.xml generated
View File

@@ -8,5 +8,5 @@
</list> </list>
</option> </option>
</component> </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> </project>

View File

@@ -22,8 +22,6 @@ public class Book {
this.language = language; this.language = language;
} }
// Getters (setters optionnels si besoin)
public String getIsbn() { public String getIsbn() {
return isbn; return isbn;
} }

View File

@@ -1,43 +1,54 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.error; 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 org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
@DisplayName("Book error and failure scenarios") @DisplayName("Book error and failure scenarios")
public class bookErrorTest { public class bookErrorTest {
private String lastErrorMessage; private BookManagement bookManagement;
private boolean lastOperationSuccess;
private List<Map<String, String>> lastBookResult;
@Test @BeforeEach
@DisplayName("Registration fails when data is invalid") void setUp() {
void testRegistrationFails() { bookManagement = new BookManagement();
lastOperationSuccess = false;
assertFalse(lastOperationSuccess);
} }
@Test @Test
@DisplayName("Error message contains expected substring") @DisplayName("Registration fails when book data is invalid")
void testErrorMessageContains() { void testRegistrationFailsInvalidData() {
lastErrorMessage = "Conflict with existing book in database"; Book invalidBook = new Book("", "", "", "", "", "", "", "");
assertNotNull(lastErrorMessage); InvalidBookDataException exception = assertThrows(InvalidBookDataException.class, () -> {
assertTrue(lastErrorMessage.contains("Conflict")); bookManagement.registerBook(invalidBook);
});
assertTrue(exception.getMessage().contains("Invalid"));
} }
@Test @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() { void testEmptyBookList() {
lastBookResult = new ArrayList<>(); assertTrue(bookManagement.getAllBooks().isEmpty());
assertNotNull(lastBookResult);
assertTrue(lastBookResult.isEmpty());
}
@Test
@DisplayName("Request fails")
void testRequestFails() {
lastOperationSuccess = false;
assertFalse(lastOperationSuccess);
} }
} }

View File

@@ -1,123 +1,130 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.function; 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 io.cucumber.datatable.DataTable;
import org.junit.jupiter.api.*; 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") @DisplayName("Book retrieval scenarios and data registration")
public class bookFunctionTest { public class bookFunctionTest {
private Map<String, Map<String, String>> books; private BookManagement bookManagement;
private List<Map<String, String>> lastBookResult;
private Map<String, String> lastSingleBookResult;
private boolean lastOperationSuccess; private boolean lastOperationSuccess;
private String lastErrorMessage; private String lastErrorMessage;
private List<Book> lastBookResult;
private Book lastSingleBookResult;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
books = new HashMap<>(); bookManagement = new BookManagement();
lastBookResult = new ArrayList<>();
lastSingleBookResult = null;
lastOperationSuccess = false; lastOperationSuccess = false;
lastErrorMessage = null; lastErrorMessage = null;
lastBookResult = null;
lastSingleBookResult = null;
} }
@Test @Test
@DisplayName("Register a new book with valid data") @DisplayName("Register a new book with valid data")
void testRegisterNewBook() { void testRegisterNewBook() throws Exception {
DataTable dataTable = DataTable.create( Book newBook = new Book(
List.of( "999",
List.of("isbn", "title", "author", "publisher", "publicationDate", "price", "quantity", "language"), "La vie de Bob",
List.of("999", "La vie de Bob", "Boby Bob", "Bob", "2025-01-01", "29.99", "10", "FR") "Boby Bob",
) "Bob",
"2025-01-01",
"29.99",
"10",
"FR"
); );
iRegisterANewBookWithTheFollowingInformation(dataTable);
assertTrue(lastOperationSuccess);
}
public void iRegisterANewBookWithTheFollowingInformation(DataTable dataTable) { try {
Map<String, String> newBook = dataTable.asMaps(String.class, String.class).get(0); bookManagement.registerBook(newBook);
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));
lastOperationSuccess = true; lastOperationSuccess = true;
lastErrorMessage = null; } catch (InvalidBookDataException | DuplicateBookException e) {
lastOperationSuccess = false;
lastErrorMessage = e.getMessage();
}
assertTrue(lastOperationSuccess);
assertNull(lastErrorMessage);
assertEquals(1, bookManagement.getBookCount());
} }
@Test @Test
@DisplayName("Request all books") @DisplayName("Request all books")
void testRequestAllBooks() { void testRequestAllBooks() throws Exception {
books.put("1", Map.of("title", "Livre A")); bookManagement.registerBook(new Book("1", "Livre A", "Auteur", "Éditeur", "2024-01-01", "10", "1", "FR"));
books.put("2", Map.of("title", "Livre B")); 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()); assertEquals(2, lastBookResult.size());
} }
public void iRequestAllBooks() {
lastBookResult = new ArrayList<>(books.values());
lastOperationSuccess = true;
lastErrorMessage = null;
}
@Test @Test
@DisplayName("Request single book by ID") @DisplayName("Request single book by ISBN")
void testRequestBookById() { void testRequestBookByIsbn() throws Exception {
books.put("123", Map.of("isbn", "123", "title", "blabla")); Book book = new Book("123", "blabla", "Auteur", "Éditeur", "2024-01-01", "15", "1", "FR");
iRequestTheBookWithId("123"); bookManagement.registerBook(book);
lastSingleBookResult = bookManagement.getBookByIsbn("123");
assertNotNull(lastSingleBookResult); assertNotNull(lastSingleBookResult);
assertEquals("blabla", lastSingleBookResult.get("title")); assertEquals("blabla", lastSingleBookResult.getTitle());
}
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;
} }
@Test @Test
@DisplayName("Attempt to register a book that should fail") @DisplayName("Attempt to register a duplicate book")
void testRegisterBookWithExpectedFailure() { void testRegisterDuplicateBook() {
DataTable dataTable = DataTable.create( Map<String, String> bookData = Map.of(
List.of( "isbn", "666",
List.of("isbn", "title", "author", "publisher", "publicationDate", "price", "quantity", "language"), "title", "La vie de Boby",
List.of("666", "La vie de Boby", "Boby Bob", "bob", "2025-01-01", "25.00", "10", "FR") "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); assertFalse(lastOperationSuccess);
assertNotNull(lastErrorMessage); assertNotNull(lastErrorMessage);
assertTrue(lastErrorMessage.contains("Conflict"));
} }
public void iTryToRegisterANewBookWithTheFollowingInformation(DataTable dataTable) { public void iRegisterANewBookWithTheFollowingInformation(Map<String, String> row) {
iRegisterANewBookWithTheFollowingInformation(dataTable); 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) { if (lastOperationSuccess) {
lastOperationSuccess = false; lastOperationSuccess = false;
lastErrorMessage = "Expected failure but succeeded"; lastErrorMessage = "Expected failure but succeeded";
@@ -126,20 +133,17 @@ public class bookFunctionTest {
@Test @Test
@DisplayName("Request all books with specific title") @DisplayName("Request all books with specific title")
void testRequestBooksByTitle() { void testRequestBooksByTitle() throws Exception {
books.put("1", Map.of("title", "Wouah")); bookManagement.registerBook(new Book("1", "Bien", "X", "Y", "2020", "1", "1", "FR"));
books.put("2", Map.of("title", "Troop")); bookManagement.registerBook(new Book("2", "Troop", "X", "Y", "2020", "1", "1", "FR"));
books.put("3", Map.of("title", "Bien")); bookManagement.registerBook(new Book("3", "Bien", "X", "Y", "2020", "1", "1", "FR"));
iRequestAllBooksWithTitle("Bien"); lastBookResult = bookManagement.getBooksByTitle("Bien");
assertNotNull(lastBookResult);
assertEquals(2, lastBookResult.size()); 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;
} }
} }

View File

@@ -1,81 +1,61 @@
package fr.iut_fbleau.but3.dev62.mylibrary.book.result; package fr.iut_fbleau.but3.dev62.mylibrary.book.result;
import fr.iut_fbleau.but3.dev62.mylibrary.book.Book;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
@DisplayName("Book result success scenarios") @DisplayName("Book result success scenarios")
public class bookResultTest { public class bookResultTest {
private Map<String, String> lastSingleBookResult;
private Map<String, Map<String, String>> books; private Map<String, Book> books;
private Book sampleBook;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
books = new HashMap<>(); books = new HashMap<>();
books.put("123", Map.of("titre", "Livre A")); books.put("123", new Book("123", "Livre A", "Auteur", "Éditeur", "2024-01-01", "10", "1", "FR"));
books.put("456", Map.of("title", "Livre B")); books.put("456", new Book("456", "Livre B", "Auteur", "Éditeur", "2024-01-01", "12", "2", "FR"));
lastSingleBookResult = Map.of( sampleBook = new Book(
"isbn", "888", "888",
"title", "La vie de Bob", "La vie de Bob",
"author", "Boby Bob", "Boby Bob",
"publisher", "Bob", "Bob",
"publicationDate", "2025-01-01", "2025-01-01",
"price", "29.99", "29.99",
"quantity", "10", "10",
"language", "Fr" "FR"
); );
} }
@Test @Test
@DisplayName("Book is successfully registered") @DisplayName("Book is successfully created with correct data")
void testBookRegistrationSuccess() { void testBookCreation() {
boolean lastOperationSuccess = true; assertEquals("888", sampleBook.getIsbn());
assertTrue(lastOperationSuccess); 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 @Test
@DisplayName("Receive detailed information of a single book") @DisplayName("Books map contains expected number of books")
void testReceiveSingleBookInformation() { void testBooksMapSize() {
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() {
assertEquals(2, books.size()); assertEquals(2, books.size());
} }
@Test @Test
@DisplayName("System still has expected number of books") @DisplayName("Books map contains expected books")
void testSystemStillHasBooks() { void testBooksMapContent() {
assertEquals(2, books.size()); assertTrue(books.containsKey("123"));
assertTrue(books.containsKey("456"));
assertEquals("Livre A", books.get("123").getTitle());
assertEquals("Livre B", books.get("456").getTitle());
} }
} }