IdeaService { public Page<IdeaOut> fetchIdeasByStatus(String campaignId, Set<IdeaStatus> statusSet, Integer page, Integer pageSize, UserEntity requestor) { Assert.notNull(campaignId, "campaignId must not be null"); final Page<IdeaEntity> dbRes = ideaRepository.findByCampaignIdAndStatusIn( campaignId, statusSet, calcPageRequest(page, pageSize) ); return new PageImpl<>(toIdeas(dbRes.getContent(), requestor), calcPageRequest(page, pageSize), dbRes.getTotalElements()); }  IdeaOut fetchIdea(String ideaId, UserEntity requestor); Page<IdeaOut> fetchIdeasByStatus(String campaignId, Set<IdeaStatus> statusSet, Integer page, Integer pageSize, UserEntity requestor); Page<IdeaOut> fetchIdeasByRequestorHasVoted(String campaignId, boolean hasVoted, Integer page, Integer pageSize, UserEntity requestor); List<IdeaOut> fetchIdeasByCampaignAndCreator(String campaignId, UserEntity creator, UserEntity requestor); IdeaOut createNewIdea(String campaignId, IdeaIn cmd, UserEntity creator); IdeaOut modifyIdea(String ideaId, IdeaIn cmd, UserEntity requestingUser); void approveIdea(String campaignId, String ideaId, UserEntity approvingAdmin); void rejectIdea(String campaignId, String ideaId, String rejectionComment, UserEntity approvingAdmin); void voteForIdea(VoteCmd voteCmd, UserEntity voter);  }
@Test public void fetchIdeasByStatus_ShouldRequestWithDefaultPagesize_onNoPageOrSizeGiven() { final String campaignId = "test_campId"; final ArgumentCaptor<PageRequest> pReqCap = ArgumentCaptor.forClass(PageRequest.class); doReturn(new PageImpl<IdeaEntity>(Collections.emptyList())).when(ideaRepository) .findByCampaignIdAndStatusIn(anyString(), anySet(), pReqCap.capture()); ideaService.fetchIdeasByStatus(campaignId, singleton(PUBLISHED), 1, null, givenUserEntity("requestorId")); assertThat(pReqCap.getValue().getPageSize(), is(IdeaService.DEFAULT_PAGE_SIZE)); assertThat(pReqCap.getValue().getPageNumber(), is(0)); ideaService.fetchIdeasByStatus(campaignId, singleton(PUBLISHED), null, 200, givenUserEntity("requestorId")); assertThat(pReqCap.getValue().getPageSize(), is(IdeaService.DEFAULT_PAGE_SIZE)); assertThat(pReqCap.getValue().getPageNumber(), is(0)); }
@Test public void fetchIdeasByStatus_ShouldRequestWith_GivenPageNumberAndCount() { final String campaignId = "test_campId"; final ArgumentCaptor<PageRequest> pReqCap = ArgumentCaptor.forClass(PageRequest.class); doReturn(new PageImpl<IdeaEntity>(Collections.emptyList())).when(ideaRepository) .findByCampaignIdAndStatusIn(anyString(), eq(singleton(PUBLISHED)), pReqCap.capture()); ideaService.fetchIdeasByStatus(campaignId, singleton(PUBLISHED), 17, 120, givenUserEntity("requestorId")); assertThat(pReqCap.getValue().getPageSize(), is(120)); assertThat(pReqCap.getValue().getPageNumber(), is(17)); }
@Test public void fetchIdeasByStatus_ShouldResetPagesizeToDefault_OnMaxPagesizeExceeded() { final String campaignId = "test_campId"; final ArgumentCaptor<PageRequest> pReqCap = ArgumentCaptor.forClass(PageRequest.class); doReturn(new PageImpl<IdeaEntity>(Collections.emptyList())).when(ideaRepository) .findByCampaignIdAndStatusIn(anyString(), eq(singleton(PUBLISHED)), pReqCap.capture()); ideaService.fetchIdeasByStatus(campaignId, singleton(PUBLISHED), 17, IdeaService.MAX_PAGE_SIZE + 1, givenUserEntity("requestorId")); assertThat(pReqCap.getValue().getPageSize(), is(IdeaService.DEFAULT_PAGE_SIZE)); assertThat(pReqCap.getValue().getPageNumber(), is(17)); }
@Test public void fetchIdeasByStatus_ShouldEnrichIdeas_ByRating() { final String campaignId = "test_campId"; final ArgumentCaptor<PageRequest> pReqCap = ArgumentCaptor.forClass(PageRequest.class); final IdeaEntity expIdea = Fixtures.givenIdeaEntity("ideaId"); doReturn(new PageImpl<>(singletonList(expIdea))).when(ideaRepository) .findByCampaignIdAndStatusIn(anyString(), eq(singleton(PUBLISHED)), pReqCap.capture()); final Rating expRating = givenVoteRepositoryReturnsVotes(expIdea.getId()); final Page<IdeaOut> res = ideaService.fetchIdeasByStatus(campaignId, singleton(PUBLISHED), 17, IdeaService.MAX_PAGE_SIZE + 1, givenUserEntity("requestorId")); assertThat(res.getContent().get(0).getRating(), equalTo(expRating)); }