Search in sources :

Example 11 with PentahoAccessControlException

use of org.pentaho.platform.api.engine.PentahoAccessControlException in project data-access by pentaho.

the class DataSourceWizardResourceTest method testRemoveError.

@Test
public void testRemoveError() throws Exception {
    Response mockResponse = mock(Response.class);
    // Test 1
    PentahoAccessControlException mockPentahoAccessControlException = mock(PentahoAccessControlException.class);
    doThrow(mockPentahoAccessControlException).when(dataSourceWizardResource.service).removeDSW("dswId");
    doReturn(mockResponse).when(dataSourceWizardResource).buildUnauthorizedResponse();
    try {
        Response response = dataSourceWizardResource.remove("dswId");
        fail("should have thrown an exception");
    } catch (Exception e) {
    // good
    }
    verify(dataSourceWizardResource, times(1)).remove("dswId");
}
Also used : Response(javax.ws.rs.core.Response) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) FileNotFoundException(java.io.FileNotFoundException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 12 with PentahoAccessControlException

use of org.pentaho.platform.api.engine.PentahoAccessControlException in project data-access by pentaho.

the class DataSourceWizardResourceTest method testPublishDswError.

@Test
public void testPublishDswError() throws Exception {
    String domainId = "domainId";
    InputStream metadataFile = mock(InputStream.class);
    boolean overwrite = false;
    boolean checkConnection = false;
    Response mockResponse = mock(Response.class);
    // Test 1
    PentahoAccessControlException mockPentahoAccessControlException = mock(PentahoAccessControlException.class);
    doThrow(mockPentahoAccessControlException).when(dataSourceWizardResource.service).publishDsw(domainId, metadataFile, overwrite, checkConnection, null);
    doReturn(mockResponse).when(dataSourceWizardResource).buildUnauthorizedResponse();
    Response response = dataSourceWizardResource.publishDsw(domainId, metadataFile, overwrite, checkConnection, null);
    assertEquals(mockResponse, response);
    // Test 2
    IllegalArgumentException mockIllegalArgumentException = mock(IllegalArgumentException.class);
    doThrow(mockIllegalArgumentException).when(dataSourceWizardResource.service).publishDsw(domainId, metadataFile, overwrite, checkConnection, null);
    doReturn(mockResponse).when(dataSourceWizardResource).buildBadRequestResponse(anyString());
    response = dataSourceWizardResource.publishDsw(domainId, metadataFile, overwrite, checkConnection, null);
    assertEquals(mockResponse, response);
    // Test 3
    DataSourceWizardService.DswPublishValidationException mockDataSourceWizardServiceDswPublishValidationException = mock(DataSourceWizardService.DswPublishValidationException.class);
    doThrow(mockDataSourceWizardServiceDswPublishValidationException).when(dataSourceWizardResource.service).publishDsw(domainId, metadataFile, overwrite, checkConnection, null);
    doReturn(mockResponse).when(dataSourceWizardResource).buildConfilictResponse(anyString());
    response = dataSourceWizardResource.publishDsw(domainId, metadataFile, overwrite, checkConnection, null);
    assertEquals(mockResponse, response);
    // Test 4
    RuntimeException mockException = mock(RuntimeException.class);
    doThrow(mockException).when(dataSourceWizardResource.service).publishDsw(domainId, metadataFile, overwrite, checkConnection, null);
    doReturn(mockResponse).when(dataSourceWizardResource).buildServerErrorResponse();
    response = dataSourceWizardResource.publishDsw(domainId, metadataFile, overwrite, checkConnection, null);
    assertEquals(mockResponse, response);
    verify(dataSourceWizardResource, times(4)).publishDsw(domainId, metadataFile, overwrite, checkConnection, null);
}
Also used : Response(javax.ws.rs.core.Response) InputStream(java.io.InputStream) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) DataSourceWizardService(org.pentaho.platform.dataaccess.datasource.api.DataSourceWizardService) Test(org.junit.Test)

Example 13 with PentahoAccessControlException

use of org.pentaho.platform.api.engine.PentahoAccessControlException in project data-access by pentaho.

the class MetadataResourceTest method doSetMetadataAcl.

@Test
public void doSetMetadataAcl() throws Exception {
    String domainId = "domainId";
    doReturn(new HashMap<String, InputStream>() {

        {
            put("test", null);
        }
    }).when(metadataResource).getDomainFilesData(domainId);
    Response response = metadataResource.doSetMetadataAcl(domainId, null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    // 
    doThrow(new PentahoAccessControlException()).when(metadataResource.service).setMetadataAcl(domainId, null);
    response = metadataResource.doSetMetadataAcl(domainId, null);
    assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
    // 
    doThrow(new FileNotFoundException()).when(metadataResource.service).setMetadataAcl(domainId, null);
    response = metadataResource.doSetMetadataAcl(domainId, null);
    assertEquals(Response.Status.CONFLICT.getStatusCode(), response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) Test(org.junit.Test)

Example 14 with PentahoAccessControlException

use of org.pentaho.platform.api.engine.PentahoAccessControlException in project data-access by pentaho.

the class DataSourceWizardService method publishDsw.

public String publishDsw(String domainId, InputStream metadataFile, List<InputStream> localizeFiles, List<String> localizeFileNames, boolean overwrite, boolean checkConnection, RepositoryFileAclDto acl) throws PentahoAccessControlException, IllegalArgumentException, DswPublishValidationException, Exception {
    if (!hasManageAccessCheck()) {
        throw new PentahoAccessControlException();
    }
    if (!endsWith(domainId, METADATA_EXT)) {
        // if doesn't end in case-sensitive '.xmi' there will be trouble later on
        final String errorMsg = "domainId must end in " + METADATA_EXT;
        throw new IllegalArgumentException(errorMsg);
    }
    if (localizeFiles == null ? (localizeFileNames != null) : (localizeFiles.size() != localizeFileNames.size())) {
        throw new IllegalArgumentException("localizeFiles and localizeFileNames must have equal size");
    }
    if (metadataFile == null) {
        throw new IllegalArgumentException("metadataFile is null");
    }
    if (!overwrite) {
        final List<String> overwritten = getOverwrittenDomains(domainId);
        if (!overwritten.isEmpty()) {
            final String domainIds = StringUtils.join(overwritten, ",");
            throw new DswPublishValidationException(DswPublishValidationException.Type.OVERWRITE_CONFLICT, domainIds);
        }
    }
    XmiParser xmiParser = createXmiParser();
    Domain domain = null;
    try {
        domain = xmiParser.parseXmi(metadataFile);
    } catch (Exception e) {
        throw new DswPublishValidationException(DswPublishValidationException.Type.INVALID_XMI, e.getMessage());
    }
    domain.setId(domainId);
    if (checkConnection) {
        final String connectionId = getMondrianDatasourceWrapper(domain);
        // Left second check with non-escaped name for backward compatibility
        if (datasourceMgmtSvc.getDatasourceByName(sanitizer.escape(connectionId)) == null && datasourceMgmtSvc.getDatasourceByName(connectionId) == null) {
            final String msg = "connection not found: '" + connectionId + "'";
            throw new DswPublishValidationException(Type.MISSING_CONNECTION, msg);
        }
    }
    // build bundles
    IPlatformImportBundle mondrianBundle = createMondrianDswBundle(domain, acl);
    InputStream metadataIn = toInputStreamWrapper(domain, xmiParser);
    IPlatformImportBundle metadataBundle = createMetadataDswBundle(domain, metadataIn, overwrite, acl);
    // add localization bundles
    if (localizeFiles != null) {
        for (int i = 0; i < localizeFiles.size(); i++) {
            IPlatformImportBundle localizationBundle = MetadataService.createNewRepositoryFileImportBundle(localizeFiles.get(i), localizeFileNames.get(i), domainId);
            metadataBundle.getChildBundles().add(localizationBundle);
            logger.info("created language file");
        }
    }
    // do import
    IPlatformImporter importer = getIPlatformImporter();
    importer.importFile(metadataBundle);
    logger.debug("imported metadata xmi");
    importer.importFile(mondrianBundle);
    logger.debug("imported mondrian schema");
    // trigger refreshes
    IPentahoSession session = getSession();
    PentahoSystem.publish(session, METADATA_PUBLISHER);
    PentahoSystem.publish(session, MONDRIAN_PUBLISHER);
    logger.info("publishDsw: Published DSW with domainId='" + domainId + "'.");
    return domainId;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) FileNotFoundException(java.io.FileNotFoundException) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) MondrianCatalogServiceException(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) IOException(java.io.IOException) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) XmiParser(org.pentaho.metadata.util.XmiParser) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter) Domain(org.pentaho.metadata.model.Domain)

Example 15 with PentahoAccessControlException

use of org.pentaho.platform.api.engine.PentahoAccessControlException in project data-access by pentaho.

the class DataSourceWizardService method doGetDSWFilesAsDownload.

public Map<String, InputStream> doGetDSWFilesAsDownload(String dswId) throws PentahoAccessControlException {
    if (!canManageACL()) {
        throw new PentahoAccessControlException();
    }
    // First get the metadata files;
    Map<String, InputStream> fileData = getMetadataFiles(dswId);
    // Then get the corresponding mondrian files
    Domain domain = metadataDomainRepository.getDomain(dswId);
    ModelerWorkspace model = createModelerWorkspace();
    model.setDomain(domain);
    LogicalModel logicalModel = model.getLogicalModel(ModelerPerspective.ANALYSIS);
    if (logicalModel == null) {
        logicalModel = model.getLogicalModel(ModelerPerspective.REPORTING);
    }
    if (logicalModel.getProperty(MONDRIAN_CATALOG_REF) != null) {
        MondrianCatalogRepositoryHelper helper = createMondrianCatalogRepositoryHelper();
        String catalogRef = (String) logicalModel.getProperty(MONDRIAN_CATALOG_REF);
        fileData.putAll(helper.getModrianSchemaFiles(catalogRef));
        parseMondrianSchemaNameWrapper(dswId, fileData);
    }
    return fileData;
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MondrianCatalogRepositoryHelper(org.pentaho.platform.plugin.services.importexport.legacy.MondrianCatalogRepositoryHelper) Domain(org.pentaho.metadata.model.Domain) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) ModelerWorkspace(org.pentaho.agilebi.modeler.ModelerWorkspace)

Aggregations

PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)48 Test (org.junit.Test)28 InputStream (java.io.InputStream)13 Response (javax.ws.rs.core.Response)13 FileNotFoundException (java.io.FileNotFoundException)10 WebApplicationException (javax.ws.rs.WebApplicationException)10 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)6 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)6 PlatformInitializationException (org.pentaho.platform.engine.core.system.boot.PlatformInitializationException)6 PlatformImportException (org.pentaho.platform.plugin.services.importer.PlatformImportException)6 DataAccessException (org.springframework.dao.DataAccessException)6 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)6 Domain (org.pentaho.metadata.model.Domain)5 IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)5 FileInputStream (java.io.FileInputStream)4 Consumes (javax.ws.rs.Consumes)4 Facet (org.codehaus.enunciate.Facet)4 Matchers.anyString (org.mockito.Matchers.anyString)4 ModelerWorkspace (org.pentaho.agilebi.modeler.ModelerWorkspace)4 FormDataContentDisposition (com.sun.jersey.core.header.FormDataContentDisposition)3