Search in sources :

Example 26 with RepositoryFileDto

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

the class FileServiceIT method testDoSetContentCreatorFileNotFoundException.

@Test
public void testDoSetContentCreatorFileNotFoundException() throws Exception {
    String param = "file1";
    RepositoryFileDto mockFileDto = mock(RepositoryFileDto.class);
    Map<String, Serializable> fileMetadata = mock(Map.class);
    when(fileService.idToPath(param)).thenReturn("/file1");
    doReturn(null).when(fileService.defaultUnifiedRepositoryWebService).getFile("/file1");
    try {
        fileService.doSetContentCreator(param, mockFileDto);
        fail();
    } catch (FileNotFoundException e) {
    }
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) Serializable(java.io.Serializable) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 27 with RepositoryFileDto

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

the class FileServiceIT 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(nullable(String.class));
    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(nullable(String.class));
    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(nullable(String.class));
    Map<String, Serializable> metadata = new HashMap<String, Serializable>();
    doReturn(metadata).when(fileService.repository).getFileMetadata(nullable(String.class));
    RepositoryFile sourceFile = mock(RepositoryFile.class);
    doReturn(sourceFile).when(fileService.repository).getFileById(nullable(String.class));
    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(nullable(String.class));
    verify(fileService.defaultUnifiedRepositoryWebService, times(9)).getAcl(nullable(String.class));
    verify(repositoryFileAclDto, times(9)).getOwner();
    verify(fileService.policy, times(11)).isAllowed(nullable(String.class));
    verify(fileService.repository, times(9)).getFileMetadata(nullable(String.class));
    verify(fileService.repository, times(7)).setFileMetadata(nullable(String.class), any(Map.class));
    verify(file, times(8)).setHidden(anyBoolean());
    verify(fileService.repository, times(8)).getFileById(nullable(String.class));
    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(nullable(String.class));
    verify(fileService.repository, times(7)).updateFile(any(RepositoryFile.class), any(IRepositoryFileData.class), nullable(String.class));
    verify(fileService.repository, times(7)).updateAcl(any(RepositoryFileAcl.class));
    verify(fileService.repository).updateFolder(any(RepositoryFile.class), nullable(String.class));
}
Also used : StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) Serializable(java.io.Serializable) HashMap(java.util.HashMap) RepositoryFileAclAceDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileAclAceDto) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) RepositoryFileAclDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileAclDto) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) 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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 28 with RepositoryFileDto

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

the class FileServiceIT method testDoGetProperties.

@Test
public void testDoGetProperties() throws Exception {
    RepositoryFileDto file = mock(RepositoryFileDto.class);
    when(fileService.defaultUnifiedRepositoryWebService.getFile(nullable(String.class))).thenReturn(file);
    String pathId = "/usr/folder/file.txt";
    fileService.doGetProperties(pathId);
    verify(fileService.defaultUnifiedRepositoryWebService, times(1)).getFile(nullable(String.class));
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 29 with RepositoryFileDto

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

the class FileServiceIT method testDoSetContentCreator.

@Test
public void testDoSetContentCreator() throws Exception {
    String param = "file1";
    RepositoryFileDto repositoryFileDto = mock(RepositoryFileDto.class);
    Map<String, Serializable> fileMetadata = mock(Map.class);
    String idToPathResult = "/file1";
    doReturn(param).when(repositoryFileDto).getId();
    when(fileService.idToPath(param)).thenReturn(idToPathResult);
    doReturn(repositoryFileDto).when(fileService.defaultUnifiedRepositoryWebService).getFile(eq(idToPathResult));
    when(fileService.getRepository().getFileMetadata(repositoryFileDto.getId())).thenReturn(fileMetadata);
    try {
        fileService.doSetContentCreator(param, repositoryFileDto);
        verify(fileService.getRepository(), times(1)).getFileMetadata(repositoryFileDto.getId());
        verify(fileService.getRepository(), times(1)).setFileMetadata(param, fileMetadata);
    } catch (FileNotFoundException e) {
        fail();
    } catch (InternalError e) {
        fail();
    }
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) Serializable(java.io.Serializable) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 30 with RepositoryFileDto

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

the class FileServiceIT method testDoSetContentCreatorFileException.

@Test
public void testDoSetContentCreatorFileException() throws Exception {
    String param = "file1";
    RepositoryFileDto repositoryFileDto = mock(RepositoryFileDto.class);
    Map<String, Serializable> fileMetadata = mock(Map.class);
    doReturn(param).when(repositoryFileDto).getId();
    when(fileService.idToPath(param)).thenReturn("/file1");
    doReturn(repositoryFileDto).when(fileService.defaultUnifiedRepositoryWebService).getFile("/file1");
    when(fileService.getRepository().getFileMetadata(repositoryFileDto.getId())).thenThrow(new InternalError());
    // failing in get
    try {
        fileService.doSetContentCreator(param, repositoryFileDto);
    } catch (FileNotFoundException e) {
        fail();
    } catch (InternalError e) {
        verify(fileMetadata, times(0)).put(PentahoJcrConstants.PHO_CONTENTCREATOR, param);
        verify(fileService.repository, times(0)).setFileMetadata(param, fileMetadata);
    }
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) Serializable(java.io.Serializable) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.junit.Test)77 RepositoryFileDto (org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto)77 FileNotFoundException (java.io.FileNotFoundException)35 ArrayList (java.util.ArrayList)34 RepositoryFileDto (org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto)29 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)27 Serializable (java.io.Serializable)26 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)26 Matchers.anyString (org.mockito.Matchers.anyString)25 GeneralSecurityException (java.security.GeneralSecurityException)14 StringKeyStringValueDto (org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto)11 IllegalSelectorException (java.nio.channels.IllegalSelectorException)9 InvalidParameterException (java.security.InvalidParameterException)9 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)9 Properties (java.util.Properties)8 UnifiedRepositoryAccessDeniedException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException)8 DefaultUnifiedRepositoryWebService (org.pentaho.platform.repository2.unified.webservices.DefaultUnifiedRepositoryWebService)8 IOException (java.io.IOException)7 IRepositoryFileData (org.pentaho.platform.api.repository2.unified.IRepositoryFileData)7 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)7