Search in sources :

Example 6 with FakeIdempotentImportExecutor

use of org.datatransferproject.test.types.FakeIdempotentImportExecutor 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)

Aggregations

FakeIdempotentImportExecutor (org.datatransferproject.test.types.FakeIdempotentImportExecutor)6 Before (org.junit.Before)4 ImportResult (org.datatransferproject.spi.transfer.provider.ImportResult)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Calendar (com.google.api.services.calendar.Calendar)1 Label (com.google.api.services.gmail.model.Label)1 ListLabelsResponse (com.google.api.services.gmail.model.ListLabelsResponse)1 Message (com.google.api.services.gmail.model.Message)1 PeopleService (com.google.api.services.people.v1.PeopleService)1 People (com.google.api.services.people.v1.PeopleService.People)1 CreateContact (com.google.api.services.people.v1.PeopleService.People.CreateContact)1 Person (com.google.api.services.people.v1.model.Person)1 HttpUrl (com.squareup.okhttp.HttpUrl)1 MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)1 RecordedRequest (com.squareup.okhttp.mockwebserver.RecordedRequest)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 OkHttpClient (okhttp3.OkHttpClient)1 Monitor (org.datatransferproject.api.launcher.Monitor)1