Skip to content

Commit

Permalink
Added test cases for the Search Book and Find All by Category ID methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Optic-Okulist committed Oct 19, 2023
1 parent a1c6581 commit 4b05f2c
Showing 1 changed file with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package spring.boot.bookstore.service.book.impl;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand All @@ -28,14 +29,18 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.junit4.SpringRunner;
import spring.boot.bookstore.dto.book.BookResponseDto;
import spring.boot.bookstore.dto.book.BookSearchParameter;
import spring.boot.bookstore.dto.book.BookWithoutCategoryIdsDto;
import spring.boot.bookstore.dto.book.CreateBookRequestDto;
import spring.boot.bookstore.exception.EntityNotFoundException;
import spring.boot.bookstore.mapper.BookMapper;
import spring.boot.bookstore.model.Book;
import spring.boot.bookstore.model.Category;
import spring.boot.bookstore.repository.BookRepository;
import spring.boot.bookstore.repository.specification.book.builders.BookSpecificationBuilder;

@ExtendWith(MockitoExtension.class)
@RunWith(SpringRunner.class)
Expand All @@ -54,6 +59,9 @@ class BookServiceImplTest {
@Mock
private BookMapper bookMapper;

@Mock
private BookSpecificationBuilder bookSpecificationBuilder;

@BeforeEach
void setUp() {
REQUEST_DTO.setTitle("Eneyida");
Expand Down Expand Up @@ -142,7 +150,7 @@ void getBookByTitle_EmptyTitle_ReturnsEmptyList() {
String emptyTitle = "";
List<BookResponseDto> returnedBooks = bookService.getBookByTitle(emptyTitle);
assertThat(returnedBooks).isNotNull();
assertThat(returnedBooks.isEmpty());
assertThat(returnedBooks).isEmpty();
}

@Test
Expand Down Expand Up @@ -195,4 +203,67 @@ void updateBook_NonExistingId_ThrowsEntityNotFoundException() {
verify(bookRepository, times(1)).findById(INVALID_BOOK_ID);
verifyNoMoreInteractions(bookRepository);
}

@Test
@DisplayName("searchBooks_ValidSearchParameters_ReturnsMatchingBooks")
void searchBooks_ValidSearchParameters_ReturnsMatchingBooks() {
BookSearchParameter searchParameters = new BookSearchParameter(
new String[]{"Eneyida"},
new String[]{"Ivan Kotliarevsky"},
new String[]{"978-0-13-516630-7"},
new String[]{"88"},
"some desk",
new String[]{"burlesque poem"}
);
Specification<Book> expectedSpecification = mock(Specification.class);
when(bookSpecificationBuilder.build(argThat(searchParameters::equals)))
.thenReturn(expectedSpecification);
List<Book> matchingBooks = new ArrayList<>();
matchingBooks.add(new Book());
matchingBooks.add(new Book());
when(bookRepository.findAll(expectedSpecification)).thenReturn(matchingBooks);
List<BookResponseDto> result = bookService.searchBooks(searchParameters);
verify(bookSpecificationBuilder).build(argThat(searchParameters::equals));
verify(bookRepository).findAll(expectedSpecification);
assertThat(result).isNotNull();
assertThat(result).hasSize(2);
verifyNoMoreInteractions(bookRepository, bookSpecificationBuilder);
}

@Test
@DisplayName("searchBooks_EmptySearchParameters_ReturnsAllBooks")
void searchBooks_EmptySearchParameters_ReturnsAllBooks() {
final BookSearchParameter searchParameters = new BookSearchParameter(
new String[0],
new String[0],
new String[0],
new String[0],
"",
new String[0]);
final Specification<Book> bookSpecification = mock(Specification.class);
List<Book> allBooks = new ArrayList<>();
allBooks.add(new Book());
allBooks.add(new Book());
allBooks.add(new Book());
when(bookSpecificationBuilder.build(searchParameters)).thenReturn(bookSpecification);
when(bookRepository.findAll(bookSpecification)).thenReturn(allBooks);
List<BookResponseDto> result = bookService.searchBooks(searchParameters);
verify(bookSpecificationBuilder).build(searchParameters);
verify(bookRepository).findAll(bookSpecification);
assertThat(result).isNotNull();
assertThat(result).hasSize(3);
verifyNoMoreInteractions(bookRepository, bookSpecificationBuilder);
}

@Test
@DisplayName("findAllByCategoryId_ReturnsMatchingBooks")
void findAllByCategoryId_ReturnsMatchingBooks() {
Long categoryId = 9L;
Pageable pageable = PageRequest.of(0, 10);
List<Book> matchingBooks = new ArrayList<>();
when(bookRepository.findBookByCategoriesId(categoryId, pageable)).thenReturn(matchingBooks);
List<BookWithoutCategoryIdsDto> result = bookService
.findAllByCategoryId(categoryId, pageable);
assertEquals(0, result.size());
}
}

0 comments on commit 4b05f2c

Please sign in to comment.