Search in sources :

Example 16 with PaginatedResponse

use of org.graylog2.rest.models.PaginatedResponse in project graylog2-server by Graylog2.

the class EventNotificationsResource method listNotifications.

@GET
@ApiOperation("List all available notifications")
public PaginatedResponse<NotificationDto> listNotifications(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query) {
    final SearchQuery searchQuery = searchQueryParser.parse(query);
    final PaginatedList<NotificationDto> result = dbNotificationService.searchPaginated(searchQuery, notification -> {
        return isPermitted(RestPermissions.EVENT_NOTIFICATIONS_READ, notification.id());
    }, "title", page, perPage);
    return PaginatedResponse.create("notifications", result, query);
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) NotificationDto(org.graylog.events.notifications.NotificationDto) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 17 with PaginatedResponse

use of org.graylog2.rest.models.PaginatedResponse in project graylog2-server by Graylog2.

the class PaginatedResponseTest method serialize.

@Test
public void serialize() throws Exception {
    final ImmutableList<String> values = ImmutableList.of("hello", "world");
    final PaginatedList<String> paginatedList = new PaginatedList<>(values, values.size(), 1, 10);
    final PaginatedResponse<String> response = PaginatedResponse.create("foo", paginatedList);
    final DocumentContext ctx = JsonPath.parse(objectMapper.writeValueAsString(response));
    final JsonPathAssert jsonPathAssert = JsonPathAssert.assertThat(ctx);
    jsonPathAssert.jsonPathAsInteger("$.total").isEqualTo(2);
    jsonPathAssert.jsonPathAsInteger("$.count").isEqualTo(2);
    jsonPathAssert.jsonPathAsInteger("$.page").isEqualTo(1);
    jsonPathAssert.jsonPathAsInteger("$.per_page").isEqualTo(10);
    jsonPathAssert.jsonPathAsString("$.foo[0]").isEqualTo("hello");
    jsonPathAssert.jsonPathAsString("$.foo[1]").isEqualTo("world");
    assertThatThrownBy(() -> jsonPathAssert.jsonPathAsString("$.context")).isInstanceOf(PathNotFoundException.class);
}
Also used : JsonPathAssert(com.revinate.assertj.json.JsonPathAssert) PaginatedList(org.graylog2.database.PaginatedList) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Example 18 with PaginatedResponse

use of org.graylog2.rest.models.PaginatedResponse in project graylog2-server by Graylog2.

the class PaginatedResponseTest method serializeWithQuery.

@Test
public void serializeWithQuery() throws Exception {
    final ImmutableList<String> values = ImmutableList.of("hello", "world");
    final PaginatedList<String> paginatedList = new PaginatedList<>(values, values.size(), 1, 10);
    final PaginatedResponse<String> response = PaginatedResponse.create("foo", paginatedList, "query1");
    final DocumentContext ctx = JsonPath.parse(objectMapper.writeValueAsString(response));
    final JsonPathAssert jsonPathAssert = JsonPathAssert.assertThat(ctx);
    jsonPathAssert.jsonPathAsString("$.query").isEqualTo("query1");
    jsonPathAssert.jsonPathAsInteger("$.total").isEqualTo(2);
    jsonPathAssert.jsonPathAsInteger("$.count").isEqualTo(2);
    jsonPathAssert.jsonPathAsInteger("$.page").isEqualTo(1);
    jsonPathAssert.jsonPathAsInteger("$.per_page").isEqualTo(10);
    jsonPathAssert.jsonPathAsString("$.foo[0]").isEqualTo("hello");
    jsonPathAssert.jsonPathAsString("$.foo[1]").isEqualTo("world");
    assertThatThrownBy(() -> jsonPathAssert.jsonPathAsString("$.context")).isInstanceOf(PathNotFoundException.class);
}
Also used : JsonPathAssert(com.revinate.assertj.json.JsonPathAssert) PaginatedList(org.graylog2.database.PaginatedList) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)14 GET (javax.ws.rs.GET)14 SearchQuery (org.graylog2.search.SearchQuery)12 BadRequestException (javax.ws.rs.BadRequestException)10 Path (javax.ws.rs.Path)8 PaginatedList (org.graylog2.database.PaginatedList)8 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)7 Produces (javax.ws.rs.Produces)5 Timed (com.codahale.metrics.annotation.Timed)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 DocumentContext (com.jayway.jsonpath.DocumentContext)4 JsonPathAssert (com.revinate.assertj.json.JsonPathAssert)4 Api (io.swagger.annotations.Api)4 ApiParam (io.swagger.annotations.ApiParam)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 NotFoundException (javax.ws.rs.NotFoundException)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3