Java 11497 (#12399)

* Added/created parent module (json-modules)

* moved json(submodule) to json-modules(parent)

* moved json-2(submodule) to json-modules(parent)

* moved json-path(submodule) to json-modules(parent)

* moved gson(submodule) to json-modules(parent)

* deleted sub-modules that we moved to json-modules

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos
2022-06-24 17:28:34 +01:00
committed by GitHub
parent 86d5df3e85
commit 7866faf761
163 changed files with 377 additions and 336 deletions

View File

@@ -0,0 +1,57 @@
{
"tool":
{
"jsonpath":
{
"creator":
{
"name": "Jayway Inc.",
"location":
[
"Malmo",
"Stockholm",
"Copenhagen",
"San Francisco",
"Karlskrona",
"Halmstad",
"Helsingborg"
]
},
"current release": "2.1"
}
},
"book":
[
{
"title": "Beginning JSON",
"author": "Ben Smith",
"price": 49.99
},
{
"title": "JSON at Work",
"author": "Tom Marrs",
"price": 29.99
},
{
"title": "Learn JSON in a DAY",
"author": "Acodemy",
"price": 8.99
},
{
"title": "JSON: Questions and Answers",
"author": "George Duckett",
"price": 6.00
}
],
"price range":
{
"cheap": 10.00,
"medium": 20.00
}
}

View File

@@ -0,0 +1,61 @@
[
{
"id": 1,
"title": "Casino Royale",
"director": "Martin Campbell",
"starring":
[
"Daniel Craig",
"Eva Green"
],
"desc": "Twenty-first James Bond movie",
"release date": 1163466000000,
"box office": 594275385
},
{
"id": 2,
"title": "Quantum of Solace",
"director": "Marc Forster",
"starring":
[
"Daniel Craig",
"Olga Kurylenko"
],
"desc": "Twenty-second James Bond movie",
"release date": 1225242000000,
"box office": 591692078
},
{
"id": 3,
"title": "Skyfall",
"director": "Sam Mendes",
"starring":
[
"Daniel Craig",
"Naomie Harris"
],
"desc": "Twenty-third James Bond movie",
"release date": 1350954000000,
"box office": 1110526981
},
{
"id": 4,
"title": "Spectre",
"director": "Sam Mendes",
"starring":
[
"Daniel Craig",
"Lea Seydoux"
],
"desc": "Twenty-fourth James Bond movie",
"release date": 1445821200000,
"box office": 879376275
}
]

View File

@@ -0,0 +1,46 @@
[
{
"username": "oracle",
"password":
{
"current":
{
"value": "Java_SE_8",
"created": 1397754000000
},
"old":
[
{
"value": "Java_SE_7",
"created": 1312650000000
}
]
}
},
{
"username": "sun",
"password":
{
"current":
{
"value": "Java_SE_6",
"created": 1168448400000
},
"old":
[
{
"value": "J2SE_5.0",
"created": 1099069200000
},
{
"value": "J2SE_1.4",
"created": 1025542800000
}
]
}
}
]

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +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
}
},
"url":"mystore.com",
"owner":"baeldung"
}

View File

@@ -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<String, String> objectMap = JsonPath.read(json, "$");
assertEquals(3, objectMap.keySet()
.size());
}
@Test
public void shouldMatchCountOfArrays() {
JSONArray jsonArray = JsonPath.read(json, "$.items.book[*]");
assertEquals(2, jsonArray.size());
}
}

View File

@@ -0,0 +1,74 @@
package com.baeldung.jsonpath.introduction;
import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class OperationIntegrationTest {
private InputStream jsonInputStream = this.getClass()
.getClassLoader()
.getResourceAsStream("intro_api.json");
private String jsonDataSourceString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z")
.next();
@Test
public void givenJsonPathWithoutPredicates_whenReading_thenCorrect() {
String jsonpathCreatorNamePath = "$['tool']['jsonpath']['creator']['name']";
String jsonpathCreatorLocationPath = "$['tool']['jsonpath']['creator']['location'][*]";
DocumentContext jsonContext = JsonPath.parse(jsonDataSourceString);
String jsonpathCreatorName = jsonContext.read(jsonpathCreatorNamePath);
List<String> jsonpathCreatorLocation = jsonContext.read(jsonpathCreatorLocationPath);
assertEquals("Jayway Inc.", jsonpathCreatorName);
assertThat(jsonpathCreatorLocation.toString(), containsString("Malmo"));
assertThat(jsonpathCreatorLocation.toString(), containsString("San Francisco"));
assertThat(jsonpathCreatorLocation.toString(), containsString("Helsingborg"));
}
@Test
public void givenJsonPathWithFilterPredicate_whenReading_thenCorrect() {
Filter expensiveFilter = Filter.filter(Criteria.where("price")
.gt(20.00));
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString)
.read("$['book'][?]", expensiveFilter);
predicateUsageAssertionHelper(expensive);
}
@Test
public void givenJsonPathWithCustomizedPredicate_whenReading_thenCorrect() {
Predicate expensivePredicate = context -> Float.valueOf(context.item(Map.class)
.get("price")
.toString()) > 20.00;
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString)
.read("$['book'][?]", expensivePredicate);
predicateUsageAssertionHelper(expensive);
}
@Test
public void givenJsonPathWithInlinePredicate_whenReading_thenCorrect() {
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString)
.read("$['book'][?(@['price'] > $['price range']['medium'])]");
predicateUsageAssertionHelper(expensive);
}
private void predicateUsageAssertionHelper(List<?> predicate) {
assertThat(predicate.toString(), containsString("Beginning JSON"));
assertThat(predicate.toString(), containsString("JSON at Work"));
assertThat(predicate.toString(), not(containsString("Learn JSON in a DAY")));
assertThat(predicate.toString(), not(containsString("JSON: Questions and Answers")));
}
}

View File

@@ -0,0 +1,101 @@
package com.baeldung.jsonpath.introduction;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import org.junit.Test;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class ServiceIntegrationTest {
private InputStream jsonInputStream = this.getClass()
.getClassLoader()
.getResourceAsStream("intro_service.json");
private String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z")
.next();
@Test
public void givenId_whenRequestingRecordData_thenSucceed() {
Object dataObject = JsonPath.parse(jsonString)
.read("$[?(@.id == 2)]");
String dataString = dataObject.toString();
assertThat(dataString, containsString("2"));
assertThat(dataString, containsString("Quantum of Solace"));
assertThat(dataString, containsString("Twenty-second James Bond movie"));
}
@Test
public void givenStarring_whenRequestingMovieTitle_thenSucceed() {
List<Map<String, Object>> dataList = JsonPath.parse(jsonString)
.read("$[?('Eva Green' in @['starring'])]");
String title = (String) dataList.get(0)
.get("title");
assertEquals("Casino Royale", title);
}
@Test
public void givenCompleteStructure_whenCalculatingTotalRevenue_thenSucceed() {
DocumentContext context = JsonPath.parse(jsonString);
int length = context.read("$.length()");
long revenue = 0;
for (int i = 0; i < length; i++) {
revenue += context.read("$[" + i + "]['box office']", Long.class);
}
assertEquals(594275385L + 591692078L + 1110526981L + 879376275L, revenue);
}
@Test
public void givenStructure_whenRequestingHighestRevenueMovieTitle_thenSucceed() {
DocumentContext context = JsonPath.parse(jsonString);
List<Object> revenueList = context.read("$[*]['box office']");
Integer[] revenueArray = revenueList.toArray(new Integer[0]);
Arrays.sort(revenueArray);
int highestRevenue = revenueArray[revenueArray.length - 1];
Configuration pathConfiguration = Configuration.builder()
.options(Option.AS_PATH_LIST)
.build();
List<String> pathList = JsonPath.using(pathConfiguration)
.parse(jsonString)
.read("$[?(@['box office'] == " + highestRevenue + ")]");
Map<String, String> dataRecord = context.read(pathList.get(0));
String title = dataRecord.get("title");
assertEquals("Skyfall", title);
}
@Test
public void givenDirector_whenRequestingLatestMovieTitle_thenSucceed() {
DocumentContext context = JsonPath.parse(jsonString);
List<Map<String, Object>> dataList = context.read("$[?(@.director == 'Sam Mendes')]");
List<Object> dateList = new ArrayList<>();
for (Map<String, Object> item : dataList) {
Object date = item.get("release date");
dateList.add(date);
}
Long[] dateArray = dateList.toArray(new Long[0]);
Arrays.sort(dateArray);
long latestTime = dateArray[dateArray.length - 1];
List<Map<String, Object>> finalDataList = context.read("$[?(@['director'] == 'Sam Mendes' && @['release date'] == " + latestTime + ")]");
String title = (String) finalDataList.get(0)
.get("title");
assertEquals("Spectre", title);
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>