Search in sources :

Example 1 with FileResource

use of org.pentaho.platform.web.http.api.resources.FileResource 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 2 with FileResource

use of org.pentaho.platform.web.http.api.resources.FileResource in project data-access by pentaho.

the class MetadataServiceTest method testImportMetadataDatasourceError.

@Test
public void testImportMetadataDatasourceError() 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);
    doNothing().when(metadataService).accessValidation();
    when(metadataFile.read(any())).thenReturn(-1);
    doReturn(mockFileResource).when(metadataService).createNewFileResource();
    doReturn(mockResponse).when(mockFileResource).doGetReservedChars();
    doReturn(null).when(mockResponse).getEntity();
    doReturn("\t\n/").when(metadataService).objectToString(null);
    doReturn("").when(metadataService).prohibitedSymbolMessage(domainId, mockFileResource);
    try {
        metadataService.importMetadataDatasource(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
        fail();
    } catch (PlatformImportException e) {
    // expected
    } catch (IllegalStateException e) {
    // expected
    }
    verify(metadataService, times(1)).importMetadataDatasource(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
}
Also used : Response(javax.ws.rs.core.Response) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) ByteArrayInputStream(java.io.ByteArrayInputStream) StringInputStream(org.apache.tools.ant.filters.StringInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) ArrayList(java.util.ArrayList) FileResource(org.pentaho.platform.web.http.api.resources.FileResource) Matchers.anyString(org.mockito.Matchers.anyString) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) Test(org.junit.Test)

Example 3 with FileResource

use of org.pentaho.platform.web.http.api.resources.FileResource in project data-access by pentaho.

the class MetadataService method importMetadataDatasource.

public void importMetadataDatasource(String domainId, InputStream metadataFile, boolean overwrite, List<InputStream> localeFileStreams, List<String> localeFileNames, RepositoryFileAclDto acl) throws PentahoAccessControlException, PlatformImportException, Exception {
    if (StringUtils.isEmpty(domainId)) {
        throw new PlatformImportException(Messages.getString("MetadataDatasourceService.ERROR_005_DOMAIN_NAME_EMPTY"));
    }
    accessValidation();
    FileResource fr = createNewFileResource();
    Object reservedCharsObject = fr.doGetReservedChars().getEntity();
    String reservedChars = objectToString(reservedCharsObject);
    if (reservedChars != null && domainId.matches(".*[" + reservedChars.replaceAll("/", "") + "]+.*")) {
        String msg = prohibitedSymbolMessage(domainId, fr);
        throw new PlatformImportException(msg, PlatformImportException.PUBLISH_PROHIBITED_SYMBOLS_ERROR);
    }
    metadataFile = validateFileSize(metadataFile, domainId);
    // domain ID comes with ".xmi" suffix when creating or editing domain
    // (see ModelerService.serializeModels( Domain, String, boolean ) ),
    // but when the user enters domain ID manually when importing metadata file,
    // it will unlikely contain that suffix, so let's add it forcibly.
    domainId = forceXmiSuffix(domainId);
    RepositoryFileImportBundle.Builder bundleBuilder = createNewRepositoryFileImportBundleBuilder(metadataFile, overwrite, domainId, acl);
    if (localeFileStreams != null) {
        for (int i = 0; i < localeFileStreams.size(); i++) {
            IPlatformImportBundle localizationBundle = createNewRepositoryFileImportBundle(localeFileStreams.get(i), localeFileNames.get(i), domainId);
            bundleBuilder.addChildBundle(localizationBundle);
        }
    }
    IPlatformImportBundle bundle = bundleBuilder.build();
    IPlatformImporter importer = getImporter();
    importer.importFile(bundle);
    IPentahoSession pentahoSession = getSession();
    publish(pentahoSession);
}
Also used : IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) 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 4 with FileResource

use of org.pentaho.platform.web.http.api.resources.FileResource in project data-access by pentaho.

the class MetadataResource method catchPlatformImportException.

private Response catchPlatformImportException(String domainId, PlatformImportException e) {
    if (e.getErrorStatus() == PlatformImportException.PUBLISH_PROHIBITED_SYMBOLS_ERROR) {
        FileResource fr = createFileResource();
        return buildServerError003Response(domainId, fr);
    } else {
        String msg = e.getMessage();
        logger.error("Error import metadata: " + msg + " status = " + e.getErrorStatus());
        Throwable throwable = e.getCause();
        if (throwable != null) {
            msg = throwable.getMessage();
            logger.error("Root cause: " + msg);
        }
        return buildOkResponse(String.valueOf(e.getErrorStatus()));
    }
}
Also used : FileResource(org.pentaho.platform.web.http.api.resources.FileResource)

Example 5 with FileResource

use of org.pentaho.platform.web.http.api.resources.FileResource in project data-access by pentaho.

the class MetadataResourceTest method testImportMetadataDatasourceError.

@Test
public void testImportMetadataDatasourceError() throws Exception {
    Response mockResponse = mock(Response.class);
    FileResource mockFileResource = mock(FileResource.class);
    String domainId = "domainId";
    InputStream metadataFile = mock(InputStream.class);
    FormDataContentDisposition metadataFileInfo = mock(FormDataContentDisposition.class);
    String overwrite = "overwrite";
    List<FormDataBodyPart> localeFiles = mock(List.class);
    List<FormDataContentDisposition> localeFilesInfo = mock(List.class);
    // Test 1
    PentahoAccessControlException mockPentahoAccessControlException = mock(PentahoAccessControlException.class);
    doThrow(mockPentahoAccessControlException).when(metadataResource.service).importMetadataDatasource(domainId, metadataFile, metadataFileInfo, false, localeFiles, localeFilesInfo, null);
    doReturn(mockResponse).when(metadataResource).buildServerErrorResponse(mockPentahoAccessControlException);
    Response response = metadataResource.importMetadataDatasourceLegacy(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
    assertEquals(mockResponse, response);
    // Test 2
    PlatformImportException mockPlatformImportException = mock(PlatformImportException.class);
    doThrow(mockPlatformImportException).when(metadataResource.service).importMetadataDatasource(domainId, metadataFile, metadataFileInfo, true, localeFiles, localeFilesInfo, null);
    doReturn(10).when(mockPlatformImportException).getErrorStatus();
    doReturn(mockFileResource).when(metadataResource).createFileResource();
    doReturn(mockResponse).when(metadataResource).buildServerError003Response(domainId, mockFileResource);
    response = metadataResource.importMetadataDatasourceLegacy(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
    assertEquals(mockResponse, response);
    // Test 3
    RuntimeException mockException = mock(RuntimeException.class);
    doThrow(mockPlatformImportException).when(metadataResource.service).importMetadataDatasource(domainId, metadataFile, metadataFileInfo, true, localeFiles, localeFilesInfo, null);
    doReturn(1).when(mockPlatformImportException).getErrorStatus();
    doReturn(mockResponse).when(metadataResource).buildOkResponse("1");
    doReturn(mockException).when(mockPlatformImportException).getCause();
    response = metadataResource.importMetadataDatasourceLegacy(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
    assertEquals(mockResponse, response);
    // Test
    doThrow(mockException).when(metadataResource.service).importMetadataDatasource(domainId, metadataFile, metadataFileInfo, true, localeFiles, localeFilesInfo, null);
    doReturn(mockResponse).when(metadataResource).buildServerError001Response();
    response = metadataResource.importMetadataDatasourceLegacy(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
    assertEquals(mockResponse, response);
    verify(metadataResource, times(4)).importMetadataDatasourceLegacy(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
}
Also used : Response(javax.ws.rs.core.Response) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) InputStream(java.io.InputStream) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) FileResource(org.pentaho.platform.web.http.api.resources.FileResource) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) Test(org.junit.Test)

Aggregations

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