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
}
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));
}
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();
}
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);
}
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);
}
Aggregations