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);
}
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();
}
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());
}
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);
}
}
}
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);
}
Aggregations