private synchronized Calendar makeCalendarInterface(TokensAndUrlAuthData authData) { Credential credential = credentialFactory.createCredential(authData); return new Calendar.Builder( credentialFactory.getHttpTransport(), credentialFactory.getJsonFactory(), credential) .setApplicationName(GoogleStaticObjects.APP_NAME) .build(); } }
@VisibleForTesting void importSingleEvent(UUID jobId, TokensAndUrlAuthData authData, CalendarEventModel eventModel) throws IOException { Event event = convertToGoogleCalendarEvent(eventModel); // calendarMappings better not be null! TempCalendarData calendarMappings = jobStore.findData(jobId, createCacheKey(), TempCalendarData.class); String newCalendarId = calendarMappings.getImportedId(eventModel.getCalendarId()); getOrCreateCalendarInterface(authData) .events() .insert(newCalendarId, event) .execute(); }
@Before public void setup() throws IOException { calendarClient = mock(Calendar.class); calendarCalendars = mock(Calendar.Calendars.class); calendarCalendarList = mock(Calendar.CalendarList.class); calendarListRequest = mock(Calendar.CalendarList.List.class); calendarEvents = mock(Calendar.Events.class); eventListRequest = mock(Calendar.Events.List.class); credentialFactory = mock(GoogleCredentialFactory.class); googleCalendarExporter = new GoogleCalendarExporter(credentialFactory, calendarClient); when(calendarClient.calendars()).thenReturn(calendarCalendars); when(calendarClient.calendarList()).thenReturn(calendarCalendarList); when(calendarCalendarList.list()).thenReturn(calendarListRequest); when(calendarClient.events()).thenReturn(calendarEvents); when(calendarEvents.list(CALENDAR_ID)).thenReturn(eventListRequest); when(eventListRequest.setMaxAttendees(MAX_ATTENDEES)).thenReturn(eventListRequest); verifyZeroInteractions(credentialFactory); }
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); if (listResult.getNextPageToken() != null) { nextPageData = new StringPaginationToken(EVENT_TOKEN_PREFIX + listResult.getNextPageToken()); List<CalendarEventModel> eventModels = new ArrayList<>(listResult.getItems().size()); for (Event eventData : listResult.getItems()) { CalendarEventModel model = convertToCalendarEventModel(id, eventData); eventModels.add(model);
GoogleCalendarImporter.convertToGoogleCalendar(calendarModel); com.google.api.services.calendar.model.Calendar responseCalendar = new com.google.api.services.calendar.model.Calendar().setId(googleCalendarId); 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); verify(calendarCalendars).insert(calendarToInsert); verify(calendarInsertRequest).execute(); verify(calendarEvents).insert(googleCalendarId, eventToInsert); verify(eventInsertRequest).execute();
private static Calendar addCalendar() throws IOException { View.header("Add Calendar"); Calendar entry = new Calendar(); entry.setSummary("Calendar for Testing 3"); Calendar result = client.calendars().insert(entry).execute(); View.display(result); return result; }
@VisibleForTesting void importSingleCalendar(UUID jobId, TokensAndUrlAuthData authData, CalendarModel calendarModel) throws IOException { com.google.api.services.calendar.model.Calendar toInsert = convertToGoogleCalendar( calendarModel); com.google.api.services.calendar.model.Calendar calendarResult = getOrCreateCalendarInterface(authData).calendars().insert(toInsert).execute(); TempCalendarData calendarMappings = jobStore.findData(jobId, createCacheKey(), TempCalendarData.class); if (calendarMappings == null) { calendarMappings = new TempCalendarData(jobId); jobStore.create(jobId, createCacheKey(), calendarMappings); } calendarMappings.addIdMapping(calendarModel.getId(), calendarResult.getId()); jobStore.update(jobId, createCacheKey(), calendarMappings); }
listRequest = getOrCreateCalendarInterface(authData).calendarList().list(); paginationToken.getToken().startsWith(CALENDAR_TOKEN_PREFIX), "Token is not applicable"); listRequest.setPageToken( ((StringPaginationToken) pageData.get()) .getToken() listResult = listRequest.execute(); } catch (IOException e) { return new ExportResult<>(e); if (listResult.getNextPageToken() != null) { nextPageData = new StringPaginationToken(CALENDAR_TOKEN_PREFIX + listResult.getNextPageToken()); 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);
private static Calendar updateCalendar(Calendar calendar) throws IOException { View.header("Update Calendar"); Calendar entry = new Calendar(); entry.setSummary("Updated Calendar for Testing"); Calendar result = client.calendars().patch(calendar.getId(), entry).execute(); View.display(result); return result; }
@Before public void setup() throws IOException { calendarClient = mock(Calendar.class); calendarCalendars = mock(Calendar.Calendars.class); calendarInsertRequest = mock(Calendar.Calendars.Insert.class); calendarEvents = mock(Calendar.Events.class); eventInsertRequest = mock(Calendar.Events.Insert.class); credentialFactory = mock(GoogleCredentialFactory.class); jobStore = new LocalJobStore(); calendarService = new GoogleCalendarImporter(credentialFactory, jobStore, calendarClient); when(calendarClient.calendars()).thenReturn(calendarCalendars); when(calendarClient.events()).thenReturn(calendarEvents); verifyZeroInteractions(credentialFactory); }
eventListResponse.setNextPageToken(NEXT_TOKEN); verify(calendarEvents).list(CALENDAR_ID); verify(eventListRequest).setMaxAttendees(MAX_ATTENDEES); verify(eventListRequest).execute();
calendarListResponse.setNextPageToken(NEXT_TOKEN); verify(calendarClient).calendarList(); verify(calendarCalendarList).list(); verify(calendarListRequest).execute();
private static void showCalendars() throws IOException { View.header("Show Calendars"); CalendarList feed = client.calendarList().list().execute(); View.display(feed); }
@Test public void exportEventSubsequentSet() throws IOException { setUpSingleEventResponse(); // Looking at subsequent page, with no pages after it ContainerResource containerResource = new IdOnlyContainerResource(CALENDAR_ID); PaginationData paginationData = new StringPaginationToken(EVENT_TOKEN_PREFIX + NEXT_TOKEN); ExportInformation exportInformation = new ExportInformation(paginationData, containerResource); eventListResponse.setNextPageToken(null); // Run test ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(UUID.randomUUID(), null, Optional.of(exportInformation)); // Check results // Verify correct methods were called in order InOrder inOrder = Mockito.inOrder(eventListRequest); inOrder.verify(eventListRequest).setPageToken(NEXT_TOKEN); inOrder.verify(eventListRequest).execute(); // Check pagination token ContinuationData continuationData = (ContinuationData) result.getContinuationData(); StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData(); assertThat(paginationToken).isNull(); }
/** Sets up a response with a single calendar, containing a single event */ private void setUpSingleCalendarResponse() throws IOException { setUpSingleEventResponse(); calendarListResponse = new CalendarList().setItems(Collections.singletonList(CALENDAR_LIST_ENTRY)); when(calendarListRequest.execute()).thenReturn(calendarListResponse); }
@Test public void exportCalendarSubsequentSet() throws IOException { setUpSingleCalendarResponse(); // Looking at subsequent page, with no page after it PaginationData paginationData = new StringPaginationToken(CALENDAR_TOKEN_PREFIX + NEXT_TOKEN); ExportInformation exportInformation = new ExportInformation(paginationData, null); calendarListResponse.setNextPageToken(null); // Run test ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(UUID.randomUUID(), null, Optional.of(exportInformation)); // Check results // Verify correct calls were made InOrder inOrder = Mockito.inOrder(calendarListRequest); inOrder.verify(calendarListRequest).setPageToken(NEXT_TOKEN); inOrder.verify(calendarListRequest).execute(); // Check pagination token ContinuationData continuationData = (ContinuationData) result.getContinuationData(); StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData(); assertThat(paginationToken).isNull(); }
private synchronized Calendar makeCalendarInterface(TokensAndUrlAuthData authData) { Credential credential = credentialFactory.createCredential(authData); return new Calendar.Builder( credentialFactory.getHttpTransport(), credentialFactory.getJsonFactory(), credential) .setApplicationName(GoogleStaticObjects.APP_NAME) .build(); }