Search in sources :

Example 1 with RepositoryFileImportBundle

use of org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle 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 2 with RepositoryFileImportBundle

use of org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle in project data-access by pentaho.

the class MetadataServiceTest method fillServiceMock.

private void fillServiceMock(String domainId, InputStream metadataFile) throws Exception {
    FileResource mockFileResource = mock(FileResource.class);
    Response mockResponse = mock(Response.class);
    IPentahoSession mockIPentahoSession = mock(IPentahoSession.class);
    IPlatformImporter mockIPlatformImporter = mock(IPlatformImporter.class);
    IPlatformImportBundle mockIPlatformImportBundle = mock(IPlatformImportBundle.class);
    RepositoryFileImportBundle.Builder mockRepositoryFileImportBundleBuilder = mock(RepositoryFileImportBundle.Builder.class);
    RepositoryFileImportBundle mockRepositoryFileImportBundle = mock(RepositoryFileImportBundle.class);
    ByteArrayInputStream mockByteArrayInputStream = mock(ByteArrayInputStream.class);
    doNothing().when(metadataService).accessValidation();
    doReturn(mockFileResource).when(metadataService).createNewFileResource();
    doReturn(mockResponse).when(mockFileResource).doGetReservedChars();
    doReturn(null).when(mockResponse).getEntity();
    doReturn("\t\n/").when(metadataService).objectToString(null);
    doReturn(mockRepositoryFileImportBundleBuilder).when(metadataService).createNewRepositoryFileImportBundleBuilder(metadataFile, false, domainId, null);
    doReturn(UPLOAD_DIR).when(metadataService).internalGetUploadDir();
    doReturn(mockByteArrayInputStream).when(metadataService).createNewByteArrayInputStream(any(byte[].class));
    // doReturn( mockRepositoryFileImportBundle ).when( metadataService ).createNewRepositoryFileImportBundle(
    // mockByteArrayInputStream, "fileName", domainId );
    doReturn(mockRepositoryFileImportBundle).when(mockRepositoryFileImportBundleBuilder).build();
    doReturn(mockIPlatformImporter).when(metadataService).getImporter();
    doNothing().when(mockIPlatformImporter).importFile(mockIPlatformImportBundle);
    doReturn(mockIPentahoSession).when(metadataService).getSession();
    doNothing().when(metadataService).publish(mockIPentahoSession);
}
Also used : Response(javax.ws.rs.core.Response) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) ByteArrayInputStream(java.io.ByteArrayInputStream) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) FileResource(org.pentaho.platform.web.http.api.resources.FileResource) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter)

Example 3 with RepositoryFileImportBundle

use of org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle in project data-access by pentaho.

the class DatasourceResourceIT method testMetadataImportExport.

@Test
public void testMetadataImportExport() throws PlatformInitializationException, IOException, PlatformImportException {
    List<IMimeType> mimeTypeList = new ArrayList<IMimeType>();
    mimeTypeList.add(new MimeType("Metadata", ".xmi"));
    File metadata = new File("target/test-classes/dsw/testData/metadata.xmi");
    RepositoryFile repoMetadataFile = new RepositoryFile.Builder(metadata.getName()).folder(false).hidden(false).build();
    MetadataImportHandler metadataImportHandler = new MetadataImportHandler(mimeTypeList, (IPentahoMetadataDomainRepositoryImporter) PentahoSystem.get(IMetadataDomainRepository.class));
    RepositoryFileImportBundle bundle1 = new RepositoryFileImportBundle.Builder().file(repoMetadataFile).charSet("UTF-8").input(new FileInputStream(metadata)).mime(".xmi").withParam("domain-id", "SalesData").build();
    metadataImportHandler.importFile(bundle1);
    final Response salesData = new DataSourceWizardResource().doGetDSWFilesAsDownload("SalesData");
    Assert.assertEquals(salesData.getStatus(), Response.Status.OK.getStatusCode());
    Assert.assertNotNull(salesData.getMetadata());
    Assert.assertNotNull(salesData.getMetadata().getFirst("Content-Disposition"));
    Assert.assertEquals(salesData.getMetadata().getFirst("Content-Disposition").getClass(), String.class);
    Assert.assertTrue(((String) salesData.getMetadata().getFirst("Content-Disposition")).endsWith(".xmi\""));
}
Also used : MetadataImportHandler(org.pentaho.platform.plugin.services.importer.MetadataImportHandler) ArrayList(java.util.ArrayList) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) MimeType(org.pentaho.platform.core.mimetype.MimeType) FileInputStream(java.io.FileInputStream) Response(javax.ws.rs.core.Response) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) DataSourceWizardResource(org.pentaho.platform.dataaccess.datasource.api.resources.DataSourceWizardResource) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) ZipFile(java.util.zip.ZipFile) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) Test(org.junit.Test)

Example 4 with RepositoryFileImportBundle

use of org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle in project data-access by pentaho.

the class DatasourceResourceIT method testMondrianImportExport.

@Test
public void testMondrianImportExport() throws Exception {
    final String domainName = "SalesData";
    List<IMimeType> mimeTypeList = new ArrayList<IMimeType>();
    mimeTypeList.add(new MimeType("Mondrian", "mondrian.xml"));
    // $NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("org.osjava.sj.root", "target/test-classes/solution1/system/simple-jndi");
    File mondrian = new File("target/test-classes/dsw/testData/SalesData.mondrian.xml");
    RepositoryFile repoMondrianFile = new RepositoryFile.Builder(mondrian.getName()).folder(false).hidden(false).build();
    RepositoryFileImportBundle bundle1 = new RepositoryFileImportBundle.Builder().file(repoMondrianFile).charSet("UTF-8").input(new FileInputStream(mondrian)).mime("mondrian.xml").withParam("parameters", "Datasource=Pentaho;overwrite=true").withParam("domain-id", "SalesData").build();
    MondrianImportHandler mondrianImportHandler = new MondrianImportHandler(mimeTypeList, PentahoSystem.get(IMondrianCatalogService.class));
    mondrianImportHandler.importFile(bundle1);
    try {
        KettleEnvironment.init();
        Props.init(Props.TYPE_PROPERTIES_EMPTY);
    } catch (Exception e) {
    // may already be initialized by another test
    }
    Domain domain = generateModel();
    ModelerWorkspace model = new ModelerWorkspace(new GwtModelerWorkspaceHelper());
    model.setModelName("ORDERS");
    model.setDomain(domain);
    model.getWorkspaceHelper().populateDomain(model);
    new ModelerService().serializeModels(domain, domainName);
    final Response salesData = new DataSourceWizardResource().doGetDSWFilesAsDownload(domainName + ".xmi");
    Assert.assertEquals(salesData.getStatus(), Response.Status.OK.getStatusCode());
    Assert.assertNotNull(salesData.getMetadata());
    Assert.assertNotNull(salesData.getMetadata().getFirst("Content-Disposition"));
    Assert.assertEquals(salesData.getMetadata().getFirst("Content-Disposition").getClass(), String.class);
    Assert.assertTrue(((String) salesData.getMetadata().getFirst("Content-Disposition")).endsWith(domainName + ".zip\""));
    File file = File.createTempFile(domainName, ".zip");
    final FileOutputStream fileOutputStream = new FileOutputStream(file);
    ((StreamingOutput) salesData.getEntity()).write(fileOutputStream);
    fileOutputStream.close();
    final ZipFile zipFile = new ZipFile(file);
    final Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry zipEntry = entries.nextElement();
        Assert.assertTrue(zipEntry.getName().equals(domainName + ".xmi") || zipEntry.getName().equals(domainName + ".mondrian.xml"));
    }
    zipFile.close();
    file.delete();
}
Also used : ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) StreamingOutput(javax.ws.rs.core.StreamingOutput) Matchers.anyString(org.mockito.Matchers.anyString) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) MimeType(org.pentaho.platform.core.mimetype.MimeType) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) MondrianImportHandler(org.pentaho.platform.plugin.services.importer.MondrianImportHandler) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) FileInputStream(java.io.FileInputStream) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) PlatformInitializationException(org.pentaho.platform.engine.core.system.boot.PlatformInitializationException) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) DataAccessException(org.springframework.dao.DataAccessException) DuplicateDatasourceException(org.pentaho.platform.api.repository.datasource.DuplicateDatasourceException) IOException(java.io.IOException) NonExistingDatasourceException(org.pentaho.platform.api.repository.datasource.NonExistingDatasourceException) GwtModelerWorkspaceHelper(org.pentaho.agilebi.modeler.gwt.GwtModelerWorkspaceHelper) Response(javax.ws.rs.core.Response) DataSourceWizardResource(org.pentaho.platform.dataaccess.datasource.api.resources.DataSourceWizardResource) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) Domain(org.pentaho.metadata.model.Domain) ZipFile(java.util.zip.ZipFile) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) ModelerWorkspace(org.pentaho.agilebi.modeler.ModelerWorkspace) Test(org.junit.Test)

Example 5 with RepositoryFileImportBundle

use of org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle in project data-access by pentaho.

the class MetadataServiceTest method testImportMetadataDatasource.

@Test
public void testImportMetadataDatasource() throws Exception {
    String domainId = DOMAIN_ID;
    InputStream metadataFile = mock(InputStream.class);
    FormDataContentDisposition metadataFileInfo = mock(FormDataContentDisposition.class);
    boolean overwrite = true;
    FormDataBodyPart mockFormDataBodyPart = mock(FormDataBodyPart.class);
    List<FormDataBodyPart> localeFiles = new ArrayList<FormDataBodyPart>();
    localeFiles.add(mockFormDataBodyPart);
    List<FormDataContentDisposition> localeFilesInfo = new ArrayList<FormDataContentDisposition>();
    FormDataContentDisposition mockFormDataContentDisposition = mock(FormDataContentDisposition.class);
    localeFilesInfo.add(mockFormDataContentDisposition);
    FileResource mockFileResource = mock(FileResource.class);
    Response mockResponse = mock(Response.class);
    IPentahoSession mockIPentahoSession = mock(IPentahoSession.class);
    IPlatformImporter mockIPlatformImporter = mock(IPlatformImporter.class);
    IPlatformImportBundle mockIPlatformImportBundle = mock(IPlatformImportBundle.class);
    RepositoryFileImportBundle.Builder mockRepositoryFileImportBundleBuilder = mock(RepositoryFileImportBundle.Builder.class);
    RepositoryFileImportBundle mockRepositoryFileImportBundle = mock(RepositoryFileImportBundle.class);
    ByteArrayInputStream mockByteArrayInputStream = mock(ByteArrayInputStream.class);
    doNothing().when(metadataService).accessValidation();
    doReturn(metadataFile).when(metadataService).validateFileSize(any(InputStream.class), anyString());
    doReturn(mockFileResource).when(metadataService).createNewFileResource();
    doReturn(mockResponse).when(mockFileResource).doGetReservedChars();
    doReturn(null).when(mockResponse).getEntity();
    doReturn("\t\n/").when(metadataService).objectToString(null);
    doReturn(mockRepositoryFileImportBundleBuilder).when(metadataService).createNewRepositoryFileImportBundleBuilder(metadataFile, false, domainId, null);
    doReturn("fileName").when(mockFormDataContentDisposition).getFileName();
    doReturn(mockByteArrayInputStream).when(metadataService).createNewByteArrayInputStream(any(byte[].class));
    // doReturn( mockRepositoryFileImportBundle ).when( metadataService ).createNewRepositoryFileImportBundle(
    // mockByteArrayInputStream, "fileName", domainId );
    doReturn(mockRepositoryFileImportBundle).when(mockRepositoryFileImportBundleBuilder).build();
    doReturn(mockIPlatformImporter).when(metadataService).getImporter();
    doNothing().when(mockIPlatformImporter).importFile(mockIPlatformImportBundle);
    doReturn(mockIPentahoSession).when(metadataService).getSession();
    doNothing().when(metadataService).publish(mockIPentahoSession);
    metadataService.importMetadataDatasource(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
    verify(metadataService, times(1)).importMetadataDatasource(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StringInputStream(org.apache.tools.ant.filters.StringInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ArrayList(java.util.ArrayList) FileResource(org.pentaho.platform.web.http.api.resources.FileResource) Matchers.anyString(org.mockito.Matchers.anyString) Response(javax.ws.rs.core.Response) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) ByteArrayInputStream(java.io.ByteArrayInputStream) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter) Test(org.junit.Test)

Aggregations

RepositoryFileImportBundle (org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle)8 Test (org.junit.Test)7 Response (javax.ws.rs.core.Response)6 FileInputStream (java.io.FileInputStream)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Matchers.anyString (org.mockito.Matchers.anyString)4 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)4 IPlatformImportBundle (org.pentaho.platform.api.repository2.unified.IPlatformImportBundle)4 IPlatformImporter (org.pentaho.platform.plugin.services.importer.IPlatformImporter)4 FileResource (org.pentaho.platform.web.http.api.resources.FileResource)4 StringInputStream (org.apache.tools.ant.filters.StringInputStream)3 FormDataContentDisposition (com.sun.jersey.core.header.FormDataContentDisposition)2 FormDataBodyPart (com.sun.jersey.multipart.FormDataBodyPart)2 File (java.io.File)2 ZipFile (java.util.zip.ZipFile)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)2 IMimeType (org.pentaho.platform.api.mimetype.IMimeType)2