ProjectService { void pledgeProjectInFinancingRound(ProjectEntity projectEntity, UserEntity userEntity, Pledge pledge) { List<PledgeEntity> pledgesSoFar = pledgeRepository.findByProjectAndFinancingRound( projectEntity, projectEntity.getFinancingRound()); PledgeEntity pledgeEntity = projectEntity.pledge( pledge, userEntity, pledgesSoFar); if (projectEntity.pledgeGoalAchieved()) { projectRepository.save(projectEntity); } userRepository.save(userEntity); pledgeRepository.save(pledgeEntity); LOG.debug("Project pledged: {}", pledgeEntity); } @Autowired  ProjectService(ProjectRepository projectRepository,
                          PledgeRepository pledgeRepository,
                          UserRepository userRepository,
                          FinancingRoundRepository financingRoundRepository,
                          UserNotificationService userNotificationService,
                          FinancingRoundService financingRoundService,
                          LikeRepository likeRepository,
                          ProjectAttachmentRepository projectAttachmentRepository); Project getProject(String projectId, UserEntity requestingUser); List<Project> getProjects(UserEntity requestingUser); Project addProject(Project project, UserEntity creator); Project modifyProjectStatus(String projectId, ProjectStatus newStatusToApply, UserEntity requestingUser); Project modifyProjectMasterdata(String projectId, Project modifiedProject, UserEntity requestingUser); void pledge(String projectId, UserEntity userEntity, Pledge pledge); Attachment addProjectAttachment(String projectId, Attachment attachment, UserEntity savingUser); Attachment loadProjectAttachment(String projectId, Attachment attachmentRequest); void deleteProjectAttachment(String projectId, Attachment attachmentRequest, UserEntity deletingUser); void likeProject(String projectId, UserEntity user); void unlikeProject(String projectId, UserEntity user);  }
@Test public void pledgeProjectInFinancingRound() throws Exception { final UserEntity user = user(USER_EMAIL); final String projectId = "some_id"; final ProjectEntity project = projectEntity(user, projectId, "title", 44, "short description", "description", ProjectStatus.PUBLISHED, null); final int budgetBeforePledge = user.getBudget(); final Pledge pledge = new Pledge(project.getPledgeGoal() - 4); FinancingRoundEntity financingRound = prepareActiveFinanzingRound(project); projectService.pledgeProjectInFinancingRound(project, user, pledge); PledgeEntity pledgeEntity = new PledgeEntity(project, user, pledge, financingRound); assertThat(user.getBudget(), is(budgetBeforePledge - pledge.getAmount())); assertThat(project.getStatus(), is(not(ProjectStatus.FULLY_PLEDGED))); verify(pledgeRepository).save(pledgeEntity); verify(userRepository).save(user); verify(projectRepository, never()).save(any(ProjectEntity.class)); }
@Test public void pledgeProjectInFinancingRound_reverse() throws Exception { final UserEntity user = user(USER_EMAIL); final String projectId = "some_id"; final ProjectEntity project = projectEntity(user, projectId, "title", 44, "short description", "description", ProjectStatus.PUBLISHED, null); final int budgetBeforePledge = user.getBudget(); final Pledge pledge = new Pledge(-4); pledgedAssertionProject(project, user, 4); FinancingRoundEntity financingRound = prepareActiveFinanzingRound(project); projectService.pledgeProjectInFinancingRound(project, user, pledge); PledgeEntity pledgeEntity = new PledgeEntity(project, user, pledge, financingRound); assertThat(user.getBudget(), is(budgetBeforePledge + 4)); assertThat(project.getStatus(), is(not(ProjectStatus.FULLY_PLEDGED))); verify(pledgeRepository).save(pledgeEntity); verify(userRepository).save(user); verify(projectRepository, never()).save(any(ProjectEntity.class)); }
@Test public void pledgeProjectInFinancingRound_settingStatusToFullyPledgedShouldPersistProjectToo() throws Exception { final UserEntity user = user(USER_EMAIL); final String projectId = "some_id"; final ProjectEntity project = projectEntity(user, projectId, "title", 44, "short description", "description", ProjectStatus.PUBLISHED, null); final Pledge pledge = new Pledge(4); final int budgetBeforePledge = user.getBudget(); pledgedAssertionProject(project, user, project.getPledgeGoal() - 4); FinancingRoundEntity finanzingRound = prepareActiveFinanzingRound(project); projectService.pledgeProjectInFinancingRound(project, user, pledge); PledgeEntity pledgeEntity = new PledgeEntity(project, user, pledge, finanzingRound); assertThat(user.getBudget(), is(budgetBeforePledge - pledge.getAmount())); assertThat(project.getStatus(), is(ProjectStatus.FULLY_PLEDGED)); verify(pledgeRepository).save(pledgeEntity); verify(userRepository).save(user); verify(projectRepository).save(project); }
@Test public void pledgeProjectInFinancingRound_errorOnPledgingShouldNotCauseAnyPersistenceActions() throws Exception { final UserEntity user = user(USER_EMAIL); final String projectId = "some_id"; final ProjectEntity project = projectEntity(user, projectId, "title", 44, "short description", "description", ProjectStatus.PUBLISHED, null); final int budgetBeforePledge = user.getBudget(); final Pledge pledge = new Pledge(45); prepareActiveFinanzingRound(project); pledgedAssertionProject(project, user, 4); InvalidRequestException res = null; try { projectService.pledgeProjectInFinancingRound(project, user, pledge); fail("InvalidRequestException expected!"); } catch (InvalidRequestException e) { res = e; } assertPledgeNotExecuted(res, InvalidRequestException.pledgeGoalExceeded(), project, user, budgetBeforePledge, ProjectStatus.PUBLISHED); }