|
|
|
package com.jayway.jsonpath;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import static java.util.Arrays.asList;
|
|
|
|
import static junit.framework.Assert.assertEquals;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by IntelliJ IDEA.
|
|
|
|
* User: kallestenflo
|
|
|
|
* Date: 11/8/11
|
|
|
|
* Time: 7:52 PM
|
|
|
|
*/
|
|
|
|
public class JsonModelTest {
|
|
|
|
|
|
|
|
public final static String DOCUMENT =
|
|
|
|
"{ \"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.99\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" +
|
|
|
|
" \"foo:bar\": \"fooBar\",\n" +
|
|
|
|
" \"dot.notation\": \"new\"\n" +
|
|
|
|
" }\n" +
|
|
|
|
" }\n" +
|
|
|
|
"}";
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void a_json_document_can_be_fetched_with_a_URL() throws Exception {
|
|
|
|
URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json");
|
|
|
|
assertEquals("REQUEST_DENIED", JsonModel.create(url).get("status"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void a_json_document_can_be_fetched_with_a_InputStream() throws Exception {
|
|
|
|
ByteArrayInputStream bis = new ByteArrayInputStream(DOCUMENT.getBytes());
|
|
|
|
assertEquals("Nigel Rees", JsonModel.create(bis).get("store.book[0].author"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test_a_sub_model_can_be_fetched_and_read() throws Exception {
|
|
|
|
JsonModel model = JsonModel.create(DOCUMENT);
|
|
|
|
assertEquals("Nigel Rees", model.getSubModel("$store.book[0]").get("author"));
|
|
|
|
assertEquals("Nigel Rees", model.getSubModel(JsonPath.compile("$store.book[0]")).get("author"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void maps_and_list_can_queried() throws Exception {
|
|
|
|
Map<String, Object> doc = new HashMap<String, Object>();
|
|
|
|
doc.put("items", asList(0, 1, 2));
|
|
|
|
doc.put("child", Collections.singletonMap("key", "value"));
|
|
|
|
|
|
|
|
JsonModel model = JsonModel.create(doc);
|
|
|
|
|
|
|
|
assertEquals("value", model.get("$child.key"));
|
|
|
|
assertEquals(1, model.get("$items[1]"));
|
|
|
|
assertEquals("{\"child\":{\"key\":\"value\"},\"items\":[0,1,2]}", model.toJson());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = InvalidPathException.class)
|
|
|
|
public void invalid_path_throws() throws Exception {
|
|
|
|
JsonModel.create(DOCUMENT).get("store.invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|