Search in sources :

Example 1 with MailContainerResource

use of org.datatransferproject.types.common.models.mail.MailContainerResource 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 2 with MailContainerResource

use of org.datatransferproject.types.common.models.mail.MailContainerResource in project data-transfer-project by google.

the class GoogleMailExporter method export.

@Override
public ExportResult<MailContainerResource> export(UUID id, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
    // Create a new gmail service for the authorized user
    Gmail gmail = getOrCreateGmail(authData);
    Messages.List request = null;
    try {
        request = gmail.users().messages().list(USER).setMaxResults(PAGE_SIZE);
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    if (exportInformation.isPresent() && exportInformation.get().getPaginationData() != null) {
        request.setPageToken(((StringPaginationToken) exportInformation.get().getPaginationData()).getToken());
    }
    ListMessagesResponse response = null;
    try {
        response = request.execute();
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    List<MailMessageModel> results = new ArrayList<>(response.getMessages().size());
    // as we can't store all the mail messages in memory at once.
    for (Message listMessage : response.getMessages()) {
        Message getResponse = null;
        try {
            getResponse = gmail.users().messages().get(USER, listMessage.getId()).setFormat("raw").execute();
        } catch (IOException e) {
            return new ExportResult<>(e);
        }
        // TODO: note this doesn't transfer things like labels
        results.add(new MailMessageModel(getResponse.getRaw(), getResponse.getLabelIds()));
    }
    PaginationData newPage = null;
    ResultType resultType = ResultType.END;
    if (response.getNextPageToken() != null) {
        newPage = new StringPaginationToken(response.getNextPageToken());
        resultType = ResultType.CONTINUE;
    }
    MailContainerResource mailContainerResource = new MailContainerResource(null, results);
    return new ExportResult<>(resultType, mailContainerResource, new ContinuationData(newPage));
}
Also used : Messages(com.google.api.services.gmail.Gmail.Users.Messages) PaginationData(org.datatransferproject.types.common.PaginationData) Message(com.google.api.services.gmail.model.Message) Gmail(com.google.api.services.gmail.Gmail) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) ListMessagesResponse(com.google.api.services.gmail.model.ListMessagesResponse) MailMessageModel(org.datatransferproject.types.common.models.mail.MailMessageModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 3 with MailContainerResource

use of org.datatransferproject.types.common.models.mail.MailContainerResource in project data-transfer-project by google.

the class GoogleMailExporterTest method exportMessagesSubsequentSet.

@Test
public void exportMessagesSubsequentSet() throws IOException {
    setUpSingleMessageResponse();
    // Looking at subsequent page, with no page after it
    PaginationData paginationData = new StringPaginationToken(NEXT_TOKEN);
    ExportInformation exportInformation = new ExportInformation(paginationData, null);
    messageListResponse.setNextPageToken(null);
    // Run test
    ExportResult<MailContainerResource> result = googleMailExporter.export(JOB_ID, null, Optional.of(exportInformation));
    // Check results
    // Verify correct calls were made (i.e., token was set before execution)
    InOrder inOrder = Mockito.inOrder(messageListRequest);
    inOrder.verify(messageListRequest).setPageToken(NEXT_TOKEN);
    inOrder.verify(messageListRequest).execute();
    // Check pagination token (should be null)
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken).isNull();
}
Also used : ExportInformation(org.datatransferproject.types.common.ExportInformation) PaginationData(org.datatransferproject.types.common.PaginationData) InOrder(org.mockito.InOrder) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 4 with MailContainerResource

use of org.datatransferproject.types.common.models.mail.MailContainerResource in project data-transfer-project by google.

the class MailContainerResourceTest method verifySerializeDeserialize.

@Test
public void verifySerializeDeserialize() throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerSubtypes(MailContainerResource.class);
    List<MailContainerModel> containers = ImmutableList.of(new MailContainerModel("id1", "container1"), new MailContainerModel("id2", "container2"));
    List<MailMessageModel> messages = ImmutableList.of(new MailMessageModel("foo", ImmutableList.of("1")), new MailMessageModel("bar", ImmutableList.of("1", "2'")));
    ContainerResource data = new MailContainerResource(containers, messages);
    String serialized = objectMapper.writeValueAsString(data);
    ContainerResource deserializedModel = objectMapper.readValue(serialized, ContainerResource.class);
    Truth.assertThat(deserializedModel).isNotNull();
    Truth.assertThat(deserializedModel).isInstanceOf(MailContainerResource.class);
    MailContainerResource deserialized = (MailContainerResource) deserializedModel;
    Truth.assertThat(deserialized.getMessages()).hasSize(2);
    Truth.assertThat(deserialized.getFolders()).hasSize(2);
    Truth.assertThat(deserialized).isEqualTo(data);
}
Also used : ContainerResource(org.datatransferproject.types.common.models.ContainerResource) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) MailContainerModel(org.datatransferproject.types.common.models.mail.MailContainerModel) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) MailMessageModel(org.datatransferproject.types.common.models.mail.MailMessageModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with MailContainerResource

use of org.datatransferproject.types.common.models.mail.MailContainerResource in project data-transfer-project by google.

the class GoogleMailExporterTest method exportMessagesFirstSet.

@Test
public void exportMessagesFirstSet() throws IOException {
    setUpSingleMessageResponse();
    // Looking at first page, with at least one page after it
    messageListResponse.setNextPageToken(NEXT_TOKEN);
    // Run test
    ExportResult<MailContainerResource> result = googleMailExporter.export(JOB_ID, null, Optional.empty());
    // Check results
    // Verify correct methods were called
    InOrder inOrder = Mockito.inOrder(messages, messageListRequest, get);
    // First request
    inOrder.verify(messages).list(GoogleMailExporter.USER);
    inOrder.verify(messageListRequest).setMaxResults(GoogleMailExporter.PAGE_SIZE);
    verify(messageListRequest, never()).setPageToken(anyString());
    // Second request
    inOrder.verify(messages).get(GoogleMailExporter.USER, MESSAGE_ID);
    inOrder.verify(get).setFormat("raw");
    inOrder.verify(get).execute();
    // Check pagination token
    ContinuationData continuationData = (ContinuationData) result.getContinuationData();
    StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
    assertThat(paginationToken.getToken()).isEqualTo(NEXT_TOKEN);
    // Check messages
    Collection<MailMessageModel> actualMail = result.getExportedData().getMessages();
    assertThat(actualMail.stream().map(MailMessageModel::getRawString).collect(Collectors.toList())).containsExactly(MESSAGE_RAW);
    assertThat(actualMail.stream().map(MailMessageModel::getContainerIds).collect(Collectors.toList())).containsExactly(MESSAGE_LABELS);
}
Also used : InOrder(org.mockito.InOrder) MailContainerResource(org.datatransferproject.types.common.models.mail.MailContainerResource) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) MailMessageModel(org.datatransferproject.types.common.models.mail.MailMessageModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Aggregations

MailContainerResource (org.datatransferproject.types.common.models.mail.MailContainerResource)5 Test (org.junit.Test)4 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)3 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)3 MailMessageModel (org.datatransferproject.types.common.models.mail.MailMessageModel)3 Message (com.google.api.services.gmail.model.Message)2 PaginationData (org.datatransferproject.types.common.PaginationData)2 InOrder (org.mockito.InOrder)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Gmail (com.google.api.services.gmail.Gmail)1 Messages (com.google.api.services.gmail.Gmail.Users.Messages)1 ListMessagesResponse (com.google.api.services.gmail.model.ListMessagesResponse)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)1 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)1 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)1 ExportInformation (org.datatransferproject.types.common.ExportInformation)1 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)1 MailContainerModel (org.datatransferproject.types.common.models.mail.MailContainerModel)1