use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class ViewServiceTest method searchPaginatedByType.
@Test
public void searchPaginatedByType() {
final ImmutableMap<String, SearchQueryField> searchFieldMapping = ImmutableMap.<String, SearchQueryField>builder().put("id", SearchQueryField.create(ViewDTO.FIELD_ID)).put("title", SearchQueryField.create(ViewDTO.FIELD_TITLE)).put("summary", SearchQueryField.create(ViewDTO.FIELD_DESCRIPTION)).build();
dbService.save(ViewDTO.builder().type(ViewDTO.Type.DASHBOARD).title("View A").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
dbService.save(ViewDTO.builder().type(ViewDTO.Type.DASHBOARD).title("View B").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
dbService.save(ViewDTO.builder().type(ViewDTO.Type.DASHBOARD).title("View C").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
dbService.save(ViewDTO.builder().type(ViewDTO.Type.SEARCH).title("View D").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
dbService.save(ViewDTO.builder().type(ViewDTO.Type.SEARCH).title("View E").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
final SearchQueryParser queryParser = new SearchQueryParser(ViewDTO.FIELD_TITLE, searchFieldMapping);
final PaginatedList<ViewDTO> result1 = dbService.searchPaginatedByType(ViewDTO.Type.DASHBOARD, queryParser.parse("A B D"), view -> true, "desc", "title", 1, 5);
assertThat(result1).hasSize(2).extracting("title").containsExactly("View B", "View A");
assertThat(result1.grandTotal()).hasValue(3L);
final PaginatedList<ViewDTO> result2 = dbService.searchPaginatedByType(ViewDTO.Type.DASHBOARD, queryParser.parse("A B D"), view -> view.title().contains("B") || view.title().contains("D"), "desc", "title", 1, 5);
assertThat(result2).hasSize(1).extracting("title").containsExactly("View B");
assertThat(result1.grandTotal()).hasValue(3L);
final PaginatedList<ViewDTO> result3 = dbService.searchPaginatedByType(ViewDTO.Type.SEARCH, queryParser.parse(""), view -> true, "asc", "title", 1, 5);
assertThat(result3).hasSize(2).extracting("title").containsExactly("View D", "View E");
assertThat(result3.grandTotal()).hasValue(2L);
}
use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class ViewServiceTest method searchPaginated.
@Test
public void searchPaginated() {
final ImmutableMap<String, SearchQueryField> searchFieldMapping = ImmutableMap.<String, SearchQueryField>builder().put("id", SearchQueryField.create(ViewDTO.FIELD_ID)).put("title", SearchQueryField.create(ViewDTO.FIELD_TITLE)).put("summary", SearchQueryField.create(ViewDTO.FIELD_DESCRIPTION)).build();
dbService.save(ViewDTO.builder().title("View A").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
dbService.save(ViewDTO.builder().title("View B").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
dbService.save(ViewDTO.builder().title("View C").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
dbService.save(ViewDTO.builder().title("View D").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
dbService.save(ViewDTO.builder().title("View E").searchId("abc123").state(Collections.emptyMap()).owner("franz").build());
final SearchQueryParser queryParser = new SearchQueryParser(ViewDTO.FIELD_TITLE, searchFieldMapping);
final PaginatedList<ViewDTO> result1 = dbService.searchPaginated(queryParser.parse("A B D"), view -> true, "desc", "title", 1, 5);
assertThat(result1).hasSize(3).extracting("title").containsExactly("View D", "View B", "View A");
assertThat(result1.grandTotal()).hasValue(5L);
final PaginatedList<ViewDTO> result2 = dbService.searchPaginated(queryParser.parse("A B D"), view -> view.title().contains("B") || view.title().contains("D"), "desc", "title", 1, 5);
assertThat(result2).hasSize(2).extracting("title").containsExactly("View D", "View B");
assertThat(result2.grandTotal()).hasValue(5L);
}
use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class ViewsResource method views.
@GET
@ApiOperation("Get a list of all views")
public PaginatedResponse<ViewDTO> views(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "id,title,created_at") @DefaultValue(ViewDTO.FIELD_TITLE) @QueryParam("sort") String sortField, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order, @ApiParam(name = "query") @QueryParam("query") String query, @Context SearchUser searchUser) {
if (!ViewDTO.SORT_FIELDS.contains(sortField.toLowerCase(ENGLISH))) {
sortField = ViewDTO.FIELD_TITLE;
}
try {
final SearchQuery searchQuery = searchQueryParser.parse(query);
final PaginatedList<ViewDTO> result = dbService.searchPaginated(searchQuery, searchUser::canReadView, order, sortField, page, perPage);
return PaginatedResponse.create("views", result, query);
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage(), e);
}
}
use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class PaginatedResponseTest method serializeWithQueryAndContext.
@Test
public void serializeWithQueryAndContext() throws Exception {
final ImmutableList<String> values = ImmutableList.of("hello", "world");
final ImmutableMap<String, Object> context = ImmutableMap.of("context1", "wow");
final PaginatedList<String> paginatedList = new PaginatedList<>(values, values.size(), 1, 10);
final PaginatedResponse<String> response = PaginatedResponse.create("foo", paginatedList, "query1", context);
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");
jsonPathAssert.jsonPathAsString("$.context.context1").isEqualTo("wow");
}
use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class PaginatedResponseTest method serializeWithContext.
@Test
public void serializeWithContext() throws Exception {
final ImmutableList<String> values = ImmutableList.of("hello", "world");
final ImmutableMap<String, Object> context = ImmutableMap.of("context1", "wow");
final PaginatedList<String> paginatedList = new PaginatedList<>(values, values.size(), 1, 10);
final PaginatedResponse<String> response = PaginatedResponse.create("foo", paginatedList, context);
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");
jsonPathAssert.jsonPathAsString("$.context.context1").isEqualTo("wow");
}
Aggregations