Search in sources :

Example 6 with ContactsModelWrapper

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

the class GoogleContactsExporterTest method exportSubsequentPage.

@Test
public void exportSubsequentPage() throws IOException {
    setUpSinglePersonResponse();
    // Looking at a subsequent page, with no pages after it
    PaginationData paginationData = new StringPaginationToken(NEXT_PAGE_TOKEN);
    ExportInformation exportInformation = new ExportInformation(paginationData, null);
    listConnectionsResponse.setNextPageToken(null);
    when(listConnectionsRequest.setPageToken(NEXT_PAGE_TOKEN)).thenReturn(listConnectionsRequest);
    // Run test
    ExportResult<ContactsModelWrapper> result = contactsService.export(UUID.randomUUID(), null, exportInformation);
    // 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 data
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    assertThat(continuationData.getContainerResources()).isEmpty();
    assertThat(continuationData.getPaginationData()).isNull();
}
Also used : ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) InOrder(org.mockito.InOrder) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Test(org.junit.Test)

Example 7 with ContactsModelWrapper

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

the class LocalImportTestRunner method main.

@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
    AuthTestDriver authTestDriver = new AuthTestDriver();
    MicrosoftTransferExtension serviceProvider = new MicrosoftTransferExtension();
    TokenAuthData token = authTestDriver.getOAuthTokenCode();
    Importer<TokenAuthData, ContactsModelWrapper> contacts = (Importer<TokenAuthData, ContactsModelWrapper>) serviceProvider.getImporter("contacts");
    ContactsModelWrapper wrapper = new ContactsModelWrapper(createCards());
    ImportResult result = contacts.importItem(UUID.randomUUID(), token, wrapper);
}
Also used : ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) MicrosoftTransferExtension(org.dataportabilityproject.transfer.microsoft.MicrosoftTransferExtension) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) AuthTestDriver(org.dataportabilityproject.auth.microsoft.harness.AuthTestDriver) Importer(org.dataportabilityproject.spi.transfer.provider.Importer)

Example 8 with ContactsModelWrapper

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

the class MicrosoftContactsExporter method transform.

private ContactsModelWrapper transform(List<Map<String, Object>> rawContacts) {
    StringWriter stringWriter = new StringWriter();
    try (JCardWriter writer = new JCardWriter(stringWriter)) {
        for (Map<String, Object> rawContact : rawContacts) {
            TransformResult<VCard> result = transformerService.transform(VCard.class, rawContact);
            if (result.hasProblems()) {
                // FIXME log problem
                continue;
            }
            writer.write(result.getTransformed());
        }
    } catch (IOException e) {
        // TODO log
        e.printStackTrace();
        return new ContactsModelWrapper("");
    }
    return new ContactsModelWrapper(stringWriter.toString());
}
Also used : StringWriter(java.io.StringWriter) JCardWriter(ezvcard.io.json.JCardWriter) IOException(java.io.IOException) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) VCard(ezvcard.VCard)

Example 9 with ContactsModelWrapper

use of org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper 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

ContactsModelWrapper (org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper)9 VCard (ezvcard.VCard)5 IOException (java.io.IOException)4 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)4 List (java.util.List)3 StringPaginationToken (org.dataportabilityproject.spi.transfer.types.StringPaginationToken)3 Test (org.junit.Test)3 Person (com.google.api.services.people.v1.model.Person)2 JCardReader (ezvcard.io.json.JCardReader)2 JCardWriter (ezvcard.io.json.JCardWriter)2 StructuredName (ezvcard.property.StructuredName)2 StringWriter (java.io.StringWriter)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 UUID (java.util.UUID)2 AuthTestDriver (org.dataportabilityproject.auth.microsoft.harness.AuthTestDriver)2 ExportResult (org.dataportabilityproject.spi.transfer.provider.ExportResult)2 Exporter (org.dataportabilityproject.spi.transfer.provider.Exporter)2 ImportResult (org.dataportabilityproject.spi.transfer.provider.ImportResult)2 Importer (org.dataportabilityproject.spi.transfer.provider.Importer)2