Search in sources :

Example 6 with StringKeyStringValueDto

use of org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto 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 7 with StringKeyStringValueDto

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

the class FileService method doGetLocaleProperties.

/**
 * Retrieve the list of locale properties for a given locale
 *
 * @param pathId (colon separated path for the repository file)
 * @param locale
 * @return
 */
public List<StringKeyStringValueDto> doGetLocaleProperties(String pathId, String locale) {
    RepositoryFileDto file = getRepoWs().getFile(idToPath(pathId));
    List<StringKeyStringValueDto> keyValueList = new ArrayList<StringKeyStringValueDto>();
    if (file != null) {
        PropertiesWrapper propertiesWrapper = getRepoWs().getLocalePropertiesForFileById(file.getId(), locale);
        if (propertiesWrapper != null) {
            Properties properties = propertiesWrapper.getProperties();
            if (properties != null && !properties.isEmpty()) {
                for (String key : properties.stringPropertyNames()) {
                    keyValueList.add(getStringKeyStringValueDto(key, properties.getProperty(key)));
                }
            }
        }
    }
    return keyValueList;
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) ArrayList(java.util.ArrayList) PropertiesWrapper(org.pentaho.platform.repository2.unified.webservices.PropertiesWrapper) Properties(java.util.Properties)

Example 8 with StringKeyStringValueDto

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

the class FileService method doGetFileLocales.

/**
 * Retrieves the list of locale map for the selected repository file. The list will be empty if a problem occurs.
 *
 * @param pathId colon separated path for the repository file
 * <pre function="syntax.xml">
 *    :path:to:file:id
 * </pre>
 * @return <code>List<LocaleMapDto></code> the list of locales
 *         <pre function="syntax.xml">
 *           <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 *           &lt;localePropertiesMapEntries&gt;
 *             &lt;localeMapDto&gt;
 *               &lt;locale&gt;default&lt;/locale&gt;
 *               &lt;properties&gt;
 *                 &lt;stringKeyStringValueDto&gt;
 *                   &lt;key&gt;file.title&lt;/key&gt;
 *                   &lt;value&gt;myFile&lt;/value&gt;
 *                 &lt;/stringKeyStringValueDto&gt;
 *                 &lt;stringKeyStringValueDto&gt;
 *                   &lt;key&gt;jcr:primaryType&lt;/key&gt;
 *                   &lt;value&gt;nt:unstructured&lt;/value&gt;
 *                 &lt;/stringKeyStringValueDto&gt;
 *                 &lt;stringKeyStringValueDto&gt;
 *                   &lt;key&gt;title&lt;/key&gt;
 *                   &lt;value&gt;myFile&lt;/value&gt;
 *                 &lt;/stringKeyStringValueDto&gt;
 *                 &lt;stringKeyStringValueDto&gt;
 *                   &lt;key&gt;file.description&lt;/key&gt;
 *                   &lt;value&gt;myFile Description&lt;/value&gt;
 *                 &lt;/stringKeyStringValueDto&gt;
 *               &lt;/properties&gt;
 *             &lt;/localeMapDto&gt;
 *           &lt;/localePropertiesMapEntries&gt;
 *         </pre>
 * @throws FileNotFoundException
 */
public List<LocaleMapDto> doGetFileLocales(String pathId) throws FileNotFoundException {
    List<LocaleMapDto> availableLocales = new ArrayList<LocaleMapDto>();
    RepositoryFileDto file = getRepoWs().getFile(idToPath(pathId));
    if (file == null) {
        throw new FileNotFoundException();
    }
    try {
        List<PentahoLocale> locales = getRepoWs().getAvailableLocalesForFileById(file.getId());
        if (locales != null && !locales.isEmpty()) {
            for (PentahoLocale locale : locales) {
                availableLocales.add(new LocaleMapDto(locale.toString(), null));
            }
        }
    } catch (Exception e) {
        throw new InternalError();
    }
    return availableLocales;
}
Also used : RepositoryFileDto(org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto) LocaleMapDto(org.pentaho.platform.api.repository2.unified.webservices.LocaleMapDto) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) GeneralSecurityException(java.security.GeneralSecurityException) InvalidParameterException(java.security.InvalidParameterException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) UnifiedRepositoryAccessDeniedException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryAccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExportException(org.pentaho.platform.plugin.services.importexport.ExportException) IllegalSelectorException(java.nio.channels.IllegalSelectorException) IOException(java.io.IOException)

Example 9 with StringKeyStringValueDto

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

the class FileResourceTest method testDoSetMetadata.

@Test
public void testDoSetMetadata() throws Exception {
    List<StringKeyStringValueDto> metadata = mock(List.class);
    doNothing().when(fileResource.fileService).doSetMetadata(PATH_ID, metadata);
    Response mockResponse = mock(Response.class);
    doReturn(mockResponse).when(fileResource).buildOkResponse();
    Response testResponse = fileResource.doSetMetadata(PATH_ID, metadata);
    assertEquals(mockResponse, testResponse);
    verify(fileResource.fileService, times(1)).doSetMetadata(PATH_ID, metadata);
    verify(fileResource, times(1)).buildOkResponse();
}
Also used : StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) Response(javax.ws.rs.core.Response) Test(org.junit.Test)

Example 10 with StringKeyStringValueDto

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

the class RepositoryFileAdapter method toFile.

public static RepositoryFile toFile(final RepositoryFileDto v) {
    if (v == null) {
        return null;
    }
    RepositoryFile.Builder builder = null;
    if (v.getId() != null) {
        builder = new RepositoryFile.Builder(v.getId(), v.getName());
    } else {
        builder = new RepositoryFile.Builder(v.getName());
    }
    if (v.getOwnerType() != -1) {
        new RepositoryFileSid(v.getOwner(), RepositoryFileSid.Type.values()[v.getOwnerType()]);
    }
    if (v.getLocalePropertiesMapEntries() != null) {
        for (LocaleMapDto localeMapDto : v.getLocalePropertiesMapEntries()) {
            String locale = localeMapDto.getLocale();
            Properties localeProperties = new Properties();
            if (localeMapDto.getProperties() != null) {
                for (StringKeyStringValueDto keyValueDto : localeMapDto.getProperties()) {
                    localeProperties.put(keyValueDto.getKey(), keyValueDto.getValue());
                }
            }
            builder.localeProperties(locale, localeProperties);
        }
    }
    return builder.path(v.getPath()).createdDate(unmarshalDate(v.getCreatedDate())).creatorId(v.getCreatorId()).description(v.getDescription()).folder(v.isFolder()).fileSize(v.getFileSize()).lastModificationDate(unmarshalDate(v.getLastModifiedDate())).locale(v.getLocale()).lockDate(unmarshalDate(v.getLockDate())).locked(v.isLocked()).lockMessage(v.getLockMessage()).lockOwner(v.getLockOwner()).title(v.getTitle()).versioned(v.isVersioned()).versionId(v.getVersionId()).originalParentFolderPath(v.getOriginalParentFolderPath()).deletedDate(unmarshalDate(v.getDeletedDate())).hidden(v.isHidden()).schedulable(!v.isNotSchedulable()).aclNode(v.isAclNode()).build();
}
Also used : StringKeyStringValueDto(org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto) RepositoryFileSid(org.pentaho.platform.api.repository2.unified.RepositoryFileSid) LocaleMapDto(org.pentaho.platform.api.repository2.unified.webservices.LocaleMapDto) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Properties(java.util.Properties)

Aggregations

StringKeyStringValueDto (org.pentaho.platform.api.repository2.unified.webservices.StringKeyStringValueDto)20 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 RepositoryFileDto (org.pentaho.platform.api.repository2.unified.webservices.RepositoryFileDto)12 GeneralSecurityException (java.security.GeneralSecurityException)9 FileNotFoundException (java.io.FileNotFoundException)8 Serializable (java.io.Serializable)8 Properties (java.util.Properties)8 Matchers.anyString (org.mockito.Matchers.anyString)6 StringKeyStringValueDto (org.pentaho.platform.repository2.unified.webservices.StringKeyStringValueDto)6 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)5 RepositoryFileDto (org.pentaho.platform.repository2.unified.webservices.RepositoryFileDto)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 IOException (java.io.IOException)4 IllegalSelectorException (java.nio.channels.IllegalSelectorException)4 InvalidParameterException (java.security.InvalidParameterException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)4 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)4