Search in sources :

Example 6 with StringPaginationToken

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

the class GooglePhotosExporter method exportPhotos.

private ExportResult<PhotosContainerResource> exportPhotos(TokensAndUrlAuthData authData, String albumId, Optional<PaginationData> paginationData) {
    try {
        int startItem = 1;
        if (paginationData.isPresent()) {
            String token = ((StringPaginationToken) paginationData.get()).getToken();
            Preconditions.checkArgument(token.startsWith(PHOTO_TOKEN_PREFIX), "Invalid pagination token " + token);
            startItem = Integer.parseInt(token.substring(PHOTO_TOKEN_PREFIX.length()));
        }
        URL photosUrl = new URL(String.format(URL_PHOTO_FEED_FORMAT, albumId, startItem, MAX_RESULTS));
        AlbumFeed photoFeed = getOrCreatePhotosService(authData).getFeed(photosUrl, AlbumFeed.class);
        PaginationData nextPageData = null;
        if (photoFeed.getEntries().size() == MAX_RESULTS) {
            int nextPageStart = startItem + MAX_RESULTS;
            nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX + nextPageStart);
        }
        ContinuationData continuationData = new ContinuationData(nextPageData);
        List<PhotoModel> photos = new ArrayList<>(photoFeed.getEntries().size());
        for (GphotoEntry photo : photoFeed.getEntries()) {
            MediaContent mediaContent = (MediaContent) photo.getContent();
            photos.add(new PhotoModel(photo.getTitle().getPlainText(), mediaContent.getUri(), photo.getDescription().getPlainText(), mediaContent.getMimeType().getMediaType(), albumId));
        }
        PhotosContainerResource containerResource = new PhotosContainerResource(null, photos);
        ResultType resultType = ResultType.CONTINUE;
        if (nextPageData == null) {
            resultType = ResultType.END;
        }
        return new ExportResult<>(resultType, containerResource, continuationData);
    } catch (ServiceException | IOException e) {
        return new ExportResult<>(ResultType.ERROR, e.getMessage());
    }
}
Also used : PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) AlbumFeed(com.google.gdata.data.photos.AlbumFeed) PhotoModel(org.dataportabilityproject.types.transfer.models.photos.PhotoModel) ArrayList(java.util.ArrayList) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) IOException(java.io.IOException) URL(java.net.URL) PhotosContainerResource(org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource) ServiceException(com.google.gdata.util.ServiceException) MediaContent(com.google.gdata.data.MediaContent) GphotoEntry(com.google.gdata.data.photos.GphotoEntry) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 7 with StringPaginationToken

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

the class GoogleTasksExporter method getTasks.

private ExportResult getTasks(Tasks tasksService, IdOnlyContainerResource resource, PaginationData paginationData) throws IOException {
    Tasks.TasksOperations.List query = tasksService.tasks().list(resource.getId()).setMaxResults(PAGE_SIZE);
    if (paginationData != null) {
        query.setPageToken(((StringPaginationToken) paginationData).getToken());
    }
    com.google.api.services.tasks.model.Tasks result = query.execute();
    List<TaskModel> newTasks = result.getItems().stream().map(t -> new TaskModel(resource.getId(), t.getTitle(), t.getNotes())).collect(Collectors.toList());
    PaginationData newPage = null;
    ResultType resultType = ResultType.END;
    if (result.getNextPageToken() != null) {
        newPage = new StringPaginationToken(result.getNextPageToken());
        resultType = ResultType.CONTINUE;
    }
    TaskContainerResource taskContainerResource = new TaskContainerResource(null, newTasks);
    return new ExportResult<>(resultType, taskContainerResource, new ContinuationData(newPage));
}
Also used : ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) Exporter(org.dataportabilityproject.spi.transfer.provider.Exporter) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) TaskModel(org.dataportabilityproject.types.transfer.models.tasks.TaskModel) BearerToken(com.google.api.client.auth.oauth2.BearerToken) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) TaskContainerResource(org.dataportabilityproject.types.transfer.models.tasks.TaskContainerResource) IOException(java.io.IOException) Tasks(com.google.api.services.tasks.Tasks) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TokensAndUrlAuthData(org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData) TaskListModel(org.dataportabilityproject.types.transfer.models.tasks.TaskListModel) List(java.util.List) TaskList(com.google.api.services.tasks.model.TaskList) ImmutableList(com.google.common.collect.ImmutableList) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) Credential(com.google.api.client.auth.oauth2.Credential) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TaskLists(com.google.api.services.tasks.model.TaskLists) GoogleStaticObjects(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) TaskContainerResource(org.dataportabilityproject.types.transfer.models.tasks.TaskContainerResource) TaskModel(org.dataportabilityproject.types.transfer.models.tasks.TaskModel) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 8 with StringPaginationToken

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

the class GoogleCalendarExporterTest method exportEventFirstSet.

@Test
public void exportEventFirstSet() throws IOException {
    setUpSingleEventResponse();
    // Looking at first page, with at least one page after it
    ContainerResource containerResource = new IdOnlyContainerResource(CALENDAR_ID);
    ExportInformation exportInformation = new ExportInformation(null, containerResource);
    eventListResponse.setNextPageToken(NEXT_TOKEN);
    // Run test
    ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(UUID.randomUUID(), null, exportInformation);
    // Check results
    // Verify correct methods were called
    verify(calendarEvents).list(CALENDAR_ID);
    verify(eventListRequest).setMaxAttendees(MAX_ATTENDEES);
    verify(eventListRequest).execute();
    // Check events
    Collection<CalendarEventModel> actualEvents = result.getExportedData().getEvents();
    assertThat(actualEvents.stream().map(CalendarEventModel::getCalendarId).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
    assertThat(actualEvents.stream().map(CalendarEventModel::getTitle).collect(Collectors.toList())).containsExactly(EVENT_DESCRIPTION);
    // Check pagination token
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(EVENT_TOKEN_PREFIX + NEXT_TOKEN);
}
Also used : ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) ContainerResource(org.dataportabilityproject.types.transfer.models.ContainerResource) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Test(org.junit.Test)

Example 9 with StringPaginationToken

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

the class GoogleCalendarExporterTest method exportCalendarFirstSet.

@Test
public void exportCalendarFirstSet() throws IOException {
    setUpSingleCalendarResponse();
    // Looking at first page, with at least one page after it
    calendarListResponse.setNextPageToken(NEXT_TOKEN);
    // Run test
    ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(JOB_ID, null);
    // Check results
    // Verify correct methods were called
    verify(calendarClient).calendarList();
    verify(calendarCalendarList).list();
    verify(calendarListRequest).execute();
    // Check pagination token
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(CALENDAR_TOKEN_PREFIX + NEXT_TOKEN);
    // Check calendars
    Collection<CalendarModel> actualCalendars = result.getExportedData().getCalendars();
    assertThat(actualCalendars.stream().map(CalendarModel::getId).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
    // Check events (should be empty, even though there is an event in the calendar
    Collection<CalendarEventModel> actualEvents = result.getExportedData().getEvents();
    assertThat(actualEvents).isEmpty();
    // Should be one container in the resource list
    List<ContainerResource> actualResources = continuationData.getContainerResources();
    assertThat(actualResources.stream().map(a -> ((IdOnlyContainerResource) a).getId()).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
}
Also used : IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) Events(com.google.api.services.calendar.model.Events) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) Calendar(com.google.api.services.calendar.Calendar) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) Before(org.junit.Before) Event(com.google.api.services.calendar.model.Event) ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) InOrder(org.mockito.InOrder) EVENT_TOKEN_PREFIX(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.EVENT_TOKEN_PREFIX) CalendarList(com.google.api.services.calendar.model.CalendarList) Collection(java.util.Collection) Test(org.junit.Test) IOException(java.io.IOException) MAX_ATTENDEES(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.MAX_ATTENDEES) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) List(java.util.List) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) CALENDAR_TOKEN_PREFIX(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.CALENDAR_TOKEN_PREFIX) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) ContainerResource(org.dataportabilityproject.types.transfer.models.ContainerResource) Collections(java.util.Collections) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) Mockito.mock(org.mockito.Mockito.mock) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) ContainerResource(org.dataportabilityproject.types.transfer.models.ContainerResource) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Test(org.junit.Test)

Example 10 with StringPaginationToken

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

the class GoogleContactsExporterTest method exportFirstPage.

@Test
public void exportFirstPage() throws IOException {
    setUpSinglePersonResponse();
    // Looking at first page, with at least one page after it
    listConnectionsResponse.setNextPageToken(NEXT_PAGE_TOKEN);
    ExportResult<ContactsModelWrapper> result = contactsService.export(UUID.randomUUID(), null);
    // Check that correct methods were called
    verify(connections).list(SELF_RESOURCE);
    InOrder inOrder = Mockito.inOrder(getBatchGet);
    inOrder.verify(getBatchGet).setResourceNames(Collections.singletonList(RESOURCE_NAME));
    inOrder.verify(getBatchGet).setPersonFields(PERSON_FIELDS);
    inOrder.verify(getBatchGet).execute();
    // Check continuation data
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    assertThat(continuationData.getContainerResources()).isEmpty();
    StringPaginationToken paginationToken = (StringPaginationToken) ((ContinuationData) result.getContinuationData()).getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(NEXT_PAGE_TOKEN);
    // Check that the right number of VCards was returned
    JCardReader reader = new JCardReader(result.getExportedData().getVCards());
    List<VCard> vCardList = reader.readAll();
    assertThat(vCardList.size()).isEqualTo(connectionsList.size());
}
Also used : InOrder(org.mockito.InOrder) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) JCardReader(ezvcard.io.json.JCardReader) VCard(ezvcard.VCard) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Test(org.junit.Test)

Aggregations

StringPaginationToken (org.dataportabilityproject.spi.transfer.types.StringPaginationToken)19 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)15 PaginationData (org.dataportabilityproject.spi.transfer.types.PaginationData)15 IdOnlyContainerResource (org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource)11 ExportResult (org.dataportabilityproject.spi.transfer.provider.ExportResult)10 ResultType (org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType)9 IOException (java.io.IOException)8 ExportInformation (org.dataportabilityproject.spi.transfer.types.ExportInformation)7 ArrayList (java.util.ArrayList)6 CalendarContainerResource (org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource)6 Test (org.junit.Test)6 InOrder (org.mockito.InOrder)5 PhotosContainerResource (org.dataportabilityproject.types.transfer.models.photos.PhotosContainerResource)4 List (java.util.List)3 UUID (java.util.UUID)3 Collectors (java.util.stream.Collectors)3 ContainerResource (org.dataportabilityproject.types.transfer.models.ContainerResource)3 CalendarEventModel (org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel)3 ContactsModelWrapper (org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper)3 BearerToken (com.google.api.client.auth.oauth2.BearerToken)2