restassured - JsonPath
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的更多相关文章
- rest-assured的JsonPath使用方法总结
JsonPath对于解析Json格式的数据来说非常简单,比如有下面这样的Json数据: {"lotto":{ "lottoId":5, "winnin ...
- 就是这么简单!使用Rest-assured 测试Restful Web Services
使用 Rest-assured 测试 Restful Web Services 转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html 这里向大家介 ...
- REST-assured 3发送图片
上传图片,需要media_id,从上传临时素材获取:https://work.weixin.qq.com/api/doc#10112 https://qyapi.weixin.qq.com/cgi-b ...
- REST-assured 2发送消息代码重构
将获取token的方法封装到公共类 #java package date811; import io.restassured.response.Response; import org.testng. ...
- REST-assured 2发送文字到接口
获取token https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT #java package ...
- rest-assured之静态导入及简单使用实例
一.静态导入 为了有效的使用rest-assured,官网推荐从下列class中静态导入方法: io.restassured.RestAssured.* io.restassured.matcher. ...
- rest-assured之获取响应数据(Getting Response Data)
我们使用rest-assured可以获得响应内容,比如:我们发起一个get请求 get("/lotto") 并且获得响应内容,我们有多种方式可以实现: // 通过流的方式获得响应内 ...
- rest-assured的xmlPath使用方法总结
xmlPath的使用方法跟JsonPath的使用方法相近,下面简单总结一下: 准备xml文件数据: <records> <car name='HSV Maloo' make='Hol ...
- 基于JsonPath和XmlPath的对象映射(Object Mapping)
rest-assured支持映射Java对象到Json和XML以及从Json和XML中映射到Java对象.Json映射需要在classpath 中有Jackson.Jackson 2或者是Gson,X ...
随机推荐
- linux下安装软件出现问题怎么办
是apt-get -f install=apt-get install -f ,是修复依赖关系(depends)的命令,就是假如你的系统上有某个package不满足依赖条件,这个命令就会自动修复,安装 ...
- java.sql.SQLException: Incorrect key file for table 'C:\Windows\TEMP\#sql578_6e2_68d.MYI'; try to repair it
java.sql.SQLException: Incorrect key file for table 'C:\Windows\TEMP\#sql578_6e2_68d.MYI'; try to re ...
- eclispe或者myeclispe maven jar包不能部署到tomcat下
我们在做web开发是,经常都要在eclipse或者myeclipse中搭建web服务器,并将开发中的web项目部署到web服务器进行调试,在此,我选择的是tomcat服务器.之前部署web项目到tom ...
- XMLPuLL解析
1 package com.bawei.day14_xmlpull; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 i ...
- js 控制Div循环显示 非插件版
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- DOM和DHTML等,复习总结
DOM(Document Object Model),文件对象模型.HTML(HyperText Markup Language),超文本标记语言.HTML的超类:Node->Document: ...
- Linux时间戳和标准时间的互转
转http://hi.baidu.com/taolizao/blog/item/2d6f9a1ba50ef3eae0fe0ba9.html 在LINUX系统中,有许多场合都使用时间戳的方式表示时间 ...
- Java项目多数据源配置
由于种种原因,有的时候可能要连接别人的数据库,或者不同的数据库没法自动转换,重构起来数据量又太大了,我们不得不在一个项目中连接多个数据源.从网上找了各种资料,只有这位大神给出的解决方案一下子就成功了. ...
- Android 本地/网路下载图片实现放大缩小
Android 本地加载/网路下载图片实现放大缩小拖拉效果,自定义控件. package com.example.ImageViewCustom; import android.app.Activi ...
- webapi aspose导出excel表格
API 通过get请求,注意用到一个[FromUri]特性,使GET接收实体参数 /// <summary> /// 导出 /// </summary> /// <par ...