IdeaService { public IdeaOut createNewIdea(String campaignId, IdeaIn cmd, UserEntity creator) { Assert.notNull(cmd, "cmd must not be null."); Assert.notNull(campaignId, "CampaignId must not be null."); Assert.notNull(creator, "Creator must not be null."); validateCampaignExists(campaignId); final IdeasCampaignEntity campaign = ideasCampaignRepository.findOne(campaignId); final IdeaEntity result = campaign.createIdea(cmd, creator); log.info("Create new idea: campaign: " + campaign.toString()); notifyAdminsOnNewIdea(result, campaign.getContent().getDe().getTitle()); return new IdeaOut(ideaRepository.save(result)); }  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 createNewIdea_shouldPersistIdeaAndNotify() { final String campaignId = "mycampaign"; final UserEntity creator = givenUserEntity("test_adminId"); final IdeaIn cmd = new IdeaIn("test_title", "test_pitch"); givenIdeaCampaignExists(campaignId, true); givenAdminUserExists(); givenIdeaRepositoryCanSave(); ideaService.createNewIdea(campaignId, cmd, creator); final ArgumentCaptor<IdeaEntity> captor = ArgumentCaptor.forClass(IdeaEntity.class); final ArgumentCaptor<IdeaEntity> captorNotification = ArgumentCaptor.forClass(IdeaEntity.class); verify(ideaRepository).save(captor.capture()); assertThat(captor.getValue().getOriginalTitle(), is(cmd.getTitle())); assertThat(captor.getValue().getOriginalPitch(), is(cmd.getPitch())); assertThat(captor.getValue().getCampaignId(), is(campaignId)); verify(userNotificationService).notifyAdminOnIdeaCreation(captorNotification.capture(), anyString(), MessageFormat.format(UserNotificationService.SUBJECT_IDEA_CREATED, anyString())); assertThat(captorNotification.getValue(), is(captor.getValue())); }
@Test public void createNewIdea_shouldThrowOn_CampaignNotActive() { final String campaignId = "mycampaign"; final UserEntity creator = givenUserEntity("test_adminId"); final IdeaIn cmd = new IdeaIn("test_title", "test_pitch"); givenIdeaCampaignExists(campaignId, false); givenAdminUserExists(); givenIdeaRepositoryCanSave(); try { ideaService.createNewIdea(campaignId, cmd, creator); fail("Expected exception not thrown"); } catch (InvalidRequestException e) { assertThat(e.getMessage(), equalTo("campaign_not_active")); } }
@Test(expected = ResourceNotFoundException.class) public void createNewIdea_shouldThrowException_OnNotExistingCampaign() { final String campaignId = "testcampaignid"; givenIdeaCampaignDoesntExist(campaignId); ideaService.createNewIdea(campaignId, new IdeaIn("test_title", "Tu wat!"), givenUserEntity("123459")); }