Search in sources :

Example 1 with GoogleAlbum

use of org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum in project data-transfer-project by google.

the class GooglePhotosImporterTest method retrieveAlbumStringOnlyOnce.

@Test
public void retrieveAlbumStringOnlyOnce() throws PermissionDeniedException, InvalidTokenException, IOException {
    String albumId = "Album Id";
    String albumName = "Album Name";
    String albumDescription = "Album Description";
    PhotoAlbum albumModel = new PhotoAlbum(albumId, albumName, albumDescription);
    PortabilityJob portabilityJob = Mockito.mock(PortabilityJob.class);
    Mockito.when(portabilityJob.userLocale()).thenReturn("it");
    JobStore jobStore = Mockito.mock(JobStore.class);
    Mockito.when(jobStore.findJob(uuid)).thenReturn(portabilityJob);
    GoogleAlbum responseAlbum = new GoogleAlbum();
    responseAlbum.setId(NEW_ALBUM_ID);
    Mockito.when(googlePhotosInterface.createAlbum(any(GoogleAlbum.class))).thenReturn(responseAlbum);
    GooglePhotosImporter sut = new GooglePhotosImporter(null, jobStore, null, null, googlePhotosInterface, imageStreamProvider, monitor, 1.0);
    sut.importSingleAlbum(uuid, null, albumModel);
    sut.importSingleAlbum(uuid, null, albumModel);
    Mockito.verify(jobStore, atMostOnce()).findJob(uuid);
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) LocalJobStore(org.datatransferproject.cloud.local.LocalJobStore) JobStore(org.datatransferproject.spi.cloud.storage.JobStore) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum) Test(org.junit.Test)

Example 2 with GoogleAlbum

use of org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum in project data-transfer-project by google.

the class GooglePhotosImporterTest method importAlbumWithITString.

@Test
public void importAlbumWithITString() throws PermissionDeniedException, InvalidTokenException, IOException {
    String albumId = "Album Id";
    String albumName = "Album Name";
    String albumDescription = "Album Description";
    PhotoAlbum albumModel = new PhotoAlbum(albumId, albumName, albumDescription);
    PortabilityJob portabilityJob = Mockito.mock(PortabilityJob.class);
    Mockito.when(portabilityJob.userLocale()).thenReturn("it");
    JobStore jobStore = Mockito.mock(JobStore.class);
    Mockito.when(jobStore.findJob(uuid)).thenReturn(portabilityJob);
    GoogleAlbum responseAlbum = new GoogleAlbum();
    responseAlbum.setId(NEW_ALBUM_ID);
    Mockito.when(googlePhotosInterface.createAlbum(any(GoogleAlbum.class))).thenReturn(responseAlbum);
    GooglePhotosImporter sut = new GooglePhotosImporter(null, jobStore, null, null, googlePhotosInterface, imageStreamProvider, monitor, 1.0);
    sut.importSingleAlbum(uuid, null, albumModel);
    ArgumentCaptor<GoogleAlbum> albumArgumentCaptor = ArgumentCaptor.forClass(GoogleAlbum.class);
    Mockito.verify(googlePhotosInterface).createAlbum(albumArgumentCaptor.capture());
    assertEquals(albumArgumentCaptor.getValue().getTitle(), albumName);
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) LocalJobStore(org.datatransferproject.cloud.local.LocalJobStore) JobStore(org.datatransferproject.spi.cloud.storage.JobStore) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum) Test(org.junit.Test)

Example 3 with GoogleAlbum

use of org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum in project data-transfer-project by google.

the class GooglePhotosExporter method exportAlbums.

/**
 * Note: not all accounts have albums to return. In that case, we just return an empty list of
 * albums instead of trying to iterate through a null list.
 */
@VisibleForTesting
ExportResult<PhotosContainerResource> exportAlbums(TokensAndUrlAuthData authData, Optional<PaginationData> paginationData, UUID jobId) throws IOException, InvalidTokenException, PermissionDeniedException {
    Optional<String> paginationToken = Optional.empty();
    if (paginationData.isPresent()) {
        String token = ((StringPaginationToken) paginationData.get()).getToken();
        Preconditions.checkArgument(token.startsWith(ALBUM_TOKEN_PREFIX), "Invalid pagination token " + token);
        paginationToken = Optional.of(token.substring(ALBUM_TOKEN_PREFIX.length()));
    }
    AlbumListResponse albumListResponse;
    albumListResponse = getOrCreatePhotosInterface(authData).listAlbums(paginationToken);
    PaginationData nextPageData;
    String token = albumListResponse.getNextPageToken();
    List<PhotoAlbum> albums = new ArrayList<>();
    GoogleAlbum[] googleAlbums = albumListResponse.getAlbums();
    if (Strings.isNullOrEmpty(token)) {
        nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX);
    } else {
        nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + token);
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    if (googleAlbums != null && googleAlbums.length > 0) {
        for (GoogleAlbum googleAlbum : googleAlbums) {
            // Add album info to list so album can be recreated later
            PhotoAlbum photoAlbum = new PhotoAlbum(googleAlbum.getId(), googleAlbum.getTitle(), null);
            albums.add(photoAlbum);
            monitor.debug(() -> String.format("%s: Google Photos exporting album: %s", jobId, photoAlbum.getId()));
            // Add album id to continuation data
            continuationData.addContainerResource(new IdOnlyContainerResource(googleAlbum.getId()));
        }
    }
    ResultType resultType = ResultType.CONTINUE;
    PhotosContainerResource containerResource = new PhotosContainerResource(albums, null);
    return new ExportResult<>(resultType, containerResource, continuationData);
}
Also used : AlbumListResponse(org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse) PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with GoogleAlbum

use of org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum in project data-transfer-project by google.

the class GooglePhotosImporter method importSingleAlbum.

@VisibleForTesting
String importSingleAlbum(UUID jobId, TokensAndUrlAuthData authData, PhotoAlbum inputAlbum) throws IOException, InvalidTokenException, PermissionDeniedException {
    // Set up album
    GoogleAlbum googleAlbum = new GoogleAlbum();
    String title = Strings.nullToEmpty(inputAlbum.getName());
    // https://developers.google.com/photos/library/guides/manage-albums#creating-new-album
    if (title.length() > 500) {
        title = title.substring(0, 497) + "...";
    }
    googleAlbum.setTitle(title);
    GoogleAlbum responseAlbum = getOrCreatePhotosInterface(jobId, authData).createAlbum(googleAlbum);
    return responseAlbum.getId();
}
Also used : GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with GoogleAlbum

use of org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum in project data-transfer-project by google.

the class GooglePhotosExporterTest method setUpSingleAlbum.

/**
 * Sets up a response with a single album, containing a single photo
 */
private void setUpSingleAlbum() {
    GoogleAlbum albumEntry = new GoogleAlbum();
    albumEntry.setId(ALBUM_ID);
    albumEntry.setTitle("Title");
    when(albumListResponse.getAlbums()).thenReturn(new GoogleAlbum[] { albumEntry });
}
Also used : GoogleAlbum(org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum)

Aggregations

GoogleAlbum (org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum)8 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 ArrayList (java.util.ArrayList)2 LocalJobStore (org.datatransferproject.cloud.local.LocalJobStore)2 AlbumListResponse (org.datatransferproject.datatransfer.google.mediaModels.AlbumListResponse)2 GoogleMediaItem (org.datatransferproject.datatransfer.google.mediaModels.GoogleMediaItem)2 JobStore (org.datatransferproject.spi.cloud.storage.JobStore)2 PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)2 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)2 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)2 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)2 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)2 ImmutableList (com.google.common.collect.ImmutableList)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 MediaItemSearchResponse (org.datatransferproject.datatransfer.google.mediaModels.MediaItemSearchResponse)1 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)1