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);  }
@Test public void createApplicationForUser() { var expectedComment = "A nice Comment!"; var expectedDate = LocalDateTime.now(clock); var expectedApplication = new ProjectApplication(projectMock, expectedComment, userMock, expectedDate); given(applicationRepoMock.existsByUserAndProject(userMock, projectMock)).willReturn(false); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(applicationRepoMock.existsByUserAndProject(userMock, projectMock)).willReturn(false); given(applicationRepoMock.save(expectedApplication)).willReturn(expectedApplication); given(projectMock.getStatus()).willReturn(APPLICATION_ALOWED_STATUS); var actualApplication = applicationService.createApplicationForUser(userMock, PROJECT_ID, expectedComment); assertThat(actualApplication).isEqualTo(expectedApplication); verify(applicationRepoMock).save(actualApplication); verify(applicationEventHandlerMock).onApplicationReceived(expectedApplication); }
@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); }
@Test public void createApplicationForUserThrowsExceptionWhenStatusForbidsApplications() { given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(APPLICATION_FORBIDDEN_STATUS); assertThatThrownBy(() -> applicationService.createApplicationForUser(userMock, PROJECT_ID, "")) .isInstanceOf(ProjectStatusPreventsApplicationException.class); }