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