Search in sources :

Example 11 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class ExporterTest method testExportDirectory.

public void testExportDirectory() throws Exception {
    when(repositoryFile.isFolder()).thenReturn(true);
    String name = "name";
    when(repositoryFile.getName()).thenReturn(name);
    Serializable mockSerializable = mock(Serializable.class);
    when(repositoryFile.getId()).thenReturn(mockSerializable);
    List<RepositoryFile> mockList = mock(List.class);
    when(unifiedRepository.getChildren(mockSerializable)).thenReturn(mockList);
    Iterator<RepositoryFile> mockIterator = mock(Iterator.class);
    RepositoryFile repositoryFile2 = mock(RepositoryFile.class);
    when(mockIterator.hasNext()).thenReturn(true, false);
    when(mockIterator.next()).thenReturn(repositoryFile2);
    when(mockList.iterator()).thenReturn(mockIterator);
    when(repositoryFile2.isFolder()).thenReturn(false);
    Serializable mockSerializable2 = mock(Serializable.class);
    when(repositoryFile2.getId()).thenReturn(mockSerializable2);
    SimpleRepositoryFileData mockRepoFileData = mock(SimpleRepositoryFileData.class);
    when(unifiedRepository.getDataForRead(mockSerializable2, SimpleRepositoryFileData.class)).thenReturn(mockRepoFileData);
    InputStream mockInputStream = new InputStream() {

        @Override
        public int read() throws IOException {
            // EOF
            return -1;
        }
    };
    when(mockRepoFileData.getStream()).thenReturn(mockInputStream);
    String name2 = "name.txt";
    when(repositoryFile2.getName()).thenReturn(name2);
    File parentDir = new File(System.getProperty("java.io.tmpdir"));
    File file = new File(parentDir, name);
    // already exists
    if (file.isDirectory()) {
        deleteDirectory(file);
    }
    exporter.exportDirectory(repositoryFile, parentDir);
    verify(repositoryFile, times(1)).isFolder();
    verify(repositoryFile, times(1)).getName();
    verify(repositoryFile, times(1)).getId();
    verify(unifiedRepository, times(1)).getChildren(mockSerializable);
    verify(mockIterator, times(2)).hasNext();
    verify(mockIterator, times(1)).next();
    verify(mockList, times(1)).iterator();
    verify(repositoryFile2, times(1)).getId();
    verify(unifiedRepository, times(1)).getDataForRead(mockSerializable2, SimpleRepositoryFileData.class);
    verify(mockRepoFileData, times(1)).getStream();
    verify(repositoryFile2, times(1)).getName();
}
Also used : Serializable(java.io.Serializable) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) InputStream(java.io.InputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 12 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class ExporterTest method testDoExportAsZip.

public void testDoExportAsZip() throws Exception {
    when(repositoryFile.isFolder()).thenReturn(true);
    when(repositoryFile.getPath()).thenReturn(REPO_PATH);
    Serializable mockSerializable = mock(Serializable.class);
    when(repositoryFile.getId()).thenReturn(mockSerializable);
    List<RepositoryFile> mockList = mock(List.class);
    when(unifiedRepository.getChildren(mockSerializable)).thenReturn(mockList);
    Iterator<RepositoryFile> mockIterator = mock(Iterator.class);
    RepositoryFile repositoryFile2 = mock(RepositoryFile.class);
    when(mockIterator.hasNext()).thenReturn(true, false);
    when(mockIterator.next()).thenReturn(repositoryFile2);
    when(mockList.iterator()).thenReturn(mockIterator);
    when(repositoryFile2.isFolder()).thenReturn(false);
    when(repositoryFile2.getPath()).thenReturn(REPO_PATH);
    Serializable mockSerializable2 = mock(Serializable.class);
    when(repositoryFile2.getId()).thenReturn(mockSerializable2);
    SimpleRepositoryFileData mockRepoFileData = mock(SimpleRepositoryFileData.class);
    when(unifiedRepository.getDataForRead(mockSerializable2, SimpleRepositoryFileData.class)).thenReturn(mockRepoFileData);
    InputStream mockInputStream = new InputStream() {

        @Override
        public int read() throws IOException {
            // EOF
            return -1;
        }
    };
    when(mockRepoFileData.getStream()).thenReturn(mockInputStream);
    File zipFile = exporter.doExportAsZip();
    // repoExport.length() = 10
    Assert.assertEquals("repoExport", zipFile.getName().substring(0, 10));
    Assert.assertNotNull(zipFile);
    verify(unifiedRepository, times(1)).getFile(REPO_PATH);
    verify(repositoryFile, times(1)).isFolder();
    verify(repositoryFile, times(1)).getId();
    verify(repositoryFile, times(1)).getPath();
    verify(repositoryFile, times(1)).getId();
    verify(unifiedRepository, times(1)).getChildren(mockSerializable);
    verify(mockIterator, times(2)).hasNext();
    verify(mockIterator, times(1)).next();
    verify(mockList, times(1)).iterator();
    verify(repositoryFile2, times(1)).isFolder();
    verify(repositoryFile2, times(1)).getPath();
    verify(repositoryFile2, times(1)).getId();
    verify(unifiedRepository, times(1)).getDataForRead(mockSerializable2, SimpleRepositoryFileData.class);
    verify(mockRepoFileData, times(1)).getStream();
}
Also used : Serializable(java.io.Serializable) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) InputStream(java.io.InputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 13 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class SimpleRepositoryFileDataDto method convert.

/**
 * Converts SimpleRepositoryFileDataDto to SimpleRepositoryFileData.
 */
public static SimpleRepositoryFileData convert(final SimpleRepositoryFileDataDto simpleJaxWsData) {
    FileOutputStream fout = null;
    InputStream in = null;
    DataHandler dh = null;
    boolean foutClosed = false;
    try {
        // $NON-NLS-1$
        File tmpFile = File.createTempFile("pentaho", null);
        // TODO mlowery this might not delete files soon enough
        tmpFile.deleteOnExit();
        fout = FileUtils.openOutputStream(tmpFile);
        // used to cast to com.sun.xml.ws.developer.StreamingDataHandler here but that stopped working
        dh = simpleJaxWsData.dataHandler;
        // used to call dh.readOnce() (instead of dh.getInputStream()) here
        in = dh.getInputStream();
        IOUtils.copy(in, fout);
        fout.close();
        foutClosed = true;
        InputStream fin = new BufferedInputStream(FileUtils.openInputStream(tmpFile));
        return new SimpleRepositoryFileData(fin, simpleJaxWsData.encoding, simpleJaxWsData.mimeType);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            // close the streams
            if (in != null) {
                in.close();
            }
            // used to have to call dh.close() on the com.sun.xml.ws.developer.StreamingDataHandler here
            if (fout != null && !foutClosed) {
                fout.close();
            }
        } catch (Exception e) {
        // CHECKSTYLES IGNORE
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) DataHandler(javax.activation.DataHandler) File(java.io.File) IOException(java.io.IOException)

Example 14 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class DefaultUnifiedRepositoryAuthorizationIT method testDeleteInheritingFile2.

@Test
public void testDeleteInheritingFile2() throws Exception {
    loginAsSysTenantAdmin();
    ITenant tenantAcme = tenantManager.createTenant(systemTenant, TENANT_ID_ACME, tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(tenantAcme, USERNAME_ADMIN, PASSWORD, "", new String[] { tenantAdminRoleName });
    login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    userRoleDao.createUser(tenantAcme, USERNAME_SUZY, PASSWORD, "", null);
    login(USERNAME_SUZY, tenantAcme, new String[] { tenantAuthenticatedRoleName });
    final String parentFolderPath = ClientRepositoryPaths.getUserHomeFolderPath(PentahoSessionHolder.getSession().getName());
    RepositoryFile parentFolder = repo.getFile(parentFolderPath);
    final String dataString = "Hello World!";
    final String encoding = "UTF-8";
    byte[] data = dataString.getBytes(encoding);
    ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
    final String mimeType = "text/plain";
    final String fileName = "helloworld.xaction";
    final SimpleRepositoryFileData content = new SimpleRepositoryFileData(dataStream, encoding, mimeType);
    RepositoryFile newFolder = null;
    // Try an inheriting file delete
    // CHECKSTYLE IGNORE AvoidNestedBlocks FOR NEXT 3 LINES
    {
        newFolder = repo.createFolder(parentFolder.getId(), new RepositoryFile.Builder("testFolder").folder(true).build(), null, null);
        RepositoryFile newFile = repo.createFile(newFolder.getId(), new RepositoryFile.Builder("testFile").folder(false).build(), content, null);
        RepositoryFileAcl acl = repo.getAcl(newFile.getId());
        RepositoryFileAcl newAcl = new RepositoryFileAcl.Builder(acl).ace(userNameUtils.getPrincipleId(tenantAcme, USERNAME_ADMIN), RepositoryFileSid.Type.USER, RepositoryFilePermission.ALL).entriesInheriting(true).build();
        repo.updateAcl(newAcl);
        login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
        try {
            repo.deleteFile(newFile.getId(), null);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
    // Now try one not inheriting
    // CHECKSTYLE IGNORE AvoidNestedBlocks FOR NEXT 3 LINES
    {
        RepositoryFile newFile = repo.createFile(newFolder.getId(), new RepositoryFile.Builder("testFile").folder(false).build(), content, null);
        RepositoryFileAcl acl = repo.getAcl(newFile.getId());
        RepositoryFileAcl newAcl = new RepositoryFileAcl.Builder(acl).ace(userNameUtils.getPrincipleId(tenantAcme, USERNAME_ADMIN), RepositoryFileSid.Type.USER, RepositoryFilePermission.ALL).entriesInheriting(false).build();
        repo.updateAcl(newAcl);
        login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
        try {
            repo.deleteFile(newFile.getId(), null);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Test(org.junit.Test)

Example 15 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class DefaultUnifiedRepositoryAuthorizationIT method testDeleteInheritingFolder.

@Test
public void testDeleteInheritingFolder() throws Exception {
    loginAsSysTenantAdmin();
    ITenant tenantAcme = tenantManager.createTenant(systemTenant, TENANT_ID_ACME, tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(tenantAcme, USERNAME_ADMIN, PASSWORD, "", new String[] { tenantAdminRoleName });
    login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    userRoleDao.createUser(tenantAcme, USERNAME_SUZY, PASSWORD, "", null);
    login(USERNAME_SUZY, tenantAcme, new String[] { tenantAuthenticatedRoleName });
    final String parentFolderPath = ClientRepositoryPaths.getUserHomeFolderPath(PentahoSessionHolder.getSession().getName());
    RepositoryFile parentFolder = repo.getFile(parentFolderPath);
    final String dataString = "Hello World!";
    final String encoding = "UTF-8";
    byte[] data = dataString.getBytes(encoding);
    ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
    final String mimeType = "text/plain";
    final String fileName = "helloworld.xaction";
    final SimpleRepositoryFileData content = new SimpleRepositoryFileData(dataStream, encoding, mimeType);
    // Try an inheriting folder delete
    // CHECKSTYLE IGNORE AvoidNestedBlocks FOR NEXT 3 LINES
    {
        RepositoryFile newFolder = repo.createFolder(parentFolder.getId(), new RepositoryFile.Builder("testFolder").folder(true).build(), null, null);
        RepositoryFileAcl acl = repo.getAcl(newFolder.getId());
        RepositoryFileAcl newAcl = new RepositoryFileAcl.Builder(acl).ace(userNameUtils.getPrincipleId(tenantAcme, USERNAME_ADMIN), RepositoryFileSid.Type.USER, RepositoryFilePermission.ALL).entriesInheriting(true).build();
        repo.updateAcl(newAcl);
        login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
        try {
            repo.deleteFile(newFolder.getId(), null);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
    // Now try one not inheriting
    // CHECKSTYLE IGNORE AvoidNestedBlocks FOR NEXT 3 LINES
    {
        RepositoryFile newFolder = repo.createFolder(parentFolder.getId(), new RepositoryFile.Builder("testFolder2").folder(true).build(), null, null);
        RepositoryFileAcl acl = repo.getAcl(newFolder.getId());
        RepositoryFileAcl newAcl = new RepositoryFileAcl.Builder(acl).clearAces().ace(userNameUtils.getPrincipleId(tenantAcme, USERNAME_ADMIN), RepositoryFileSid.Type.USER, RepositoryFilePermission.ALL).entriesInheriting(false).build();
        repo.updateAcl(newAcl);
        login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
        try {
            repo.deleteFile(newFolder.getId(), null);
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Test(org.junit.Test)

Aggregations

SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)58 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)43 ByteArrayInputStream (java.io.ByteArrayInputStream)31 Test (org.junit.Test)25 Matchers.anyString (org.mockito.Matchers.anyString)18 ITenant (org.pentaho.platform.api.mt.ITenant)17 InputStream (java.io.InputStream)16 IOException (java.io.IOException)14 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)12 Serializable (java.io.Serializable)10 File (java.io.File)9 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)9 FileNotFoundException (java.io.FileNotFoundException)5 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)5 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)5 VersionSummary (org.pentaho.platform.api.repository2.unified.VersionSummary)5 FileOutputStream (java.io.FileOutputStream)4 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)4 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)4 RepositoryRequest (org.pentaho.platform.api.repository2.unified.RepositoryRequest)4