/**
   * uses getCalendar(fixture.getHttpClient(), query)
   *
   * @throws Exception
   */
  @Test
  public void testGetEventResources() throws Exception {
    Date beginDate = ICalendarUtils.createDateTime(2006, 0, 1, null, true);
    Date endDate = ICalendarUtils.createDateTime(2006, 11, 9, null, true);
    List<Calendar> l = collection.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());
  }
  @Test
  public void testReportCalendarWithTimezone() throws Exception {

    GenerateQuery gq = new GenerateQuery();
    gq.setComponent("VEVENT :");
    CalendarQuery query = gq.generate();

    Calendar calendar = null;
    try {
      calendar = collection.getCalendarForEventUID(fixture.getHttpClient(), ICS_DAILY_NY_5PM_UID);
    } 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 =
          collection.getCalendarForEventUID(fixture.getHttpClient(), "NON_EXISTENT_RESOURCE");
    } catch (CalDAV4JException ce) {
      calDAV4JException = ce;
    }

    assertNotNull(calDAV4JException);
  }
  @Test
  public void testGetCalendarByPath() throws Exception {
    Calendar calendar = null;
    try {
      calendar =
          uncachedCollection.getCalendar(
              fixture.getHttpClient(), ICS_GOOGLE_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 = uncachedCollection.getCalendar(fixture.getHttpClient(), "NON_EXISTENT_RESOURCE");
    } catch (CalDAV4JException ce) {
      calDAV4JException = ce;
    }

    assertNotNull(calDAV4JException);
  }
  /** do a calendar-multiget with a valid event and an invalid one */
  @Test
  public void testMultigetCalendar() throws Exception {

    final String baseUri =
        caldavCredential.protocol
            + "://"
            + caldavCredential.host
            + ":"
            + caldavCredential.port
            + caldavCredential.home
            + "/"
            + caldavCredential.collection;

    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 =
        uncachedCollection.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
  public void testGetEventResourcesFloatingIssues() throws Exception {

    // 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 =
        uncachedCollection.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 = uncachedCollection.getEventResources(fixture.getHttpClient(), beginDate, endDate);
    assertFalse(hasEventWithUID(l, ICS_FLOATING_JAN2_7PM_UID));
  }
  // get a Calendar by uid, then by summary, then by recurrence-id
  @Test
  public void queryCalendar() throws CalDAV4JException {
    Calendar calendar = null;
    GenerateQuery gq = new GenerateQuery();

    // query by uid
    calendar =
        collection.queryCalendar(
            fixture.getHttpClient(), Component.VEVENT, ICS_GOOGLE_DAILY_NY_5PM_UID, null);
    assertNotNull(calendar);

    // check if is cache
    assertNotNull(collection.getCache().getHrefForEventUID(ICS_GOOGLE_DAILY_NY_5PM_UID));

    // query by SUMMARY
    calendar = null;
    gq.setFilter("VEVENT : SUMMARY==" + ICS_GOOGLE_NORMAL_PACIFIC_1PM_SUMMARY);
    List<Calendar> calendars = collection.queryCalendars(fixture.getHttpClient(), gq.generate());
    assertNotNull(calendars);
    assertEquals("non unique result", calendars.size(), 1);
    calendar = calendars.get(0);
    assertEquals(ICalendarUtils.getUIDValue(calendar), ICS_GOOGLE_NORMAL_PACIFIC_1PM_UID);
    // check if is in cache

  }
  /** @throws Exception */
  @Test
  public void testUpdateEvent() throws Exception {

    Calendar calendar =
        collection.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"));

    collection.updateMasterEvent(fixture.getHttpClient(), ve, null);

    calendar =
        collection.getCalendarForEventUID(fixture.getHttpClient(), ICS_NORMAL_PACIFIC_1PM_UID);

    ve = ICalendarUtils.getFirstEvent(calendar);
    assertEquals("NEW", ICalendarUtils.getSummaryValue(ve));
  }
  // TODO: this is work in progress; see issue 48
  @Ignore
  @Test
  public void queryPartialCalendar() throws CalDAV4JException {
    Calendar calendar = null;
    GenerateQuery gq = new GenerateQuery();

    // query by UID in a given timerange
    calendar = null;
    gq.setFilter("VEVENT : UID==" + ICS_GOOGLE_DAILY_NY_5PM_UID);
    gq.setRecurrenceSet("20060101T170000Z", "20060103T230000Z", CalendarData.EXPAND);

    List<Calendar> calendars = collection.queryCalendars(fixture.getHttpClient(), gq.generate());
    assertNotNull(calendars);
    assertEquals("bad number of responses: ", 3, calendars.size());
    for (Calendar c : calendars) {
      assertEquals(ICalendarUtils.getUIDValue(calendar), ICS_GOOGLE_DAILY_NY_5PM_UID);
      assertNotNull(
          ICalendarUtils.getPropertyValue(
              c.getComponent(Component.VEVENT), Property.RECURRENCE_ID));
    }
    // check if is in cache

  }
  //
  // private
  //
  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;
  }