@Test void findAll() { ZonedDateTime now = ZonedDateTime.now(); List<EcoNews> ecoNews = Collections.singletonList(ModelUtils.getEcoNews()); PageRequest pageRequest = PageRequest.of(0, 2); Page<EcoNews> translationPage = new PageImpl<>(ecoNews, pageRequest, ecoNews.size()); List<EcoNewsDto> dtoList = Collections.singletonList( new EcoNewsDto(now, "test image path", 1L, "test title", "test text", null, ModelUtils.getEcoNewsAuthorDto(), Collections.emptyList()) ); PageableDto<EcoNewsDto> pageableDto = new PageableDto<>(dtoList, dtoList.size(), 0,1); when(ecoNewsRepo.findAllByOrderByCreationDateDesc(pageRequest)).thenReturn(translationPage); when(modelMapper.map(ecoNews.get(0), EcoNewsDto.class)).thenReturn(dtoList.get(0)); PageableDto<EcoNewsDto> actual = ecoNewsService.findAll(pageRequest); assertEquals(pageableDto, actual); }
@Override public PageableDto<EcoNewsDto> findAll(Pageable page) { Page<EcoNews> pages = ecoNewsRepo.findAllByOrderByCreationDateDesc(page); List<EcoNewsDto> ecoNewsDtos = pages .stream() .map(ecoNews -> modelMapper.map(ecoNews, EcoNewsDto.class)) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }
EcoNewsServiceImpl implements EcoNewsService { @Override public PageableDto<EcoNewsDto> findAll(Pageable page) { Page<EcoNews> pages = ecoNewsRepo.findAllByOrderByCreationDateDesc(page); List<EcoNewsDto> ecoNewsDtos = pages .stream() .map(ecoNews -> modelMapper.map(ecoNews, EcoNewsDto.class)) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); } }
EcoNewsServiceImpl implements EcoNewsService { @Override public PageableDto<EcoNewsDto> findAll(Pageable page) { Page<EcoNews> pages = ecoNewsRepo.findAllByOrderByCreationDateDesc(page); List<EcoNewsDto> ecoNewsDtos = pages .stream() .map(ecoNews -> modelMapper.map(ecoNews, EcoNewsDto.class)) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  }
EcoNewsServiceImpl implements EcoNewsService { @Override public PageableDto<EcoNewsDto> findAll(Pageable page) { Page<EcoNews> pages = ecoNewsRepo.findAllByOrderByCreationDateDesc(page); List<EcoNewsDto> ecoNewsDtos = pages .stream() .map(ecoNews -> modelMapper.map(ecoNews, EcoNewsDto.class)) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest,
                                      MultipartFile image, String email); @Cacheable(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME) @Override List<EcoNewsDto> getThreeLastEcoNews(); @Override List<EcoNewsDto> getThreeRecommendedEcoNews(Long openedEcoNewsId); @Override PageableDto<EcoNewsDto> findAll(Pageable page); @Override PageableDto<EcoNewsDto> find(Pageable page, List<String> tags); @Override EcoNews findById(Long id); @Override EcoNewsDto findDtoById(Long id); @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override void delete(Long id); @Override PageableDto<SearchNewsDto> search(String searchQuery); @Override Long getAmountOfPublishedNewsByUserId(Long id); }
EcoNewsServiceImpl implements EcoNewsService { @Override public PageableDto<EcoNewsDto> findAll(Pageable page) { Page<EcoNews> pages = ecoNewsRepo.findAllByOrderByCreationDateDesc(page); List<EcoNewsDto> ecoNewsDtos = pages .stream() .map(ecoNews -> modelMapper.map(ecoNews, EcoNewsDto.class)) .collect(Collectors.toList()); return new PageableDto<>( ecoNewsDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override AddEcoNewsDtoResponse save(AddEcoNewsDtoRequest addEcoNewsDtoRequest,
                                      MultipartFile image, String email); @Cacheable(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME) @Override List<EcoNewsDto> getThreeLastEcoNews(); @Override List<EcoNewsDto> getThreeRecommendedEcoNews(Long openedEcoNewsId); @Override PageableDto<EcoNewsDto> findAll(Pageable page); @Override PageableDto<EcoNewsDto> find(Pageable page, List<String> tags); @Override EcoNews findById(Long id); @Override EcoNewsDto findDtoById(Long id); @CacheEvict(value = CacheConstants.NEWEST_ECO_NEWS_CACHE_NAME, allEntries = true) @Override void delete(Long id); @Override PageableDto<SearchNewsDto> search(String searchQuery); @Override Long getAmountOfPublishedNewsByUserId(Long id);  }