package org.osaf.caldav4j.google; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.List; import net.fortuna.ical4j.model.Calendar; import net.fortuna.ical4j.model.Component; import net.fortuna.ical4j.model.ComponentList; import net.fortuna.ical4j.model.Date; import net.fortuna.ical4j.model.DateTime; import net.fortuna.ical4j.model.component.CalendarComponent; import net.fortuna.ical4j.model.component.VEvent; import net.fortuna.ical4j.model.property.DtStart; import net.fortuna.ical4j.model.property.Summary; import net.fortuna.ical4j.model.property.Uid; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.osaf.caldav4j.BaseTestCase; import org.osaf.caldav4j.CalDAVCalendarCollection; import org.osaf.caldav4j.CalDAVConstants; import org.osaf.caldav4j.credential.GCaldavCredential; import org.osaf.caldav4j.exceptions.CalDAV4JException; import org.osaf.caldav4j.exceptions.ResourceNotFoundException; import org.osaf.caldav4j.methods.HttpClient; import org.osaf.caldav4j.util.GenerateQuery; import org.osaf.caldav4j.util.ICalendarUtils; // TODO: migrate use of CalDAVCalendarCollection to CalDAVCollection @Ignore public class GCalDAVCalendarCollectionTest extends BaseTestCase { private static final Log log = LogFactory .getLog(GCalDAVCalendarCollectionTest.class); public static final Integer TEST_TIMEOUT = 3600; public static final boolean TEST_READ = true; public static final boolean TEST_WRITE = true; public static final Integer TEST_VISITS = CalDAVConstants.INFINITY; public static final String TEST_TIMEOUT_UNITS = "Second"; /** * put events on calendar */ @Before public void setUp() throws Exception { caldavCredential = new GCaldavCredential(); super.setUp(); } /** * remove events from calendar */ @After public void tearDown() throws Exception { super.tearDown(); } /** * test: get calendar by UID using REPORT, then checks if SUMMARY matches * @throws Exception */ @Test // XXX this will probably fail public void testGetCalendar() throws Exception { CalDAVCalendarCollection collection = createCalDAVCalendarCollection(); Calendar calendar = null; try { GenerateQuery gq = new GenerateQuery(null, Component.VEVENT + "UID=="+ICS_DAILY_NY_5PM_UID ); log.info(gq.prettyPrint()); calendar = collection.getComponentByQuery(fixture.getHttpClient(), Component.VEVENT, gq.generate()) .get(0); } catch (CalDAV4JException ce) { assertNull(ce); } assertNotNull(calendar); VEvent vevent = ICalendarUtils.getFirstEvent(calendar); assertNotNull(vevent); String summary = ICalendarUtils.getSummaryValue(vevent); assertEquals(ICS_DAILY_NY_5PM_SUMMARY, summary); CalDAV4JException calDAV4JException = null; try { GenerateQuery gq = new GenerateQuery(null, Component.VEVENT + "UID==NON_EXISTENT_RESOURCE"); calendar = collection.getComponentByQuery(fixture.getHttpClient(), Component.VEVENT, gq.generate()) .get(0); } catch (CalDAV4JException ce) { calDAV4JException = ce; } assertNotNull(calDAV4JException); } /** * retrieve calendar by /path/to/resource * @throws Exception */ @Test public void testGetCalendarByPath() throws Exception { CalDAVCalendarCollection calendarCollection = createCalDAVCalendarCollection(); log.info("GET "+ calendarCollection.getCalendarCollectionRoot()+ICS_DAILY_NY_5PM_UID+".ics"); Calendar calendar = null; try { calendar = calendarCollection.getCalendarByPath(fixture.getHttpClient(), ICS_DAILY_NY_5PM_UID+".ics"); } catch (CalDAV4JException ce) { assertNull(ce); } assertNotNull(calendar); VEvent vevent = ICalendarUtils.getFirstEvent(calendar); assertNotNull(vevent); String summary = ICalendarUtils.getSummaryValue(vevent); assertEquals(ICS_DAILY_NY_5PM_SUMMARY, summary); CalDAV4JException calDAV4JException = null; try { calendar = calendarCollection.getCalendarByPath(fixture.getHttpClient(), "NON_EXISTENT_RESOURCE"); } catch (CalDAV4JException ce) { calDAV4JException = ce; } assertNotNull(calDAV4JException); } /** * get VEVENT by date TODO * @throws Exception */ @Test public void testGetEventResources() throws Exception { CalDAVCalendarCollection calendarCollection = createCalDAVCalendarCollection(); Date beginDate = ICalendarUtils.createDateTime(2008, 0, 1, null, true); Date endDate = ICalendarUtils.createDateTime(2008, 8, 1, null, true); List<Calendar> l = calendarCollection.getEventResources(fixture.getHttpClient(), beginDate, endDate); for (Calendar calendar : l) { ComponentList vevents = calendar.getComponents().getComponents( Component.VEVENT); VEvent ve = (VEvent) vevents.get(0); String uid = ICalendarUtils.getUIDValue(ve); int correctNumberOfEvents = -1; if (ICS_DAILY_NY_5PM_UID.equals(uid)) { // one for each day correctNumberOfEvents = 1; } else if (ICS_ALL_DAY_JAN1_UID.equals(uid)) { correctNumberOfEvents = 1; } else if (ICS_NORMAL_PACIFIC_1PM_UID.equals(uid)) { correctNumberOfEvents = 1; } else if (ICS_FLOATING_JAN2_7PM_UID.equals(uid)) { correctNumberOfEvents = 0; } else { fail(uid + " is not the uid of any event that should have been returned"); } assertEquals(correctNumberOfEvents, vevents.size()); } // 3 calendars - one for each resource (not including expanded // recurrences) assertEquals(3, l.size()); } // TODO wait on floating test until we can pass timezones /** * @throws Exception */ @Test @Ignore public void testGetEventResourcesFloatingIssues() throws Exception { CalDAVCalendarCollection calendarCollection = createCalDAVCalendarCollection(); // make sure our 7pm event gets returned Date beginDate = ICalendarUtils.createDateTime(2006, 0, 2, 19, 0, 0, 0, null, true); Date endDate = ICalendarUtils.createDateTime(2006, 0, 2, 20, 1, 0, 0, null, true); List<Calendar> l = calendarCollection.getEventResources(fixture.getHttpClient(), beginDate, endDate); assertTrue(hasEventWithUID(l, ICS_FLOATING_JAN2_7PM_UID)); beginDate = ICalendarUtils.createDateTime(2006, 0, 2, 20, 1, 0, 0, null, true); endDate = ICalendarUtils.createDateTime(2006, 0, 2, 20, 2, 0, 0, null, true); l = calendarCollection .getEventResources(fixture.getHttpClient(), beginDate, endDate); assertFalse(hasEventWithUID(l, ICS_FLOATING_JAN2_7PM_UID)); } /** * @throws Exception */ @Test public void testAddNewRemove() throws Exception { String newUid = "NEW_UID"; String newEvent = "NEW_EVENT"; VEvent ve = new VEvent(); DtStart dtStart = new DtStart(new DateTime()); Summary summary = new Summary(newEvent); Uid uid = new Uid(newUid); ve.getProperties().add(dtStart); ve.getProperties().add(summary); ve.getProperties().add(uid); CalDAVCalendarCollection calendarCollection = createCalDAVCalendarCollection(); try { calendarCollection.addEvent(fixture.getHttpClient(), ve, null); } catch (CalDAV4JException e){ fail("It's probably a google issue. See http://code.google.com/p/google-caldav-issues/issues/detail?id=26#c25"); } Calendar calendar = calendarCollection.getCalendarByPath(fixture.getHttpClient(), newUid+".ics"); assertNotNull(calendar); calendarCollection.deleteEvent(fixture.getHttpClient(), newUid); calendar = null; try { calendar = calendarCollection.getCalendarByPath(fixture.getHttpClient(), newUid+".ics"); } catch (ResourceNotFoundException e) { } assertNull(calendar); } /** * @throws Exception */ @Test public void testUpdateEvent() throws Exception { CalDAVCalendarCollection calendarCollection = createCalDAVCalendarCollection(); Calendar calendar = calendarCollection.getCalendarForEventUID( fixture.getHttpClient(), ICS_NORMAL_PACIFIC_1PM_UID); VEvent ve = ICalendarUtils.getFirstEvent(calendar); // sanity! assertNotNull(calendar); assertEquals(ICS_NORMAL_PACIFIC_1PM_SUMMARY, ICalendarUtils .getSummaryValue(ve)); ICalendarUtils.addOrReplaceProperty(ve, new Summary("NEW")); calendarCollection.updateMasterEvent(fixture.getHttpClient(), ve, null); calendar = calendarCollection.getCalendarForEventUID(fixture.getHttpClient(), ICS_NORMAL_PACIFIC_1PM_UID); ve = ICalendarUtils.getFirstEvent(calendar); assertEquals("NEW", ICalendarUtils.getSummaryValue(ve)); } /** * do a calendar-multiget with a valid event and an invalid one */ @Test public void testMultigetCalendar() throws Exception { CalDAVCalendarCollection calendarCollection = createCalDAVCalendarCollection(); final String baseUri = caldavCredential.protocol +"://" + caldavCredential.host+":" +caldavCredential.port +fixture.getCollectionPath(); List <String> calendarUris = new ArrayList<String>(); calendarUris.add( baseUri +"/"+BaseTestCase.ICS_ALL_DAY_JAN1_UID+".ics"); calendarUris.add( baseUri +"/"+BaseTestCase.CALDAV_SERVER_BAD_USERNAME); List<Calendar> calendarList = calendarCollection.multigetCalendarUris(fixture.getHttpClient(), calendarUris ); // sanity assertNotNull(calendarList); assertEquals( ICS_ALL_DAY_JAN1_UID, ICalendarUtils.getUIDValue(calendarList.get(0).getComponent(CalendarComponent.VEVENT)) ); } /** * @throws Exception */ @Test @Ignore // google doesn't support Tickets public void testTicket() throws Exception { CalDAVCalendarCollection calendarCollection = createCalDAVCalendarCollection(); // Create the Ticket String ticketID = calendarCollection.createTicket(fixture.getHttpClient(), BaseTestCase.ICS_DAILY_NY_5PM, CalDAVConstants.INFINITY, TEST_TIMEOUT, TEST_READ, TEST_WRITE); assertNotNull(ticketID); // Make sure ticket is there List<String> ticketIDs = calendarCollection.getTicketsIDs(fixture.getHttpClient(), BaseTestCase.ICS_DAILY_NY_5PM); assertEquals("Number of IDs Returned from getTickets:", ticketIDs .size(), 1); // Setup a HttpClient with no username or password HttpClient badHttpClient = createHttpClientWithNoCredentials(); // Set the ticket badHttpClient.setTicket(ticketID); // Attempt to get the Calendar Calendar calendar = calendarCollection.getCalendarByPath(badHttpClient, ICS_DAILY_NY_5PM); assertNotNull(calendar); // Attempt to delete the Tickets calendarCollection.deleteTicket(fixture.getHttpClient(), BaseTestCase.ICS_DAILY_NY_5PM, ticketID); // Make sure ticket is gone CalDAV4JException calDAV4JException = null; try { calendar = calendarCollection.getCalendarByPath(badHttpClient, ICS_DAILY_NY_5PM); } catch (CalDAV4JException ce) { calDAV4JException = ce; } assertNotNull(calDAV4JException); } private boolean hasEventWithUID(List<Calendar> cals, String uid) { for (Calendar cal : cals) { ComponentList vEvents = cal.getComponents().getComponents( Component.VEVENT); if (vEvents.size() == 0) { return false; } VEvent ve = (VEvent) vEvents.get(0); String curUid = ICalendarUtils.getUIDValue(ve); if (curUid != null && uid.equals(curUid)) { return true; } } return false; } private CalDAVCalendarCollection createCalDAVCalendarCollection() { CalDAVCalendarCollection calendarCollection = new CalDAVCalendarCollection( fixture.getCollectionPath(), createHostConfiguration(), fixture.getMethodFactory(), CalDAVConstants.PROC_ID_DEFAULT); return calendarCollection; } }