@Test public void createApplicationForUserThrowsExceptionWhenAlreadyApplied() { given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(applicationRepoMock.existsByUserAndProject(userMock, projectMock)).willReturn(true); given(projectMock.getStatus()).willReturn(APPLICATION_ALOWED_STATUS); assertThatThrownBy(() -> applicationService.createApplicationForUser(userMock, PROJECT_ID, "")) .isInstanceOf(AlreadyAppliedException.class); }
@Override @Transactional public ProjectApplication createApplicationForUser(User user, String projectId, String comment) { var project = projectService.getProjectById(projectId); var projectStatus = project.getStatus().toLowerCase(); if(userHasAppliedForProject(user, project)) { throw new AlreadyAppliedException(); } if(applicationsForbiddenStatus.contains(projectStatus)) { throw new ProjectStatusPreventsApplicationException(); } var applicationDate = LocalDateTime.now(clock); var application = new ProjectApplication(project, comment, user, applicationDate); var savedApplication = applicationRepo.save(application); applicationEventHandler.onApplicationReceived(savedApplication); return savedApplication; }
RepositoryApplicationService implements ApplicationService { @Override @Transactional public ProjectApplication createApplicationForUser(User user, String projectId, String comment) { var project = projectService.getProjectById(projectId); var projectStatus = project.getStatus().toLowerCase(); if(userHasAppliedForProject(user, project)) { throw new AlreadyAppliedException(); } if(applicationsForbiddenStatus.contains(projectStatus)) { throw new ProjectStatusPreventsApplicationException(); } var applicationDate = LocalDateTime.now(clock); var application = new ProjectApplication(project, comment, user, applicationDate); var savedApplication = applicationRepo.save(application); applicationEventHandler.onApplicationReceived(savedApplication); return savedApplication; } }
RepositoryApplicationService implements ApplicationService { @Override @Transactional public ProjectApplication createApplicationForUser(User user, String projectId, String comment) { var project = projectService.getProjectById(projectId); var projectStatus = project.getStatus().toLowerCase(); if(userHasAppliedForProject(user, project)) { throw new AlreadyAppliedException(); } if(applicationsForbiddenStatus.contains(projectStatus)) { throw new ProjectStatusPreventsApplicationException(); } var applicationDate = LocalDateTime.now(clock); var application = new ProjectApplication(project, comment, user, applicationDate); var savedApplication = applicationRepo.save(application); applicationEventHandler.onApplicationReceived(savedApplication); return savedApplication; } @Autowired  RepositoryApplicationService(ProjectService projectService,
                                        ProjectApplicationRepository applicationRepo,
                                        ProjectApplicationEventHandler applicationEventHandler,
                                        Clock clock,
                                        ProjectBoardConfigurationProperties properties); }
RepositoryApplicationService implements ApplicationService { @Override @Transactional public ProjectApplication createApplicationForUser(User user, String projectId, String comment) { var project = projectService.getProjectById(projectId); var projectStatus = project.getStatus().toLowerCase(); if(userHasAppliedForProject(user, project)) { throw new AlreadyAppliedException(); } if(applicationsForbiddenStatus.contains(projectStatus)) { throw new ProjectStatusPreventsApplicationException(); } var applicationDate = LocalDateTime.now(clock); var application = new ProjectApplication(project, comment, user, applicationDate); var savedApplication = applicationRepo.save(application); applicationEventHandler.onApplicationReceived(savedApplication); return savedApplication; } @Autowired  RepositoryApplicationService(ProjectService projectService,
                                        ProjectApplicationRepository applicationRepo,
                                        ProjectApplicationEventHandler applicationEventHandler,
                                        Clock clock,
                                        ProjectBoardConfigurationProperties properties); @Override boolean userHasAppliedForProject(User user, Project project); @Override @Transactional ProjectApplication createApplicationForUser(User user, String projectId, String comment); @Override List<ProjectApplication> getApplicationsOfUser(User user, Sort sort); @Override List<ProjectApplication> getApplicationsOfUsers(Collection<User> users, Sort sort); @Transactional @Override ProjectApplication deleteApplication(User user, long applicationId); }
RepositoryApplicationService implements ApplicationService { @Override @Transactional public ProjectApplication createApplicationForUser(User user, String projectId, String comment) { var project = projectService.getProjectById(projectId); var projectStatus = project.getStatus().toLowerCase(); if(userHasAppliedForProject(user, project)) { throw new AlreadyAppliedException(); } if(applicationsForbiddenStatus.contains(projectStatus)) { throw new ProjectStatusPreventsApplicationException(); } var applicationDate = LocalDateTime.now(clock); var application = new ProjectApplication(project, comment, user, applicationDate); var savedApplication = applicationRepo.save(application); applicationEventHandler.onApplicationReceived(savedApplication); return savedApplication; } @Autowired  RepositoryApplicationService(ProjectService projectService,
                                        ProjectApplicationRepository applicationRepo,
                                        ProjectApplicationEventHandler applicationEventHandler,
                                        Clock clock,
                                        ProjectBoardConfigurationProperties properties); @Override boolean userHasAppliedForProject(User user, Project project); @Override @Transactional ProjectApplication createApplicationForUser(User user, String projectId, String comment); @Override List<ProjectApplication> getApplicationsOfUser(User user, Sort sort); @Override List<ProjectApplication> getApplicationsOfUsers(Collection<User> users, Sort sort); @Transactional @Override ProjectApplication deleteApplication(User user, long applicationId);  }