@Test(expected = ValidationException.class) public void shouldHandleConstraintViolationExceptionOnProjectUpdateGracefully() throws NotFoundException { User user = new User(); user.setId(USER_ID); Project project = new Project(); project.addOwner(user); project.setId(PROJECT_ID); given(projectRepository.save(project)).willThrow(ConstraintViolationException.class); given(projectRepository.findById(PROJECT_ID)).willReturn(Optional.of(project)); projectDAO.update(user, PROJECT_ID, project); }
public Project update(User user, Long projectId, Project project) throws NotFoundException, ValidationException { LOGGER.traceEntry("update({})", project); final Project projectInDb = projectRepository.findById(projectId).orElse(null); checkAccess(user, projectInDb); if (!projectInDb.getOwners().contains(user) && user.getRole() != UserRole.ADMIN) { throw new UnauthorizedException("You are not allowed to update this project."); } projectInDb.setName(project.getName()); projectInDb.setDescription(project.getDescription()); final Project updatedProject = projectRepository.save(projectInDb); loadLazyRelations(updatedProject); LOGGER.traceExit(project); return updatedProject; }
ProjectDAO { public Project update(User user, Long projectId, Project project) throws NotFoundException, ValidationException { LOGGER.traceEntry("update({})", project); final Project projectInDb = projectRepository.findById(projectId).orElse(null); checkAccess(user, projectInDb); if (!projectInDb.getOwners().contains(user) && user.getRole() != UserRole.ADMIN) { throw new UnauthorizedException("You are not allowed to update this project."); } projectInDb.setName(project.getName()); projectInDb.setDescription(project.getDescription()); final Project updatedProject = projectRepository.save(projectInDb); loadLazyRelations(updatedProject); LOGGER.traceExit(project); return updatedProject; } }
ProjectDAO { public Project update(User user, Long projectId, Project project) throws NotFoundException, ValidationException { LOGGER.traceEntry("update({})", project); final Project projectInDb = projectRepository.findById(projectId).orElse(null); checkAccess(user, projectInDb); if (!projectInDb.getOwners().contains(user) && user.getRole() != UserRole.ADMIN) { throw new UnauthorizedException("You are not allowed to update this project."); } projectInDb.setName(project.getName()); projectInDb.setDescription(project.getDescription()); final Project updatedProject = projectRepository.save(projectInDb); loadLazyRelations(updatedProject); LOGGER.traceExit(project); return updatedProject; } @Autowired  ProjectDAO(ProjectRepository projectRepository,
                      LearnerResultRepository learnerResultRepository,
                      TestReportRepository testReportRepository,
                      @Lazy FileDAO fileDAO,
                      ParameterizedSymbolRepository parameterizedSymbolRepository,
                      SymbolStepRepository symbolStepRepository,
                      SymbolActionRepository symbolActionRepository,
                      @Lazy ProjectEnvironmentDAO projectEnvironmentDAO,
                      ProjectUrlRepository projectUrlRepository,
                      TestExecutionConfigRepository testExecutionConfigRepository,
                      @Lazy TestDAO testDAO,
                      @Lazy UserDAO userDAO,
                      ProjectEnvironmentRepository environmentRepository,
                      @Lazy SymbolGroupDAO symbolGroupDAO,
                      TestRepository testRepository,
                      SymbolParameterRepository symbolParameterRepository,
                      UploadableFileRepository uploadableFileRepository,
                      LearnerSetupRepository learnerSetupRepository); }
ProjectDAO { public Project update(User user, Long projectId, Project project) throws NotFoundException, ValidationException { LOGGER.traceEntry("update({})", project); final Project projectInDb = projectRepository.findById(projectId).orElse(null); checkAccess(user, projectInDb); if (!projectInDb.getOwners().contains(user) && user.getRole() != UserRole.ADMIN) { throw new UnauthorizedException("You are not allowed to update this project."); } projectInDb.setName(project.getName()); projectInDb.setDescription(project.getDescription()); final Project updatedProject = projectRepository.save(projectInDb); loadLazyRelations(updatedProject); LOGGER.traceExit(project); return updatedProject; } @Autowired  ProjectDAO(ProjectRepository projectRepository,
                      LearnerResultRepository learnerResultRepository,
                      TestReportRepository testReportRepository,
                      @Lazy FileDAO fileDAO,
                      ParameterizedSymbolRepository parameterizedSymbolRepository,
                      SymbolStepRepository symbolStepRepository,
                      SymbolActionRepository symbolActionRepository,
                      @Lazy ProjectEnvironmentDAO projectEnvironmentDAO,
                      ProjectUrlRepository projectUrlRepository,
                      TestExecutionConfigRepository testExecutionConfigRepository,
                      @Lazy TestDAO testDAO,
                      @Lazy UserDAO userDAO,
                      ProjectEnvironmentRepository environmentRepository,
                      @Lazy SymbolGroupDAO symbolGroupDAO,
                      TestRepository testRepository,
                      SymbolParameterRepository symbolParameterRepository,
                      UploadableFileRepository uploadableFileRepository,
                      LearnerSetupRepository learnerSetupRepository); Project create(final User user, final CreateProjectForm projectForm); List<Project> getAll(User user); Project getByID(User user, Long projectId); Project update(User user, Long projectId, Project project); void delete(User user, Long projectId); void delete(User user, List<Long> projectIds); Project importProject(User user, ProjectExportableEntity projectExportableEntity); void checkAccess(User user, Project project); Project addOwners(User user, Long projectId, List<Long> ownerIds); Project addMembers(User user, Long projectId, List<Long> memberIds); Project removeOwners(User user, Long projectId, List<Long> ownerIds); Project removeMembers(User user, Long projectId, List<Long> memberIds); }
ProjectDAO { public Project update(User user, Long projectId, Project project) throws NotFoundException, ValidationException { LOGGER.traceEntry("update({})", project); final Project projectInDb = projectRepository.findById(projectId).orElse(null); checkAccess(user, projectInDb); if (!projectInDb.getOwners().contains(user) && user.getRole() != UserRole.ADMIN) { throw new UnauthorizedException("You are not allowed to update this project."); } projectInDb.setName(project.getName()); projectInDb.setDescription(project.getDescription()); final Project updatedProject = projectRepository.save(projectInDb); loadLazyRelations(updatedProject); LOGGER.traceExit(project); return updatedProject; } @Autowired  ProjectDAO(ProjectRepository projectRepository,
                      LearnerResultRepository learnerResultRepository,
                      TestReportRepository testReportRepository,
                      @Lazy FileDAO fileDAO,
                      ParameterizedSymbolRepository parameterizedSymbolRepository,
                      SymbolStepRepository symbolStepRepository,
                      SymbolActionRepository symbolActionRepository,
                      @Lazy ProjectEnvironmentDAO projectEnvironmentDAO,
                      ProjectUrlRepository projectUrlRepository,
                      TestExecutionConfigRepository testExecutionConfigRepository,
                      @Lazy TestDAO testDAO,
                      @Lazy UserDAO userDAO,
                      ProjectEnvironmentRepository environmentRepository,
                      @Lazy SymbolGroupDAO symbolGroupDAO,
                      TestRepository testRepository,
                      SymbolParameterRepository symbolParameterRepository,
                      UploadableFileRepository uploadableFileRepository,
                      LearnerSetupRepository learnerSetupRepository); Project create(final User user, final CreateProjectForm projectForm); List<Project> getAll(User user); Project getByID(User user, Long projectId); Project update(User user, Long projectId, Project project); void delete(User user, Long projectId); void delete(User user, List<Long> projectIds); Project importProject(User user, ProjectExportableEntity projectExportableEntity); void checkAccess(User user, Project project); Project addOwners(User user, Long projectId, List<Long> ownerIds); Project addMembers(User user, Long projectId, List<Long> memberIds); Project removeOwners(User user, Long projectId, List<Long> ownerIds); Project removeMembers(User user, Long projectId, List<Long> memberIds);  }