This commit is contained in:
2025-06-13 21:12:24 +02:00
parent cc4dfa140a
commit cdf4c046bc
6 changed files with 375 additions and 2 deletions

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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());
}
}

View File

@@ -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";
}