Search in sources :

Example 1 with ImportSession

use of org.pentaho.platform.plugin.services.importexport.ImportSession in project pentaho-platform by pentaho.

the class RepositoryImportResourceTest method setUp.

@Before
public void setUp() throws ObjectFactoryException, PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException, IOException {
    PentahoSystem.init();
    IPentahoSession session = mock(IPentahoSession.class);
    doReturn("sampleSession").when(session).getName();
    PentahoSessionHolder.setSession(session);
    handler = mock(SolutionImportHandler.class);
    importer = mock(PentahoPlatformImporter.class);
    policy = mock(IAuthorizationPolicy.class);
    ITenant tenat = mock(ITenant.class);
    resolver = mock(ITenantedPrincipleNameResolver.class);
    doReturn(tenat).when(resolver).getTenant(anyString());
    doReturn(REAL_USER).when(resolver).getPrincipleName(anyString());
    policy = mock(IAuthorizationPolicy.class);
    pentahoObjectFactory = mock(IPentahoObjectFactory.class);
    iPlatformMimeResolver = mock(NameBaseMimeResolver.class);
    iRepositoryImportLogger = mock(IRepositoryImportLogger.class);
    catalogService = mock(MondrianCatalogHelper.class);
    doReturn("xml").when(iPlatformMimeResolver).resolveMimeForFileName("");
    doReturn(iRepositoryImportLogger).when(importer).getRepositoryImportLogger();
    // for calling importFile in RepositoryImportResource
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            handler.importFile(any(IPlatformImportBundle.class));
            return null;
        }
    }).when(importer).importFile(any(IPlatformImportBundle.class));
    // for calling importFile in PentahoPlatformImporter
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            handler.getImportSession();
            return null;
        }
    }).when(handler).importFile(any(IPlatformImportBundle.class));
    // for calling getImportSession in SolutionImportHandler
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ImportSession importsession = ImportSession.getSession();
            importsession.setManifest(mock(ExportManifest.class));
            return null;
        }
    }).when(handler).getImportSession();
    when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
    when(pentahoObjectFactory.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getArguments()[0].equals(IAuthorizationPolicy.class)) {
                return policy;
            }
            if (invocation.getArguments()[0].equals(ITenantedPrincipleNameResolver.class)) {
                return resolver;
            }
            if (invocation.getArguments()[0].equals(IMondrianCatalogService.class)) {
                return catalogService;
            }
            return null;
        }
    });
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);
    PentahoSystem.registerObject(iPlatformMimeResolver);
    PentahoSystem.registerObject(iRepositoryImportLogger);
    PentahoSystem.registerObject(catalogService);
    PentahoSystem.registerObject(handler);
    PentahoSystem.registerObject(importer);
}
Also used : ImportSession(org.pentaho.platform.plugin.services.importexport.ImportSession) PentahoPlatformImporter(org.pentaho.platform.plugin.services.importer.PentahoPlatformImporter) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) NameBaseMimeResolver(org.pentaho.platform.plugin.services.importer.NameBaseMimeResolver) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.IRepositoryImportLogger) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) MondrianCatalogHelper(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogHelper) ITenant(org.pentaho.platform.api.mt.ITenant) SolutionImportHandler(org.pentaho.platform.plugin.services.importer.SolutionImportHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ITenantedPrincipleNameResolver(org.pentaho.platform.api.mt.ITenantedPrincipleNameResolver) Before(org.junit.Before)

Example 2 with ImportSession

use of org.pentaho.platform.plugin.services.importexport.ImportSession in project pentaho-platform by pentaho.

the class ArchiveLoaderTest method testLoadAllClearSession.

@Test
public void testLoadAllClearSession() throws Exception {
    PowerMockito.mockStatic(ImportSession.class);
    ImportSession importSession = mock(ImportSession.class);
    when(ImportSession.getSession()).thenReturn(importSession);
    IPlatformImporter importer = mock(IPlatformImporter.class);
    ArchiveLoader loader = new ArchiveLoader(importer);
    File directoryMock = mock(File.class);
    File file1Mock = mock(File.class);
    File file2Mock = mock(File.class);
    when(directoryMock.listFiles(ZIPS_FILTER)).thenReturn(new File[] { file1Mock, file2Mock });
    loader.loadAll(directoryMock, ArchiveLoader.ZIPS_FILTER);
    PowerMockito.verifyStatic(VerificationModeFactory.times(2));
    ImportSession.clearSession();
}
Also used : ImportSession(org.pentaho.platform.plugin.services.importexport.ImportSession) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with ImportSession

use of org.pentaho.platform.plugin.services.importexport.ImportSession in project pentaho-platform by pentaho.

the class FileServiceTest method testSystemRestore.

@Test
public void testSystemRestore() throws Exception {
    InputStream inputStreamMock = mock(InputStream.class);
    IAuthorizationPolicy authorizationPolicy = mock(IAuthorizationPolicy.class);
    IRepositoryImportLogger iRepositoryImportLogger = mock(IRepositoryImportLogger.class);
    doReturn(authorizationPolicy).when(fileService).getPolicy();
    doReturn(true).when(authorizationPolicy).isAllowed(RepositoryReadAction.NAME);
    doReturn(true).when(authorizationPolicy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(true).when(authorizationPolicy).isAllowed(AdministerSecurityAction.NAME);
    doReturn(iRepositoryImportLogger).when(platformImporter).getRepositoryImportLogger();
    fileService.systemRestore(inputStreamMock, "true", "false", "true");
    verify(fileService).doCanAdminister();
    verify(iRepositoryImportLogger).startJob(any(), anyString(), any());
    verify(iRepositoryImportLogger).endJob();
    ArgumentCaptor<RepositoryFileImportBundle> argumentCaptor = ArgumentCaptor.forClass(RepositoryFileImportBundle.class);
    verify(platformImporter).importFile(argumentCaptor.capture());
    RepositoryFileImportBundle bundle = argumentCaptor.getValue();
    assertTrue(bundle.getInputStream() == inputStreamMock);
    assertEquals("UTF-8", bundle.getCharSet());
    assertEquals(RepositoryFile.HIDDEN_BY_DEFAULT, bundle.isHidden());
    assertEquals(RepositoryFile.SCHEDULABLE_BY_DEFAULT, bundle.isSchedulable());
    assertEquals("/", bundle.getPath());
    assertEquals(true, bundle.overwriteInRepository());
    assertEquals("SystemBackup.zip", bundle.getName());
    assertFalse(bundle.isApplyAclSettings());
    assertTrue(bundle.isRetainOwnership());
    assertTrue(bundle.isOverwriteAclSettings());
    assertTrue(bundle.isPreserveDsw());
    ImportSession session = ImportSession.getSession();
    assertFalse(session.isApplyAclSettings());
    assertTrue(session.isRetainOwnership());
    assertTrue(session.isOverwriteAclSettings());
}
Also used : ImportSession(org.pentaho.platform.plugin.services.importexport.ImportSession) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) IRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.IRepositoryImportLogger) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) Test(org.junit.Test)

Example 4 with ImportSession

use of org.pentaho.platform.plugin.services.importexport.ImportSession in project pentaho-platform by pentaho.

the class RepositoryFileImportFileHandler method importFile.

@Override
public void importFile(IPlatformImportBundle bnd) throws PlatformImportException {
    if (bnd instanceof RepositoryFileImportBundle == false) {
        throw new PlatformImportException("Error importing bundle. RepositoryFileImportBundle expected");
    }
    RepositoryFileImportBundle bundle = (RepositoryFileImportBundle) bnd;
    if (bundle.isSchedulable() == null) {
        bundle.setSchedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT);
    }
    String repositoryFilePath = RepositoryFilenameUtils.concat(bundle.getPath(), bundle.getName());
    getLogger().trace("Processing [" + repositoryFilePath + "]");
    // Verify if destination already exists in the repository.
    RepositoryFile file = repository.getFile(repositoryFilePath);
    if (file != null) {
        if (file.isFolder() && getImportSession().getFoldersCreatedImplicitly().contains(repositoryFilePath)) {
            getLogger().trace("Skipping entry for folder [" + repositoryFilePath + "]. It was already processed implicitly.");
        } else {
            if (bundle.overwriteInRepossitory()) {
                // If file exists, overwrite is true and is not a folder then update it.
                if (!file.isFolder()) {
                    file = finalAdjustFile(bundle, file);
                    copyFileToRepository(bundle, repositoryFilePath, file);
                } else {
                    // The folder exists. Possible ACL changes.
                    getLogger().trace("Existing folder [" + repositoryFilePath + "]");
                    file = finalAdjustFolder(bundle, file.getId());
                    repository.updateFolder(file, null);
                    if (bundle.getAcl() != null) {
                        updateAclFromBundle(false, bundle, file);
                    }
                }
            } else {
                if (getImportSession().getIsNotRunningImport()) {
                    throw new PlatformImportException(messages.getString("DefaultImportHandler.ERROR_0009_OVERWRITE_CONTENT", repositoryFilePath), PlatformImportException.PUBLISH_CONTENT_EXISTS_ERROR);
                } else {
                    getLogger().trace("Not importing existing file [" + repositoryFilePath + "]");
                    ImportSession importSession = ImportSession.getSession();
                    importSession.getSkippedFiles().add(repositoryFilePath);
                }
            }
        }
    } else {
        if (bundle.isFolder()) {
            // The file doesn't exist and it is a folder. Create folder.
            getLogger().trace("Creating folder [" + repositoryFilePath + "]");
            final Serializable parentId = getParentId(repositoryFilePath);
            bundle.setFile(bundle.getFile());
            RepositoryFile repoFile = finalAdjustFolder(bundle, null);
            if (bundle.getAcl() != null) {
                repoFile = repository.createFolder(parentId, repoFile, bundle.getAcl(), null);
                updateAclFromBundle(true, bundle, repoFile);
            } else {
                repository.createFolder(parentId, repoFile, null);
            }
        } else {
            // The file doesn't exist. Create file.
            getLogger().trace("Creating file [" + repositoryFilePath + "]");
            copyFileToRepository(bundle, repositoryFilePath, null);
        }
    }
}
Also used : ImportSession(org.pentaho.platform.plugin.services.importexport.ImportSession) Serializable(java.io.Serializable) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 5 with ImportSession

use of org.pentaho.platform.plugin.services.importexport.ImportSession in project pentaho-platform by pentaho.

the class PRPTImportHandlerTest method setUp.

@Before
public void setUp() throws ObjectFactoryException, IOException {
    final IPlatformMimeResolver resolver = mock(IPlatformMimeResolver.class);
    pentahoObjectFactory = mock(IPentahoObjectFactory.class);
    when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
    when(pentahoObjectFactory.get(eq(IPlatformMimeResolver.class), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return resolver;
        }
    });
    when(pentahoObjectFactory.get(eq(IPlatformImporter.class), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return importer;
        }
    });
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);
    InputStream stream = new ByteArrayInputStream(SAMPLE_STREAM.getBytes());
    bundle = mock(RepositoryFileImportBundle.class);
    when(bundle.getName()).thenReturn(SAMPLE_NAME);
    when(bundle.getInputStream()).thenReturn(stream);
    Log logger = mock(Log.class);
    ImportSession session = mock(ImportSession.class);
    doReturn(logger).when(session).getLogger();
    IUnifiedRepository repository = mock(IUnifiedRepository.class);
    List<IMimeType> mimeTypes = Arrays.asList(mock(IMimeType.class));
    handler = spy(new PRPTImportHandler(mimeTypes));
    doReturn(session).when(handler).getImportSession();
    handler.setRepository(repository);
}
Also used : ImportSession(org.pentaho.platform.plugin.services.importexport.ImportSession) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) Log(org.apache.commons.logging.Log) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) IPlatformMimeResolver(org.pentaho.platform.api.mimetype.IPlatformMimeResolver) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) Before(org.junit.Before)

Aggregations

ImportSession (org.pentaho.platform.plugin.services.importexport.ImportSession)5 InputStream (java.io.InputStream)2 Before (org.junit.Before)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)2 IPentahoObjectFactory (org.pentaho.platform.api.engine.IPentahoObjectFactory)2 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)2 IRepositoryImportLogger (org.pentaho.platform.plugin.services.importexport.IRepositoryImportLogger)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Serializable (java.io.Serializable)1 Log (org.apache.commons.logging.Log)1 IMimeType (org.pentaho.platform.api.mimetype.IMimeType)1 IPlatformMimeResolver (org.pentaho.platform.api.mimetype.IPlatformMimeResolver)1 ITenant (org.pentaho.platform.api.mt.ITenant)1 ITenantedPrincipleNameResolver (org.pentaho.platform.api.mt.ITenantedPrincipleNameResolver)1 IPlatformImportBundle (org.pentaho.platform.api.repository2.unified.IPlatformImportBundle)1 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)1 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)1