ProjectAssignmentServiceImpl implements ProjectAssignmentService { @Override @Transactional(readOnly = true) public List<ProjectAssignment> getProjectAssignmentsAndCheckDeletability(Project project) { List<ProjectAssignment> assignmentsForProject = projectAssignmentDAO.findAllProjectAssignmentsForProject(project); if (!assignmentsForProject.isEmpty()) { Map<Integer, ProjectAssignment> assignmentMap = Maps.newHashMap(); for (ProjectAssignment projectAssignment : assignmentsForProject) { projectAssignment.setDeletable(true); assignmentMap.put(projectAssignment.getPK(), projectAssignment); } List<AssignmentAggregateReportElement> aggregates = reportAggregatedDAO.getCumulatedHoursPerAssignmentForAssignments(Lists.newArrayList(assignmentMap.keySet())); for (AssignmentAggregateReportElement aggregate : aggregates) { assignmentMap.get(aggregate.getProjectAssignment().getAssignmentId()).setDeletable(false); } } return assignmentsForProject; } @Autowired  ProjectAssignmentServiceImpl(ProjectAssignmentDao projectAssignmentDAO, ProjectAssignmentStatusService projectAssignmentStatusService, ReportAggregatedDao reportAggregatedDAO); @Override @Transactional(readOnly = true) List<ProjectAssignment> getProjectAssignmentsForUser(Integer userId, DateRange dateRange); @Override @Transactional(readOnly = true) List<ProjectAssignment> getProjectAssignmentsForUser(User user); @Override @Transactional(readOnly = true) ProjectAssignment getProjectAssignment(Integer assignmentId); @Override @Transactional(readOnly = true) List<ProjectAssignment> getProjectAssignments(Project project, DateRange range); @Override @Transactional(readOnly = true) List<ProjectAssignment> getProjectAssignmentsAndCheckDeletability(Project project); @Override @Transactional(readOnly = true) List<ProjectAssignmentType> getProjectAssignmentTypes();  }
@Test public void should_find_project_assignments_for_project_and_check_deletability() throws ObjectNotFoundException { Project project = ProjectObjectMother.createProject(1); ProjectAssignment deleteableAssignment = ProjectAssignmentObjectMother.createProjectAssignment(1); ProjectAssignment nondeleteableAssignment = ProjectAssignmentObjectMother.createProjectAssignment(2); when(projectAssignmentDAO.findAllProjectAssignmentsForProject(project)).thenReturn(Arrays.asList(deleteableAssignment, nondeleteableAssignment)); when(reportAggregatedDAO.getCumulatedHoursPerAssignmentForAssignments(Lists.newArrayList(deleteableAssignment.getPK(), nondeleteableAssignment.getPK()))) .thenReturn(Lists.newArrayList(new AssignmentAggregateReportElement(nondeleteableAssignment, 5))); List<ProjectAssignment> assignments = projectAssignmentService.getProjectAssignmentsAndCheckDeletability(project); assertTrue(assignments.get(0).isDeletable()); assertFalse(assignments.get(1).isDeletable()); }