Search in sources :

Example 1 with JsonPathAssert

use of com.revinate.assertj.json.JsonPathAssert 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)

Example 2 with JsonPathAssert

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

Example 3 with JsonPathAssert

use of com.revinate.assertj.json.JsonPathAssert in project graylog2-server by Graylog2.

the class AssertJsonPath method assertJsonPath.

public static void assertJsonPath(String json, Consumer<JsonPathAssert> consumer) {
    final DocumentContext context = JsonPath.parse(json, configuration);
    final JsonPathAssert jsonPathAssert = JsonPathAssert.assertThat(context);
    consumer.accept(jsonPathAssert);
}
Also used : JsonPathAssert(com.revinate.assertj.json.JsonPathAssert) DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 4 with JsonPathAssert

use of com.revinate.assertj.json.JsonPathAssert 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 5 with JsonPathAssert

use of com.revinate.assertj.json.JsonPathAssert 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

DocumentContext (com.jayway.jsonpath.DocumentContext)6 JsonPathAssert (com.revinate.assertj.json.JsonPathAssert)6 PaginatedList (org.graylog2.database.PaginatedList)4 Test (org.junit.Test)4