https://github.com/rest-assured/rest-assured/blob/master/json-path/src/test/java/io/restassured/path/json/JsonPathTest.java

/*
  * Copyright 2016 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
   
  package io.restassured.path.json;
   
  import io.restassured.path.json.config.JsonPathConfig;
  import io.restassured.path.json.exception.JsonPathException;
  import io.restassured.path.json.support.Book;
  import org.junit.Test;
   
  import java.math.BigDecimal;
  import java.util.List;
  import java.util.Map;
  import java.util.UUID;
   
  import static org.hamcrest.Matchers.*;
  import static org.junit.Assert.assertThat;
   
  public class JsonPathTest {
   
  private final String JSON = "{ \"store\": {\n" +
  " \"book\": [ \n" +
  " { \"category\": \"reference\",\n" +
  " \"author\": \"Nigel Rees\",\n" +
  " \"title\": \"Sayings of the Century\",\n" +
  " \"price\": 8.95\n" +
  " },\n" +
  " { \"category\": \"fiction\",\n" +
  " \"author\": \"Evelyn Waugh\",\n" +
  " \"title\": \"Sword of Honour\",\n" +
  " \"price\": 12\n" +
  " },\n" +
  " { \"category\": \"fiction\",\n" +
  " \"author\": \"Herman Melville\",\n" +
  " \"title\": \"Moby Dick\",\n" +
  " \"isbn\": \"0-553-21311-3\",\n" +
  " \"price\": 8.99\n" +
  " },\n" +
  " { \"category\": \"fiction\",\n" +
  " \"author\": \"J. R. R. Tolkien\",\n" +
  " \"title\": \"The Lord of the Rings\",\n" +
  " \"isbn\": \"0-395-19395-8\",\n" +
  " \"price\": 22.99\n" +
  " }\n" +
  " ],\n" +
  " \"bicycle\": {\n" +
  " \"color\": \"red\",\n" +
  " \"price\": 19.95,\n" +
  " \"atoms\": " + Long.MAX_VALUE + ",\n" +
  " }\n" +
  " }\n" +
  "}";
   
  private final String JSON2 = "[{\"email\":\"name1@mail.com\",\"alias\":\"name one\",\"phone\":\"3456789\"},\n" +
  "{\"email\":\"name2@mail.com\",\"alias\":\"name two\",\"phone\":\"1234567\"},\n" +
  "{\"email\":\"name3@mail.com\",\"alias\":\"name three\",\"phone\":\"2345678\"}]";
   
  private final String JSON3 = "{\"id\":\"db24eeeb-7fe5-41d3-8f06-986b793ecc91\"}";
   
   
  private final String JSON_MAP = "{ \"price1\" : 12.3,\n" +
  " \"price2\": 15.0 }";
   
  private final String JSON_PATH_STARTING_WITH_NUMBER = "{ \"0\" : 12.3,\n" +
  " \"1\": 15.0 }";
   
  private final String JSON_PATH_WITH_NUMBER = "{ \"map\" : { \"0\" : 12.3,\n" +
  " \"1\": 15.0 } }";
   
  private final String JSON_PATH_WITH_SIZE = "{ \"map\" : { \"size\" : 12.3,\n" +
  " \"1\": 15.0 } }";
   
  private final String JSON_PATH_WITH_BOOLEAN = "{ \"map\" : { \"true\" : 12.3,\n" +
  " \"false\": 15.0 } }";
   
  private final String MALFORMED_JSON = "{\n" +
  " \"a\": 123456\n" +
  " \"b\":\"string\"\n" +
  "}";
   
  @Test
  public void getList() throws Exception {
  final List<String> categories = new JsonPath(JSON).get("store.book.category");
  assertThat(categories.size(), equalTo(4));
  assertThat(categories, hasItems("reference", "fiction"));
  }
   
  @Test
  public void firstBookCategory() throws Exception {
  final String category = JsonPath.with(JSON).get("store.book[0].category");
  assertThat(category, equalTo("reference"));
  }
   
  @Test
  public void lastBookTitle() throws Exception {
  final String title = JsonPath.with(JSON).get("store.book[-1].title");
  assertThat(title, equalTo("The Lord of the Rings"));
  }
   
  @Test
  public void booksWithArgAuthor() throws Exception {
  String author = "Herman Melville";
  final List<Map<String, ?>> books = JsonPath.with(JSON)
  .param("author", author)
  .get("store.book.findAll { book -> book.author == author }");
  assertThat(books.size(), equalTo(1));
   
  final String authorActual = (String) books.get(0).get("author");
  assertThat(authorActual, equalTo(author));
  }
   
  @Test
  public void booksBetween5And15() throws Exception {
  final List<Map<String, ?>> books = JsonPath.with(JSON).get("store.book.findAll { book -> book.price >= 5 && book.price <= 15 }");
  assertThat(books.size(), equalTo(3));
   
  final String author = (String) books.get(0).get("author");
  assertThat(author, equalTo("Nigel Rees"));
   
  final int price = (Integer) books.get(1).get("price");
  assertThat(price, equalTo(12));
  }
   
  @Test
  public void sizeInPath() throws Exception {
  final Integer size = JsonPath.with(JSON).get("store.book.size()");
  assertThat(size, equalTo(4));
  }
   
  @Test
  public void getRootObjectAsMap() throws Exception {
  final Map<String, Map> store = JsonPath.given(JSON).get("store");
  assertThat(store.size(), equalTo(2));
   
  final Map<String, Object> bicycle = store.get("bicycle");
  final String color = (String) bicycle.get("color");
  final float price = (Float) bicycle.get("price");
  assertThat(color, equalTo("red"));
  assertThat(price, equalTo(19.95f));
  }
   
  @Test
  public void getFloatAndDoublesAsBigDecimal() throws Exception {
  final JsonPath using = JsonPath.with(JSON).using(new JsonPathConfig(JsonPathConfig.NumberReturnType.BIG_DECIMAL));
  assertThat(using.<Map<String, Map>>get("store").size(), equalTo(2));
   
  final Map<String, Object> bicycle = using.<Map<String, Map>>get("store").get("bicycle");
  final String color = (String) bicycle.get("color");
  final BigDecimal price = (BigDecimal) bicycle.get("price");
  assertThat(color, equalTo("red"));
  assertThat(price, equalTo(new BigDecimal("19.95")));
  }
   
  @Test
  public void getFloatAndDoublesAsBigDecimalUsingStaticConfiguration() throws Exception {
  JsonPath.config = new JsonPathConfig().numberReturnType(JsonPathConfig.NumberReturnType.BIG_DECIMAL);
  try {
  final Map<String, Map> store = JsonPath.with(JSON).get("store");
  assertThat(store.size(), equalTo(2));
   
  final Map<String, Object> bicycle = store.get("bicycle");
  final String color = (String) bicycle.get("color");
  final BigDecimal price = (BigDecimal) bicycle.get("price");
  assertThat(color, equalTo("red"));
  assertThat(price, equalTo(new BigDecimal("19.95")));
  } finally {
  JsonPath.config = null;
  }
  }
   
  @Test
  public void nonStaticJsonPathConfigHasPrecedenceOverStaticConfiguration() throws Exception {
  JsonPath.config = new JsonPathConfig().numberReturnType(JsonPathConfig.NumberReturnType.FLOAT_AND_DOUBLE);
  try {
  final Map<String, Map> store = JsonPath.with(JSON).using(new JsonPathConfig(JsonPathConfig.NumberReturnType.BIG_DECIMAL)).get("store");
  assertThat(store.size(), equalTo(2));
   
  final Map<String, Object> bicycle = store.get("bicycle");
  final String color = (String) bicycle.get("color");
  final BigDecimal price = (BigDecimal) bicycle.get("price");
  assertThat(color, equalTo("red"));
  assertThat(price, equalTo(new BigDecimal("19.95")));
  } finally {
  JsonPath.config = null;
  }
  }
   
  @Test
  public void getRootObjectAsMap2() throws Exception {
  final Map<String, Object> store = JsonPath.from(JSON).get("store.book[0]");
  for (Map.Entry<String, Object> stringObjectEntry : store.entrySet()) {
  System.out.println(stringObjectEntry.getKey() + " = " + stringObjectEntry.getValue());
  }
  }
   
  @Test
  public void rootPath() throws Exception {
  final JsonPath jsonPath = new JsonPath(JSON).setRoot("store.book");
  assertThat(jsonPath.getInt("size()"), equalTo(4));
  assertThat(jsonPath.getList("author", String.class), hasItem("J. R. R. Tolkien"));
  }
   
  @Test
  public void rootPathFollowedByArrayIndexing() throws Exception {
  final JsonPath jsonPath = new JsonPath(JSON).setRoot("store.book");
  assertThat(jsonPath.getString("[0].author"), equalTo("Nigel Rees"));
  }
   
  @Test
  public void supportsGettingEntireObjectGraphUsingEmptyString() throws Exception {
  final List<Map<String, String>> object = JsonPath.from(JSON2).get("");
  assertThat(object.get(0).get("email"), equalTo("name1@mail.com"));
  }
   
  @Test
  public void supportsGettingEntireObjectGraphUsing$() throws Exception {
  final List<Map<String, String>> object = JsonPath.from(JSON2).get("$");
  assertThat(object.get(0).get("email"), equalTo("name1@mail.com"));
  }
   
  @Test
  public void supportsGettingEntireObjectGraphUsingNoArgumentGet() throws Exception {
  final List<Map<String, String>> object = JsonPath.from(JSON2).get();
  assertThat(object.get(0).get("email"), equalTo("name1@mail.com"));
  }
   
  @Test
  public void getValueFromUnnamedRootObject() throws Exception {
  final Map<String, String> object = JsonPath.from(JSON2).get("get(0)");
  assertThat(object.get("email"), equalTo("name1@mail.com"));
  }
   
  @Test
  public void getValueFromUnnamedRootObjectUsingBrackets() throws Exception {
  final Map<String, String> object = JsonPath.from(JSON2).get("[0]");
  assertThat(object.get("email"), equalTo("name1@mail.com"));
  }
   
  @Test
  public void getSubValueFromUnnamedRootObjectUsingBrackets() throws Exception {
  final String object = JsonPath.from(JSON2).getString("[0].email");
  assertThat(object, equalTo("name1@mail.com"));
  }
   
  @Test
  public void getNumericalValues() {
  assertThat(JsonPath.with(JSON).getDouble("store.book[0].price"), equalTo(8.95D));
  assertThat(JsonPath.with(JSON).getFloat("store.book[0].price"), equalTo(8.95F));
   
  // The price is stored as an integer
  assertThat(JsonPath.with(JSON).getByte("store.book[1].price"), equalTo((byte) 12));
  assertThat(JsonPath.with(JSON).getShort("store.book[1].price"), equalTo((short) 12));
  assertThat(JsonPath.with(JSON).getInt("store.book[1].price"), equalTo(12));
  assertThat(JsonPath.with(JSON).getLong("store.book[1].price"), equalTo(12L));
   
  // The atoms is stored as a long
  assertThat(JsonPath.with(JSON).getByte("store.bicycle.atoms"), equalTo((byte) Long.MAX_VALUE));
  assertThat(JsonPath.with(JSON).getShort("store.bicycle.atoms"), equalTo((short) Long.MAX_VALUE));
  assertThat(JsonPath.with(JSON).getInt("store.bicycle.atoms"), equalTo((int) Long.MAX_VALUE));
  assertThat(JsonPath.with(JSON).getLong("store.bicycle.atoms"), equalTo(Long.MAX_VALUE));
  }
   
  @Test
  public void convertsValueToStringWhenExplicitlyRequested() throws Exception {
  String phoneNumber = JsonPath.from(JSON2).getString("phone[0]");
   
  assertThat(phoneNumber, equalTo("3456789"));
  }
   
  @Test
  public void convertsValueToIntWhenExplicitlyRequested() throws Exception {
  int phoneNumber = JsonPath.from(JSON2).getInt("phone[0]");
   
  assertThat(phoneNumber, equalTo(3456789));
  }
   
  @Test
  public void convertsValueToDoubleWhenExplicitlyRequested() throws Exception {
  double phoneNumber = JsonPath.from(JSON2).getDouble("phone[0]");
   
  assertThat(phoneNumber, equalTo(3456789d));
  }
   
  @Test
  public void convertsValueToFloatWhenExplicitlyRequested() throws Exception {
  float phoneNumber = JsonPath.from(JSON2).getFloat("phone[0]");
   
  assertThat(phoneNumber, equalTo(3456789f));
  }
   
  @Test
  public void convertsValueToUUIDWhenExplicitlyRequested() throws Exception {
  UUID uuid = JsonPath.from(JSON3).getUUID("id");
   
  assertThat(uuid, equalTo(UUID.fromString("db24eeeb-7fe5-41d3-8f06-986b793ecc91")));
  }
   
  @Test
  public void convertsListMembersToDefinedTypeIfPossible() throws Exception {
  final List<Integer> phoneNumbers = JsonPath.with(JSON2).getList("phone", int.class);
   
  assertThat(phoneNumbers, hasItems(3456789, 1234567, 2345678));
  }
   
  @Test
  public void getMapWithGenericType() throws Exception {
  final Map<String, String> map = JsonPath.with(JSON_MAP).getMap("$", String.class, String.class);
   
  assertThat(map, allOf(hasEntry("price1", "12.3"), hasEntry("price2", "15.0")));
  }
   
  @Test
  public void getMapWithAnotherGenericType() throws Exception {
  final Map<String, Float> map = JsonPath.with(JSON_MAP).getMap("$", String.class, float.class);
   
  assertThat(map, allOf(hasEntry("price1", 12.3f), hasEntry("price2", 15.0f)));
  }
   
  @Test
  public void getStringConvertsTheResultToAString() throws Exception {
  final String priceAsString = JsonPath.with(JSON).getString("store.book.price[0]");
   
  assertThat(priceAsString, is("8.95"));
  }
   
  @Test(expected = JsonPathException.class)
  public void malformedJson() throws Exception {
  JsonPath.from(MALFORMED_JSON).get("a");
  }
   
  @Test
  public void getObjectWorksWhenPathPointsToAJsonObject() throws Exception {
  final Book book = JsonPath.from(JSON).getObject("store.book[2]", Book.class);
   
  assertThat(book, equalTo(new Book("fiction", "Herman Melville", "Moby Dick", "0-553-21311-3", 8.99f)));
  }
   
  @Test
  public void getObjectWorksWhenPathPointsToAJsonObject2() throws Exception {
  final List<Book> books = JsonPath.from(JSON).getList("store.book", Book.class);
   
  assertThat(books, hasSize(4));
  assertThat(books.get(0).getAuthor(), equalTo("Nigel Rees"));
  }
   
  @Test
  public void getObjectAsMapWorksWhenPathPointsToAJsonObject() throws Exception {
  final Map<String, String> book = JsonPath.from(JSON).getObject("store.book[2]", Map.class);
   
  assertThat(book, hasEntry("category", "fiction"));
  assertThat(book, hasEntry("author", "Herman Melville"));
  }
   
  @Test
  public void getObjectWorksWhenPathPointsToAList() throws Exception {
  final List<String> categories = JsonPath.from(JSON).getObject("store.book.category", List.class);
   
  assertThat(categories, hasItems("reference", "fiction"));
  }
   
  @Test
  public void getObjectAsFloatWorksWhenPathPointsToAFloat() throws Exception {
  final Float price = JsonPath.from(JSON).getObject("store.book.price[0]", Float.class);
   
  assertThat(price, equalTo(8.95f));
  }
   
  @Test
  public void getObjectAsStringWorksWhenPathPointsToAString() throws Exception {
  final String category = JsonPath.from(JSON).getObject("store.book.category[0]", String.class);
   
  assertThat(category, equalTo("reference"));
  }
   
  @Test
  public void jsonPathSupportsPrettifiyingJson() throws Exception {
  final String prettyJson = JsonPath.with(JSON2).prettify();
   
  assertThat(prettyJson, equalTo("[\n {\n \"phone\": \"3456789\",\n \"alias\": \"name one\",\n \"email\": \"name1@mail.com\"\n },\n {\n \"phone\": \"1234567\",\n \"alias\": \"name two\",\n \"email\": \"name2@mail.com\"\n },\n {\n \"phone\": \"2345678\",\n \"alias\": \"name three\",\n \"email\": \"name3@mail.com\"\n }\n]"));
  }
   
  @Test
  public void jsonPathSupportsPrettyPrintingJson() throws Exception {
  final String prettyJson = JsonPath.with(JSON2).prettyPrint();
   
  assertThat(prettyJson, equalTo("[\n {\n \"phone\": \"3456789\",\n \"alias\": \"name one\",\n \"email\": \"name1@mail.com\"\n },\n {\n \"phone\": \"1234567\",\n \"alias\": \"name two\",\n \"email\": \"name2@mail.com\"\n },\n {\n \"phone\": \"2345678\",\n \"alias\": \"name three\",\n \"email\": \"name3@mail.com\"\n }\n]"));
  }
   
  @Test
  public void jsonPathSupportsPrettyPeekingJson() throws Exception {
  final String phone = JsonPath.with(JSON2).prettyPeek().getString("phone[0]");
   
  assertThat(phone, equalTo("3456789"));
  }
   
  @Test
  public void jsonPathSupportsPeekingAtTheJson() throws Exception {
  final String phone = JsonPath.with(JSON2).peek().getString("phone[0]");
   
  assertThat(phone, equalTo("3456789"));
  }
   
  @Test
  public void canParseJsonDocumentWhenFirstKeyIsIntegerUsingManualEscaping() throws Exception {
  final float number = JsonPath.from(JSON_PATH_STARTING_WITH_NUMBER).getFloat("'0'");
   
  assertThat(number, equalTo(12.3f));
  }
   
  @Test
  public void canParseJsonDocumentWhenFirstKeyThatIsAIntegerUsingNoEscaping() throws Exception {
  final float number = JsonPath.from(JSON_PATH_STARTING_WITH_NUMBER).getFloat("0");
   
  assertThat(number, equalTo(12.3f));
  }
   
  @Test
  public void canParseJsonDocumentWhenPathIncludesKeyThatIsAIntegerUsingNoEscaping() throws Exception {
  final float number = JsonPath.from(JSON_PATH_WITH_NUMBER).getFloat("map.0");
   
  assertThat(number, equalTo(12.3f));
  }
   
  @Test
  public void canParseJsonDocumentWhenPathIncludesKeyThatIsABooleanUsingEscaping() throws Exception {
  final float number = JsonPath.from(JSON_PATH_WITH_BOOLEAN).getFloat("map.'false'");
   
  assertThat(number, equalTo(15.0f));
  }
   
  @Test
  public void canParseJsonDocumentWhenPathIncludesKeyThatIsABooleanUsingNoEscaping() throws Exception {
  final float number = JsonPath.from(JSON_PATH_WITH_BOOLEAN).getFloat("map.true");
   
  assertThat(number, equalTo(12.3f));
  }
   
  @Test
  public void canParseJsonDocumentWhenPathIncludesMinusInsideEscaped() throws Exception {
  JsonPath path = new JsonPath("{ \"a-b\" : \"minus\" , \"a.b\" : \"dot\" , \"a.b-c\" : \"both\" }");
   
  assertThat(path.getString("'a.b-c'"), equalTo("both"));
  }
   
  /**
  * Verifies that issue 195 is resolved.
  */
  @Test
  public void canParseJsonDocumentWithMultipleConsecutiveIntegersInsidePath() throws Exception {
  String json = "{\n" +
  " \"foo.bar.baz\": {\n" +
  " \"0.2.0\": \"test\"\n" +
  " }\n" +
  "}";
   
  final String string = JsonPath.from(json).getString("'foo.bar.baz'.'0.2.0'");
   
  assertThat(string, equalTo("test"));
  }
   
  @Test
  public void
  can_parse_multiple_values() {
  // Given
  final JsonPath jsonPath = new JsonPath(JSON);
   
  // When
  final String category1 = jsonPath.getString("store.book.category[0]");
  final String category2 = jsonPath.getString("store.book.category[1]");
   
  // Then
  assertThat(category1, equalTo("reference"));
  assertThat(category2, equalTo("fiction"));
  }
   
  @Test
  public void
  pretty_printing_works() {
  // Given
  String json = "{\"data\": [{" +
  " \"uid\": 10,\n" +
  " \"name\": \"abc\"\n" +
  " }\n" +
  " ]\n" +
  "}";
  // When
  final JsonPath jsonPath = new JsonPath(json);
   
  // Then
  final String string = jsonPath.prettyPrint();
  assertThat(string, equalTo("{\n" +
  " \"data\": [\n" +
  " {\n" +
  " \"uid\": 10,\n" +
  " \"name\": \"abc\"\n" +
  " }\n" +
  " ]\n" +
  "}"));
  }
   
  @Test
  public void
  parses_json_document_with_attribute_name_equal_to_properties() {
  // Given
  final String jsonWithPropertyAttribute = "[{\"properties\":\"test\"}]"; // properties is a reserved word in Groovy
   
  // When
  final String value = new JsonPath(jsonWithPropertyAttribute).getString("[0].properties");
   
  // Then
  assertThat(value, equalTo("test"));
  }
   
  @Test
  public void
  parses_json_document_with_attribute_name_equal_to_size() {
  // When
  final float anInt = new JsonPath(JSON_PATH_WITH_SIZE).getFloat("map.size");
   
  // Then
  assertThat(anInt, is(12.3f));
  }
   
  @Test public void
  can_find_if_a_key_exists_in_json_object() {
  // Given
  String json = "{\n" +
  "\"status\": \"success\",\n" +
  "\"fund_code\":\"00200\",\n" +
  "\"fund_name\":\"My Fund Name\",\n" +
  "\"models\": [\n" +
  " {\n" +
  " \"model_id\": 639506,\n" +
  " \"model_name\": \"New Validated Model\",\n" +
  " \"model_type\": null,\n" +
  " \"portfolios\": null,\n" +
  " \"created_date\": 1390978800000,\n" +
  " \"display_create_date\": \"01/29/2014\",\n" +
  " \"updated_date\": 1392274800000,\n" +
  " \"display_update_date\": \"02/13/2014\",\n" +
  " \"number_of_clients\": 1,\n" +
  " \"risk\": \"N/A\",\n" +
  " \"time_frame\": \"N/A\"\n" +
  " },\n" +
  " {\n" +
  " \"model_id\": 639507,\n" +
  " \"model_name\": \"My Validated Model\",\n" +
  " \"model_type\": null,\n" +
  " \"portfolios\": null,\n" +
  " \"created_date\": 1390978800000,\n" +
  " \"display_create_date\": \"01/29/2014\",\n" +
  " \"updated_date\": 1392274800000,\n" +
  " \"display_update_date\": \"02/13/2014\",\n" +
  " \"number_of_clients\": 1,\n" +
  " \"risk\": \"N/A\",\n" +
  " \"time_frame\": \"N/A\"\n" +
  " }\n" +
  "]\n" +
  "}";
   
  // When
  JsonPath jsonPath = new JsonPath(json);
   
  // Then
  assertThat(jsonPath.getBoolean("any { it.key == 'fund_code' }"), is(true));
  assertThat(jsonPath.getBoolean("models.any { it.containsKey('number_of_clients') }"), is(true));
  }
   
  @Test public void
  can_parse_json_attributes_starting_with_a_number() {
  // Given
  String json = "{\n" +
  " \"6269f15a0bb9b1b7d86ae718e84cddcd\" : {\n" +
  " \"attr1\":\"val1\",\n" +
  " \"attr2\":\"val2\",\n" +
  " \"attrx\":\"valx\"\n" +
  " }\n" +
  "}";
   
  // When
  JsonPath jsonPath = new JsonPath(json);
   
  // Then
  assertThat(jsonPath.getString("6269f15a0bb9b1b7d86ae718e84cddcd.attr1"), equalTo("val1"));
  }
   
  @Test public void
  automatically_escapes_json_attributes_whose_name_equals_properties() {
  // Given
  String json = "{\n" +
  " \"features\":[\n" +
  " {\n" +
  " \"type\":\"Feature\",\n" +
  " \"geometry\":{\n" +
  " \"type\":\"GeometryCollection\",\n" +
  " \"geometries\":[\n" +
  " {\n" +
  " \"type\":\"Point\",\n" +
  " \"coordinates\":[\n" +
  " 19.883992823270653,\n" +
  " 50.02026203045478\n" +
  " ]\n" +
  " }\n" +
  " ]\n" +
  " },\n" +
  " \"properties\":{\n" +
  " \"gridId\":6\n" +
  " }\n" +
  " },\n" +
  " {\n" +
  " \"type\":\"Feature\",\n" +
  " \"geometry\":{\n" +
  " \"type\":\"GeometryCollection\",\n" +
  " \"geometries\":[\n" +
  " {\n" +
  " \"type\":\"Point\",\n" +
  " \"coordinates\":[\n" +
  " 19.901266347582094,\n" +
  " 50.07074684071764\n" +
  " ]\n" +
  " }\n" +
  " ]\n" +
  " },\n" +
  " \"properties\":{\n" +
  " \"gridId\":7\n" +
  " }\n" +
  " }\n" +
  " ]\n" +
  "}";
  // When
  JsonPath jsonPath = new JsonPath(json);
   
  // Then
  assertThat(jsonPath.getList("features.properties.gridId", Integer.class), hasItems(7));
  }
   
  @Test public void
  can_manually_escape_class_property() {
  // Given
  String json = "{\n" +
  " \"semester\": \"Fall 2015\",\n" +
  " \"groups\": [\n" +
  " {\n" +
  " \"siteUrl\": \"http://cphbusinessjb.cloudapp.net/CA2/\",\n" +
  " \"error\": \"NO AUTHOR/CLASS-INFO\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"http://ca2-ebski.rhcloud.com/CA2New/\",\n" +
  " \"authors\": \"Ebbe, Kasper, Christoffer\",\n" +
  " \"class\": \"A klassen\",\n" +
  " \"group\": \"Gruppe: Johns Llama Herders A/S\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"http://ca2-chrislind.rhcloud.com/CA2Final/\",\n" +
  " \"error\": \"NO AUTHOR/CLASS-INFO\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"http://ca2-pernille.rhcloud.com/NYCA2/\",\n" +
  " \"authors\": \"Marta, Jeanette, Pernille\",\n" +
  " \"class\": \"DAT A\",\n" +
  " \"group\": \"Group: MJP\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"https://ca2-afn.rhcloud.com:8443/company.jsp\",\n" +
  " \"error\": \"NO AUTHOR/CLASS-INFO\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"http://ca-smcphbusiness.rhcloud.com/ca2/index.jsp\",\n" +
  " \"authors\": \"Mikkel, Steffen, B Andersen\",\n" +
  " \"class\": \"A Class Computer Science\",\n" +
  " \"group\": \"1\"\n" +
  " }\n" +
  " ]\n" +
  "}";
  // When
  JsonPath jsonPath = new JsonPath(json);
   
  // Then
  assertThat(jsonPath.getList("groups.getAt('class')", String.class), hasItems("A klassen"));
  }
   
  @Test public void
  automatically_escapes_class_property() {
  // Given
  String json = "{\n" +
  " \"semester\": \"Fall 2015\",\n" +
  " \"groups\": [\n" +
  " {\n" +
  " \"siteUrl\": \"http://cphbusinessjb.cloudapp.net/CA2/\",\n" +
  " \"error\": \"NO AUTHOR/CLASS-INFO\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"http://ca2-ebski.rhcloud.com/CA2New/\",\n" +
  " \"authors\": \"Ebbe, Kasper, Christoffer\",\n" +
  " \"class\": \"A klassen\",\n" +
  " \"group\": \"Gruppe: Johns Llama Herders A/S\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"http://ca2-chrislind.rhcloud.com/CA2Final/\",\n" +
  " \"error\": \"NO AUTHOR/CLASS-INFO\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"http://ca2-pernille.rhcloud.com/NYCA2/\",\n" +
  " \"authors\": \"Marta, Jeanette, Pernille\",\n" +
  " \"class\": \"DAT A\",\n" +
  " \"group\": \"Group: MJP\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"https://ca2-afn.rhcloud.com:8443/company.jsp\",\n" +
  " \"error\": \"NO AUTHOR/CLASS-INFO\"\n" +
  " },\n" +
  " {\n" +
  " \"siteUrl\": \"http://ca-smcphbusiness.rhcloud.com/ca2/index.jsp\",\n" +
  " \"authors\": \"Mikkel, Steffen, B Andersen\",\n" +
  " \"class\": \"A Class Computer Science\",\n" +
  " \"group\": \"1\"\n" +
  " }\n" +
  " ]\n" +
  "}";
  // When
  JsonPath jsonPath = new JsonPath(json);
   
  // Then
  assertThat(jsonPath.getList("groups.class", String.class), hasItems("A klassen"));
  }
   
  /**
  * Asserts that https://github.com/jayway/rest-assured/issues/556 is resolved
  */
  @Test public void
  unicode_json_values_are_pretty_printed_without_unicode_escaping() {
  final String prettyJson = JsonPath.with("{\"some\":\"ŘÍŠŽŤČÝŮŇÚĚĎÁÉÓ\"}").prettyPrint();
   
  assertThat(prettyJson, equalTo("{\n \"some\": \"ŘÍŠŽŤČÝŮŇÚĚĎÁÉÓ\"\n}"));
  }
   
  @Test public void
  need_to_escape_lists_with_hyphen_and_brackets() {
  // Given
  String json = "{ \"some-list[0]\" : [ \"one\", \"two\" ] }";
   
  // When
  JsonPath jsonPath = JsonPath.from(json);
   
  // Then
  assertThat(jsonPath.getString("'some-list[0]'[0]"), equalTo("one"));
  }
   
  @Test public void
  doesnt_need_to_escape_lists_with_hyphen_without_brackets() {
  // Given
  String json = "{ \"some-list\" : [ \"one\", \"two\" ] }";
   
  // When
  JsonPath jsonPath = JsonPath.from(json);
   
  // Then
  assertThat(jsonPath.getString("some-list[0]"), equalTo("one"));
  }
  }

restassured - JsonPath的更多相关文章

  1. rest-assured的JsonPath使用方法总结

    JsonPath对于解析Json格式的数据来说非常简单,比如有下面这样的Json数据: {"lotto":{ "lottoId":5, "winnin ...

  2. 就是这么简单!使用Rest-assured 测试Restful Web Services

    使用 Rest-assured 测试 Restful Web Services 转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html 这里向大家介 ...

  3. REST-assured 3发送图片

    上传图片,需要media_id,从上传临时素材获取:https://work.weixin.qq.com/api/doc#10112 https://qyapi.weixin.qq.com/cgi-b ...

  4. REST-assured 2发送消息代码重构

    将获取token的方法封装到公共类 #java package date811; import io.restassured.response.Response; import org.testng. ...

  5. REST-assured 2发送文字到接口

    获取token https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT #java package ...

  6. rest-assured之静态导入及简单使用实例

    一.静态导入 为了有效的使用rest-assured,官网推荐从下列class中静态导入方法: io.restassured.RestAssured.* io.restassured.matcher. ...

  7. rest-assured之获取响应数据(Getting Response Data)

    我们使用rest-assured可以获得响应内容,比如:我们发起一个get请求 get("/lotto") 并且获得响应内容,我们有多种方式可以实现: // 通过流的方式获得响应内 ...

  8. rest-assured的xmlPath使用方法总结

    xmlPath的使用方法跟JsonPath的使用方法相近,下面简单总结一下: 准备xml文件数据: <records> <car name='HSV Maloo' make='Hol ...

  9. 基于JsonPath和XmlPath的对象映射(Object Mapping)

    rest-assured支持映射Java对象到Json和XML以及从Json和XML中映射到Java对象.Json映射需要在classpath 中有Jackson.Jackson 2或者是Gson,X ...

随机推荐

  1. SecureCRT:保存输出日志的方法

    处理地址: http://blog.sina.com.cn/s/blog_64c1dd210101gzgz.html 或者: http://renchen.blog.51cto.com/4531967 ...

  2. java关键字 super 和 this

    简单粗暴的说就是: super: 是指父类,想要在子类方法中调用父类的实例变量或方法可以通过super 来访问 this:是指当前类,想要访问当前类的实例变量和方法可以使用this,同时可以省略

  3. python--常见模块

    本节大纲: 1.模块介绍 2.time&datetime 3.random. 4.os 5.sys 6.shutil 7.json&picle 8.shelve 9.xml处理 10. ...

  4. 【转载】Log4j详细使用教程

    本文由林炳文Evankaka创作,出处http://blog.csdn.net/evankaka 日志是应用软件中不可缺少的部分,Apache的开源项目Log4j是一个功能强大的日志组件,提供方便的日 ...

  5. [已解决] github merge指定commit

    比如说有两个branch,分别是master和m1,我们在m1上修改的bug怎么merge到master上呢, 怎么merge我不知道,但是有另外一个命令可以做到,比如m1做commit,sha-1为 ...

  6. wireshark常用过滤条件

    抓取指定IP地址的数据流: 如果你的抓包环境下有很多主机正在通讯,可以考虑使用所观察主机的IP地址来进行过滤.以下为IP地址抓包过滤示例: host 10.3.1.1:抓取发到/来自10.3.1.1的 ...

  7. build.gradle文件介绍

    对于以前用Eclipse开发安卓的小伙伴来说,Gradle文件是陌生的. 不同于Eclipse,而Android Studio 是采用Gradle来构建项目的. 先来介绍最外层目录下的build.gr ...

  8. seaJS

    1. seajs是用来进行模块化管理,将每一个功能当做是一个功能模块,在模块之间运用require进行连接,类似于java/C++/C等语言中的类. 2. 在文件html 的尾部引入入seajs的文件 ...

  9. sql数据库不能用localhost/phpMyadmin登陆,真正要修改的文件是哪个

    今天,数据库抽风,在地址栏输入localhost/phpmyadmin尽然显示没有访问权限的英文....查了资料都是有好多解决方案,基本都是先点击绿色的w图标,然后找到apache下的httpd.co ...

  10. android xml中的xliff属性

    <resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:xliff=" ...