VotingService { public void voteForIdea(IdeaEntity idea, IdeasCampaignEntity campaign, UserEntity voter, int vote) { Assert.isTrue(idea.getCampaignId().equals(campaign.getId()), "Idea must belong to the campaign."); if (DateTime.now().isBefore(campaign.getStartDate()) || DateTime.now().isAfter(campaign.getEndDate())) { throw InvalidRequestException.campaignNotActive(); } if (vote == 0) { voteRepository.delete(new VoteId(voter.getId(), idea.getId())); } else { final VoteEntity voteRes = idea.vote(voter, vote); voteRepository.save(voteRes); } }  void voteForIdea(IdeaEntity idea, IdeasCampaignEntity campaign, UserEntity voter, int vote);  }
@Test(expected = IllegalArgumentException.class) public void voteForIdea_shouldThrow_on_idea_not_of_campaign() { final IdeaEntity givenIdea = givenIdeaEntity(); ReflectionTestUtils.setField(givenIdea, "campaignId", "1111"); final IdeasCampaignEntity givenCampaign = givenIdeasCampaignEntity("test_InitUser"); givenCampaign.setId("2222"); votingService.voteForIdea(givenIdea, givenCampaign, givenUserEntity("aUser"), 5); }
@Test(expected = InvalidRequestException.class) public void voteForIdea_ShouldThrow_onVoteOfIdeaOfExpiredCampaign() { final IdeasCampaignEntity givenCampaign = givenIdeasCampaignEntity("test_InitUser"); givenCampaign.setId("2222"); givenCampaign.setEndDate(DateTime.now().minusSeconds(1)); final IdeaEntity givenIdea = IdeaEntity.createIdeaEntity(new IdeaIn("test_title", "test_campaignId"), givenCampaign.getId(), givenUserEntity("creator")); votingService.voteForIdea(givenIdea, givenCampaign, givenUserEntity("voterId"), 5); }
@Test(expected = InvalidRequestException.class) public void voteForIdea_ShouldThrow_onVoteofIdeaBeforeCampaignStart() { final IdeasCampaignEntity givenCampaign = givenIdeasCampaignEntity("test_InitUser"); givenCampaign.setId("2222"); givenCampaign.setStartDate(DateTime.now().plusDays(1)); final IdeaEntity givenIdea = IdeaEntity.createIdeaEntity(new IdeaIn("test_title", "test_campaignId"), givenCampaign.getId(), givenUserEntity("creator")); votingService.voteForIdea(givenIdea, givenCampaign, givenUserEntity("voterId"), 5); }
@Test public void voteForIdea_ShouldPersist() { final IdeasCampaignEntity givenCampaign = givenIdeasCampaignEntity("test_InitUser"); givenCampaign.setId("2222"); final IdeaEntity givenIdea = IdeaEntity.createIdeaEntity(new IdeaIn("test_title", "test_campaignId"), givenCampaign.getId(), givenUserEntity("creator")); givenIdea.setId("test_ideaId"); givenIdea.approveIdea(Fixtures.givenUserEntity("fake_admin")); votingService.voteForIdea(givenIdea, givenCampaign, givenUserEntity("voterId"), 5); verify(voteRepository).save(isA(VoteEntity.class)); }
@Test public void voteForIdea_ShouldDeleteExistingVoteOnZero() { final IdeasCampaignEntity givenCampaign = givenIdeasCampaignEntity("test_InitUser"); givenCampaign.setId("2222"); final IdeaEntity givenIdea = IdeaEntity.createIdeaEntity(new IdeaIn("test_title", "test_campaignId"), givenCampaign.getId(), givenUserEntity("creator")); givenIdea.setId("testid"); givenIdea.approveIdea(Fixtures.givenUserEntity("fake_admin")); final VoteId expectedVoteId = new VoteId("voterId", givenIdea.getId()); votingService.voteForIdea(givenIdea, givenCampaign, givenUserEntity("voterId"), 0); verify(voteRepository).delete(eq(expectedVoteId)); verify(voteRepository, never()).save(any(VoteEntity.class)); }