From 8ebe69f176a827d805f7f055b08a9ed93f8efd24 Mon Sep 17 00:00:00 2001 From: psysane Date: Mon, 11 Jun 2018 00:19:21 +0530 Subject: [PATCH 1/2] Example unit test for the article "Count with JsonPath" --- .../src/main/resources/online_store.json | 21 +++++++++ .../introduction/JsonPathUnitTest.java | 46 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 json-path/src/main/resources/online_store.json create mode 100644 json-path/src/test/java/com/baeldung/jsonpath/introduction/JsonPathUnitTest.java diff --git a/json-path/src/main/resources/online_store.json b/json-path/src/main/resources/online_store.json new file mode 100644 index 0000000000..d8e2402b25 --- /dev/null +++ b/json-path/src/main/resources/online_store.json @@ -0,0 +1,21 @@ +{ + "items": { + "book": [ + { + "author": "Arthur Conan Doyle", + "title": "Sherlock Holmes", + "price": 8.99 + }, + { + "author": "J. R. R. Tolkien", + "title": "The Lord of the Rings", + "isbn": "0-395-19395-8", + "price": 22.99 + } + ], + "bicycle": { + "color": "red", + "price": 19.95 + } + } +} diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/JsonPathUnitTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/JsonPathUnitTest.java new file mode 100644 index 0000000000..6b780c9c10 --- /dev/null +++ b/json-path/src/test/java/com/baeldung/jsonpath/introduction/JsonPathUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.jsonpath.introduction; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Map; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.jayway.jsonpath.JsonPath; + +import net.minidev.json.JSONArray; + +public class JsonPathUnitTest { + + private static String json; + private static File jsonFile = new File("src/main/resources/online_store.json"); + + private static String readFile(File file, Charset charset) throws IOException { + return new String(Files.readAllBytes(file.toPath()), charset); + } + + @BeforeClass + public static void init() throws IOException { + json = readFile(jsonFile, StandardCharsets.UTF_8); + } + + @Test + public void shouldMatchCountOfObjects() { + Map objectMap = JsonPath.read(json, "$"); + assertEquals(1, objectMap.keySet() + .size()); + } + + @Test + public void shouldMatchCountOfArrays() { + JSONArray jsonArray = JsonPath.read(json, "$.items.book[*]"); + assertEquals(2, jsonArray.size()); + } + +} From 36eacfb3c3cf5e56e6c845ce7ab98190f4b3d915 Mon Sep 17 00:00:00 2001 From: psysane Date: Mon, 11 Jun 2018 21:25:53 +0530 Subject: [PATCH 2/2] Update json data --- .../src/main/resources/online_store.json | 42 ++++++++++--------- .../introduction/JsonPathUnitTest.java | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/json-path/src/main/resources/online_store.json b/json-path/src/main/resources/online_store.json index d8e2402b25..c0ddf274d8 100644 --- a/json-path/src/main/resources/online_store.json +++ b/json-path/src/main/resources/online_store.json @@ -1,21 +1,23 @@ { - "items": { - "book": [ - { - "author": "Arthur Conan Doyle", - "title": "Sherlock Holmes", - "price": 8.99 - }, - { - "author": "J. R. R. Tolkien", - "title": "The Lord of the Rings", - "isbn": "0-395-19395-8", - "price": 22.99 - } - ], - "bicycle": { - "color": "red", - "price": 19.95 - } - } -} + "items":{ + "book":[ + { + "author":"Arthur Conan Doyle", + "title":"Sherlock Holmes", + "price":8.99 + }, + { + "author":"J. R. R. Tolkien", + "title":"The Lord of the Rings", + "isbn":"0-395-19395-8", + "price":22.99 + } + ], + "bicycle":{ + "color":"red", + "price":19.95 + } + }, + "url":"mystore.com", + "owner":"baeldung" +} \ No newline at end of file diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/JsonPathUnitTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/JsonPathUnitTest.java index 6b780c9c10..3408629a6d 100644 --- a/json-path/src/test/java/com/baeldung/jsonpath/introduction/JsonPathUnitTest.java +++ b/json-path/src/test/java/com/baeldung/jsonpath/introduction/JsonPathUnitTest.java @@ -33,7 +33,7 @@ public class JsonPathUnitTest { @Test public void shouldMatchCountOfObjects() { Map objectMap = JsonPath.read(json, "$"); - assertEquals(1, objectMap.keySet() + assertEquals(3, objectMap.keySet() .size()); }