ProjectAttachmentRepository { public InputStream loadAttachment(AttachmentValue attachment) { Assert.notNull(attachment.getFileReference(), "AttachmentValue.fileReference must not be null"); GridFSDBFile res = gridFsOperations.findOne(new Query(Criteria.where("_id").is(attachment.getFileReference()))); if(res == null){ return null; } return res.getInputStream(); }  AttachmentValue storeAttachment(AttachmentValue fileMetadata, InputStream binaryData); InputStream loadAttachment(AttachmentValue attachment); void deleteAttachment(AttachmentValue attachment);  }
@Test public void loadAttachment_shouldReturnNullWhenQueryReturnsNull() throws Exception { when(gridFsOperations.findOne(any(Query.class))).thenReturn(null); InputStream res = projectAttachmentRepository.loadAttachment(aPersitedAttachment()); assertThat(res, is(nullValue())); }
@Test public void loadAttachment_shouldQueryByFileReference() throws Exception { AttachmentValue attachment = aPersitedAttachment(); InputStream expRes = mockedInputStream(); when(gridFsOperations.findOne(eq(new Query(Criteria.where("_id").is(attachment.getFileReference()))))).thenReturn( new TestGridFsFile(expRes) ); InputStream res = projectAttachmentRepository.loadAttachment(attachment); assertTrue(res == expRes); }