ProjectService { public void deleteProjectAttachment(String projectId, Attachment attachmentRequest, UserEntity deletingUser) { final ProjectEntity project = loadProjectEntity(projectId); final AttachmentValue attachment2Delete = project.findAttachmentByReference(attachmentRequest); project.deleteAttachmentAllowed(deletingUser); projectAttachmentRepository.deleteAttachment(attachment2Delete); project.deleteAttachment(attachment2Delete); projectRepository.save(project); } @Autowired  ProjectService(ProjectRepository projectRepository,
                          PledgeRepository pledgeRepository,
                          UserRepository userRepository,
                          FinancingRoundRepository financingRoundRepository,
                          UserNotificationService userNotificationService,
                          FinancingRoundService financingRoundService,
                          LikeRepository likeRepository,
                          ProjectAttachmentRepository projectAttachmentRepository); Project getProject(String projectId, UserEntity requestingUser); List<Project> getProjects(UserEntity requestingUser); Project addProject(Project project, UserEntity creator); Project modifyProjectStatus(String projectId, ProjectStatus newStatusToApply, UserEntity requestingUser); Project modifyProjectMasterdata(String projectId, Project modifiedProject, UserEntity requestingUser); void pledge(String projectId, UserEntity userEntity, Pledge pledge); Attachment addProjectAttachment(String projectId, Attachment attachment, UserEntity savingUser); Attachment loadProjectAttachment(String projectId, Attachment attachmentRequest); void deleteProjectAttachment(String projectId, Attachment attachmentRequest, UserEntity deletingUser); void likeProject(String projectId, UserEntity user); void unlikeProject(String projectId, UserEntity user);  }
@Test public void deleteAttachment_shouldCallProjectAttachmentRepositoryAndPersistProject() throws Exception { final String fileRef = "attachId_0"; final ProjectEntity project = givenAProjectEntityWithAttachments(fileRef, "anotherFileRef"); final int attachmentSizeBeforeDel = project.getAttachments().size(); project.setStatus(ProjectStatus.PROPOSED); when(projectRepository.findOne(project.getId())).thenReturn(project); projectService.deleteProjectAttachment(project.getId(), Attachment.asLookupByIdCommand(fileRef), admin("anAdmin")); final ArgumentCaptor<AttachmentValue> fileRepoCapture = ArgumentCaptor.forClass(AttachmentValue.class); final ArgumentCaptor<ProjectEntity> projectEntityRepCapture = ArgumentCaptor.forClass(ProjectEntity.class); verify(projectAttachmentRepository).deleteAttachment(fileRepoCapture.capture()); verify(projectRepository).save(projectEntityRepCapture.capture()); assertThat(fileRepoCapture.getValue().getFileReference(), is(fileRef)); assertThat(projectEntityRepCapture.getValue().getAttachments().size(), is(attachmentSizeBeforeDel - 1)); assertThat("Should have removed AttachmentValue from project", !projectEntityRepCapture.getValue().getAttachments().contains(new AttachmentValue(fileRef, null, null, 17, null))); }
@Test(expected = ResourceNotFoundException.class) public void deleteAttachment_shouldThrowResourceNotFoundExceptionOnNotExistingProject() throws Exception { final String fileRef = "attachId_0"; final ProjectEntity project = givenAProjectEntityWithAttachments(fileRef, "anotherFileRef"); project.setStatus(ProjectStatus.PROPOSED); when(projectRepository.findOne(project.getId())).thenReturn(null); projectService.deleteProjectAttachment(project.getId(), Attachment.asLookupByIdCommand(fileRef), admin("anAdmin")); }
@Test(expected = ResourceNotFoundException.class) public void deleteAttachment_shouldThrowResourceNotFoundOnNotExistingAttachmentValue() throws Exception { final String fileRef = "attachId_0"; final UserEntity projectCreator = user(USER_EMAIL); final ProjectEntity project = projectEntity("projectId", ProjectStatus.PROPOSED, projectCreator); projectService.deleteProjectAttachment(project.getId(), Attachment.asLookupByIdCommand(fileRef), admin("anAdmin")); }
@Test(expected = NotAuthorizedException.class) public void deleteAttachment_shouldThrowNotAllowedExceptionOnDeletionByNonAdminAndNonCreator() throws Exception { final String fileRef = "attachId_0"; final ProjectEntity project = givenAProjectEntityWithAttachments(fileRef, "anotherFileRef"); project.setStatus(ProjectStatus.PROPOSED); when(projectRepository.findOne(project.getId())).thenReturn(project); when(projectAttachmentRepository.loadAttachment(any(AttachmentValue.class))).thenReturn(null); projectService.deleteProjectAttachment(project.getId(), Attachment.asLookupByIdCommand(fileRef), user("aUser")); }
@Test public void deleteAttachment_shouldThrowExceptionWhenChangesNotAllowedDueToProjectStatus() throws Exception { final String fileRef = "attachId_0"; final ProjectEntity project = givenAProjectEntityWithCreatorAndAttachments(user("aCreator@asideas.de"), fileRef, "anotherFileRef"); project.setStatus(ProjectStatus.PUBLISHED); project.setFinancingRound(prepareActiveFinanzingRound(project)); when(projectRepository.findOne(project.getId())).thenReturn(project); when(projectAttachmentRepository.loadAttachment(any(AttachmentValue.class))).thenReturn(null); try { projectService.deleteProjectAttachment(project.getId(), Attachment.asLookupByIdCommand(fileRef), project.getCreator()); fail("InvalidRequestException expected!"); } catch (InvalidRequestException e) { assertThat(e.getMessage(), is(InvalidRequestException.masterdataChangeNotAllowed().getMessage())); } }