Search in sources :

Example 96 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManagerTest method testConcurrentAddUpdateEvent.

/**
 * Test concurrent add/update event with two threads and code-point to control concurrency.
 */
@Test
public void testConcurrentAddUpdateEvent() {
    final String TEST_EVENT_ID_1 = "id-testConcurrentAddUpdateEvent-1";
    final String TEST_EVENT_SUBJECT_1 = "testEvent1";
    final String TEST_EVENT_ID_2 = "id-testConcurrentAddUpdateEvent-2";
    final String TEST_EVENT_SUBJECT_2 = "testEvent2";
    final String TEST_EVENT_SUBJECT_2_UPDATED = "testUpdatedEvent2";
    final Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-3-");
    final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
    final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));
    // Generate event for update
    Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
    calendarManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
    cal = calendarManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2, null);
    assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, event2);
    assertEquals("Wrong calendar-event subject", event2.getSubject(), TEST_EVENT_SUBJECT_2);
    log.info("testConcurrentAddUpdateEvent thread2 addEvent2 DONE");
    final CountDownLatch doneSignal = new CountDownLatch(2);
    // thread 1
    Thread thread1 = new Thread() {

        public void run() {
            try {
                // 1. load calendar
                Kalendar currentCalendar = calendarManager.getPersonalCalendar(test).getKalendar();
                // 2. add Event1 => breakpoint hit
                log.info("testConcurrentAddUpdateEvent thread1 addEvent1");
                calendarManager.addEventTo(currentCalendar, new KalendarEvent(TEST_EVENT_ID_1, TEST_EVENT_SUBJECT_1, new Date(), 1));
                log.info("testConcurrentAddUpdateEvent thread1 addEvent1 DONE");
                // 3. check event1 exist
                currentCalendar = calendarManager.getPersonalCalendar(test).getKalendar();
                KalendarEvent event1 = currentCalendar.getEvent(TEST_EVENT_ID_1, null);
                assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
                assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
                // 4. sleep 2sec
                // 5. check event1 still exist (event2 added in meantime)
                currentCalendar = calendarManager.getPersonalCalendar(test).getKalendar();
                event1 = currentCalendar.getEvent(TEST_EVENT_ID_1, null);
                assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
                assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
                statusList.add(Boolean.TRUE);
                log.info("testConcurrentAddUpdateEvent thread1 finished");
            } catch (Exception ex) {
                // no exception should happen
                exceptionHolder.add(ex);
            } finally {
                doneSignal.countDown();
                DBFactory.getInstance().commitAndCloseSession();
            }
        }
    };
    // thread 2
    Thread thread2 = new Thread() {

        public void run() {
            try {
                Kalendar calendar = calendarManager.getPersonalCalendar(test).getKalendar();
                // 3. add Event2 (breakpoint of thread1 blocks)
                log.info("testConcurrentAddUpdateEvent thread2 updateEvent2");
                calendarManager.updateEventFrom(calendar, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
                log.info("testConcurrentAddUpdateEvent thread1 updateEvent2 DONE");
                // 4. check event2 exist
                calendar = calendarManager.getPersonalCalendar(test).getKalendar();
                KalendarEvent updatedEvent = calendar.getEvent(TEST_EVENT_ID_2, null);
                assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, updatedEvent);
                assertEquals("Wrong calendar-event subject", updatedEvent.getSubject(), TEST_EVENT_SUBJECT_2_UPDATED);
                // 5. check event1 exist
                calendar = calendarManager.getPersonalCalendar(test).getKalendar();
                KalendarEvent event1 = calendar.getEvent(TEST_EVENT_ID_1, null);
                assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
                assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
                // Delete Event
                boolean removed = calendarManager.removeEventFrom(calendar, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
                assertTrue(removed);
                statusList.add(Boolean.TRUE);
                log.info("testConcurrentAddUpdateEvent thread2 finished");
            } catch (Exception ex) {
                // no exception should happen
                exceptionHolder.add(ex);
            } finally {
                doneSignal.countDown();
                DBFactory.getInstance().commitAndCloseSession();
            }
        }
    };
    thread1.start();
    thread2.start();
    try {
        boolean interrupt = doneSignal.await(10, TimeUnit.SECONDS);
        assertTrue("Test takes too long (more than 10s)", interrupt);
    } catch (InterruptedException e) {
        fail("" + e.getMessage());
    }
    // if not -> they are in deadlock and the db did not detect it
    for (Exception exception : exceptionHolder) {
        log.info("exception: " + exception.getMessage());
        exception.printStackTrace();
    }
    assertTrue("It throws an exception in test => see sysout", exceptionHolder.isEmpty());
    log.info("testConcurrentAddUpdateEvent finish successful");
}
Also used : KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ValidationException(net.fortuna.ical4j.model.ValidationException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Date(java.util.Date) Kalendar(org.olat.commons.calendar.model.Kalendar) Identity(org.olat.core.id.Identity) CalendarImportTest(org.olat.commons.calendar.CalendarImportTest) Test(org.junit.Test)

Example 97 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManagerTest method testTodayEvent.

/**
 * Check a NPE
 * @throws IOException
 */
@Test
public void testTodayEvent() throws IOException {
    Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-3-");
    Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
    // 1. Test Today Event
    String eventId = "today-" + UUID.randomUUID();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    Date start = calendar.getTime();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    Date end = calendar.getTime();
    KalendarEvent testEvent = new KalendarEvent(eventId, null, "Today Event", start, end);
    calendarManager.addEventTo(cal, testEvent);
    // Next days event
    String nextEventId = "next-" + UUID.randomUUID();
    calendar = Calendar.getInstance();
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.add(Calendar.DATE, 3);
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    Date nextStart = calendar.getTime();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    Date nextEnd = calendar.getTime();
    KalendarEvent nextEvent = new KalendarEvent(nextEventId, null, "Next Event", nextStart, nextEnd);
    calendarManager.addEventTo(cal, nextEvent);
    // 2. reload and test
    emptyCalendarCache();
    KalendarEvent reloadedEvent = calendarManager.getPersonalCalendar(test).getKalendar().getEvent(eventId, null);
    Assert.assertNotNull(reloadedEvent);
    Assert.assertEquals("Today Event", reloadedEvent.getSubject());
    Assert.assertEquals(start, reloadedEvent.getBegin());
    Assert.assertEquals(end, reloadedEvent.getEnd());
    Assert.assertTrue(reloadedEvent.isToday());
    Assert.assertTrue(reloadedEvent.isWithinOneDay());
    Assert.assertFalse(reloadedEvent.isAllDayEvent());
    KalendarEvent reloadedNextEvent = calendarManager.getPersonalCalendar(test).getKalendar().getEvent(nextEventId, null);
    Assert.assertNotNull(reloadedNextEvent);
    Assert.assertEquals("Next Event", reloadedNextEvent.getSubject());
    Assert.assertEquals(nextStart, reloadedNextEvent.getBegin());
    Assert.assertEquals(nextEnd, reloadedNextEvent.getEnd());
    Assert.assertFalse(reloadedNextEvent.isToday());
    Assert.assertTrue(reloadedNextEvent.isWithinOneDay());
    Assert.assertFalse(reloadedNextEvent.isAllDayEvent());
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) Calendar(java.util.Calendar) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) Identity(org.olat.core.id.Identity) Date(java.util.Date) CalendarImportTest(org.olat.commons.calendar.CalendarImportTest) Test(org.junit.Test)

Example 98 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManagerTest method testCalendarRecurringEventInversed.

/**
 * A recurring event with the start and end date reversed. This error
 * in the calendar cause the whole calendar to crash.
 *
 * @throws URISyntaxException
 * @throws IOException
 */
@Test
public void testCalendarRecurringEventInversed() throws URISyntaxException, IOException {
    Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ur1-");
    URL calendarUrl = CalendarImportTest.class.getResource("ReversedRecurringEvent.ics");
    File calendarFile = new File(calendarUrl.toURI());
    String calendarName = UUID.randomUUID().toString().replace("-", "");
    KalendarRenderWrapper importedCalendar = importCalendarManager.importCalendar(test, calendarName, CalendarManager.TYPE_USER, calendarFile);
    List<KalendarEvent> events = importedCalendar.getKalendar().getEvents();
    Assert.assertEquals(1, events.size());
    Calendar cal = Calendar.getInstance();
    cal.set(2018, 03, 10, 10, 00);
    Date startDate = cal.getTime();
    cal.set(2018, 03, 17);
    Date endDate = cal.getTime();
    List<KalendarEvent> recurringEvents = calendarManager.getEvents(importedCalendar.getKalendar(), startDate, endDate, true);
    Assert.assertEquals(0, recurringEvents.size());
}
Also used : Calendar(java.util.Calendar) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) Identity(org.olat.core.id.Identity) KalendarRenderWrapper(org.olat.commons.calendar.ui.components.KalendarRenderWrapper) File(java.io.File) URL(java.net.URL) Date(java.util.Date) CalendarImportTest(org.olat.commons.calendar.CalendarImportTest) Test(org.junit.Test)

Example 99 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManagerTest method updateCalendar.

@Test
public void updateCalendar() throws ValidationException, IOException {
    Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("u1-");
    Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
    String eventIdMarker = "id-marker-event";
    // 1. Add a standard event, not managed
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MILLISECOND, 0);
    Date start = calendar.getTime();
    calendar.add(Calendar.HOUR_OF_DAY, 1);
    Date end = calendar.getTime();
    KalendarEvent markerEvent = new KalendarEvent(eventIdMarker, null, "markerEvent", start, end);
    calendarManager.addEventTo(cal, markerEvent);
    // 2. Update with a first calendar
    Kalendar baseCalendar = calendarManager.createCalendar("user", "first-update");
    String eventId1 = "event-1";
    KalendarEvent event1 = new KalendarEvent(eventId1, null, "event 1", start, end);
    baseCalendar.addEvent(event1);
    String eventId2 = "event-2";
    KalendarEvent event2 = new KalendarEvent(eventId2, null, "event 2", start, end);
    baseCalendar.addEvent(event2);
    calendarManager.updateCalendar(cal, baseCalendar);
    // 3. Update with a second calendar
    Kalendar updateCalendar = calendarManager.createCalendar("user", "first-update");
    KalendarEvent event1alt = new KalendarEvent(eventId1, null, "event 1 alt", start, end);
    updateCalendar.addEvent(event1alt);
    String eventId3 = "event-3";
    KalendarEvent event3 = new KalendarEvent(eventId3, null, "event 3", start, end);
    updateCalendar.addEvent(event3);
    calendarManager.updateCalendar(cal, updateCalendar);
    // Check
    emptyCalendarCache();
    Kalendar reloadedCal = calendarManager.getPersonalCalendar(test).getKalendar();
    KalendarEvent marker = reloadedCal.getEvent(eventIdMarker, null);
    Assert.assertNotNull(marker);
    Assert.assertEquals("markerEvent", marker.getSubject());
    KalendarEvent reloaded1 = reloadedCal.getEvent(eventId1, null);
    Assert.assertNotNull(reloaded1);
    Assert.assertEquals("event 1 alt", reloaded1.getSubject());
    KalendarEvent reloaded2 = reloadedCal.getEvent(eventId2, null);
    Assert.assertNotNull(reloaded2);
    Assert.assertEquals("event 2", reloaded2.getSubject());
    KalendarEvent reloaded3 = reloadedCal.getEvent(eventId3, null);
    Assert.assertNotNull(reloaded3);
    Assert.assertEquals("event 3", reloaded3.getSubject());
}
Also used : Kalendar(org.olat.commons.calendar.model.Kalendar) Calendar(java.util.Calendar) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) Identity(org.olat.core.id.Identity) Date(java.util.Date) CalendarImportTest(org.olat.commons.calendar.CalendarImportTest) Test(org.junit.Test)

Example 100 with KalendarEvent

use of org.olat.commons.calendar.model.KalendarEvent in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManagerTest method testListEventsForPeriodWithoutDTEndEvent.

/**
 * Check a NPE
 * @throws IOException
 */
@Test
public void testListEventsForPeriodWithoutDTEndEvent() throws IOException {
    // replace the standard calendar with a forged one
    Identity identity = JunitTestHelper.createAndPersistIdentityAsRndUser("cal-test-1-");
    File calendarFile = calendarManager.getCalendarFile("user", identity.getName());
    if (calendarFile.exists()) {
        calendarFile.delete();
    }
    File newCalendarFile = new File(calendarFile.getParentFile(), calendarFile.getName());
    InputStream in = CalendarImportTest.class.getResourceAsStream("cal_without_dtend.ics");
    FileUtils.copyInputStreamToFile(in, newCalendarFile);
    // to be sure
    emptyCalendarCache();
    // load the calendar
    KalendarRenderWrapper reloadCalWrapper = calendarManager.getPersonalCalendar(identity);
    // check if its the right calendar
    Collection<KalendarEvent> events = reloadCalWrapper.getKalendar().getEvents();
    Assert.assertNotNull(events);
    Assert.assertEquals(1, events.size());
    KalendarEvent event = events.iterator().next();
    Assert.assertEquals("Arbeitszeit: 1-3h", event.getSubject());
    Assert.assertEquals("e73iiu9masoddi4g0vllmi2ht0@google.com", event.getID());
    Assert.assertNull(event.getEnd());
    // test persist
    boolean allOk = calendarManager.persistCalendar(reloadCalWrapper.getKalendar());
    Assert.assertTrue(allOk);
    // an other possible RS
    // within period
    Date periodStart = CalendarUtils.getDate(2010, Calendar.MARCH, 15);
    Date periodEnd = CalendarUtils.getDate(2010, Calendar.MARCH, 20);
    List<KalendarEvent> eventsForPeriod = calendarManager.getEvents(reloadCalWrapper.getKalendar(), periodStart, periodEnd, true);
    Assert.assertNotNull(eventsForPeriod);
    Assert.assertEquals(0, eventsForPeriod.size());
    KalendarEvent eventForPeriod = events.iterator().next();
    Assert.assertEquals("e73iiu9masoddi4g0vllmi2ht0@google.com", eventForPeriod.getID());
    // out of scope
    Date periodStart2 = CalendarUtils.getDate(2008, Calendar.APRIL, 15);
    Date periodEnd2 = CalendarUtils.getDate(2008, Calendar.APRIL, 17);
    List<KalendarEvent> eventsOutOfPeriod = calendarManager.getEvents(reloadCalWrapper.getKalendar(), periodStart2, periodEnd2, true);
    Assert.assertNotNull(eventsOutOfPeriod);
    Assert.assertTrue(eventsOutOfPeriod.isEmpty());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) Identity(org.olat.core.id.Identity) KalendarRenderWrapper(org.olat.commons.calendar.ui.components.KalendarRenderWrapper) File(java.io.File) Date(java.util.Date) CalendarImportTest(org.olat.commons.calendar.CalendarImportTest) Test(org.junit.Test)

Aggregations

KalendarEvent (org.olat.commons.calendar.model.KalendarEvent)172 Date (java.util.Date)80 Kalendar (org.olat.commons.calendar.model.Kalendar)68 Test (org.junit.Test)48 KalendarRenderWrapper (org.olat.commons.calendar.ui.components.KalendarRenderWrapper)46 Identity (org.olat.core.id.Identity)46 ArrayList (java.util.ArrayList)42 Calendar (java.util.Calendar)40 CalendarImportTest (org.olat.commons.calendar.CalendarImportTest)34 CalendarManager (org.olat.commons.calendar.CalendarManager)32 OLATResourceable (org.olat.core.id.OLATResourceable)20 CalendarGUIModifiedEvent (org.olat.commons.calendar.ui.events.CalendarGUIModifiedEvent)18 File (java.io.File)16 ExDate (net.fortuna.ical4j.model.property.ExDate)16 ICourse (org.olat.course.ICourse)16 URI (java.net.URI)14 HttpResponse (org.apache.http.HttpResponse)14 URL (java.net.URL)12 KalendarRecurEvent (org.olat.commons.calendar.model.KalendarRecurEvent)12 EventVO (org.olat.commons.calendar.restapi.EventVO)12