Search in sources :

Example 11 with ImportResult

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
}
Also used : ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) Message(com.google.api.services.gmail.model.Message) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) Test(org.junit.Test)

Example 12 with ImportResult

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));
    }
}
Also used : ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) Request(okhttp3.Request) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 13 with ImportResult

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);
    }
}
Also used : VCard(ezvcard.VCard) IdempotentImportExecutor(org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) UUID(java.util.UUID) RequestHelper.createRequest(org.datatransferproject.transfer.microsoft.common.RequestHelper.createRequest) TokenAuthData(org.datatransferproject.types.transfer.auth.TokenAuthData) ArrayList(java.util.ArrayList) TransformerService(org.datatransferproject.transfer.microsoft.transformer.TransformerService) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) OkHttpClient(okhttp3.OkHttpClient) TransformResult(org.datatransferproject.transfer.microsoft.transformer.TransformResult) Map(java.util.Map) JCardReader(ezvcard.io.json.JCardReader) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) Importer(org.datatransferproject.spi.transfer.provider.Importer) RequestHelper.batchRequest(org.datatransferproject.transfer.microsoft.common.RequestHelper.batchRequest) TransformResult(org.datatransferproject.transfer.microsoft.transformer.TransformResult) ImportResult(org.datatransferproject.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)

Example 14 with ImportResult

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);
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) Test(org.junit.Test)

Example 15 with ImportResult

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);
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) Test(org.junit.Test)

Aggregations

ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)27 Test (org.junit.Test)14 IOException (java.io.IOException)11 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)8 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)6 UUID (java.util.UUID)5 Map (java.util.Map)4 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 List (java.util.List)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 ResponseBody (okhttp3.ResponseBody)3 AppCredentials (org.datatransferproject.types.transfer.auth.AppCredentials)3 Stopwatch (com.google.common.base.Stopwatch)2 LinkedHashMap (java.util.LinkedHashMap)2 Call (okhttp3.Call)2 Importer (org.datatransferproject.spi.transfer.provider.Importer)2 ContactsModelWrapper (org.datatransferproject.types.common.models.contacts.ContactsModelWrapper)2 TaskListModel (org.datatransferproject.types.common.models.tasks.TaskListModel)2