ProjectService { public Attachment loadProjectAttachment(String projectId, Attachment attachmentRequest) { final ProjectEntity project = loadProjectEntity(projectId); final AttachmentValue attachment2Serve = project.findAttachmentByReference(attachmentRequest); final InputStream payload = projectAttachmentRepository.loadAttachment(attachment2Serve); if (payload == null) { LOG.error("A project's attachment file entry's actual binary data couldn't be found: " + "projectId:{}; fileAttachmentMissing: {}", projectId, attachment2Serve); throw new ResourceNotFoundException(); } return Attachment.asResponse(attachment2Serve, project, payload); } @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 loadProjectAttachment_shouldCallProjectAttachmentRepository() throws Exception { final String fileRef = "attachId_0"; final ProjectEntity projectEntity = givenAProjectEntityWithAttachments(fileRef, "anotherFileRef"); final InputStream expInputStream = mockedInputStream(); final ArgumentCaptor<AttachmentValue> repoReqCapture = ArgumentCaptor.forClass(AttachmentValue.class); when(projectAttachmentRepository.loadAttachment(repoReqCapture.capture())).thenReturn(expInputStream); final Attachment res = projectService.loadProjectAttachment(projectEntity.getId(), Attachment.asLookupByIdCommand(fileRef)); assertThat(repoReqCapture.getValue().getFileReference(), is(fileRef)); assertThat("Expected specific input stream in result", res.getPayload() == expInputStream); assertPresentationAttachmentConformsPersited(res, projectEntity.findAttachmentByReference(Attachment.asLookupByIdCommand(fileRef))); }
@Test(expected = ResourceNotFoundException.class) public void loadProjectAttachment_shouldThrowResourceNotFoundExceptionOnNullPayload() throws Exception { final String fileRef = "attachId_0"; final ProjectEntity projectEntity = givenAProjectEntityWithAttachments(fileRef, "anotherFileRef"); when(projectAttachmentRepository.loadAttachment(any(AttachmentValue.class))).thenReturn(null); projectService.loadProjectAttachment(projectEntity.getId(), Attachment.asLookupByIdCommand(fileRef)); }
@Test(expected = ResourceNotFoundException.class) public void loadProjectAttachment_shouldThrowResourceNotFoundExceptionOnProjectNotExisting() throws Exception { projectService.loadProjectAttachment("notExistingProjectId", Attachment.asLookupByIdCommand("aFileRef")); }