use of com.tvd12.ezydata.example.jpa.exception.DuplicatedBookException in project ezyfox-examples by tvd12.
the class BookServiceTest method addBookFailedDueToBookRepository.
@Test
public void addBookFailedDueToBookRepository() {
// given
final AddBookData addBookData = randomAddBookData();
final Book book = new Book(RandomUtil.randomLong(), addBookData.getCategoryId(), addBookData.getAuthorId(), addBookData.getBookName(), addBookData.getPrice(), addBookData.getReleaseDate(), addBookData.getReleaseTime(), LocalDateTime.now(), LocalDateTime.now());
when(bookRepository.findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId())).thenReturn(book);
// when
final Throwable throwable = assertThrows(() -> sut.addBook(addBookData));
// then
assertTrue(throwable instanceof DuplicatedBookException);
verify(bookRepository, times(1)).findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId());
validateMockitoUsage();
}
use of com.tvd12.ezydata.example.jpa.exception.DuplicatedBookException in project ezyfox-examples by tvd12.
the class BookService method addBook.
public BookData addBook(AddBookData data) {
Book existedBook = bookRepository.findByNameAndAuthorId(data.getBookName(), data.getAuthorId());
if (existedBook != null) {
throw new DuplicatedBookException("author: " + data.getAuthorId() + " has already registered book: " + data.getBookName());
}
final Author author = authorRepository.findById(data.getAuthorId());
if (author == null) {
throw new InvalidAuthorIdException("author: " + data.getAuthorId() + " not found");
}
final Category category = categoryRepository.findById(data.getCategoryId());
if (category == null) {
throw new InvalidCategoryIdException("category: " + data.getCategoryId() + " not found");
}
final Book book = dataToEntityConverter.toEntity(data);
bookRepository.save(book);
return entityToDataConverter.toData(book, author, category);
}
Aggregations