@Test void search() { List<TipsAndTricks> tipsAndTricks = Collections.singletonList(ModelUtils.getTipsAndTricks()); PageRequest pageRequest = PageRequest.of(0, 3); Page<TipsAndTricks> page = new PageImpl<>(tipsAndTricks, pageRequest, tipsAndTricks.size()); List<SearchTipsAndTricksDto> dtoList = page.stream() .map(t -> modelMapper.map(t, SearchTipsAndTricksDto.class)) .collect(Collectors.toList()); PageableDto<SearchTipsAndTricksDto> pageableDto = new PageableDto<>(dtoList, dtoList.size(), 0,1); when(tipsAndTricksRepo.searchTipsAndTricks(pageRequest, tipsAndTricks.get(0).getTitle())).thenReturn(page); when(modelMapper.map(tipsAndTricks.get(0), SearchTipsAndTricksDto.class)).thenReturn(dtoList.get(0)); PageableDto<SearchTipsAndTricksDto> actual = tipsAndTricksService.search(tipsAndTricks.get(0).getTitle()); assertEquals(pageableDto, actual); }
@Override public PageableDto<SearchTipsAndTricksDto> search(String searchQuery) { Page<TipsAndTricks> page = tipsAndTricksRepo.searchTipsAndTricks(PageRequest.of(0, 3), searchQuery); List<SearchTipsAndTricksDto> tipsAndTricksDtos = page.stream() .map(tipsAndTricks -> modelMapper.map(tipsAndTricks, SearchTipsAndTricksDto.class)) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksDtos, page.getTotalElements(), page.getPageable().getPageNumber(), page.getTotalPages() ); }
TipsAndTricksServiceImpl implements TipsAndTricksService { @Override public PageableDto<SearchTipsAndTricksDto> search(String searchQuery) { Page<TipsAndTricks> page = tipsAndTricksRepo.searchTipsAndTricks(PageRequest.of(0, 3), searchQuery); List<SearchTipsAndTricksDto> tipsAndTricksDtos = page.stream() .map(tipsAndTricks -> modelMapper.map(tipsAndTricks, SearchTipsAndTricksDto.class)) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksDtos, page.getTotalElements(), page.getPageable().getPageNumber(), page.getTotalPages() ); } }
TipsAndTricksServiceImpl implements TipsAndTricksService { @Override public PageableDto<SearchTipsAndTricksDto> search(String searchQuery) { Page<TipsAndTricks> page = tipsAndTricksRepo.searchTipsAndTricks(PageRequest.of(0, 3), searchQuery); List<SearchTipsAndTricksDto> tipsAndTricksDtos = page.stream() .map(tipsAndTricks -> modelMapper.map(tipsAndTricks, SearchTipsAndTricksDto.class)) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksDtos, page.getTotalElements(), page.getPageable().getPageNumber(), page.getTotalPages() ); }  }
TipsAndTricksServiceImpl implements TipsAndTricksService { @Override public PageableDto<SearchTipsAndTricksDto> search(String searchQuery) { Page<TipsAndTricks> page = tipsAndTricksRepo.searchTipsAndTricks(PageRequest.of(0, 3), searchQuery); List<SearchTipsAndTricksDto> tipsAndTricksDtos = page.stream() .map(tipsAndTricks -> modelMapper.map(tipsAndTricks, SearchTipsAndTricksDto.class)) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksDtos, page.getTotalElements(), page.getPageable().getPageNumber(), page.getTotalPages() ); }  @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override TipsAndTricksDtoResponse save(TipsAndTricksDtoRequest tipsAndTricksDtoRequest, MultipartFile image,
                                         String email); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void update(TipsAndTricksDtoManagement tipsAndTricksDtoManagement,
                       MultipartFile image); @Cacheable(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME) @Override PageableDto<TipsAndTricksDtoResponse> findAll(Pageable page); @Override PageableDto<TipsAndTricksDtoResponse> find(Pageable page, List<String> tags); @Override TipsAndTricksDtoResponse findDtoById(Long id); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void delete(Long id); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void deleteAll(List<Long> listId); TipsAndTricks findById(Long id); @Override TipsAndTricksDtoManagement findManagementDtoById(Long id); @Override PageableDto<SearchTipsAndTricksDto> search(String searchQuery); @Override Long getAmountOfWrittenTipsAndTrickByUserId(Long id); }
TipsAndTricksServiceImpl implements TipsAndTricksService { @Override public PageableDto<SearchTipsAndTricksDto> search(String searchQuery) { Page<TipsAndTricks> page = tipsAndTricksRepo.searchTipsAndTricks(PageRequest.of(0, 3), searchQuery); List<SearchTipsAndTricksDto> tipsAndTricksDtos = page.stream() .map(tipsAndTricks -> modelMapper.map(tipsAndTricks, SearchTipsAndTricksDto.class)) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksDtos, page.getTotalElements(), page.getPageable().getPageNumber(), page.getTotalPages() ); }  @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override TipsAndTricksDtoResponse save(TipsAndTricksDtoRequest tipsAndTricksDtoRequest, MultipartFile image,
                                         String email); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void update(TipsAndTricksDtoManagement tipsAndTricksDtoManagement,
                       MultipartFile image); @Cacheable(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME) @Override PageableDto<TipsAndTricksDtoResponse> findAll(Pageable page); @Override PageableDto<TipsAndTricksDtoResponse> find(Pageable page, List<String> tags); @Override TipsAndTricksDtoResponse findDtoById(Long id); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void delete(Long id); @CacheEvict(value = CacheConstants.TIPS_AND_TRICKS_CACHE_NAME, allEntries = true) @Override void deleteAll(List<Long> listId); TipsAndTricks findById(Long id); @Override TipsAndTricksDtoManagement findManagementDtoById(Long id); @Override PageableDto<SearchTipsAndTricksDto> search(String searchQuery); @Override Long getAmountOfWrittenTipsAndTrickByUserId(Long id);  }