forked from pierront/mylibrary-template
BookUseCaseTest fait ?
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerInfo;
|
||||
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 org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class BookUseCaseTest {
|
||||
|
||||
@Mock
|
||||
private BookRepository customerRepository;
|
||||
|
||||
@InjectMocks
|
||||
private BookUseCase bookUseCase;
|
||||
|
||||
private String bookISBN;
|
||||
private Book testBook;
|
||||
private BookInfo validBookInfo;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
testBook = Book.builder()
|
||||
.isbn(bookISBN)
|
||||
.title("LivreRandom")
|
||||
.author("John Doe")
|
||||
.publisher("RandomPublisher")
|
||||
.publicationDate(date)
|
||||
.price(12.5)
|
||||
.initialStock(50)
|
||||
.categories(cat)
|
||||
.description("Je suis un livre qui est composé de mots.")
|
||||
.language("Francais")
|
||||
.build();
|
||||
|
||||
validBookInfo = new CustomerInfo("LivreRandom", "John Doe", "RandomPublisher", date, 12.5, 50, cat, "Je suis un livre qui est composé de mots.", "Francais");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Nested
|
||||
@DisplayName("Register Book Tests")
|
||||
public class RegisterBookTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should register customer when valid data is provided")
|
||||
void testRegisterBookWithValidData() throws NotValidBookException {
|
||||
when(BookRepository.save(any(Book.class))).thenReturn(testBook);
|
||||
|
||||
String registeredISBN = BookUseCase.registerBook(validBookInfo);
|
||||
|
||||
assertNotNull(registeredISBN);
|
||||
assertEquals(bookISBN, registeredISBN);
|
||||
verify(bookRepository, times(1)).save(any(Book.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when customer data is not valid")
|
||||
void testRegisterBookWithInvalidData() {
|
||||
BookInfo invalidBookInfo = new BookInfo("", "", "");
|
||||
|
||||
assertThrows(NotValidBookException.class,
|
||||
() -> bookUseCase.registerBook(invalidBookInfo));
|
||||
|
||||
verify(bookRepository, never()).save(any(Book.class));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
@DisplayName("Find customer tests")
|
||||
class FindBookTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return book when ISBN exists")
|
||||
void testFindBookByISBN() {
|
||||
when(BookRepository.findBookByISBN("1234567890123")).thenReturn(Optional.of(testISBN));
|
||||
|
||||
Optional<BookDTO> foundBook = bookUseCase.findBookByISBN("1234567890123");
|
||||
|
||||
assertTrue(foundBook.isPresent());
|
||||
assertEquals(testBook.getISBN(), foundBook.get().getISBN());
|
||||
assertEquals(testBook.getName(), foundBook.get().getName());
|
||||
verify(bookRepository, times(1)).findBookByISBN("1234567890123");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when ISBN doesn't exist")
|
||||
void testFindBookByISBNNotFound() {
|
||||
when(bookRepository.findByISBN("9999999999999")).thenReturn(Optional.empty());
|
||||
|
||||
Optional<BookDTO> foundBook = bookUseCase.findBookByISBN("9999999999999");
|
||||
|
||||
assertTrue(foundBook.isEmpty());
|
||||
verify(bookRepository, times(1)).findByISBN("9999999999999");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Delete book tests")
|
||||
class DeleteBookTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should delete book when ISBN exists")
|
||||
void testDeleteBook() throws BookNotFoundException {
|
||||
when(bookRepository.findByISBN(bookISBN)).thenReturn(Optional.of(testBook));
|
||||
doNothing().when(bookRepository).delete(testBook);
|
||||
|
||||
bookUseCase.deleteBook(bookId);
|
||||
|
||||
verify(bookRepository, times(1)).findByISBN(bookId);
|
||||
verify(bookRepository, times(1)).delete(testBook);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when book ISBN doesn't exist")
|
||||
void testDeleteBookNotFound() {
|
||||
ISBN nonExistentId = ISBN.randomISBN();
|
||||
when(bookRepository.findByISBN(nonExistentISBN)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(BookNotFoundException.class,
|
||||
() -> bookUseCase.deleteBook(nonExistentISBN));
|
||||
|
||||
verify(bookRepository, times(1)).findByISBN(nonExistentISBN);
|
||||
verify(bookRepository, never()).delete(any(Book.class));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user