Search in sources :

Example 16 with IdOnlyContainerResource

use of org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource in project data-transfer-project by google.

the class FlickrPhotosExporterTest method exportPhotosFromPhotoset.

@Test
public void exportPhotosFromPhotoset() throws FlickrException {
    // set up auth, flickr service
    when(user.getId()).thenReturn("userId");
    when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
    when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
    when(flickr.getPhotosInterface()).thenReturn(photosInterface);
    when(flickr.getAuthInterface()).thenReturn(authInterface);
    // getting photos from a set with id photosetsId and page 1
    int page = 1;
    String photosetsId = "photosetsId";
    ExportInformation exportInformation = new ExportInformation(null, new IdOnlyContainerResource(photosetsId));
    // make lots of photos and add them to PhotoList (also adding pagination information)
    int numPhotos = 4;
    PhotoList<Photo> photosList = new PhotoList<>();
    for (int i = 0; i < numPhotos; i++) {
        photosList.add(FlickrTestUtils.initializePhoto("title" + 1, "url" + i, "description" + i, MEDIA_TYPE));
    }
    photosList.setPage(page);
    photosList.setPages(page + 1);
    when(photosetsInterface.getPhotos(anyString(), anySet(), anyInt(), anyInt(), anyInt())).thenReturn(photosList);
    // run test
    FlickrPhotosExporter exporter = new FlickrPhotosExporter(flickr);
    ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), new TokenSecretAuthData("token", "secret"), exportInformation);
    assertThat(result.getExportedData().getPhotos().size()).isEqualTo(numPhotos);
    assertThat(result.getExportedData().getAlbums()).isEmpty();
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    assertThat(continuationData.getContainerResources()).isEmpty();
    assertThat(((IntPaginationToken) continuationData.getPaginationData()).getStart()).isEqualTo(page + 1);
}
Also used : IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) IntPaginationToken(org.dataportabilityproject.spi.transfer.types.IntPaginationToken) Token(org.scribe.model.Token) Photo(com.flickr4java.flickr.photos.Photo) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) Matchers.anyString(org.mockito.Matchers.anyString) ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) PhotoList(com.flickr4java.flickr.photos.PhotoList) TokenSecretAuthData(org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) Test(org.junit.Test)

Example 17 with IdOnlyContainerResource

use of org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource in project data-transfer-project by google.

the class SmugMugPhotosExporter method exportAlbums.

private ExportResult<PhotosContainerResource> exportAlbums(Optional<PaginationData> paginationData) throws IOException {
    // Make request to SmugMug
    String albumInfoUri;
    if (paginationData.isPresent()) {
        String token = ((StringPaginationToken) paginationData.get()).getToken();
        Preconditions.checkState(token.startsWith(ALBUM_TOKEN_PREFIX), "Invalid pagination token " + token);
        albumInfoUri = token.substring(ALBUM_TOKEN_PREFIX.length());
    } else {
        SmugMugResponse<SmugMugUserResponse> userResponse = smugMugInterface.makeUserRequest(USER_URL);
        albumInfoUri = userResponse.getResponse().getUser().getUris().get(ALBUMS_KEY).getUri();
    }
    SmugMugResponse<SmugMugAlbumsResponse> albumsResponse = smugMugInterface.makeAlbumRequest(albumInfoUri);
    // Set up continuation data
    StringPaginationToken paginationToken = null;
    if (albumsResponse.getResponse().getPageInfo() != null && albumsResponse.getResponse().getPageInfo().getNextPage() != null) {
        paginationToken = new StringPaginationToken(ALBUM_TOKEN_PREFIX + albumsResponse.getResponse().getPageInfo().getNextPage());
    }
    ContinuationData continuationData = new ContinuationData(paginationToken);
    // Build album list
    List<PhotoAlbum> albumsList = new ArrayList<>();
    for (SmugMugAlbum album : albumsResponse.getResponse().getAlbums()) {
        albumsList.add(new PhotoAlbum(album.getAlbumKey(), album.getTitle(), album.getDescription()));
        continuationData.addContainerResource(new IdOnlyContainerResource(album.getAlbumKey()));
    }
    PhotosContainerResource resource = new PhotosContainerResource(albumsList, null);
    // Get result type
    ResultType resultType = ResultType.CONTINUE;
    if (paginationToken == null) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, resource, continuationData);
}
Also used : SmugMugUserResponse(org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugUserResponse) ArrayList(java.util.ArrayList) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) SmugMugAlbum(org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugAlbum) SmugMugAlbumsResponse(org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugAlbumsResponse) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Aggregations

IdOnlyContainerResource (org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource)17 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)12 PaginationData (org.dataportabilityproject.spi.transfer.types.PaginationData)12 StringPaginationToken (org.dataportabilityproject.spi.transfer.types.StringPaginationToken)11 ExportResult (org.dataportabilityproject.spi.transfer.provider.ExportResult)8 ResultType (org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType)7 IOException (java.io.IOException)6 ExportInformation (org.dataportabilityproject.spi.transfer.types.ExportInformation)6 ArrayList (java.util.ArrayList)5 PhotosContainerResource (org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource)5 Test (org.junit.Test)5 ImmutableList (com.google.common.collect.ImmutableList)4 PhotoAlbum (org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum)4 FlickrException (com.flickr4java.flickr.FlickrException)3 Photoset (com.flickr4java.flickr.photosets.Photoset)3 Photosets (com.flickr4java.flickr.photosets.Photosets)3 List (java.util.List)3 UUID (java.util.UUID)3 Collectors (java.util.stream.Collectors)3 IntPaginationToken (org.dataportabilityproject.spi.transfer.types.IntPaginationToken)3