RepositoryTodoSearchService implements TodoSearchService { @Transactional(readOnly = true) @Override public Page<TodoDTO> findBySearchTerm(String searchTerm, Pageable pageable) { LOGGER.info("Finding {} todo entries for page {} by using search term: {}", pageable.getPageSize(), pageable.getPageNumber(), searchTerm ); Page<Todo> searchResults = repository.findBySearchTerm(searchTerm, pageable); LOGGER.info("Found {} todo entries for page: {}", searchResults.getNumberOfElements(), searchResults.getNumber() ); List<TodoDTO> dtos = transformer.convertList(searchResults.getContent(), TodoDTO.class); return new PageImpl<>(dtos, new PageRequest(searchResults.getNumber(), searchResults.getSize(), searchResults.getSort()), searchResults.getTotalElements() ); } @Autowired  RepositoryTodoSearchService(TodoRepository repository, JTransfo transformer); @Transactional(readOnly = true) @Override Page<TodoDTO> findBySearchTerm(String searchTerm, Pageable pageable);  }
@Test public void findBySearchTerm_NoTodoEntriesFound_ShouldReturnEmptyList() { Page<Todo> page = new PageBuilder<Todo>() .pageNumber(PAGE_NUMBER) .pageSize(PAGE_SIZE) .sort(SORT) .build(); when(repositoryMock.findBySearchTerm(SEARCH_TERM, pageRequest)).thenReturn(page); Page<TodoDTO> searchResults = service.findBySearchTerm(SEARCH_TERM, pageRequest); verify(repositoryMock, times(1)).findBySearchTerm(SEARCH_TERM, pageRequest); verifyNoMoreInteractions(repositoryMock); assertThatPage(searchResults) .isFirstPage() .isLastPage() .hasPageNumber(PAGE_NUMBER) .hasPageSize(PAGE_SIZE) .hasSort(SORT) .hasTotalNumberOfElements(ZERO_ELEMENTS) .hasTotalNumberOfPages(ZERO_PAGES) .isEmpty(); }
@Test public void findBySearchTerm_OneTodoEntryFound_ShouldReturnFoundTodoEntry() { Todo expectedTodoEntry = Todo.getBuilder(TITLE) .creationTime(CREATION_TIME) .description(DESCRIPTION) .id(ID) .modificationTime(MODIFICATION_TIME) .build(); Page<Todo> page = new PageBuilder<Todo>() .itemsOnPage(expectedTodoEntry) .pageNumber(PAGE_NUMBER) .pageSize(PAGE_SIZE) .totalNumberOfItems(ONE_ELEMENT) .sort(SORT) .build(); when(repositoryMock.findBySearchTerm(SEARCH_TERM, pageRequest)).thenReturn(page); Page<TodoDTO> searchResults = service.findBySearchTerm(SEARCH_TERM, pageRequest); verify(repositoryMock, times(1)).findBySearchTerm(SEARCH_TERM, pageRequest); verifyNoMoreInteractions(repositoryMock); assertThatPage(searchResults) .isFirstPage() .isLastPage() .hasPageNumber(PAGE_NUMBER) .hasPageSize(PAGE_SIZE) .hasSort(SORT) .hasNumberOfElements(ONE_ELEMENT_ON_PAGE) .hasTotalNumberOfElements(ONE_ELEMENT) .hasTotalNumberOfPages(ONE_PAGE); TodoDTO found = searchResults.getContent().get(0); assertThatTodoDTO(found) .hasDescription(DESCRIPTION) .hasId(ID) .hasTitle(TITLE) .wasCreatedAt(CREATION_TIME_STRING) .wasModifiedAt(MODIFICATION_TIME_STRING); }