Search in sources :

Example 1 with ExportInformation

use of org.dataportabilityproject.dataModels.ExportInformation in project data-transfer-project by google.

the class PortabilityCopier method copy.

private static <T extends DataModel> void copy(Exporter<T> exporter, Importer<T> importer, ExportInformation exportInformation) throws IOException {
    logger.debug("copy iteration: {}", 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.
    logger.debug("Starting export, exportInformation: {}", exportInformation);
    T items = exporter.export(exportInformation);
    logger.debug("Finished export, results: {}", items);
    logger.debug("Starting import");
    // The collection of items can be both containers and items
    importer.importItem(items);
    logger.debug("Finished import");
    ContinuationInformation continuationInfo = items.getContinuationInformation();
    if (null != continuationInfo) {
        // Process the next page of items for the resource
        if (null != continuationInfo.getPaginationInformation()) {
            logger.debug("Start off a new copy iteration with pagination info");
            copy(exporter, importer, new ExportInformation(// Resource with additional pages to fetch
            exportInformation.getResource(), Optional.of(continuationInfo.getPaginationInformation())));
        }
        // Start processing sub-resources
        if (continuationInfo.getSubResources() != null && !continuationInfo.getSubResources().isEmpty()) {
            logger.debug("Start off a new copy iteration with a sub resource, size: {}", continuationInfo.getSubResources().size());
            for (Resource resource : continuationInfo.getSubResources()) {
                copy(exporter, importer, new ExportInformation(Optional.of(resource), Optional.empty()));
            }
        }
    }
}
Also used : ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Resource(org.dataportabilityproject.dataModels.Resource)

Example 2 with ExportInformation

use of org.dataportabilityproject.dataModels.ExportInformation in project data-transfer-project by google.

the class GoogleCalendarServiceTest method testExportCalendarFirstSet.

@Test
public void testExportCalendarFirstSet() throws IOException {
    setUpSingleCalendarResponse();
    // Looking at first page, with at least one page after it
    ExportInformation emptyExportInformation = new ExportInformation(Optional.empty(), Optional.empty());
    calendarListResponse.setNextPageToken(NEXT_TOKEN);
    // Run test
    CalendarModelWrapper wrapper = calendarService.export(emptyExportInformation);
    // Check results
    // Verify correct methods were called
    verify(calendarClient).calendarList();
    verify(calendarCalendarList).list();
    verify(calendarListRequest).execute();
    // Check pagination token
    StringPaginationToken paginationToken = (StringPaginationToken) wrapper.getContinuationInformation().getPaginationInformation();
    assertThat(paginationToken.getId()).isEqualTo(NEXT_TOKEN);
    // Check calendars
    Collection<CalendarModel> calendars = wrapper.getCalendars();
    assertThat(calendars.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> events = wrapper.getEvents();
    assertThat(events).isEmpty();
    // Should be one event in the resource list
    Collection<? extends Resource> subResources = wrapper.getContinuationInformation().getSubResources();
    assertThat(subResources.stream().map(a -> ((IdOnlyResource) a).getId()).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
}
Also used : CalendarModelWrapper(org.dataportabilityproject.dataModels.calendar.CalendarModelWrapper) Events(com.google.api.services.calendar.model.Events) InMemoryJobDataCache(org.dataportabilityproject.cloud.local.InMemoryJobDataCache) PaginationInformation(org.dataportabilityproject.dataModels.PaginationInformation) JobDataCache(org.dataportabilityproject.cloud.interfaces.JobDataCache) Calendar(com.google.api.services.calendar.Calendar) GoogleStaticObjects(org.dataportabilityproject.serviceProviders.google.GoogleStaticObjects) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) Before(org.junit.Before) Event(com.google.api.services.calendar.model.Event) InOrder(org.mockito.InOrder) Resource(org.dataportabilityproject.dataModels.Resource) ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) CalendarList(com.google.api.services.calendar.model.CalendarList) Collection(java.util.Collection) CalendarModelWrapper(org.dataportabilityproject.dataModels.calendar.CalendarModelWrapper) IOException(java.io.IOException) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) Test(org.junit.Test) 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) CalendarEventModel(org.dataportabilityproject.dataModels.calendar.CalendarEventModel) CalendarModel(org.dataportabilityproject.dataModels.calendar.CalendarModel) Mockito(org.mockito.Mockito) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) Optional(java.util.Optional) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) CalendarModel(org.dataportabilityproject.dataModels.calendar.CalendarModel) CalendarEventModel(org.dataportabilityproject.dataModels.calendar.CalendarEventModel) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) Test(org.junit.Test)

Example 3 with ExportInformation

use of org.dataportabilityproject.dataModels.ExportInformation in project data-transfer-project by google.

the class GoogleContactsServiceTest method exportSubsequentPage.

@Test
public void exportSubsequentPage() throws IOException {
    setUpSinglePersonResponse();
    // Looking at a subsequent page, with no pages after it
    ExportInformation nextPageExportInformation = new ExportInformation(Optional.empty(), Optional.of(new StringPaginationToken(NEXT_PAGE_TOKEN)));
    listConnectionsResponse.setNextPageToken(null);
    when(listConnectionsRequest.setPageToken(NEXT_PAGE_TOKEN)).thenReturn(listConnectionsRequest);
    // Run test
    ContactsModelWrapper wrapper = contactsService.export(nextPageExportInformation);
    // Verify correct calls were made - i.e., token was added before execution
    InOrder inOrder = Mockito.inOrder(listConnectionsRequest);
    inOrder.verify(listConnectionsRequest).setPageToken(NEXT_PAGE_TOKEN);
    inOrder.verify(listConnectionsRequest).execute();
    // Check continuation information
    assertThat(wrapper.getContinuationInformation()).isNull();
}
Also used : ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) InOrder(org.mockito.InOrder) ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) Test(org.junit.Test)

Example 4 with ExportInformation

use of org.dataportabilityproject.dataModels.ExportInformation in project data-transfer-project by google.

the class GoogleContactsService method export.

public ContactsModelWrapper export(ExportInformation continuationInformation) throws IOException {
    // Set up connection
    Connections.List connectionsList = peopleService.people().connections().list(GoogleContactsConstants.SELF_RESOURCE);
    // Get next page, if we have a page token
    if (continuationInformation.getPaginationInformation().isPresent()) {
        String pageToken = ((StringPaginationToken) continuationInformation.getPaginationInformation().get()).getId();
        connectionsList.setPageToken(pageToken);
    }
    // Get list of connections (nb: not a list containing full info of each Person)
    ListConnectionsResponse response = connectionsList.setPersonFields(GoogleContactsConstants.PERSON_FIELDS).execute();
    List<Person> peopleList = response.getConnections();
    // Get list of resource names, then get list of Persons
    List<String> resourceNames = peopleList.stream().map(Person::getResourceName).collect(Collectors.toList());
    GetPeopleResponse batchResponse = peopleService.people().getBatchGet().setResourceNames(resourceNames).setPersonFields(GoogleContactsConstants.PERSON_FIELDS).execute();
    List<PersonResponse> personResponseList = batchResponse.getResponses();
    // Convert Persons to VCards
    List<VCard> vCards = personResponseList.stream().map(a -> GoogleContactToVCardConverter.convert(a.getPerson())).collect(Collectors.toList());
    // Determine if there's a next page
    StringPaginationToken newPage = null;
    if (response.getNextPageToken() != null) {
        newPage = new StringPaginationToken(response.getNextPageToken());
    }
    ContinuationInformation newContinuationInformation = null;
    if (newPage != null) {
        newContinuationInformation = new ContinuationInformation(null, newPage);
    }
    return new ContactsModelWrapper(vCards, newContinuationInformation);
}
Also used : Connections(com.google.api.services.people.v1.PeopleService.People.Connections) VCard(ezvcard.VCard) PeopleService(com.google.api.services.people.v1.PeopleService) Importer(org.dataportabilityproject.dataModels.Importer) Logger(org.slf4j.Logger) Connections(com.google.api.services.people.v1.PeopleService.People.Connections) ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Exporter(org.dataportabilityproject.dataModels.Exporter) ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) Person(com.google.api.services.people.v1.model.Person) List(java.util.List) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) JobDataCache(org.dataportabilityproject.cloud.interfaces.JobDataCache) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Credential(com.google.api.client.auth.oauth2.Credential) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GoogleStaticObjects(org.dataportabilityproject.serviceProviders.google.GoogleStaticObjects) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Person(com.google.api.services.people.v1.model.Person) VCard(ezvcard.VCard) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) PersonResponse(com.google.api.services.people.v1.model.PersonResponse)

Example 5 with ExportInformation

use of org.dataportabilityproject.dataModels.ExportInformation in project data-transfer-project by google.

the class PortabilityCopier method copyDataType.

// Start the copy data process
public static <T extends DataModel> void copyDataType(ServiceProviderRegistry registry, PortableDataType dataType, String exportService, AuthData exportAuthData, String importService, AuthData importAuthData, UUID jobId) throws IOException {
    Exporter<T> exporter = registry.getExporter(exportService, dataType, jobId, exportAuthData);
    Importer<T> importer = registry.getImporter(importService, dataType, jobId, importAuthData);
    ExportInformation emptyExportInfo = new ExportInformation(Optional.empty(), Optional.empty());
    logger.debug("Starting copy job, id: {}, source: {}, destination: {}", jobId, exportService, importService);
    copy(exporter, importer, emptyExportInfo);
}
Also used : ExportInformation(org.dataportabilityproject.dataModels.ExportInformation)

Aggregations

ExportInformation (org.dataportabilityproject.dataModels.ExportInformation)11 Test (org.junit.Test)8 StringPaginationToken (org.dataportabilityproject.shared.StringPaginationToken)7 IdOnlyResource (org.dataportabilityproject.shared.IdOnlyResource)5 InOrder (org.mockito.InOrder)5 Resource (org.dataportabilityproject.dataModels.Resource)4 CalendarModelWrapper (org.dataportabilityproject.dataModels.calendar.CalendarModelWrapper)4 ContinuationInformation (org.dataportabilityproject.dataModels.ContinuationInformation)3 ContactsModelWrapper (org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper)3 VCard (ezvcard.VCard)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Collectors (java.util.stream.Collectors)2 JobDataCache (org.dataportabilityproject.cloud.interfaces.JobDataCache)2 PaginationInformation (org.dataportabilityproject.dataModels.PaginationInformation)2 CalendarEventModel (org.dataportabilityproject.dataModels.calendar.CalendarEventModel)2 PhotosModelWrapper (org.dataportabilityproject.dataModels.photos.PhotosModelWrapper)2 GoogleStaticObjects (org.dataportabilityproject.serviceProviders.google.GoogleStaticObjects)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Photo (com.flickr4java.flickr.photos.Photo)1