ProjectService { void pledgeProjectUsingPostRoundBudget(ProjectEntity projectEntity, UserEntity userEntity, Pledge pledge) { FinancingRoundEntity financingRound = projectEntity.getFinancingRound(); List<PledgeEntity> postRoundPledges = pledgeRepository.findByFinancingRoundAndCreatedDateGreaterThan( financingRound, financingRound.getEndDate()); int postRoundPledgableBudget = financingRound.postRoundPledgableBudgetRemaining(postRoundPledges); List<PledgeEntity> pledgesSoFar = pledgeRepository.findByProjectAndFinancingRound( projectEntity, projectEntity.getFinancingRound()); PledgeEntity pledgeResult = projectEntity.pledgeUsingPostRoundBudget( pledge, userEntity, pledgesSoFar, postRoundPledgableBudget); if (projectEntity.pledgeGoalAchieved()) { projectRepository.save(projectEntity); } userRepository.save(userEntity); pledgeRepository.save(pledgeResult); LOG.debug("Project pledged using post round budget: {}", pledgeResult); } @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 pledgeProjectUsingPostRoundBudget() throws Exception { final UserEntity user = admin(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(3); final int budgetBeforePledge = user.getBudget(); pledgedAssertionProject(project, user, project.getPledgeGoal() - 4); FinancingRoundEntity finanzingRound = prepareInactiveFinancingRound(project); finanzingRound.initPostRoundBudget(6000); finanzingRound.setTerminationPostProcessingDone(true); when(pledgeRepository.findByFinancingRoundAndCreatedDateGreaterThan(finanzingRound, finanzingRound.getEndDate())).thenReturn(Collections.emptyList()); projectService.pledgeProjectUsingPostRoundBudget(project, user, pledge); PledgeEntity pledgeEntity = new PledgeEntity(project, user, pledge, finanzingRound); assertThat(user.getBudget(), is(budgetBeforePledge)); assertThat(project.getStatus(), is(ProjectStatus.PUBLISHED)); verify(pledgeRepository).save(pledgeEntity); verify(userRepository).save(user); verify(projectRepository, never()).save(project); }
@Test public void pledgeProjectUsingPostRoundBudget_settingStatusToFullyPledgedShouldPersistProjectToo() throws Exception { final UserEntity user = admin(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 = prepareInactiveFinancingRound(project); finanzingRound.initPostRoundBudget(6000); finanzingRound.setTerminationPostProcessingDone(true); when(pledgeRepository.findByFinancingRoundAndCreatedDateGreaterThan(finanzingRound, finanzingRound.getEndDate())).thenReturn(Collections.emptyList()); projectService.pledgeProjectUsingPostRoundBudget(project, user, pledge); PledgeEntity pledgeEntity = new PledgeEntity(project, user, pledge, finanzingRound); assertThat(user.getBudget(), is(budgetBeforePledge)); assertThat(project.getStatus(), is(ProjectStatus.FULLY_PLEDGED)); verify(pledgeRepository).save(pledgeEntity); verify(userRepository).save(user); verify(projectRepository).save(project); }
@Test public void pledgeProjectUsingPostRoundBudget_errorOnPledgingShouldNotCauseAnyPersistenceActions() throws Exception { final UserEntity user = admin(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); pledgedAssertionProject(project, user, project.getPledgeGoal() - 4); FinancingRoundEntity finanzingRound = prepareInactiveFinancingRound(project); finanzingRound.initPostRoundBudget(6000); finanzingRound.setTerminationPostProcessingDone(true); when(pledgeRepository.findByFinancingRoundAndCreatedDateGreaterThan(finanzingRound, finanzingRound.getEndDate())).thenReturn(Collections.emptyList()); InvalidRequestException res = null; try { projectService.pledgeProjectUsingPostRoundBudget(project, user, pledge); fail("InvalidRequestException expected!"); } catch (InvalidRequestException e) { res = e; } assertPledgeNotExecuted(res, InvalidRequestException.pledgeGoalExceeded(), project, user, budgetBeforePledge, ProjectStatus.PUBLISHED); }