ProjectService { public void pledge(String projectId, UserEntity userEntity, Pledge pledge) { ProjectEntity projectEntity = loadProjectEntity(projectId); FinancingRoundEntity financingRound = financingRoundService.mostRecentRoundEntity(); if (financingRound != null && financingRound.terminated() && financingRound.getTerminationPostProcessingDone() && userEntity.getRoles().contains(Roles.ROLE_ADMIN)) { if (!financingRound.idenitityEquals(projectEntity.getFinancingRound())) { throw InvalidRequestException.projectTookNotPartInLastFinancingRond(); } thisInstance.pledgeProjectUsingPostRoundBudget(projectEntity, userEntity, pledge); } else { thisInstance.pledgeProjectInFinancingRound(projectEntity, userEntity, pledge); } } @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 pledge_shouldDispatchToPledgeProjectUsingPostRoundBudgetOnTerminatedPostProcessedRoundAndAdminUser() 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); prepareInactiveFinancingRound(project); FinancingRoundEntity mostRecentRound = prepareInactiveFinancingRound(null); mostRecentRound.setTerminationPostProcessingDone(true); mostRecentRound.setId("test_roundId_another"); when(financingRoundService.mostRecentRoundEntity()).thenReturn(mostRecentRound); project.getFinancingRound().setTerminationPostProcessingDone(true); try { projectService.pledge(projectId, user, pledge); fail("Exception expected to be thrown"); } catch (InvalidRequestException e) { assertThat(e.getMessage(), is(InvalidRequestException.projectTookNotPartInLastFinancingRond().getMessage())); } }
@Test public void pledge_throwsResourceNotFoundExOnNotExistingProject() { 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); when(projectRepository.findOne(anyString())).thenReturn(null); ResourceNotFoundException res = null; try { projectService.pledge(projectId, user, pledge); fail("InvalidRequestException expected!"); } catch (ResourceNotFoundException e) { res = e; } assertPledgeNotExecuted(res, new ResourceNotFoundException(), project, user, budgetBeforePledge, ProjectStatus.PUBLISHED); }