Search in sources :

Example 6 with AddBookData

use of com.tvd12.ezydata.example.jpa.data.AddBookData in project ezyfox-examples by tvd12.

the class BookServiceTest method addBookSuccess.

@Test
public void addBookSuccess() {
    // given
    final AddBookData addBookData = randomAddBookData();
    when(bookRepository.findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId())).thenReturn(null);
    final Author author = new Author(RandomUtil.randomLong(), RandomUtil.randomShortAlphabetString());
    when(authorRepository.findById(addBookData.getAuthorId())).thenReturn(author);
    final Category category = new Category(RandomUtil.randomLong(), RandomUtil.randomShortAlphabetString());
    when(categoryRepository.findById(addBookData.getCategoryId())).thenReturn(category);
    final Book book = new Book(0L, addBookData.getCategoryId(), addBookData.getAuthorId(), addBookData.getBookName(), addBookData.getPrice(), addBookData.getReleaseDate(), addBookData.getReleaseTime(), LocalDateTime.now(), LocalDateTime.now());
    when(dataToEntityConverter.toEntity(addBookData)).thenReturn(book);
    doAnswer(it -> {
        Book inputBook = (Book) it.getArguments()[0];
        inputBook.setId(RandomUtil.randomLong());
        return null;
    }).when(bookRepository).save(book);
    BookData bookData = BookData.builder().id(book.getId()).name(book.getName()).author(AuthorData.builder().id(author.getId()).name(author.getName()).build()).category(CategoryData.builder().id(category.getId()).name(category.getName()).build()).price(addBookData.getPrice()).releaseDate(addBookData.getReleaseDate()).releaseTime(addBookData.getReleaseTime()).build();
    when(entityToDataConverter.toData(book, author, category)).thenReturn(bookData);
    // when
    BookData actual = sut.addBook(addBookData);
    // then
    assertThat(actual).isEqualsTo(bookData);
    verify(bookRepository, times(1)).findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId());
    verify(authorRepository, times(1)).findById(addBookData.getAuthorId());
    verify(categoryRepository, times(1)).findById(addBookData.getCategoryId());
    verify(bookRepository, times(1)).save(book);
    verify(dataToEntityConverter, times(1)).toEntity(addBookData);
    verify(entityToDataConverter, times(1)).toData(book, author, category);
    validateMockitoUsage();
}
Also used : Category(com.tvd12.ezydata.example.jpa.entity.Category) Book(com.tvd12.ezydata.example.jpa.entity.Book) AddBookData(com.tvd12.ezydata.example.jpa.data.AddBookData) Author(com.tvd12.ezydata.example.jpa.entity.Author) BookData(com.tvd12.ezydata.example.jpa.data.BookData) AddBookData(com.tvd12.ezydata.example.jpa.data.AddBookData) Test(org.junit.jupiter.api.Test)

Aggregations

Book (com.tvd12.ezydata.example.jpa.entity.Book)5 AddBookData (com.tvd12.ezydata.example.jpa.data.AddBookData)4 BookData (com.tvd12.ezydata.example.jpa.data.BookData)2 Author (com.tvd12.ezydata.example.jpa.entity.Author)2 Category (com.tvd12.ezydata.example.jpa.entity.Category)2 DuplicatedBookException (com.tvd12.ezydata.example.jpa.exception.DuplicatedBookException)2 Test (org.junit.jupiter.api.Test)2 InvalidAuthorIdException (com.tvd12.ezydata.example.jpa.exception.InvalidAuthorIdException)1 InvalidCategoryIdException (com.tvd12.ezydata.example.jpa.exception.InvalidCategoryIdException)1 ArrayList (java.util.ArrayList)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1