Search in sources :

Example 6 with CalendarEventModel

use of org.datatransferproject.types.common.models.calendar.CalendarEventModel in project data-transfer-project by google.

the class MicrosoftCalendarImportTest method testImport.

@Test
@SuppressWarnings("unchecked")
public void testImport() throws Exception {
    server.enqueue(new MockResponse().setBody(BATCH_CALENDAR_RESPONSE));
    server.enqueue(new MockResponse().setResponseCode(201).setBody(BATCH_EVENT_RESPONSE));
    server.start();
    HttpUrl baseUrl = server.url("");
    MicrosoftCalendarImporter importer = new MicrosoftCalendarImporter(baseUrl.toString(), client, mapper, transformerService);
    CalendarModel calendarModel = new CalendarModel("OldId1", "name", "name");
    CalendarAttendeeModel attendeeModel = new CalendarAttendeeModel("Test Attendee", "test@test.com", false);
    CalendarEventModel.CalendarEventTime start = new CalendarEventModel.CalendarEventTime(ZonedDateTime.now(ZoneId.of("GMT")).toOffsetDateTime(), false);
    CalendarEventModel.CalendarEventTime end = new CalendarEventModel.CalendarEventTime(ZonedDateTime.now(ZoneId.of("GMT")).toOffsetDateTime(), false);
    CalendarEventModel eventModel = new CalendarEventModel("OldId1", "Event1", "Test Notes", singletonList(attendeeModel), "Location1", start, end, null);
    CalendarContainerResource resource = new CalendarContainerResource(singleton(calendarModel), singleton(eventModel));
    FakeIdempotentImportExecutor executor = new FakeIdempotentImportExecutor();
    ImportResult result = importer.importItem(JOB_ID, executor, token, resource);
    Assert.assertEquals(ImportResult.ResultType.OK, result.getType());
    // verify the batch calendar request
    RecordedRequest calendarBatch = server.takeRequest();
    Map<String, Object> calendarBody = (Map<String, Object>) mapper.readValue(calendarBatch.getBody().readUtf8(), Map.class);
    List<Map<String, Object>> calendarRequests = (List<Map<String, Object>>) calendarBody.get("requests");
    Assert.assertNotNull(calendarRequests);
    Assert.assertEquals(1, calendarRequests.size());
    Map<String, Object> calendarRequest = calendarRequests.get(0);
    Assert.assertNotNull(calendarRequest.get("headers"));
    Assert.assertEquals("POST", calendarRequest.get("method"));
    Assert.assertEquals("/v1.0/me/calendars", calendarRequest.get("url"));
    Map<String, Object> calendarRequestBody = (Map<String, Object>) calendarRequest.get("body");
    Assert.assertNotNull(calendarRequestBody);
    Assert.assertEquals("name", calendarRequestBody.get("name"));
    // verify the calendar id mapping from old id to new id was saved
    Assert.assertEquals("NewId1", executor.getCachedValue("OldId1"));
    // verify the batch event request
    RecordedRequest eventBatch = server.takeRequest();
    Map<String, Object> eventRequests = (Map<String, Object>) mapper.readValue(eventBatch.getBody().readUtf8(), Map.class);
    Map<String, Object> eventRequest = (Map<String, Object>) ((List<Map<String, Object>>) eventRequests.get("requests")).get(0);
    Assert.assertNotNull(eventRequest.get("headers"));
    Assert.assertEquals("POST", eventRequest.get("method"));
    Assert.assertEquals("/v1.0/me/calendars/NewId1/events", // verify the URL is contructed correctly with NewId
    eventRequest.get("url"));
    Map<String, Object> eventRequestBody = (Map<String, Object>) eventRequest.get("body");
    Assert.assertNotNull(eventRequestBody);
    Assert.assertEquals("Event1", eventRequestBody.get("subject"));
    Map<String, Object> location = (Map<String, Object>) eventRequestBody.get("location");
    Assert.assertEquals("Location1", location.get("displayName"));
    Assert.assertEquals("Default", location.get("locationType"));
    Map<String, Object> body = (Map<String, Object>) eventRequestBody.get("body");
    Assert.assertEquals("Test Notes", body.get("content"));
    Assert.assertEquals("HTML", body.get("contentType"));
    List<Map<String, Object>> attendees = (List<Map<String, Object>>) eventRequestBody.get("attendees");
    Assert.assertEquals(1, attendees.size());
    Map<String, Object> attendee = (Map<String, Object>) attendees.get(0);
    Assert.assertEquals("required", attendee.get("type"));
    Map<String, Object> emailAddress = (Map<String, Object>) attendee.get("emailAddress");
    Assert.assertEquals("test@test.com", emailAddress.get("address"));
    Assert.assertEquals("Test Attendee", emailAddress.get("name"));
    // verify dates
    Map<String, Object> startDate = (Map<String, Object>) eventRequestBody.get("start");
    Assert.assertNotNull(startDate.get("dateTime"));
    Assert.assertEquals("UTC", startDate.get("timeZone"));
    Map<String, Object> endDate = (Map<String, Object>) eventRequestBody.get("end");
    Assert.assertNotNull(endDate.get("dateTime"));
    Assert.assertEquals("UTC", endDate.get("timeZone"));
}
Also used : RecordedRequest(com.squareup.okhttp.mockwebserver.RecordedRequest) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MicrosoftCalendarImporter(org.datatransferproject.transfer.microsoft.calendar.MicrosoftCalendarImporter) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) FakeIdempotentImportExecutor(org.datatransferproject.test.types.FakeIdempotentImportExecutor) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) HttpUrl(com.squareup.okhttp.HttpUrl) CalendarAttendeeModel(org.datatransferproject.types.common.models.calendar.CalendarAttendeeModel) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 7 with CalendarEventModel

use of org.datatransferproject.types.common.models.calendar.CalendarEventModel 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, Optional.empty());
    // 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);
}
Also used : MAX_ATTENDEES(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects.MAX_ATTENDEES) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) EVENT_TOKEN_PREFIX(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects.EVENT_TOKEN_PREFIX) ExportInformation(org.datatransferproject.types.common.ExportInformation) Events(com.google.api.services.calendar.model.Events) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Calendar(com.google.api.services.calendar.Calendar) GoogleCredentialFactory(org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory) Before(org.junit.Before) Event(com.google.api.services.calendar.model.Event) InOrder(org.mockito.InOrder) CalendarList(com.google.api.services.calendar.model.CalendarList) Collection(java.util.Collection) IOException(java.io.IOException) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) PaginationData(org.datatransferproject.types.common.PaginationData) Truth.assertThat(com.google.common.truth.Truth.assertThat) CALENDAR_TOKEN_PREFIX(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects.CALENDAR_TOKEN_PREFIX) Collectors(java.util.stream.Collectors) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) Mockito.verify(org.mockito.Mockito.verify) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) Mockito(org.mockito.Mockito) List(java.util.List) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) Optional(java.util.Optional) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) ContainerResource(org.datatransferproject.types.common.models.ContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) Test(org.junit.Test)

Example 8 with CalendarEventModel

use of org.datatransferproject.types.common.models.calendar.CalendarEventModel in project data-transfer-project by google.

the class GoogleCalendarImporterTest method importCalendarAndEvent.

@Test
public void importCalendarAndEvent() throws Exception {
    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, 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, executor, null, calendarContainerResource);
    // Check the right methods were called
    verify(calendarCalendars).insert(calendarToInsert);
    verify(calendarInsertRequest).execute();
    verify(calendarEvents).insert(googleCalendarId, eventToInsert);
    verify(eventInsertRequest).execute();
}
Also used : CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) Event(com.google.api.services.calendar.model.Event) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) UUID(java.util.UUID) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) Test(org.junit.Test)

Example 9 with CalendarEventModel

use of org.datatransferproject.types.common.models.calendar.CalendarEventModel in project data-transfer-project by google.

the class GoogleCalendarExporter method getCalendarEvents.

private ExportResult<CalendarContainerResource> getCalendarEvents(TokensAndUrlAuthData authData, String id, Optional<PaginationData> pageData) {
    Calendar.Events.List listRequest;
    Events listResult;
    // Get event information
    try {
        listRequest = getOrCreateCalendarInterface(authData).events().list(id).setMaxAttendees(GoogleStaticObjects.MAX_ATTENDEES);
        if (pageData.isPresent()) {
            StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
            Preconditions.checkState(paginationToken.getToken().startsWith(EVENT_TOKEN_PREFIX), "Token is not applicable");
            listRequest.setPageToken(((StringPaginationToken) pageData.get()).getToken().substring(EVENT_TOKEN_PREFIX.length()));
        }
        listResult = listRequest.execute();
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    // Set up continuation data
    PaginationData nextPageData = null;
    if (listResult.getNextPageToken() != null) {
        nextPageData = new StringPaginationToken(EVENT_TOKEN_PREFIX + listResult.getNextPageToken());
    }
    ContinuationData continuationData = new ContinuationData(nextPageData);
    // Process event list
    List<CalendarEventModel> eventModels = new ArrayList<>(listResult.getItems().size());
    for (Event eventData : listResult.getItems()) {
        CalendarEventModel model = convertToCalendarEventModel(id, eventData);
        eventModels.add(model);
    }
    CalendarContainerResource calendarContainerResource = new CalendarContainerResource(null, eventModels);
    // Get result type
    ExportResult.ResultType resultType = ResultType.CONTINUE;
    if (nextPageData == null) {
        resultType = ResultType.END;
    }
    return new ExportResult<>(resultType, calendarContainerResource, continuationData);
}
Also used : PaginationData(org.datatransferproject.types.common.PaginationData) ArrayList(java.util.ArrayList) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) Events(com.google.api.services.calendar.model.Events) Event(com.google.api.services.calendar.model.Event) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Aggregations

CalendarEventModel (org.datatransferproject.types.common.models.calendar.CalendarEventModel)9 CalendarContainerResource (org.datatransferproject.types.common.models.calendar.CalendarContainerResource)6 CalendarModel (org.datatransferproject.types.common.models.calendar.CalendarModel)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4 Event (com.google.api.services.calendar.model.Event)3 IOException (java.io.IOException)3 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)3 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)3 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)3 CalendarAttendeeModel (org.datatransferproject.types.common.models.calendar.CalendarAttendeeModel)3 Events (com.google.api.services.calendar.model.Events)2 UUID (java.util.UUID)2 ExportInformation (org.datatransferproject.types.common.ExportInformation)2 PaginationData (org.datatransferproject.types.common.PaginationData)2 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)2 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)2 Calendar (com.google.api.services.calendar.Calendar)1