use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class GoogleMailImporterTest method importMessage.
@Test
public void importMessage() throws Exception {
MailContainerResource resource = new MailContainerResource(null, Collections.singletonList(MESSAGE_MODEL));
ImportResult result = googleMailImporter.importItem(JOB_ID, executor, null, resource);
// Getting list of labels from Google
verify(labelsList, atLeastOnce()).execute();
// Importing message
ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
verify(messages).insert(eq(GoogleMailImporter.USER), messageArgumentCaptor.capture());
assertThat(messageArgumentCaptor.getValue().getRaw()).isEqualTo(MESSAGE_RAW);
// TODO(olsona): test labels
}
use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class RequestHelper method batchRequest.
/**
* Creates a Graph API batch request with required the authorization header.
*
* @param authData the auth token
* @param requests the batch request data
* @param client the client to construct the request with
* @param objectMapper the mapper to serialize data
*/
@SuppressWarnings("unchecked")
public static BatchResponse batchRequest(TokenAuthData authData, List<Map<String, Object>> requests, String baseUrl, OkHttpClient client, ObjectMapper objectMapper) {
try {
Map<String, Object> batch = new LinkedHashMap<>();
batch.put("requests", requests);
Request.Builder requestBuilder = new Request.Builder().url(baseUrl + BATCH_URL);
requestBuilder.header("Authorization", "Bearer " + authData.getToken());
requestBuilder.post(RequestBody.create(MediaType.parse("application/json"), objectMapper.writeValueAsString(batch)));
try (Response response = client.newCall(requestBuilder.build()).execute()) {
int code = response.code();
if (code >= 200 && code <= 299) {
ResponseBody body = response.body();
if (body == null) {
// FIXME evaluate HTTP response and return whether to retry
return new BatchResponse(new ImportResult(ImportResult.ResultType.ERROR));
}
Map<String, Object> responseData = objectMapper.readValue(body.bytes(), Map.class);
return new BatchResponse(new ImportResult(ImportResult.ResultType.OK), (List<Map<String, Object>>) responseData.get("responses"));
} else {
// FIXME evaluate HTTP response and return whether to retry
return new BatchResponse(new ImportResult(ImportResult.ResultType.ERROR));
}
}
} catch (IOException e) {
// TODO log
e.printStackTrace();
return new BatchResponse(new ImportResult(e));
}
}
use of org.datatransferproject.spi.transfer.provider.ImportResult 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.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class BackblazePhotosImporterTest method testNullPhotosAndAlbums.
@Test
public void testNullPhotosAndAlbums() throws Exception {
PhotosContainerResource data = mock(PhotosContainerResource.class);
when(data.getAlbums()).thenReturn(null);
when(data.getPhotos()).thenReturn(null);
BackblazePhotosImporter sut = new BackblazePhotosImporter(monitor, dataStore, streamProvider, clientFactory);
ImportResult result = sut.importItem(UUID.randomUUID(), executor, authData, data);
assertEquals(ImportResult.OK, result);
}
use of org.datatransferproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class BackblazePhotosImporterTest method testEmptyPhotosAndAlbums.
@Test
public void testEmptyPhotosAndAlbums() throws Exception {
PhotosContainerResource data = mock(PhotosContainerResource.class);
when(data.getAlbums()).thenReturn(new ArrayList<>());
when(data.getPhotos()).thenReturn(new ArrayList<>());
BackblazePhotosImporter sut = new BackblazePhotosImporter(monitor, dataStore, streamProvider, clientFactory);
ImportResult result = sut.importItem(UUID.randomUUID(), executor, authData, data);
assertEquals(ImportResult.OK, result);
}
Aggregations