Search in sources :

Example 6 with ImportResult

use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class MicrosoftPhotosImporterTest method testImportItemPermissionDenied.

@Test(expected = PermissionDeniedException.class)
public void testImportItemPermissionDenied() throws Exception {
    List<PhotoAlbum> albums = ImmutableList.of(new PhotoAlbum("id1", "album1.", "This is a fake albumb"));
    PhotosContainerResource data = new PhotosContainerResource(albums, null);
    Call call = mock(Call.class);
    doReturn(call).when(client).newCall(argThat((Request r) -> r.url().toString().equals("https://www.baseurl.com/v1.0/me/drive/special/photos/children")));
    Response response = mock(Response.class);
    ResponseBody body = mock(ResponseBody.class);
    when(body.bytes()).thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").bytes());
    when(body.string()).thenReturn(ResponseBody.create(MediaType.parse("application/json"), "{\"id\": \"id1\"}").string());
    when(response.code()).thenReturn(403);
    when(response.message()).thenReturn("Access Denied");
    when(response.body()).thenReturn(body);
    when(call.execute()).thenReturn(response);
    ImportResult result = importer.importItem(uuid, executor, authData, data);
}
Also used : Response(okhttp3.Response) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) Call(okhttp3.Call) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) Request(okhttp3.Request) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 7 with ImportResult

use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class PortabilityAbstractInMemoryDataCopier method copyIteration.

protected ExportResult<?> copyIteration(UUID jobId, AuthData exportAuthData, AuthData importAuthData, Optional<ExportInformation> exportInformation, String jobIdPrefix, int copyIteration) throws CopyException {
    monitor.debug(() -> jobIdPrefix + "Copy iteration: " + copyIteration);
    RetryStrategyLibrary retryStrategyLibrary = retryStrategyLibraryProvider.get();
    monitor.debug(() -> jobIdPrefix + "Starting export, copy iteration: " + copyIteration, EventCode.COPIER_STARTED_EXPORT);
    CallableExporter callableExporter = new CallableExporter(exporterProvider, jobId, exportAuthData, exportInformation, metricRecorder);
    RetryingCallable<ExportResult> retryingExporter = new RetryingCallable<>(callableExporter, retryStrategyLibrary, Clock.systemUTC(), monitor, JobMetadata.getDataType(), JobMetadata.getExportService());
    ExportResult<?> exportResult;
    boolean exportSuccess = false;
    Stopwatch exportStopwatch = Stopwatch.createStarted();
    try {
        exportResult = retryingExporter.call();
        exportSuccess = exportResult.getType() != ExportResult.ResultType.ERROR;
    } catch (RetryException | RuntimeException e) {
        if (e.getClass() == RetryException.class && CopyExceptionWithFailureReason.class.isAssignableFrom(e.getCause().getClass())) {
            throw (CopyExceptionWithFailureReason) e.getCause();
        }
        throw new CopyException(jobIdPrefix + "Error happened during export", e);
    } finally {
        metricRecorder.exportPageFinished(JobMetadata.getDataType(), JobMetadata.getExportService(), exportSuccess, exportStopwatch.elapsed());
    }
    monitor.debug(() -> jobIdPrefix + "Finished export, copy iteration: " + copyIteration, EventCode.COPIER_FINISHED_EXPORT);
    if (exportResult.getExportedData() != null) {
        monitor.debug(() -> jobIdPrefix + "Starting import, copy iteration: " + copyIteration, EventCode.COPIER_STARTED_IMPORT);
        CallableImporter callableImporter = new CallableImporter(importerProvider, jobId, idempotentImportExecutor, importAuthData, exportResult.getExportedData(), metricRecorder);
        RetryingCallable<ImportResult> retryingImporter = new RetryingCallable<>(callableImporter, retryStrategyLibrary, Clock.systemUTC(), monitor, JobMetadata.getDataType(), JobMetadata.getImportService());
        boolean importSuccess = false;
        Stopwatch importStopwatch = Stopwatch.createStarted();
        try {
            ImportResult importResult = retryingImporter.call();
            importSuccess = importResult.getType() == ImportResult.ResultType.OK;
            if (importSuccess) {
                try {
                    jobStore.addCounts(jobId, importResult.getCounts().orElse(null));
                    jobStore.addBytes(jobId, importResult.getBytes().orElse(null));
                } catch (IOException e) {
                    monitor.debug(() -> jobIdPrefix + "Unable to add counts to job: ", e);
                }
            }
        } catch (RetryException | RuntimeException e) {
            if (e.getClass() == RetryException.class && CopyExceptionWithFailureReason.class.isAssignableFrom(e.getCause().getClass())) {
                throw (CopyExceptionWithFailureReason) e.getCause();
            }
            throw new CopyException(jobIdPrefix + "Error happened during import", e);
        } finally {
            metricRecorder.importPageFinished(JobMetadata.getDataType(), JobMetadata.getImportService(), importSuccess, importStopwatch.elapsed());
        }
        monitor.debug(() -> jobIdPrefix + "Finished import, copy iteration: " + copyIteration, EventCode.COPIER_FINISHED_IMPORT);
    }
    return exportResult;
}
Also used : CopyException(org.datatransferproject.spi.transfer.types.CopyException) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) RetryingCallable(org.datatransferproject.types.transfer.retry.RetryingCallable) CallableImporter(org.datatransferproject.transfer.CallableImporter) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) RetryException(org.datatransferproject.types.transfer.retry.RetryException) CallableExporter(org.datatransferproject.transfer.CallableExporter) RetryStrategyLibrary(org.datatransferproject.types.transfer.retry.RetryStrategyLibrary) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 8 with ImportResult

use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class CallableImporter method call.

@Override
public ImportResult call() throws Exception {
    boolean success = false;
    Stopwatch stopwatch = Stopwatch.createStarted();
    try {
        idempotentImportExecutor.resetRecentErrors();
        ImportResult result = importerProvider.get().importItem(jobId, idempotentImportExecutor, authData, data);
        Collection<ErrorDetail> errors = idempotentImportExecutor.getRecentErrors();
        success = result.getType() == ImportResult.ResultType.OK && errors.isEmpty();
        if (!success) {
            throw new IOException("Problem with importer, forcing a retry, " + "first error: " + (errors.iterator().hasNext() ? errors.iterator().next().exception() : "none"));
        }
        result = result.copyWithCounts(data.getCounts());
        return result;
    } finally {
        metricRecorder.importPageAttemptFinished(JobMetadata.getDataType(), JobMetadata.getImportService(), success, stopwatch.elapsed());
    }
}
Also used : ErrorDetail(org.datatransferproject.types.transfer.errors.ErrorDetail) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException)

Example 9 with ImportResult

use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class FlickrPhotosImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentExecutor, AuthData authData, PhotosContainerResource data) throws Exception, IOException {
    Auth auth;
    try {
        auth = FlickrUtils.getAuth(authData, flickr);
    } catch (FlickrException e) {
        return new ImportResult(e);
    }
    RequestContext.getRequestContext().setAuth(auth);
    Preconditions.checkArgument(data.getAlbums() != null || data.getPhotos() != null, "Error: There is no data to import");
    if (data.getAlbums() != null) {
        storeAlbums(jobId, data.getAlbums());
    }
    if (data.getPhotos() != null) {
        for (PhotoModel photo : data.getPhotos()) {
            try {
                importSinglePhoto(idempotentExecutor, jobId, photo);
            } catch (FlickrException e) {
                if (e.getMessage().contains("Upload limit reached")) {
                    throw new DestinationMemoryFullException("Flickr destination memory reached", e);
                } else if (e.getMessage().contains("Photo already in set")) {
                    // uploaded
                    continue;
                }
                throw new IOException(e);
            }
        }
    }
    return new ImportResult(ImportResult.ResultType.OK);
}
Also used : ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) FlickrException(com.flickr4java.flickr.FlickrException) Auth(com.flickr4java.flickr.auth.Auth) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) DestinationMemoryFullException(org.datatransferproject.spi.transfer.types.DestinationMemoryFullException) IOException(java.io.IOException)

Example 10 with ImportResult

use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class FlickrPhotosImporterTest method importStoresAlbumInJobStore.

@Test
public void importStoresAlbumInJobStore() throws FlickrException, Exception {
    UUID jobId = UUID.randomUUID();
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(Collections.singletonList(PHOTO_ALBUM), Collections.singletonList(PHOTO_MODEL));
    // Setup Mock
    when(user.getId()).thenReturn("userId");
    when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
    when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
    when(flickr.getUploader()).thenReturn(uploader);
    when(flickr.getAuthInterface()).thenReturn(authInterface);
    when(imageStreamProvider.get(FETCHABLE_URL)).thenReturn(bufferedInputStream);
    when(uploader.upload(any(BufferedInputStream.class), any(UploadMetaData.class))).thenReturn(FLICKR_PHOTO_ID);
    String flickrAlbumTitle = ALBUM_NAME;
    Photoset photoset = FlickrTestUtils.initializePhotoset(FLICKR_ALBUM_ID, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
    when(photosetsInterface.create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID)).thenReturn(photoset);
    // Run test
    FlickrPhotosImporter importer = new FlickrPhotosImporter(flickr, jobStore, imageStreamProvider, monitor, TransferServiceConfig.getDefaultInstance());
    ImportResult result = importer.importItem(jobId, EXECUTOR, new TokenSecretAuthData("token", "secret"), photosContainerResource);
    // Verify that the image stream provider got the correct URL and that the correct info was
    // uploaded
    verify(imageStreamProvider).get(FETCHABLE_URL);
    ArgumentCaptor<UploadMetaData> uploadMetaDataArgumentCaptor = ArgumentCaptor.forClass(UploadMetaData.class);
    verify(uploader).upload(eq(bufferedInputStream), uploadMetaDataArgumentCaptor.capture());
    UploadMetaData actualUploadMetaData = uploadMetaDataArgumentCaptor.getValue();
    assertThat(actualUploadMetaData.getTitle()).isEqualTo(PHOTO_TITLE);
    assertThat(actualUploadMetaData.getDescription()).isEqualTo(PHOTO_DESCRIPTION);
    // Verify the photosets interface got the command to create the correct album
    verify(photosetsInterface).create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
    assertThat((String) EXECUTOR.getCachedValue(ALBUM_ID)).isEqualTo(FLICKR_ALBUM_ID);
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) TokenSecretAuthData(org.datatransferproject.types.transfer.auth.TokenSecretAuthData) BufferedInputStream(java.io.BufferedInputStream) Photoset(com.flickr4java.flickr.photosets.Photoset) Token(org.scribe.model.Token) UploadMetaData(com.flickr4java.flickr.uploader.UploadMetaData) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)27 Test (org.junit.Test)14 IOException (java.io.IOException)11 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)8 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)6 UUID (java.util.UUID)5 Map (java.util.Map)4 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 List (java.util.List)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 ResponseBody (okhttp3.ResponseBody)3 AppCredentials (org.datatransferproject.types.transfer.auth.AppCredentials)3 Stopwatch (com.google.common.base.Stopwatch)2 LinkedHashMap (java.util.LinkedHashMap)2 Call (okhttp3.Call)2 Importer (org.datatransferproject.spi.transfer.provider.Importer)2 ContactsModelWrapper (org.datatransferproject.types.common.models.contacts.ContactsModelWrapper)2 TaskListModel (org.datatransferproject.types.common.models.tasks.TaskListModel)2