use of org.pentaho.platform.repository.RepositoryDownloadWhitelist in project pentaho-platform by pentaho.
the class FileServiceTest method testDoGetFileAsInline.
@Test
public void testDoGetFileAsInline() throws FileNotFoundException {
/*
* TEST 1
*/
doReturn(true).when(fileService).isPath(anyString());
doReturn(true).when(fileService).isPathValid(anyString());
RepositoryDownloadWhitelist mockWhiteList = mock(RepositoryDownloadWhitelist.class);
doReturn(mockWhiteList).when(fileService).getWhitelist();
doReturn(true).when(mockWhiteList).accept(anyString());
RepositoryFile mockRepoFile = mock(RepositoryFile.class);
doReturn(mockRepoFile).when(fileService.repository).getFile(anyString());
SimpleRepositoryFileData mockData = mock(SimpleRepositoryFileData.class);
doReturn(mockData).when(fileService.repository).getDataForRead(any(Serializable.class), any(Class.class));
InputStream mockInputStream = mock(InputStream.class);
doReturn(mockInputStream).when(mockData).getInputStream();
StreamingOutput mockStreamingOutput = mock(StreamingOutput.class);
doReturn(mockStreamingOutput).when(fileService).getStreamingOutput(mockInputStream);
FileService.RepositoryFileToStreamWrapper wrapper = fileService.doGetFileAsInline("test");
verify(fileService.repository, times(1)).getFile(anyString());
verify(mockWhiteList, times(1)).accept(anyString());
verify(fileService, times(2)).getRepository();
verify(fileService.repository, times(1)).getDataForRead(any(Serializable.class), any(Class.class));
verify(mockData, times(1)).getInputStream();
assertEquals(mockRepoFile, wrapper.getRepositoryFile());
assertEquals(mockStreamingOutput, wrapper.getOutputStream());
/*
* TEST 2
*/
doReturn(false).when(fileService).isPath(anyString());
doReturn(mockRepoFile).when(fileService.repository).getFileById(anyString());
wrapper = fileService.doGetFileAsInline("test");
verify(fileService.repository, times(1)).getFileById(anyString());
verify(fileService, times(4)).getRepository();
assertEquals(mockRepoFile, wrapper.getRepositoryFile());
assertEquals(mockStreamingOutput, wrapper.getOutputStream());
}
use of org.pentaho.platform.repository.RepositoryDownloadWhitelist in project pentaho-platform by pentaho.
the class FileServiceTest method testDoGetFileAsInlineException.
@Test
public void testDoGetFileAsInlineException() {
/*
* TEST 1
*/
doReturn(true).when(fileService).isPath(anyString());
doReturn(false).when(fileService).isPathValid(anyString());
try {
fileService.doGetFileAsInline("test");
fail();
} catch (IllegalArgumentException e) {
// Excpected
} catch (FileNotFoundException e) {
fail();
}
/*
* TEST 2
*/
doReturn(true).when(fileService).isPathValid(anyString());
doReturn(null).when(fileService.repository).getFile(anyString());
try {
fileService.doGetFileAsInline("test");
fail();
} catch (FileNotFoundException e) {
// Expected
}
/*
* TEST 3
*/
RepositoryFile mockFile = mock(RepositoryFile.class);
doReturn(mockFile).when(fileService.repository).getFile(anyString());
RepositoryDownloadWhitelist mockWhiteList = mock(RepositoryDownloadWhitelist.class);
doReturn(mockWhiteList).when(fileService).getWhitelist();
doReturn(false).when(mockWhiteList).accept(anyString());
IAuthorizationPolicy mockPolicy = mock(IAuthorizationPolicy.class);
doReturn(mockPolicy).when(fileService).getPolicy();
doReturn(false).when(mockPolicy).isAllowed(anyString());
try {
fileService.doGetFileAsInline("test");
fail();
} catch (IllegalArgumentException e) {
// Excpected
} catch (FileNotFoundException e) {
fail();
}
/*
* TEST 4
*/
doReturn(true).when(mockWhiteList).accept(anyString());
doThrow(new InternalError()).when(fileService.repository).getDataForRead(any(Serializable.class), any(Class.class));
try {
fileService.doGetFileAsInline("test");
fail();
} catch (InternalError e) {
// Excpected
} catch (FileNotFoundException e) {
fail();
}
}
Aggregations