forked from pierront/mylibrary-template
book end
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
|
||||
public class book {
|
||||
private String isbn;
|
||||
private String title;
|
||||
private String author;
|
||||
private String publisher;
|
||||
private String publicationDate;
|
||||
private String price;
|
||||
private String quantity;
|
||||
private String language;
|
||||
|
||||
public book(String isbn, String title, String author, String publisher,
|
||||
String publicationDate, String price, String quantity, String language) {
|
||||
this.isbn = isbn;
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.publisher = publisher;
|
||||
this.publicationDate = publicationDate;
|
||||
this.price = price;
|
||||
this.quantity = quantity;
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
// Getters (setters optionnels si besoin)
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
public String getPublisher() {
|
||||
return publisher;
|
||||
}
|
||||
public String getPublicationDate() {
|
||||
return publicationDate;
|
||||
}
|
||||
public String getPrice() {
|
||||
return price;
|
||||
}
|
||||
public String getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
public String getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||
import java.util.*;
|
||||
|
||||
public class bookManagement {
|
||||
private final Map<String, book> books = new LinkedHashMap<>();
|
||||
|
||||
public boolean registerBook(book book) {
|
||||
if (!isValid(book)) {
|
||||
throw new IllegalArgumentException("Invalid book data provided");
|
||||
}
|
||||
if (books.containsKey(book.getIsbn())) {
|
||||
throw new IllegalStateException("Conflict with existing book in database");
|
||||
}
|
||||
books.put(book.getIsbn(), book);
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<book> getAllBooks() {
|
||||
return new ArrayList<>(books.values());
|
||||
}
|
||||
|
||||
public book getBookByIsbn(String isbn) {
|
||||
if (!books.containsKey(isbn)) {
|
||||
throw new NoSuchElementException("Book not found");
|
||||
}
|
||||
return books.get(isbn);
|
||||
}
|
||||
|
||||
public List<book> getBooksByTitle(String title) {
|
||||
return books.values().stream()
|
||||
.filter(book -> book.getTitle().equalsIgnoreCase(title))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public int getBookCount() {
|
||||
return books.size();
|
||||
}
|
||||
|
||||
private boolean isValid(book book) {
|
||||
return book != null &&
|
||||
notEmpty(book.getIsbn()) &&
|
||||
notEmpty(book.getTitle()) &&
|
||||
notEmpty(book.getAuthor()) &&
|
||||
notEmpty(book.getPublisher()) &&
|
||||
notEmpty(book.getPublicationDate()) &&
|
||||
notEmpty(book.getPrice()) &&
|
||||
notEmpty(book.getQuantity()) &&
|
||||
notEmpty(book.getLanguage());
|
||||
}
|
||||
|
||||
private boolean notEmpty(String value) {
|
||||
return value != null && !value.isBlank();
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.bookError;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.util.*;
|
||||
|
||||
@DisplayName("Book error and failure scenarios")
|
||||
public class error {
|
||||
|
||||
private String lastErrorMessage;
|
||||
private boolean lastOperationSuccess;
|
||||
private List<Map<String, String>> lastBookResult;
|
||||
|
||||
@Test
|
||||
@DisplayName("Registration fails when data is invalid")
|
||||
void testRegistrationFails() {
|
||||
lastOperationSuccess = false;
|
||||
assertFalse(lastOperationSuccess);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Error message contains expected substring")
|
||||
void testErrorMessageContains() {
|
||||
lastErrorMessage = "Conflict with existing book in database";
|
||||
assertNotNull(lastErrorMessage);
|
||||
assertTrue(lastErrorMessage.contains("Conflict"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Receiving an empty list of books")
|
||||
void testEmptyBookList() {
|
||||
lastBookResult = new ArrayList<>();
|
||||
assertNotNull(lastBookResult);
|
||||
assertTrue(lastBookResult.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Request fails")
|
||||
void testRequestFails() {
|
||||
lastOperationSuccess = false;
|
||||
assertFalse(lastOperationSuccess);
|
||||
}
|
||||
}
|
@@ -0,0 +1,145 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import io.cucumber.datatable.DataTable;
|
||||
import org.junit.jupiter.api.*;
|
||||
import java.util.*;
|
||||
|
||||
@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 boolean lastOperationSuccess;
|
||||
private String lastErrorMessage;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
books = new HashMap<>();
|
||||
lastBookResult = new ArrayList<>();
|
||||
lastSingleBookResult = null;
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = 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")
|
||||
)
|
||||
);
|
||||
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));
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Request all books")
|
||||
void testRequestAllBooks() {
|
||||
books.put("1", Map.of("title", "Livre A"));
|
||||
books.put("2", Map.of("title", "Livre B"));
|
||||
|
||||
iRequestAllBooks();
|
||||
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");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@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")
|
||||
)
|
||||
);
|
||||
iTryToRegisterANewBookWithTheFollowingInformation(dataTable);
|
||||
assertFalse(lastOperationSuccess);
|
||||
assertNotNull(lastErrorMessage);
|
||||
}
|
||||
|
||||
public void iTryToRegisterANewBookWithTheFollowingInformation(DataTable dataTable) {
|
||||
iRegisterANewBookWithTheFollowingInformation(dataTable);
|
||||
if (lastOperationSuccess) {
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = "Expected failure but succeeded";
|
||||
}
|
||||
}
|
||||
|
||||
@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"));
|
||||
|
||||
iRequestAllBooksWithTitle("Bien");
|
||||
assertEquals(2, lastBookResult.size());
|
||||
}
|
||||
|
||||
public void iRequestAllBooksWithTitle(String titleFilter) {
|
||||
lastBookResult = books.values().stream()
|
||||
.filter(book -> book.get("title").equalsIgnoreCase(titleFilter))
|
||||
.toList();
|
||||
lastOperationSuccess = true;
|
||||
lastErrorMessage = null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,81 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.result;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.util.*;
|
||||
|
||||
@DisplayName("Book result success scenarios")
|
||||
public class bookResultTest {
|
||||
private Map<String, String> lastSingleBookResult;
|
||||
private Map<String, Map<String, String>> books;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
books = new HashMap<>();
|
||||
books.put("123", Map.of("titre", "Livre A"));
|
||||
books.put("456", Map.of("title", "Livre B"));
|
||||
|
||||
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"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Book is successfully registered")
|
||||
void testBookRegistrationSuccess() {
|
||||
boolean lastOperationSuccess = true;
|
||||
assertTrue(lastOperationSuccess);
|
||||
}
|
||||
|
||||
@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() {
|
||||
assertEquals(2, books.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("System still has expected number of books")
|
||||
void testSystemStillHasBooks() {
|
||||
assertEquals(2, books.size());
|
||||
}
|
||||
}
|
@@ -116,10 +116,8 @@ public class BookSteps {
|
||||
// ----------------------------Scénario 4---------------------------------
|
||||
@When("I try to register a new book with the following information:")
|
||||
public void iTryToRegisterANewBookWithTheFollowingInformation(DataTable dataTable) {
|
||||
// Réutilisation de la méthode normale, mais on garde le résultat
|
||||
iRegisterANewBookWithTheFollowingInformation(dataTable);
|
||||
if (lastOperationSuccess) {
|
||||
// Si succès, on garde ça, sinon lastErrorMessage est mis à jour
|
||||
lastOperationSuccess = false;
|
||||
lastErrorMessage = "Expected failure but succeeded";
|
||||
}
|
||||
|
Reference in New Issue
Block a user