ProjectAssignmentManagementServiceImpl implements ProjectAssignmentManagementService { @Transactional @Auditable(actionType = AuditActionType.UPDATE) public ProjectAssignment persistUpdatedProjectAssignment(ProjectAssignment projectAssignment) throws ProjectAssignmentValidationException { if (!projectAssignment.isNew()) { List<Issue> issues = Lists.newArrayList(); if (!timesheetDao.getTimesheetEntriesAfter(projectAssignment, projectAssignment.getDateEnd()).isEmpty()) { issues.add(Issue.EXISTING_DATA_AFTER_END); } if (!timesheetDao.getTimesheetEntriesBefore(projectAssignment, projectAssignment.getDateStart()).isEmpty()) { issues.add(Issue.EXISTING_DATA_BEFORE_START); } if (!issues.isEmpty()) { throw new ProjectAssignmentValidationException(issues); } } return projectAssignmentDao.persist(projectAssignment); } @Autowired  ProjectAssignmentManagementServiceImpl(UserDao userDAO,
                                                  ProjectDao projectDao,
                                                  ProjectAssignmentDao projectAssignmentDao,
                                                  TimesheetDao timesheetDao); @Transactional @Auditable(actionType = AuditActionType.CREATE) void assignAllUsersToProject(Project project); @Override @Transactional @Auditable(actionType = AuditActionType.CREATE) void assignUsersToProjects(List<User> users, ProjectAssignment assignmentTemplate); @Transactional @Auditable(actionType = AuditActionType.UPDATE) ProjectAssignment persistUpdatedProjectAssignment(ProjectAssignment projectAssignment); @Override ProjectAssignment persistNewProjectAssignment(ProjectAssignment assignment); @Transactional @Auditable(actionType = AuditActionType.UPDATE) User assignUserToDefaultProjects(User user); @Transactional @Auditable(actionType = AuditActionType.DELETE) void deleteProjectAssignment(ProjectAssignment assignment);  }
@Test public void should_not_update_assignment_when_there_is_data_before_the_start() { ProjectAssignment assignment = ProjectAssignmentObjectMother.createProjectAssignment(1); Date dateStart = new Date(); assignment.setDateStart(dateStart); when(timesheetDao.getTimesheetEntriesBefore(assignment, dateStart)).thenReturn(Lists.newArrayList(TimesheetEntryObjectMother.createTimesheetEntry(1, new Date(), 5f))); try { service.persistUpdatedProjectAssignment(assignment); fail(); } catch (ProjectAssignmentValidationException e) { List<ProjectAssignmentValidationException.Issue> issues = e.getIssues(); assertThat(issues, contains(ProjectAssignmentValidationException.Issue.EXISTING_DATA_BEFORE_START)); } }
@Test public void should_not_update_assignment_when_there_is_data_after_the_end() { ProjectAssignment assignment = ProjectAssignmentObjectMother.createProjectAssignment(1); Date date = new Date(); assignment.setDateEnd(date); when(timesheetDao.getTimesheetEntriesAfter(assignment, date)).thenReturn(Lists.newArrayList(TimesheetEntryObjectMother.createTimesheetEntry(1, new Date(), 5f))); try { service.persistUpdatedProjectAssignment(assignment); fail(); } catch (ProjectAssignmentValidationException e) { List<ProjectAssignmentValidationException.Issue> issues = e.getIssues(); assertThat(issues, contains(ProjectAssignmentValidationException.Issue.EXISTING_DATA_AFTER_END)); } }