Search in sources :

Example 1 with RepositoryDownloadWhitelist

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());
}
Also used : Serializable(java.io.Serializable) RepositoryDownloadWhitelist(org.pentaho.platform.repository.RepositoryDownloadWhitelist) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) StreamingOutput(javax.ws.rs.core.StreamingOutput) Test(org.junit.Test)

Example 2 with RepositoryDownloadWhitelist

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();
    }
}
Also used : IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) Serializable(java.io.Serializable) RepositoryDownloadWhitelist(org.pentaho.platform.repository.RepositoryDownloadWhitelist) FileNotFoundException(java.io.FileNotFoundException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Test(org.junit.Test)

Aggregations

Serializable (java.io.Serializable)2 Test (org.junit.Test)2 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)2 RepositoryDownloadWhitelist (org.pentaho.platform.repository.RepositoryDownloadWhitelist)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)1 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)1 RepositoryFileInputStream (org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream)1