Search in sources :

Example 1 with ContactsModelWrapper

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

the class GoogleContactsServiceTest method importFirstResources.

@Test
public void importFirstResources() throws IOException {
    // Set up: small number of VCards to be imported
    int numberOfVCards = 5;
    List<VCard> vCardList = new LinkedList<>();
    for (int i = 0; i < numberOfVCards; i++) {
        StructuredName structuredName = new StructuredName();
        structuredName.setFamily("Family" + i);
        structuredName.setParameter(SOURCE_PARAM_NAME_TYPE, CONTACT_SOURCE_TYPE);
        VCard vCard = new VCard();
        vCard.setStructuredName(structuredName);
        vCardList.add(vCard);
    }
    ContactsModelWrapper wrapper = new ContactsModelWrapper(vCardList, null);
    // Run test
    contactsService.importItem(wrapper);
    // Check that the right methods were called
    verify(people, times(numberOfVCards)).createContact(any(Person.class));
    verify(createContact, times(numberOfVCards)).execute();
}
Also used : ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) VCard(ezvcard.VCard) StructuredName(ezvcard.property.StructuredName) Person(com.google.api.services.people.v1.model.Person) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with ContactsModelWrapper

use of org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper 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 3 with ContactsModelWrapper

use of org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper 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 4 with ContactsModelWrapper

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

the class GoogleContactsServiceTest method exportFirstPage.

@Test
public void exportFirstPage() throws IOException {
    setUpSinglePersonResponse();
    // Looking at first page, with at least one page after it
    ExportInformation emptyExportInformation = new ExportInformation(Optional.empty(), Optional.empty());
    listConnectionsResponse.setNextPageToken(NEXT_PAGE_TOKEN);
    // Run test
    ContactsModelWrapper wrapper = contactsService.export(emptyExportInformation);
    // 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 information
    assertThat(wrapper.getContinuationInformation().getSubResources()).isEmpty();
    StringPaginationToken paginationToken = (StringPaginationToken) wrapper.getContinuationInformation().getPaginationInformation();
    assertThat(paginationToken.getId()).isEqualTo(NEXT_PAGE_TOKEN);
    // Check that the right number of VCards was returned
    Collection<VCard> vCardCollection = wrapper.getVCards();
    assertThat(vCardCollection.size()).isEqualTo(connectionsList.size());
}
Also used : ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) InOrder(org.mockito.InOrder) ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) VCard(ezvcard.VCard) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) Test(org.junit.Test)

Aggregations

ContactsModelWrapper (org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper)4 VCard (ezvcard.VCard)3 ExportInformation (org.dataportabilityproject.dataModels.ExportInformation)3 StringPaginationToken (org.dataportabilityproject.shared.StringPaginationToken)3 Test (org.junit.Test)3 Person (com.google.api.services.people.v1.model.Person)2 InOrder (org.mockito.InOrder)2 Credential (com.google.api.client.auth.oauth2.Credential)1 PeopleService (com.google.api.services.people.v1.PeopleService)1 Connections (com.google.api.services.people.v1.PeopleService.People.Connections)1 GetPeopleResponse (com.google.api.services.people.v1.model.GetPeopleResponse)1 ListConnectionsResponse (com.google.api.services.people.v1.model.ListConnectionsResponse)1 PersonResponse (com.google.api.services.people.v1.model.PersonResponse)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 StructuredName (ezvcard.property.StructuredName)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1