Api java #6
+42
@@ -0,0 +1,42 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
public final class BookRepository {
|
||||||
|
|
||||||
|
private final List<Book> books = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<Book> findAll() {
|
||||||
|
return books;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAll() {
|
||||||
|
books.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book save(Book book) {
|
||||||
|
Optional<Book> existing = findByIsbn(book.getIsbn());
|
||||||
|
existing.ifPresent(books::remove);
|
||||||
|
books.add(book);
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Book> findByIsbn(long isbn) {
|
||||||
|
return books.stream()
|
||||||
|
.filter(b -> b.getIsbn() == isbn)
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsByIsbn(long isbn) {
|
||||||
|
return books.stream().anyMatch(b -> b.getIsbn() == isbn);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(Book book) {
|
||||||
|
books.remove(book);
|
||||||
|
}
|
||||||
|
}
|
||||||
+149
@@ -0,0 +1,149 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Category;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class BookRepositoryTest {
|
||||||
|
|
||||||
|
private BookRepository repository;
|
||||||
|
private Book book1;
|
||||||
|
private Book book2;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
repository = new BookRepository();
|
||||||
|
|
||||||
|
book1 = Book.builder()
|
||||||
|
.isbn(1111111111111L)
|
||||||
|
.title("Book One")
|
||||||
|
.author("Author A")
|
||||||
|
.publisher("Pub")
|
||||||
|
.publicationDate(LocalDate.of(2020, 1, 1))
|
||||||
|
.price(10.0)
|
||||||
|
.quantity(2)
|
||||||
|
.categories(List.of(Category.FICTION))
|
||||||
|
.language("EN")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
book2 = Book.builder()
|
||||||
|
.isbn(2222222222222L)
|
||||||
|
.title("Book Two")
|
||||||
|
.author("Author B")
|
||||||
|
.publisher("Pub")
|
||||||
|
.publicationDate(LocalDate.of(2021, 1, 1))
|
||||||
|
.price(20.0)
|
||||||
|
.quantity(0)
|
||||||
|
.categories(List.of(Category.HISTORY))
|
||||||
|
.language("FR")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("New repository should be empty")
|
||||||
|
void testNewRepositoryIsEmpty() {
|
||||||
|
assertTrue(repository.findAll().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Save operations")
|
||||||
|
class SaveOperations {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should add a new book")
|
||||||
|
void testSave() {
|
||||||
|
Book saved = repository.save(book1);
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(book1.getIsbn(), saved.getIsbn());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should replace existing book with same ISBN (idempotency)")
|
||||||
|
void testSaveReplacesExisting() {
|
||||||
|
repository.save(book1);
|
||||||
|
Book updated = Book.builder()
|
||||||
|
.isbn(book1.getIsbn())
|
||||||
|
.title("New title")
|
||||||
|
.author("Author A")
|
||||||
|
.publisher("Pub")
|
||||||
|
.publicationDate(LocalDate.of(2020, 1, 1))
|
||||||
|
.price(10.0)
|
||||||
|
.quantity(99)
|
||||||
|
.categories(List.of(Category.FICTION))
|
||||||
|
.language("EN")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
repository.save(updated);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(99, repository.findByIsbn(book1.getIsbn()).orElseThrow().getQuantity());
|
||||||
|
assertEquals("New title", repository.findByIsbn(book1.getIsbn()).orElseThrow().getTitle());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find operations")
|
||||||
|
class FindOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void seed() {
|
||||||
|
repository.save(book1);
|
||||||
|
repository.save(book2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByIsbn returns the matching book")
|
||||||
|
void testFindByIsbn() {
|
||||||
|
Optional<Book> found = repository.findByIsbn(book1.getIsbn());
|
||||||
|
assertTrue(found.isPresent());
|
||||||
|
assertEquals(book1.getTitle(), found.get().getTitle());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByIsbn returns empty when not found")
|
||||||
|
void testFindByIsbnNotFound() {
|
||||||
|
assertTrue(repository.findByIsbn(99L).isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("existsByIsbn returns true / false consistently")
|
||||||
|
void testExistsByIsbn() {
|
||||||
|
assertTrue(repository.existsByIsbn(book1.getIsbn()));
|
||||||
|
assertFalse(repository.existsByIsbn(99L));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Delete operations")
|
||||||
|
class DeleteOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void seed() {
|
||||||
|
repository.save(book1);
|
||||||
|
repository.save(book2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("delete should remove the specified book")
|
||||||
|
void testDelete() {
|
||||||
|
repository.delete(book1);
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertFalse(repository.existsByIsbn(book1.getIsbn()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("deleteAll should clear the repository")
|
||||||
|
void testDeleteAll() {
|
||||||
|
repository.deleteAll();
|
||||||
|
assertTrue(repository.findAll().isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user