}
@Test
public void testAddGetEventOnSourceWithPaging() throws Exception {
EventSource es = new EventSource();
es.setResourceId(_platformId);
es.setName("Event Log"); // Name of the event definition
es.setLocation("-x-test-location");
Response response =
given()
.header(acceptJson)
.contentType(ContentType.JSON)
.pathParam("id",_platformId)
.body(es)
.expect()
.statusCode(200)
.log().ifError()
.when()
.post("/event/{id}/sources");
EventSource eventSource = response.as(EventSource.class);
long now = System.currentTimeMillis();
try {
// Add an event
Event event = new Event(eventSource.getId(),now,"Li la lu 1:->");
Event event1 = new Event(eventSource.getId(),now,"Li la lu 2:->");
Event event2 = new Event(eventSource.getId(),now,"Li la lu 3:->");
Event event3 = new Event(eventSource.getId(),now,"Li la lu 4:->");
List<Event> events = new ArrayList<Event>(4);
events.add(event);
events.add(event1);
events.add(event2);
events.add(event3);
given()
.header(acceptJson)
.contentType(ContentType.JSON)
.pathParam("id",eventSource.getId())
.body(events)
.expect()
.statusCode(204) // no content returned
.log().ifError()
.when()
.post("/event/source/{id}/events");
// and retrieve it again from the event source
response =
given()
.header(acceptJson)
.pathParam("id", eventSource.getId())
.queryParam("startTime",now - 10)
.queryParam("endTime",now + 10)
.expect()
.statusCode(200)
.log().ifError()
.header("X-collection-size", CoreMatchers.is("4"))
.when()
.get("/event/source/{id}/events");
List list = response.as(List.class);
assert list.size()>0;
// Get the list of events from the resource
response =
given()
.header(acceptJson)
.pathParam("id", _platformId)
.queryParam("startTime",now - 10)
.queryParam("endTime",now + 10)
.queryParam("page",0)
.queryParam("ps",2)
.expect()
.statusCode(200)
.log().ifError()
.header("X-collection-size", CoreMatchers.is("4"))
.header("Link",not(containsString("prev")))
.when()
.get("/event/{id}/events");
list = response.as(List.class);
assert list.size()==2;
response =
given()
.header(acceptJson)
.pathParam("id", _platformId)
.queryParam("startTime",now - 10)
.queryParam("endTime",now + 10)
.queryParam("page",1)
.queryParam("ps",2)
.expect()
.statusCode(200)
.log().ifError()
.header("X-collection-size", CoreMatchers.is("4"))
.header("Link",not(containsString("next")))
.when()
.get("/event/{id}/events");
Headers headers = response.getHeaders();
int found = 0;
for (String link: headers.getValues("Link")) {
if (link.contains("rel=\"last\""))
found++;
if (link.contains("rel=\"prev\""))
found++;
if (link.contains("rel=\"current\""))
found++;
assert !link.contains("rel=\"next\"");
}
assert found == 3;
}
finally {
// Delete the source again
given()
.pathParam("id", eventSource.getId())
.expect()
.statusCode(204)
.when()
.delete("/event/source/{id}");
}