Search in sources :

Example 56 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project graylog2-server by Graylog2.

the class ESMessageListTest method passesTypeOfSortingFieldAsUnmappedType.

@Test
public void passesTypeOfSortingFieldAsUnmappedType() {
    final MessageList messageList = someMessageListWithSorting("stream1", "somefield");
    final ESGeneratedQueryContext context = mockQueryContext(messageList);
    when(context.fieldType(Collections.singleton("stream1"), "somefield")).thenReturn(Optional.of("long"));
    final ESGeneratedQueryContext queryContext = generateQueryPartWithContextFor(messageList, true, Collections.emptySet(), context);
    final DocumentContext doc = JsonPath.parse(queryContext.searchSourceBuilder(messageList).toString());
    JsonPathAssert.assertThat(doc).jsonPathAsString("$.sort[0].somefield.unmapped_type").isEqualTo("long");
}
Also used : ESGeneratedQueryContext(org.graylog.storage.elasticsearch7.views.ESGeneratedQueryContext) DocumentContext(com.jayway.jsonpath.DocumentContext) MessageList(org.graylog.plugins.views.search.searchtypes.MessageList) Test(org.junit.Test)

Example 57 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project graylog2-server by Graylog2.

the class ElasticsearchBackendQueryStringDecoratorsTest method generateAppliesQueryStringDecorators.

@Test
void generateAppliesQueryStringDecorators() throws Exception {
    final Query query = mock(Query.class);
    final SearchJob searchJob = searchJobWithRootQueryString(query);
    final DocumentContext request = generateJsonRequest(query, searchJob);
    assertThat(request).jsonPathAsString(PATH_TO_QUERY_STRING).isEqualTo("decorated");
}
Also used : Query(org.graylog.plugins.views.search.Query) SearchJob(org.graylog.plugins.views.search.SearchJob) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.jupiter.api.Test)

Example 58 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project graylog2-server by Graylog2.

the class ElasticsearchBackendQueryStringDecoratorsTest method generateAppliesQueryStringDecoratorsOnSearchTypes.

@Test
void generateAppliesQueryStringDecoratorsOnSearchTypes() throws Exception {
    final Query query = mock(Query.class);
    final SearchJob searchJob = searchJobWithSearchTypeQueryString(query);
    final DocumentContext request = generateJsonRequest(query, searchJob);
    assertThat(request).jsonPathAsString(PATH_TO_QUERY_STRING).isEqualTo("decorated");
}
Also used : Query(org.graylog.plugins.views.search.Query) SearchJob(org.graylog.plugins.views.search.SearchJob) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.jupiter.api.Test)

Example 59 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project graylog2-server by Graylog2.

the class ESMessageListTest method passesNullForUnmappedTypeIfTypeIsNotFound.

@Test
public void passesNullForUnmappedTypeIfTypeIsNotFound() {
    final MessageList messageList = someMessageListWithSorting("stream1", "somefield");
    final ESGeneratedQueryContext context = mockQueryContext(messageList);
    when(context.fieldType(Collections.singleton("stream1"), "somefield")).thenReturn(Optional.empty());
    final ESGeneratedQueryContext queryContext = generateQueryPartWithContextFor(messageList, true, Collections.emptySet(), context);
    final DocumentContext doc = JsonPath.parse(queryContext.searchSourceBuilder(messageList).toString());
    assertThat(doc.read("$.sort[0].somefield", Map.class)).doesNotContainKey("unmapped_type");
}
Also used : ESGeneratedQueryContext(org.graylog.storage.elasticsearch6.views.ESGeneratedQueryContext) DocumentContext(com.jayway.jsonpath.DocumentContext) MessageList(org.graylog.plugins.views.search.searchtypes.MessageList) Test(org.junit.Test)

Example 60 with DocumentContext

use of com.jayway.jsonpath.DocumentContext 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");
}
Also used : JsonPathAssert(com.revinate.assertj.json.JsonPathAssert) PaginatedList(org.graylog2.database.PaginatedList) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Aggregations

DocumentContext (com.jayway.jsonpath.DocumentContext)146 Test (org.junit.Test)106 HashMap (java.util.HashMap)14 BaseTest (com.jayway.jsonpath.BaseTest)12 Map (java.util.Map)12 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)8 JsonPath (com.jayway.jsonpath.JsonPath)7 File (java.io.File)7 List (java.util.List)7 JsonPathAssert (com.revinate.assertj.json.JsonPathAssert)6 Query (org.graylog.plugins.views.search.Query)6 MessageList (org.graylog.plugins.views.search.searchtypes.MessageList)6 Time (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Time)6 Values (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Values)6 DateTime (org.joda.time.DateTime)6 Configuration (com.jayway.jsonpath.Configuration)5 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 InvalidJsonException (com.jayway.jsonpath.InvalidJsonException)4