Search in sources :

Example 1 with JCardReader

use of ezvcard.io.json.JCardReader 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)

Example 2 with JCardReader

use of ezvcard.io.json.JCardReader in project data-transfer-project by google.

the class GoogleContactsImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, TokensAndUrlAuthData authData, ContactsModelWrapper data) {
    JCardReader reader = new JCardReader(data.getVCards());
    try {
        // TODO(olsona): address any other problems that might arise in conversion
        List<VCard> vCardList = reader.readAll();
        for (VCard vCard : vCardList) {
            Person person = convert(vCard);
            getOrCreatePeopleService(authData).people().createContact(person).execute();
        }
        return ImportResult.OK;
    } catch (IOException e) {
        return new ImportResult(ImportResult.ResultType.ERROR, e.getMessage());
    }
}
Also used : ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) IOException(java.io.IOException) JCardReader(ezvcard.io.json.JCardReader) VCard(ezvcard.VCard) Person(com.google.api.services.people.v1.model.Person)

Example 3 with JCardReader

use of ezvcard.io.json.JCardReader in project data-transfer-project by google.

the class MicrosoftContactsImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, TokenAuthData authData, ContactsModelWrapper wrapper) {
    JCardReader reader = new JCardReader(wrapper.getVCards());
    try {
        List<VCard> cards = reader.readAll();
        List<String> problems = new ArrayList<>();
        int[] id = new int[] { 1 };
        List<Map<String, Object>> requests = cards.stream().map(card -> {
            TransformResult<LinkedHashMap> result = transformerService.transform(LinkedHashMap.class, card);
            problems.addAll(result.getProblems());
            LinkedHashMap contact = result.getTransformed();
            Map<String, Object> request = createRequest(id[0], CONTACTS_URL, contact);
            id[0]++;
            return request;
        }).collect(toList());
        if (!problems.isEmpty()) {
        // TODO log problems
        }
        return batchRequest(authData, requests, baseUrl, client, objectMapper).getResult();
    } catch (IOException e) {
        // TODO log
        e.printStackTrace();
        return new ImportResult(ImportResult.ResultType.ERROR, "Error deserializing contacts: " + e.getMessage());
    }
}
Also used : VCard(ezvcard.VCard) RequestHelper.createRequest(org.dataportabilityproject.transfer.microsoft.common.RequestHelper.createRequest) RequestHelper.batchRequest(org.dataportabilityproject.transfer.microsoft.common.RequestHelper.batchRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) IOException(java.io.IOException) UUID(java.util.UUID) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TransformResult(org.dataportabilityproject.transfer.microsoft.transformer.TransformResult) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) OkHttpClient(okhttp3.OkHttpClient) Map(java.util.Map) JCardReader(ezvcard.io.json.JCardReader) TransformerService(org.dataportabilityproject.transfer.microsoft.transformer.TransformerService) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) Importer(org.dataportabilityproject.spi.transfer.provider.Importer) TransformResult(org.dataportabilityproject.transfer.microsoft.transformer.TransformResult) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) JCardReader(ezvcard.io.json.JCardReader) VCard(ezvcard.VCard) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

VCard (ezvcard.VCard)3 JCardReader (ezvcard.io.json.JCardReader)3 IOException (java.io.IOException)2 ImportResult (org.dataportabilityproject.spi.transfer.provider.ImportResult)2 ContactsModelWrapper (org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Person (com.google.api.services.people.v1.model.Person)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Collectors.toList (java.util.stream.Collectors.toList)1 OkHttpClient (okhttp3.OkHttpClient)1 Importer (org.dataportabilityproject.spi.transfer.provider.Importer)1 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)1 StringPaginationToken (org.dataportabilityproject.spi.transfer.types.StringPaginationToken)1 RequestHelper.batchRequest (org.dataportabilityproject.transfer.microsoft.common.RequestHelper.batchRequest)1 RequestHelper.createRequest (org.dataportabilityproject.transfer.microsoft.common.RequestHelper.createRequest)1 TransformResult (org.dataportabilityproject.transfer.microsoft.transformer.TransformResult)1