Search in sources :

Example 1 with SchedulerRestService

use of org.opencastproject.scheduler.endpoint.SchedulerRestService in project opencast by opencast.

the class SchedulerServiceImplTest method testCalendarNotModified.

@Test
public void testCalendarNotModified() throws Exception {
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.replay(request);
    SchedulerRestService restService = new SchedulerRestService();
    restService.setService(schedSvc);
    String device = "Test Device";
    // Store an event
    MediaPackage mediaPackage = generateEvent(Opt.<String>none());
    DublinCoreCatalog dublinCore = generateEvent(device, new Date(), new Date(System.currentTimeMillis() + 60000));
    addDublinCore(Opt.<String>none(), mediaPackage, dublinCore);
    schedSvc.addEvent(new Date(), new Date(System.currentTimeMillis() + 60000), device, Collections.<String>emptySet(), mediaPackage, Collections.<String, String>emptyMap(), Collections.<String, String>emptyMap(), Opt.<Boolean>none(), Opt.<String>none(), SchedulerService.ORIGIN);
    // Request the calendar without specifying an etag. We should get a 200 with the iCalendar in the response body
    Response response = restService.getCalendar(device, null, null, request);
    Assert.assertNotNull(response.getEntity());
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    final String etag = (String) response.getMetadata().getFirst(HttpHeaders.ETAG);
    EasyMock.reset(request);
    EasyMock.expect(request.getHeader("If-None-Match")).andAnswer(new IAnswer<String>() {

        @Override
        public String answer() throws Throwable {
            return etag;
        }
    }).anyTimes();
    EasyMock.replay(request);
    // Request using the etag from the first response. We should get a 304 (not modified)
    response = restService.getCalendar(device, null, null, request);
    assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
    Assert.assertNull(response.getEntity());
    // Update the event and clear to cache to make sure it's reloaded
    schedSvc.updateEvent(mediaPackage.getIdentifier().compact(), Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), Opt.<Set<String>>none(), Opt.<MediaPackage>none(), Opt.some(wfPropertiesUpdated), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
    // Try using the same old etag. We should get a 200, since the event has changed
    response = restService.getCalendar(device, null, null, request);
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    Assert.assertNotNull(response.getEntity());
    final String secondEtag = (String) response.getMetadata().getFirst(HttpHeaders.ETAG);
    Assert.assertNotNull(secondEtag);
    Assert.assertFalse(etag.equals(secondEtag));
    EasyMock.reset(request);
    EasyMock.expect(request.getHeader("If-None-Match")).andAnswer(new IAnswer<String>() {

        @Override
        public String answer() throws Throwable {
            return secondEtag;
        }
    }).anyTimes();
    EasyMock.replay(request);
    // do opt out update
    schedSvc.updateEvent(mediaPackage.getIdentifier().compact(), Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), Opt.<Set<String>>none(), Opt.<MediaPackage>none(), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.some(Opt.some(true)), SchedulerService.ORIGIN);
    Assert.assertTrue(schedSvc.isOptOut(mediaPackage.getIdentifier().compact()));
    // Try using the same old etag. We should get a 200, since the event has changed
    response = restService.getCalendar(device, null, null, request);
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    Assert.assertNotNull(response.getEntity());
    String thirdEtag = (String) response.getMetadata().getFirst(HttpHeaders.ETAG);
    Assert.assertNotNull(thirdEtag);
    Assert.assertFalse(secondEtag.equals(thirdEtag));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) IAnswer(org.easymock.IAnswer) SchedulerRestService(org.opencastproject.scheduler.endpoint.SchedulerRestService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Date (java.util.Date)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Response (javax.ws.rs.core.Response)1 IAnswer (org.easymock.IAnswer)1 Test (org.junit.Test)1 MediaPackage (org.opencastproject.mediapackage.MediaPackage)1 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)1 SchedulerRestService (org.opencastproject.scheduler.endpoint.SchedulerRestService)1