use of org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource in project data-transfer-project by google.
the class GoogleCalendarExporter method exportCalendars.
private ExportResult<CalendarContainerResource> exportCalendars(AuthData authData, Optional<PaginationData> pageData) {
Calendar.CalendarList.List listRequest;
CalendarList listResult;
// Get calendar information
try {
listRequest = getOrCreateCalendarInterface(authData).calendarList().list();
if (pageData.isPresent()) {
StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
Preconditions.checkState(paginationToken.getToken().startsWith(CALENDAR_TOKEN_PREFIX), "Token is not applicable");
listRequest.setPageToken(((StringPaginationToken) pageData.get()).getToken().substring(CALENDAR_TOKEN_PREFIX.length()));
}
listResult = listRequest.execute();
} catch (IOException e) {
return new ExportResult<CalendarContainerResource>(ResultType.ERROR, e.getMessage());
}
// Set up continuation data
PaginationData nextPageData = null;
if (listResult.getNextPageToken() != null) {
nextPageData = new StringPaginationToken(CALENDAR_TOKEN_PREFIX + listResult.getNextPageToken());
}
ContinuationData continuationData = new ContinuationData(nextPageData);
// Process calendar list
List<CalendarModel> calendarModels = new ArrayList<>(listResult.getItems().size());
for (CalendarListEntry calendarData : listResult.getItems()) {
CalendarModel model = convertToCalendarModel(calendarData);
continuationData.addContainerResource(new IdOnlyContainerResource(calendarData.getId()));
calendarModels.add(model);
}
CalendarContainerResource calendarContainerResource = new CalendarContainerResource(calendarModels, null);
// Get result type
ExportResult.ResultType resultType = ResultType.CONTINUE;
if (calendarModels.isEmpty()) {
resultType = ResultType.END;
}
return new ExportResult<CalendarContainerResource>(resultType, calendarContainerResource, continuationData);
}
use of org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource in project data-transfer-project by google.
the class GoogleCalendarExporterTest method exportEventFirstSet.
@Test
public void exportEventFirstSet() throws IOException {
setUpSingleEventResponse();
// Looking at first page, with at least one page after it
ContainerResource containerResource = new IdOnlyContainerResource(CALENDAR_ID);
ExportInformation exportInformation = new ExportInformation(null, containerResource);
eventListResponse.setNextPageToken(NEXT_TOKEN);
// Run test
ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(UUID.randomUUID(), null, exportInformation);
// Check results
// Verify correct methods were called
verify(calendarEvents).list(CALENDAR_ID);
verify(eventListRequest).setMaxAttendees(MAX_ATTENDEES);
verify(eventListRequest).execute();
// Check events
Collection<CalendarEventModel> actualEvents = result.getExportedData().getEvents();
assertThat(actualEvents.stream().map(CalendarEventModel::getCalendarId).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
assertThat(actualEvents.stream().map(CalendarEventModel::getTitle).collect(Collectors.toList())).containsExactly(EVENT_DESCRIPTION);
// Check pagination token
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
assertThat(paginationToken.getToken()).isEqualTo(EVENT_TOKEN_PREFIX + NEXT_TOKEN);
}
use of org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource in project data-transfer-project by google.
the class GoogleCalendarExporterTest method exportCalendarFirstSet.
@Test
public void exportCalendarFirstSet() throws IOException {
setUpSingleCalendarResponse();
// Looking at first page, with at least one page after it
calendarListResponse.setNextPageToken(NEXT_TOKEN);
// Run test
ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(JOB_ID, null);
// Check results
// Verify correct methods were called
verify(calendarClient).calendarList();
verify(calendarCalendarList).list();
verify(calendarListRequest).execute();
// Check pagination token
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
assertThat(paginationToken.getToken()).isEqualTo(CALENDAR_TOKEN_PREFIX + NEXT_TOKEN);
// Check calendars
Collection<CalendarModel> actualCalendars = result.getExportedData().getCalendars();
assertThat(actualCalendars.stream().map(CalendarModel::getId).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
// Check events (should be empty, even though there is an event in the calendar
Collection<CalendarEventModel> actualEvents = result.getExportedData().getEvents();
assertThat(actualEvents).isEmpty();
// Should be one container in the resource list
List<ContainerResource> actualResources = continuationData.getContainerResources();
assertThat(actualResources.stream().map(a -> ((IdOnlyContainerResource) a).getId()).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
}
use of org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource in project data-transfer-project by google.
the class GoogleCalendarImporterTest method importCalendarAndEvent.
@Test
public void importCalendarAndEvent() throws IOException {
String modelCalendarId = "modelCalendarId";
String googleCalendarId = "googleCalendarId";
UUID jobId = UUID.randomUUID();
// Set up calendar, events, and mocks
CalendarModel calendarModel = new CalendarModel(modelCalendarId, null, null);
com.google.api.services.calendar.model.Calendar calendarToInsert = GoogleCalendarImporter.convertToGoogleCalendar(calendarModel);
com.google.api.services.calendar.model.Calendar responseCalendar = new com.google.api.services.calendar.model.Calendar().setId(googleCalendarId);
CalendarEventModel eventModel = new CalendarEventModel(modelCalendarId, null, null, null, null, null, null);
Event eventToInsert = GoogleCalendarImporter.convertToGoogleCalendarEvent(eventModel);
Event responseEvent = new Event();
when(eventInsertRequest.execute()).thenReturn(responseEvent);
when(calendarEvents.insert(googleCalendarId, eventToInsert)).thenReturn(eventInsertRequest);
when(calendarInsertRequest.execute()).thenReturn(responseCalendar);
when(calendarCalendars.insert(calendarToInsert)).thenReturn(calendarInsertRequest);
CalendarContainerResource calendarContainerResource = new CalendarContainerResource(Collections.singleton(calendarModel), Collections.singleton(eventModel));
// Run test
calendarService.importItem(jobId, null, calendarContainerResource);
// Check the right methods were called
verify(calendarCalendars).insert(calendarToInsert);
verify(calendarInsertRequest).execute();
verify(calendarEvents).insert(googleCalendarId, eventToInsert);
verify(eventInsertRequest).execute();
// Check jobStore contents
assertThat(jobStore.findData(TempCalendarData.class, jobId).getImportedId(modelCalendarId)).isEqualTo(googleCalendarId);
}
use of org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource in project data-transfer-project by google.
the class MicrosoftCalendarExporter method export.
@Override
public ExportResult<CalendarContainerResource> export(UUID jobId, TokenAuthData authData) {
Request.Builder calendarsBuilder = getBuilder(baseUrl + CALENDARS_SUBPATH, authData);
List<CalendarModel> calendarModels = new ArrayList<>();
try (Response graphResponse = client.newCall(calendarsBuilder.build()).execute()) {
ResponseBody body = graphResponse.body();
if (body == null) {
return new ExportResult<>(ExportResult.ResultType.ERROR, "Error retrieving contacts: response body was null");
}
String graphBody = new String(body.bytes());
Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
// TODO String nextLink = (String) graphMap.get(ODATA_NEXT);
// TODO ContinuationData continuationData = nextLink == null
// ? null : new ContinuationData(new GraphPagination(nextLink));
@SuppressWarnings("unchecked") List<Map<String, Object>> rawCalendars = (List<Map<String, Object>>) graphMap.get("value");
if (rawCalendars == null) {
return new ExportResult<>(ExportResult.ResultType.END);
}
for (Map<String, Object> rawCalendar : rawCalendars) {
TransformResult<CalendarModel> result = transformerService.transform(CalendarModel.class, rawCalendar);
if (result.hasProblems()) {
// FIXME log problem
continue;
}
calendarModels.add(result.getTransformed());
}
} catch (IOException e) {
// FIXME log error
e.printStackTrace();
return new ExportResult<>(ExportResult.ResultType.ERROR, "Error retrieving calendar: " + e.getMessage());
}
List<CalendarEventModel> calendarEventModels = new ArrayList<>();
for (CalendarModel calendarModel : calendarModels) {
String id = calendarModel.getId();
Request.Builder eventsBuilder = getBuilder(calculateEventsUrl(id), authData);
try (Response graphResponse = client.newCall(eventsBuilder.build()).execute()) {
ResponseBody body = graphResponse.body();
if (body == null) {
return new ExportResult<>(ExportResult.ResultType.ERROR, "Error retrieving calendar: response body was null");
}
String graphBody = new String(body.bytes());
Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
// TODO String nextLink = (String) graphMap.get(ODATA_NEXT);
// TODO ContinuationData continuationData = nextLink == null
// ? null : new ContinuationData(new GraphPagination(nextLink));
@SuppressWarnings("unchecked") List<Map<String, Object>> rawEvents = (List<Map<String, Object>>) graphMap.get("value");
if (rawEvents == null) {
return new ExportResult<>(ExportResult.ResultType.END);
}
for (Map<String, Object> rawEvent : rawEvents) {
Map<String, String> properties = new HashMap<>();
properties.put(CALENDAR_ID, id);
TransformResult<CalendarEventModel> result = transformerService.transform(CalendarEventModel.class, rawEvent, properties);
if (result.hasProblems()) {
// FIXME log problem
continue;
}
calendarEventModels.add(result.getTransformed());
}
} catch (IOException e) {
// FIXME log error
e.printStackTrace();
return new ExportResult<>(ExportResult.ResultType.ERROR, "Error retrieving contacts: " + e.getMessage());
}
}
CalendarContainerResource resource = new CalendarContainerResource(calendarModels, calendarEventModels);
return new ExportResult<>(ExportResult.ResultType.END, resource, null);
}
Aggregations