Search in sources :

Example 96 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class FileServiceTest method doSetMetadata.

@Test
public void doSetMetadata() {
    String pathId = "path:to:file:file1.ext";
    List<StringKeyStringValueDto> stringKeyStringValueDtos = new ArrayList<StringKeyStringValueDto>();
    StringKeyStringValueDto stringKeyStringValueDto1 = mock(StringKeyStringValueDto.class);
    doReturn("key1").when(stringKeyStringValueDto1).getKey();
    doReturn("value1").when(stringKeyStringValueDto1).getValue();
    StringKeyStringValueDto stringKeyStringValueDto2 = mock(StringKeyStringValueDto.class);
    doReturn("key2").when(stringKeyStringValueDto2).getKey();
    doReturn("value2").when(stringKeyStringValueDto2).getValue();
    stringKeyStringValueDtos.add(stringKeyStringValueDto1);
    stringKeyStringValueDtos.add(stringKeyStringValueDto2);
    doReturn("/path/to/file/file1.ext").when(fileService).idToPath(pathId);
    doReturn(true).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
    doReturn(true).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(true).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
    RepositoryFileDto file = mock(RepositoryFileDto.class);
    doReturn(false).when(file).isFolder();
    doReturn(true).when(file).isHidden();
    doReturn(file).when(fileService.defaultUnifiedRepositoryWebService).getFile(anyString());
    List<RepositoryFileAclAceDto> repositoryFileAclAceDtos = new ArrayList<RepositoryFileAclAceDto>();
    RepositoryFileAclDto repositoryFileAclDto = mock(RepositoryFileAclDto.class);
    doReturn("sessionName").when(repositoryFileAclDto).getOwner();
    doReturn(true).when(repositoryFileAclDto).isEntriesInheriting();
    doReturn(repositoryFileAclAceDtos).when(repositoryFileAclDto).getAces();
    doReturn(repositoryFileAclDto).when(fileService.defaultUnifiedRepositoryWebService).getAcl(anyString());
    IPentahoSession pentahoSession = mock(IPentahoSession.class);
    doReturn(pentahoSession).when(fileService).getSession();
    doReturn("sessionName").when(pentahoSession).getName();
    RepositoryFileAclAceDto repositoryFileAclAceDto = mock(RepositoryFileAclAceDto.class);
    List<Integer> permissions = new ArrayList<Integer>();
    permissions.add(RepositoryFilePermission.ACL_MANAGEMENT.ordinal());
    doReturn(permissions).when(repositoryFileAclAceDto).getPermissions();
    doReturn("sessionName").when(repositoryFileAclAceDto).getRecipient();
    repositoryFileAclAceDtos.add(repositoryFileAclAceDto);
    doReturn(repositoryFileAclAceDtos).when(fileService.defaultUnifiedRepositoryWebService).getEffectiveAces(anyString());
    Map<String, Serializable> metadata = new HashMap<String, Serializable>();
    doReturn(metadata).when(fileService.repository).getFileMetadata(anyString());
    RepositoryFile sourceFile = mock(RepositoryFile.class);
    doReturn(sourceFile).when(fileService.repository).getFileById(anyString());
    RepositoryFileDto destFileDto = mock(RepositoryFileDto.class);
    doReturn(destFileDto).when(fileService).toFileDto(sourceFile, null, false);
    RepositoryFile destFile = mock(RepositoryFile.class);
    doReturn(destFile).when(fileService).toFile(destFileDto);
    RepositoryFileAcl acl = mock(RepositoryFileAcl.class);
    doReturn(acl).when(fileService.repository).getAcl(acl);
    // Test 1 - canManage should be true at start
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
    } catch (GeneralSecurityException e) {
        fail();
    }
    // Test 2 - canManage should be false at start
    doReturn(false).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
    doReturn(false).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(false).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
    doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
    } catch (GeneralSecurityException e) {
        fail();
    }
    // Test 3 - canManage should be false at start
    doReturn(true).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
    doReturn(false).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(false).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
    doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
    } catch (GeneralSecurityException e) {
        fail();
    }
    // Test 4 - canManage should be false at start
    doReturn(false).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
    doReturn(true).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(false).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
    doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
    } catch (GeneralSecurityException e) {
        fail();
    }
    // Test 5 - canManage should be false at start
    doReturn(false).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
    doReturn(false).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(true).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
    doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
    } catch (GeneralSecurityException e) {
        fail();
    }
    // Test 6 - canManage should be false at start
    doReturn(true).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
    doReturn(true).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(false).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
    doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
    } catch (GeneralSecurityException e) {
        fail();
    }
    // Test 7 - canManage should be false at start
    doReturn(false).when(fileService.policy).isAllowed(RepositoryReadAction.NAME);
    doReturn(true).when(fileService.policy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(true).when(fileService.policy).isAllowed(AdministerSecurityAction.NAME);
    doReturn("sessionName1").when(repositoryFileAclDto).getOwner();
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
    } catch (GeneralSecurityException e) {
        fail();
    }
    // Test 8 - canManage should be false at start
    doReturn(true).when(file).isFolder();
    doReturn(true).when(file).isHidden();
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
    } catch (GeneralSecurityException e) {
        fail();
    }
    // Test 9
    StringKeyStringValueDto stringKeyStringValueDto3 = mock(StringKeyStringValueDto.class);
    doReturn("_PERM_HIDDEN").when(stringKeyStringValueDto3).getKey();
    doReturn("true").when(stringKeyStringValueDto3).getValue();
    stringKeyStringValueDtos.add(stringKeyStringValueDto3);
    try {
        fileService.doSetMetadata(pathId, stringKeyStringValueDtos);
    } catch (GeneralSecurityException e) {
        fail();
    }
    verify(fileService.defaultUnifiedRepositoryWebService, times(9)).getFile(anyString());
    verify(fileService.defaultUnifiedRepositoryWebService, times(9)).getAcl(anyString());
    verify(repositoryFileAclDto, times(9)).getOwner();
    verify(fileService.policy, times(11)).isAllowed(anyString());
    verify(fileService.repository, times(9)).getFileMetadata(anyString());
    verify(fileService.repository, times(7)).setFileMetadata(anyString(), any(Map.class));
    verify(file, times(8)).setHidden(anyBoolean());
    verify(fileService.repository, times(8)).getFileById(anyString());
    verify(fileService, times(8)).toFileDto(any(RepositoryFile.class), anySet(), anyBoolean());
    verify(fileService, times(8)).toFile(any(RepositoryFileDto.class));
    verify(destFileDto, times(8)).setHidden(anyBoolean());
    verify(fileService.repository, times(8)).getAcl(anyString());
    verify(fileService.repository, times(7)).updateFile(any(RepositoryFile.class), any(IRepositoryFileData.class), anyString());
    verify(fileService.repository, times(7)).updateAcl(any(RepositoryFileAcl.class));
    verify(fileService.repository).updateFolder(any(RepositoryFile.class), anyString());
}
Also used : StringKeyStringValueDto(org.pentaho.platform.repository2.unified.webservices.StringKeyStringValueDto) RepositoryFileDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto) IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) Serializable(java.io.Serializable) HashMap(java.util.HashMap) RepositoryFileAclAceDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclAceDto) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) RepositoryFileAclDto(org.pentaho.platform.repository2.unified.webservices.RepositoryFileAclDto) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 97 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class FileServiceTest method testDoRenameNegative.

@Test
public void testDoRenameNegative() throws Exception {
    RepositoryFile repositoryFile = mock(RepositoryFile.class);
    when(repositoryFile.getPath()).thenReturn("/dir/file.txt");
    when(repositoryFile.getName()).thenReturn("file.txt");
    when(fileService.repository.getFile(anyString())).thenReturn(repositoryFile);
    String pathId = ":dir:file.txt";
    String newName = "file1.txt";
    boolean success = fileService.doRename(pathId, newName);
    assertFalse(success);
}
Also used : RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 98 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class FileServiceTest method testDoGetFileOrDir.

@Test
public void testDoGetFileOrDir() throws Exception {
    RepositoryFile file = mock(RepositoryFile.class);
    doReturn("file.txt").when(file).getName();
    when(fileService.repository.getFile(anyString())).thenReturn(file);
    RepositoryFileInputStream mockInputStream = mock(RepositoryFileInputStream.class);
    doReturn(1).when(fileService).copy(any(java.io.InputStream.class), any(java.io.OutputStream.class));
    doReturn(mockInputStream).when(fileService).getRepositoryFileInputStream(any(RepositoryFile.class));
    String pathId = "/usr/folder/file.txt";
    FileService.RepositoryFileToStreamWrapper wrapper = fileService.doGetFileOrDir(pathId);
    assertEquals("file.txt", wrapper.getRepositoryFile().getName());
}
Also used : RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) RepositoryFileOutputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) Test(org.junit.Test)

Example 99 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class FileServiceTest method getMockedRepoFile.

public RepositoryFile getMockedRepoFile(String fileName) {
    RepositoryFile repoFileDto = mock(RepositoryFile.class);
    when(repoFileDto.getName()).thenReturn(fileName);
    return repoFileDto;
}
Also used : RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 100 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class OlapServiceImplTest method testRemoveOlap4jCatalogsWithoutPermission.

/**
 * Validates getting a list of remote catalogs.
 */
@Test
public void testRemoveOlap4jCatalogsWithoutPermission() throws Exception {
    stubGetChildren(repository, olapFolderPath, "myServer");
    // Stub /etc/olap-servers/myServer
    final String testServerPath = olapFolderPath + RepositoryFile.SEPARATOR + "myServer";
    stubGetFolder(repository, testServerPath);
    stubGetChildren(repository, testServerPath, "metadata");
    // Stub /etc/olap-servers/myServer/metadata
    final String metadataPath = testServerPath + RepositoryFile.SEPARATOR + "metadata";
    stubGetFile(repository, metadataPath);
    stubGetData(repository, metadataPath + RepositoryFile.SEPARATOR + "myServer", "server", pathPropertyPair("/server/name", "myServer"), pathPropertyPair("/server/user", "myUser"), pathPropertyPair("/server/password", "myPassword"), pathPropertyPair("/server/URL", "myUrl"), pathPropertyPair("/server/className", "someClass"));
    // Get a list of catalogs.
    final List<String> catalogs = olapService.getCatalogNames(session);
    assertEquals(1, catalogs.size());
    assertEquals("myServer", catalogs.get(0));
    verify(repository).getChildren(eq(makeIdObject(olapFolderPath)));
    // Stub the security
    accessMock = new DefaultAccessImpl() {

        public boolean hasAccess(String path, EnumSet<RepositoryFilePermission> perms, IPentahoSession session) {
            if (perms.contains(RepositoryFilePermission.DELETE) && path.equals("myServer")) {
                return false;
            }
            return true;
        }
    };
    // Try to delete it. We expect it to fail.
    try {
        olapService.removeCatalog("myServer", session);
        fail();
    } catch (IOlapServiceException e) {
        assertEquals(IOlapServiceException.Reason.ACCESS_DENIED, e.getReason());
        assertTrue(e.getMessage().contains("OlapServiceImpl.ERROR_0003"));
    }
    // Make sure we didn't invoke the delete method.
    verify(repository, never()).deleteFile((RepositoryFile) anyObject(), anyString());
}
Also used : IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) RepositoryFilePermission(org.pentaho.platform.api.repository2.unified.RepositoryFilePermission) IOlapServiceException(org.pentaho.platform.plugin.action.olap.IOlapServiceException) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)455 Test (org.junit.Test)183 ITenant (org.pentaho.platform.api.mt.ITenant)87 Matchers.anyString (org.mockito.Matchers.anyString)86 ArrayList (java.util.ArrayList)85 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)83 Serializable (java.io.Serializable)56 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)53 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)49 KettleException (org.pentaho.di.core.exception.KettleException)40 NodeRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData)40 ByteArrayInputStream (java.io.ByteArrayInputStream)39 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)39 IOException (java.io.IOException)37 File (java.io.File)34 MetaStoreException (org.pentaho.metastore.api.exceptions.MetaStoreException)34 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)33 MetaStoreNamespaceExistsException (org.pentaho.metastore.api.exceptions.MetaStoreNamespaceExistsException)33 IdNotFoundException (org.pentaho.di.core.exception.IdNotFoundException)32 KettleFileException (org.pentaho.di.core.exception.KettleFileException)32