use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper in project data-transfer-project by google.
the class GoogleContactsImporterTest method importFirstResources.
@Test
public void importFirstResources() throws Exception {
// 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);
vCard.setFormattedName("First " + structuredName.getFamily());
vCardList.add(vCard);
}
String vCardString = GoogleContactsExporter.makeVCardString(vCardList);
ContactsModelWrapper wrapper = new ContactsModelWrapper(vCardString);
// Run test
contactsService.importItem(UUID.randomUUID(), executor, null, wrapper);
// Check that the right methods were called
verify(people, times(numberOfVCards)).createContact(any(Person.class));
verify(createContact, times(numberOfVCards)).execute();
}
use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper in project data-transfer-project by google.
the class MicrosoftContactsExporter method doExport.
@SuppressWarnings("unchecked")
private ExportResult<ContactsModelWrapper> doExport(TokensAndUrlAuthData authData, String url) {
Request.Builder graphReqBuilder = new Request.Builder().url(url);
graphReqBuilder.header("Authorization", "Bearer " + authData.getAccessToken());
try (Response graphResponse = client.newCall(graphReqBuilder.build()).execute()) {
ResponseBody body = graphResponse.body();
if (body == null) {
return new ExportResult<>(new Exception("Error retrieving contacts: response body was null"));
}
String graphBody = new String(body.bytes());
Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
String nextLink = (String) graphMap.get(ODATA_NEXT);
ContinuationData continuationData = nextLink == null ? null : new ContinuationData(new GraphPagination(nextLink));
List<Map<String, Object>> rawContacts = (List<Map<String, Object>>) graphMap.get("value");
if (rawContacts == null) {
return new ExportResult<>(ExportResult.ResultType.END);
}
ContactsModelWrapper wrapper = transform(rawContacts);
return new ExportResult<>(ExportResult.ResultType.CONTINUE, wrapper, continuationData);
} catch (IOException e) {
// FIXME log error
e.printStackTrace();
return new ExportResult<>(e);
}
}
use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper in project data-transfer-project by google.
the class MicrosoftContactsImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentImportExecutor, 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(e);
}
}
use of org.datatransferproject.types.common.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());
}
use of org.datatransferproject.types.common.models.contacts.ContactsModelWrapper in project data-transfer-project by google.
the class LocalExportTestRunner method main.
@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
AuthTestDriver authTestDriver = new AuthTestDriver();
MicrosoftTransferExtension serviceProvider = new MicrosoftTransferExtension();
TokenAuthData token = authTestDriver.getOAuthTokenCode();
Exporter<TokenAuthData, ContactsModelWrapper> contacts = (Exporter<TokenAuthData, ContactsModelWrapper>) serviceProvider.getExporter("CONTACTS");
ExportResult<ContactsModelWrapper> wrapper = contacts.export(UUID.randomUUID(), token, Optional.empty());
}
Aggregations