Search in sources :

Example 6 with CalendarVO

use of org.olat.commons.calendar.restapi.CalendarVO in project openolat by klemens.

the class CalendarTest method testOutputGetCalendarEvents.

@Test
public void testOutputGetCalendarEvents() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    URI uri = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).path("calendars").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    List<CalendarVO> vos = parseArray(response);
    assertNotNull(vos);
    // Root-1
    assertTrue(2 <= vos.size());
    CalendarVO calendar = getCourseCalendar(vos, course1);
    // get events and output as JSON
    URI eventUri = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).path("calendars").path(calendar.getId()).path("events").build();
    HttpGet eventMethod = conn.createGet(eventUri, MediaType.APPLICATION_JSON, true);
    HttpResponse eventResponse = conn.execute(eventMethod);
    assertEquals(200, eventResponse.getStatusLine().getStatusCode());
    String outputJson = EntityUtils.toString(eventResponse.getEntity());
    System.out.println("*** JSON");
    System.out.println(outputJson);
    // get events and output as XML
    URI eventXmlUri = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).path("calendars").path(calendar.getId()).path("events").build();
    HttpGet eventXmlMethod = conn.createGet(eventXmlUri, MediaType.APPLICATION_XML, true);
    HttpResponse eventXmlResponse = conn.execute(eventXmlMethod);
    assertEquals(200, eventXmlResponse.getStatusLine().getStatusCode());
    String outputXml = EntityUtils.toString(eventXmlResponse.getEntity());
    System.out.println("*** XML");
    System.out.println(outputXml);
    conn.shutdown();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CalendarVO(org.olat.commons.calendar.restapi.CalendarVO) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 7 with CalendarVO

use of org.olat.commons.calendar.restapi.CalendarVO in project OpenOLAT by OpenOLAT.

the class CalendarTest method testGetCalendarEvents_paging.

@Test
public void testGetCalendarEvents_paging() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    URI uri = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).path("calendars").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    List<CalendarVO> vos = parseArray(response);
    assertNotNull(vos);
    // course1 + personal
    assertTrue(2 <= vos.size());
    CalendarVO calendar = getCourseCalendar(vos, course1);
    URI eventUri = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).path("calendars").path(calendar.getId()).path("events").queryParam("start", "0").queryParam("limit", "5").queryParam("onlyFuture", "true").build();
    HttpGet eventMethod = conn.createGet(eventUri, MediaType.APPLICATION_JSON + ";pagingspec=1.0", true);
    HttpResponse eventResponse = conn.execute(eventMethod);
    assertEquals(200, eventResponse.getStatusLine().getStatusCode());
    EventVOes events = conn.parse(eventResponse, EventVOes.class);
    assertNotNull(events);
    assertEquals(10, events.getTotalCount());
    assertNotNull(events.getEvents());
    assertEquals(5, events.getEvents().length);
    conn.shutdown();
}
Also used : EventVOes(org.olat.commons.calendar.restapi.EventVOes) HttpGet(org.apache.http.client.methods.HttpGet) CalendarVO(org.olat.commons.calendar.restapi.CalendarVO) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 8 with CalendarVO

use of org.olat.commons.calendar.restapi.CalendarVO in project OpenOLAT by OpenOLAT.

the class CalendarTest method putCalendarEvents_notAuthorized.

@Test
public void putCalendarEvents_notAuthorized() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id2.getName(), "A6B7C8"));
    URI calUri = UriBuilder.fromUri(getContextURI()).path("users").path(id2.getKey().toString()).path("calendars").build();
    HttpGet calMethod = conn.createGet(calUri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(calMethod);
    assertEquals(200, response.getStatusLine().getStatusCode());
    List<CalendarVO> vos = parseArray(response);
    assertNotNull(vos);
    assertTrue(2 <= vos.size());
    CalendarVO calendarCourse_1 = getCourseCalendar(vos, course1);
    // create an event
    EventVO event = new EventVO();
    Calendar cal = Calendar.getInstance();
    event.setBegin(cal.getTime());
    cal.add(Calendar.HOUR_OF_DAY, 1);
    event.setEnd(cal.getTime());
    String subject = UUID.randomUUID().toString();
    event.setSubject(subject);
    URI eventUri = UriBuilder.fromUri(getContextURI()).path("users").path(id2.getKey().toString()).path("calendars").path(calendarCourse_1.getId()).path("event").build();
    HttpPut putEventMethod = conn.createPut(eventUri, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(putEventMethod, event);
    HttpResponse putEventResponse = conn.execute(putEventMethod);
    assertEquals(401, putEventResponse.getStatusLine().getStatusCode());
    EntityUtils.consume(putEventResponse.getEntity());
    conn.shutdown();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) Calendar(java.util.Calendar) CalendarVO(org.olat.commons.calendar.restapi.CalendarVO) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) EventVO(org.olat.commons.calendar.restapi.EventVO) Test(org.junit.Test)

Example 9 with CalendarVO

use of org.olat.commons.calendar.restapi.CalendarVO in project OpenOLAT by OpenOLAT.

the class CalendarTest method testGetCalendarEvents_onlyFuture.

@Test
public void testGetCalendarEvents_onlyFuture() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    URI uri = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).path("calendars").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    List<CalendarVO> vos = parseArray(response);
    assertNotNull(vos);
    // course1 + personal
    assertTrue(2 <= vos.size());
    CalendarVO calendar = getCourseCalendar(vos, course1);
    URI eventUri = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).path("calendars").path(calendar.getId()).path("events").queryParam("onlyFuture", "true").build();
    HttpGet eventMethod = conn.createGet(eventUri, MediaType.APPLICATION_JSON, true);
    HttpResponse eventResponse = conn.execute(eventMethod);
    assertEquals(200, eventResponse.getStatusLine().getStatusCode());
    List<EventVO> events = parseEventArray(eventResponse);
    assertNotNull(events);
    assertEquals(10, events.size());
    conn.shutdown();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CalendarVO(org.olat.commons.calendar.restapi.CalendarVO) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) EventVO(org.olat.commons.calendar.restapi.EventVO) Test(org.junit.Test)

Example 10 with CalendarVO

use of org.olat.commons.calendar.restapi.CalendarVO in project OpenOLAT by OpenOLAT.

the class CalendarTest method putCalendarEvent.

@Test
public void putCalendarEvent() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id2.getName(), "A6B7C8"));
    URI calUri = UriBuilder.fromUri(getContextURI()).path("users").path(id2.getKey().toString()).path("calendars").build();
    HttpGet calMethod = conn.createGet(calUri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(calMethod);
    assertEquals(200, response.getStatusLine().getStatusCode());
    List<CalendarVO> vos = parseArray(response);
    assertNotNull(vos);
    assertTrue(2 <= vos.size());
    CalendarVO calendar = getCourseCalendar(vos, course2);
    Assert.assertNotNull(calendar);
    // create an event
    EventVO event = new EventVO();
    Calendar cal = Calendar.getInstance();
    event.setBegin(cal.getTime());
    cal.add(Calendar.HOUR_OF_DAY, 1);
    event.setEnd(cal.getTime());
    String subject = UUID.randomUUID().toString();
    event.setSubject(subject);
    URI eventUri = UriBuilder.fromUri(getContextURI()).path("users").path(id2.getKey().toString()).path("calendars").path(calendar.getId()).path("event").build();
    HttpPut putEventMethod = conn.createPut(eventUri, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(putEventMethod, event);
    HttpResponse putEventResponse = conn.execute(putEventMethod);
    assertEquals(200, putEventResponse.getStatusLine().getStatusCode());
    EntityUtils.consume(putEventResponse.getEntity());
    // check if the event is saved
    KalendarRenderWrapper calendarWrapper = calendarManager.getCourseCalendar(course2);
    Collection<KalendarEvent> savedEvents = calendarWrapper.getKalendar().getEvents();
    boolean found = false;
    for (KalendarEvent savedEvent : savedEvents) {
        if (subject.equals(savedEvent.getSubject())) {
            found = true;
        }
    }
    Assert.assertTrue(found);
    conn.shutdown();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) Calendar(java.util.Calendar) CalendarVO(org.olat.commons.calendar.restapi.CalendarVO) HttpResponse(org.apache.http.HttpResponse) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) KalendarRenderWrapper(org.olat.commons.calendar.ui.components.KalendarRenderWrapper) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) EventVO(org.olat.commons.calendar.restapi.EventVO) Test(org.junit.Test)

Aggregations

URI (java.net.URI)18 HttpResponse (org.apache.http.HttpResponse)18 HttpGet (org.apache.http.client.methods.HttpGet)18 Test (org.junit.Test)18 CalendarVO (org.olat.commons.calendar.restapi.CalendarVO)18 EventVO (org.olat.commons.calendar.restapi.EventVO)12 Calendar (java.util.Calendar)8 HttpPut (org.apache.http.client.methods.HttpPut)6 KalendarEvent (org.olat.commons.calendar.model.KalendarEvent)6 KalendarRenderWrapper (org.olat.commons.calendar.ui.components.KalendarRenderWrapper)6 HttpPost (org.apache.http.client.methods.HttpPost)2 EventVOes (org.olat.commons.calendar.restapi.EventVOes)2