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();
}
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();
}
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
}
}
}
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();
}
}
}
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();
}
}
}
Aggregations