### Question:
MetadataDownloadManager { public InputStream getInputStream(final Long folderId, final String entityClass, final String fileExtension) { final MetadataFileFormat fileFormat = retrieveMetadataFileFormat(fileExtension); final List<MetadataEntity> entities = retrieveMetadataEntities(folderId, entityClass); final StringWriter stringWriter = new StringWriter(); final MetadataWriter metadataWriter = metadataWriterProvider.getMetadataWriter(stringWriter, fileFormat); metadataWriter.writeEntities(entityClass, entities); try { return new StringInputStream(stringWriter.toString()); } catch (IOException e) { throw new MetadataWriterException( messageHelper.getMessage(MessageConstants.ERROR_METADATA_ENTITY_WRITING_BAD_ENCODING), e); } }  InputStream getInputStream(final Long folderId,
                                      final String entityClass,
                                      final String fileExtension);  }### Answer:
@Test(expected = IllegalArgumentException.class) public void getInputStreamShouldThrowIfFolderDoesNotExist() { when(folderManager.load(FOLDER_ID)).thenThrow(new IllegalArgumentException()); manager.getInputStream(FOLDER_ID, SAMPLE, TSV); }
@Test(expected = IllegalArgumentException.class) public void getInputStreamShouldThrowIfMetadataEntityDoesNotExistInParentFolder() { when(metadataEntityManager.loadMetadataEntityByClassNameAndFolderId(FOLDER_ID, SAMPLE)) .thenReturn(Collections.emptyList()); manager.getInputStream(FOLDER_ID, SAMPLE, TSV); }
@Test(expected = IllegalArgumentException.class) public void getInputStreamShouldThrowIfRequestedFileFormatIsNotSupported() { final List<MetadataEntity> entities = Collections.singletonList(new MetadataEntity()); when(metadataEntityManager.loadMetadataEntityByClassNameAndFolderId(FOLDER_ID, SAMPLE)).thenReturn(entities); manager.getInputStream(FOLDER_ID, SAMPLE, "unsupportedFileFormat"); }
@Test public void getInputStreamShouldRetrieveMetadataWriterFromProvider() { final List<MetadataEntity> entities = Collections.singletonList(new MetadataEntity()); when(metadataEntityManager.loadMetadataEntityByClassNameAndFolderId(FOLDER_ID, SAMPLE)).thenReturn(entities); final MetadataWriter metadataWriter = mock(MetadataWriter.class); when(metadataWriterProvider.getMetadataWriter(any(), any())).thenReturn(metadataWriter); manager.getInputStream(FOLDER_ID, SAMPLE, TSV); verify(metadataWriter).writeEntities(eq(SAMPLE), eq(entities)); }
@Test public void getInputStreamShouldReadFromWriterThatWasPassedToMetadataWriter() { final List<MetadataEntity> entities = Collections.singletonList(new MetadataEntity()); when(metadataEntityManager.loadMetadataEntityByClassNameAndFolderId(FOLDER_ID, SAMPLE)).thenReturn(entities); final MetadataWriter metadataWriter = mock(MetadataWriter.class); final ArgumentCaptor<Writer> writerCaptor = ArgumentCaptor.forClass(Writer.class); when(metadataWriterProvider.getMetadataWriter(writerCaptor.capture(), any())).thenReturn(metadataWriter); doAnswer(invocation -> writeMessageAndReturnNothing(writerCaptor.getValue(), "message")) .when(metadataWriter).writeEntities(any(), any()); final InputStream actualInputStream = manager.getInputStream(FOLDER_ID, SAMPLE, TSV); Assert.assertEquals("message", inputStreamAsString(actualInputStream)); }