Search in sources :

Example 1 with CalendarEventModel

use of org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel 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);
}
Also used : ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) ContainerResource(org.dataportabilityproject.types.transfer.models.ContainerResource) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Test(org.junit.Test)

Example 2 with CalendarEventModel

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

Example 3 with CalendarEventModel

use of org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel 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);
}
Also used : TempCalendarData(org.dataportabilityproject.spi.transfer.types.TempCalendarData) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) Event(com.google.api.services.calendar.model.Event) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) UUID(java.util.UUID) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) Test(org.junit.Test)

Example 4 with CalendarEventModel

use of org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel 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);
}
Also used : HashMap(java.util.HashMap) Request(okhttp3.Request) ArrayList(java.util.ArrayList) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) IOException(java.io.IOException) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 5 with CalendarEventModel

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

the class MicrosoftCalendarImporter method importItem.

@SuppressWarnings("unchecked")
@Override
public ImportResult importItem(UUID jobId, TokenAuthData authData, CalendarContainerResource data) {
    TempCalendarData calendarMappings = jobStore.findData(TempCalendarData.class, jobId);
    if (calendarMappings == null) {
        calendarMappings = new TempCalendarData(jobId);
        jobStore.create(jobId, calendarMappings);
    }
    Map<String, String> requestIdToExportedId = new HashMap<>();
    List<String> problems = new ArrayList<>();
    int requestId = 1;
    List<Map<String, Object>> calendarRequests = new ArrayList<>();
    for (CalendarModel calendar : data.getCalendars()) {
        Map<String, Object> request = createRequestItem(calendar, requestId, CALENDAR_SUBPATH, problems);
        requestIdToExportedId.put(String.valueOf(requestId), calendar.getId());
        requestId++;
        calendarRequests.add(request);
    }
    if (!problems.isEmpty()) {
    // TODO log problems
    }
    RequestHelper.BatchResponse calendarResponse = RequestHelper.batchRequest(authData, calendarRequests, baseUrl, client, objectMapper);
    if (ImportResult.ResultType.OK != calendarResponse.getResult().getType()) {
        // TODO log problems
        return calendarResponse.getResult();
    }
    List<Map<String, Object>> batchResponses = calendarResponse.getBatchResponse();
    for (Map<String, Object> batchResponse : batchResponses) {
        String batchRequestId = (String) batchResponse.get("id");
        if (batchRequestId == null) {
            problems.add("Null request id returned by batch response");
            continue;
        }
        Integer status = (Integer) batchResponse.get("status");
        if (status == null || 201 != status) {
            problems.add("Error creating calendar: " + batchRequestId);
            continue;
        }
        Map<String, Object> body = (Map<String, Object>) batchResponse.get("body");
        if (body == null) {
            problems.add("Invalid body returned from batch calendar create: " + batchRequestId);
            continue;
        }
        calendarMappings.addIdMapping(requestIdToExportedId.get(batchRequestId), (String) body.get("id"));
    }
    jobStore.update(jobId, calendarMappings);
    List<Map<String, Object>> eventRequests = new ArrayList<>();
    requestId = 1;
    for (CalendarEventModel event : data.getEvents()) {
        String importedId = calendarMappings.getImportedId(event.getCalendarId());
        Map<String, Object> request = createRequestItem(event, requestId, String.format(EVENT_SUBPATH, importedId), problems);
        requestId++;
        eventRequests.add(request);
    }
    if (!problems.isEmpty()) {
    // TODO log problems
    }
    RequestHelper.BatchResponse eventResponse = RequestHelper.batchRequest(authData, eventRequests, baseUrl, client, objectMapper);
    if (ImportResult.ResultType.OK != eventResponse.getResult().getType()) {
        // TODO log problems
        return eventResponse.getResult();
    }
    return eventResponse.getResult();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) TempCalendarData(org.dataportabilityproject.spi.transfer.types.TempCalendarData) RequestHelper(org.dataportabilityproject.transfer.microsoft.common.RequestHelper) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

CalendarEventModel (org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel)9 CalendarContainerResource (org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource)6 CalendarModel (org.dataportabilityproject.types.transfer.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.dataportabilityproject.spi.transfer.provider.ExportResult)3 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)3 StringPaginationToken (org.dataportabilityproject.spi.transfer.types.StringPaginationToken)3 CalendarAttendeeModel (org.dataportabilityproject.types.transfer.models.calendar.CalendarAttendeeModel)3 Events (com.google.api.services.calendar.model.Events)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 ExportInformation (org.dataportabilityproject.spi.transfer.types.ExportInformation)2 IdOnlyContainerResource (org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource)2 PaginationData (org.dataportabilityproject.spi.transfer.types.PaginationData)2 TempCalendarData (org.dataportabilityproject.spi.transfer.types.TempCalendarData)2