Search in sources :

Example 6 with ContainerResource

use of org.datatransferproject.types.common.models.ContainerResource in project data-transfer-project by google.

the class MicrosoftPhotosExporterTest method exportAlbumWithoutNextPage.

@Test
public void exportAlbumWithoutNextPage() throws IOException {
    // Setup
    MicrosoftDriveItem folderItem = setUpSingleAlbum();
    when(driveItemsResponse.getDriveItems()).thenReturn(new MicrosoftDriveItem[] { folderItem });
    when(driveItemsResponse.getNextPageLink()).thenReturn(null);
    StringPaginationToken inputPaginationToken = new StringPaginationToken(DRIVE_TOKEN_PREFIX + DRIVE_PAGE_URL);
    // Run
    ExportResult<PhotosContainerResource> result = microsoftPhotosExporter.exportOneDrivePhotos(null, Optional.empty(), Optional.of(inputPaginationToken), uuid);
    // Verify method calls
    verify(photosInterface).getDriveItems(Optional.empty(), Optional.of(DRIVE_PAGE_URL));
    verify(driveItemsResponse).getDriveItems();
    // Verify next pagination token is absent
    ContinuationData continuationData = result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken).isEqualTo(null);
    // Verify one album is ready for import
    Collection<PhotoAlbum> actualAlbums = result.getExportedData().getAlbums();
    assertThat(actualAlbums.stream().map(PhotoAlbum::getId).collect(Collectors.toList())).containsExactly(FOLDER_ID);
    // Verify photos should be empty (in the root)
    Collection<PhotoModel> actualPhotos = result.getExportedData().getPhotos();
    assertThat(actualPhotos).isEmpty();
    // Verify there is one container ready for sub-processing
    List<ContainerResource> actualResources = continuationData.getContainerResources();
    assertThat(actualResources.stream().map(a -> ((IdOnlyContainerResource) a).getId()).collect(Collectors.toList())).containsExactly(FOLDER_ID);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) Matchers(org.mockito.Matchers) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) Before(org.junit.Before) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Collection(java.util.Collection) IOException(java.io.IOException) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) Test(org.junit.Test) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Mockito(org.mockito.Mockito) List(java.util.List) Monitor(org.datatransferproject.api.launcher.Monitor) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) DRIVE_TOKEN_PREFIX(org.datatransferproject.transfer.microsoft.photos.MicrosoftPhotosExporter.DRIVE_TOKEN_PREFIX) Optional(java.util.Optional) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) MicrosoftCredentialFactory(org.datatransferproject.transfer.microsoft.common.MicrosoftCredentialFactory) org.datatransferproject.transfer.microsoft.driveModels(org.datatransferproject.transfer.microsoft.driveModels) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 7 with ContainerResource

use of org.datatransferproject.types.common.models.ContainerResource in project data-transfer-project by google.

the class MicrosoftPhotosExporterTest method exportPhotoWithoutNextPage.

@Test
public void exportPhotoWithoutNextPage() throws IOException {
    // Setup
    when(driveItemsResponse.getNextPageLink()).thenReturn(null);
    MicrosoftDriveItem photoItem = setUpSinglePhoto(IMAGE_URI, PHOTO_ID);
    when(driveItemsResponse.getDriveItems()).thenReturn(new MicrosoftDriveItem[] { photoItem });
    when(driveItemsResponse.getNextPageLink()).thenReturn(null);
    StringPaginationToken inputPaginationToken = new StringPaginationToken(DRIVE_TOKEN_PREFIX + DRIVE_PAGE_URL);
    IdOnlyContainerResource idOnlyContainerResource = new IdOnlyContainerResource(FOLDER_ID);
    // Run
    ExportResult<PhotosContainerResource> result = microsoftPhotosExporter.exportOneDrivePhotos(null, Optional.of(idOnlyContainerResource), Optional.of(inputPaginationToken), uuid);
    // Verify method calls
    verify(photosInterface).getDriveItems(Optional.of(FOLDER_ID), Optional.of(DRIVE_PAGE_URL));
    verify(driveItemsResponse).getDriveItems();
    // Verify next pagination token is absent
    ContinuationData continuationData = result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken).isEqualTo(null);
    // Verify no albums are exported
    Collection<PhotoAlbum> actualAlbums = result.getExportedData().getAlbums();
    assertThat(actualAlbums).isEmpty();
    // Verify one photo (in an album) should be exported
    Collection<PhotoModel> actualPhotos = result.getExportedData().getPhotos();
    assertThat(actualPhotos.stream().map(PhotoModel::getFetchableUrl).collect(Collectors.toList())).containsExactly(IMAGE_URI);
    assertThat(actualPhotos.stream().map(PhotoModel::getAlbumId).collect(Collectors.toList())).containsExactly(FOLDER_ID);
    assertThat(actualPhotos.stream().map(PhotoModel::getTitle).collect(Collectors.toList())).containsExactly(FILENAME);
    // Verify there are no containers ready for sub-processing
    List<ContainerResource> actualResources = continuationData.getContainerResources();
    assertThat(actualResources).isEmpty();
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 8 with ContainerResource

use of org.datatransferproject.types.common.models.ContainerResource in project data-transfer-project by google.

the class PortabilityInMemoryDataCopierTest method continuationDataWithMultipleSubResources.

@Test
public void continuationDataWithMultipleSubResources() throws CopyException, IOException {
    // Arrange
    ContainerResource subResource1 = Mockito.mock(ContainerResource.class);
    ContainerResource subResource2 = Mockito.mock(ContainerResource.class);
    ExportInformation subResource1ExportInfo = new ExportInformation(null, subResource1);
    ExportInformation subResource2ExportInfo = new ExportInformation(null, subResource2);
    Mockito.when(continuationData.getContainerResources()).thenReturn(Arrays.asList(subResource1, subResource2));
    Mockito.when(initialExportResult.getContinuationData()).thenReturn(continuationData);
    Mockito.doReturn(initialExportResult).when(inMemoryDataCopier).copyIteration(jobId, exportAuthData, importAuthData, Optional.of(exportInfo), jobIdPrefix, 1);
    // Act
    inMemoryDataCopier.copy(exportAuthData, importAuthData, jobId, Optional.of(exportInfo));
    // Assert
    InOrder orderVerifier = Mockito.inOrder(inMemoryDataCopier);
    orderVerifier.verify(inMemoryDataCopier).copyIteration(jobId, exportAuthData, importAuthData, Optional.of(exportInfo), jobIdPrefix, 1);
    orderVerifier.verify(inMemoryDataCopier).copyIteration(jobId, exportAuthData, importAuthData, Optional.of(subResource1ExportInfo), jobIdPrefix, 2);
    orderVerifier.verify(inMemoryDataCopier).copyIteration(jobId, exportAuthData, importAuthData, Optional.of(subResource2ExportInfo), jobIdPrefix, 3);
}
Also used : ExportInformation(org.datatransferproject.types.common.ExportInformation) InOrder(org.mockito.InOrder) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) Test(org.junit.Test)

Example 9 with ContainerResource

use of org.datatransferproject.types.common.models.ContainerResource in project data-transfer-project by google.

the class PortabilityInMemoryDataCopierTest method continuationDataWithPaginationDataAndMultipleSubResources.

@Test
public void continuationDataWithPaginationDataAndMultipleSubResources() throws CopyException, IOException {
    // Arrange
    PaginationData paginationData = Mockito.mock(PaginationData.class);
    ContainerResource subResource1 = Mockito.mock(ContainerResource.class);
    ContainerResource subResource2 = Mockito.mock(ContainerResource.class);
    ExportInformation paginationExportInfo = new ExportInformation(paginationData, null);
    ExportInformation subResource1ExportInfo = new ExportInformation(null, subResource1);
    ExportInformation subResource2ExportInfo = new ExportInformation(null, subResource2);
    Mockito.when(continuationData.getPaginationData()).thenReturn(paginationData);
    Mockito.when(continuationData.getContainerResources()).thenReturn(Arrays.asList(subResource1, subResource2));
    Mockito.when(initialExportResult.getContinuationData()).thenReturn(continuationData);
    Mockito.doReturn(initialExportResult).when(inMemoryDataCopier).copyIteration(jobId, exportAuthData, importAuthData, Optional.of(exportInfo), jobIdPrefix, 1);
    // Act
    inMemoryDataCopier.copy(exportAuthData, importAuthData, jobId, Optional.of(exportInfo));
    // Assert
    InOrder orderVerifier = Mockito.inOrder(inMemoryDataCopier);
    orderVerifier.verify(inMemoryDataCopier).copyIteration(jobId, exportAuthData, importAuthData, Optional.of(exportInfo), jobIdPrefix, 1);
    orderVerifier.verify(inMemoryDataCopier).copyIteration(jobId, exportAuthData, importAuthData, Optional.of(paginationExportInfo), jobIdPrefix, 2);
    orderVerifier.verify(inMemoryDataCopier).copyIteration(jobId, exportAuthData, importAuthData, Optional.of(subResource1ExportInfo), jobIdPrefix, 3);
    orderVerifier.verify(inMemoryDataCopier).copyIteration(jobId, exportAuthData, importAuthData, Optional.of(subResource2ExportInfo), jobIdPrefix, 4);
}
Also used : ExportInformation(org.datatransferproject.types.common.ExportInformation) PaginationData(org.datatransferproject.types.common.PaginationData) InOrder(org.mockito.InOrder) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) Test(org.junit.Test)

Example 10 with ContainerResource

use of org.datatransferproject.types.common.models.ContainerResource in project data-transfer-project by google.

the class PortabilityInMemoryDataCopier method copyHelper.

/**
 * Transfers data from the given {@code exporter} optionally starting at the point specified in
 * the provided {@code exportInfo}. Imports the data using the provided {@code importer}. If there
 * is more data to required to be exported, recursively copies using the specific {@link
 * ExportInformation} to continue the process.
 *
 * @param exportAuthData The auth data for the export
 * @param importAuthData The auth data for the import
 * @param exportInfo Any pagination or resource information to use for subsequent calls.
 */
private Collection<ErrorDetail> copyHelper(AuthData exportAuthData, AuthData importAuthData, UUID jobId, Optional<ExportInformation> exportInfo) throws CopyException {
    String jobIdPrefix = "Job " + jobId + ": ";
    final int copyIteration = COPY_ITERATION_COUNTER.incrementAndGet();
    // NOTE: order is important below, do the import of all the items, then do continuation
    // then do sub resources, this ensures all parents are populated before children get
    // processed.
    ExportResult<?> exportResult = copyIteration(jobId, exportAuthData, importAuthData, exportInfo, jobIdPrefix, copyIteration);
    // Import and Export were successful, determine what to do next
    ContinuationData continuationData = exportResult.getContinuationData();
    if (null != continuationData) {
        // Process the next page of items for the resource
        if (null != continuationData.getPaginationData()) {
            monitor.debug(() -> jobIdPrefix + "Starting off a new copy iteration with pagination info, copy iteration: " + copyIteration);
            copyHelper(exportAuthData, importAuthData, jobId, Optional.of(new ExportInformation(continuationData.getPaginationData(), exportInfo.isPresent() ? exportInfo.get().getContainerResource() : null)));
        }
        // Start processing sub-resources
        if (continuationData.getContainerResources() != null && !continuationData.getContainerResources().isEmpty()) {
            for (ContainerResource resource : continuationData.getContainerResources()) {
                monitor.debug(() -> jobIdPrefix + "Starting off a new copy iteration with a new container resource, copy iteration: " + copyIteration);
                copyHelper(exportAuthData, importAuthData, jobId, Optional.of(new ExportInformation(null, resource)));
            }
        }
    }
    return idempotentImportExecutor.getErrors();
}
Also used : ExportInformation(org.datatransferproject.types.common.ExportInformation) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData)

Aggregations

ContainerResource (org.datatransferproject.types.common.models.ContainerResource)24 Test (org.junit.Test)20 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)12 ExportInformation (org.datatransferproject.types.common.ExportInformation)11 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)10 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)7 InOrder (org.mockito.InOrder)7 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 PaginationData (org.datatransferproject.types.common.PaginationData)6 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)6 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 IOException (java.io.IOException)5 Collection (java.util.Collection)5 Optional (java.util.Optional)5 UUID (java.util.UUID)5 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)5 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)5