Skip to content

Commit 2182576

Browse files
committed
Resolve conflicts in JsonPath examples
2 parents cf4cce7 + 516729b commit 2182576

File tree

6 files changed

+166
-191
lines changed

6 files changed

+166
-191
lines changed

json-path/pom.xml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
<version>${json-path.version}</version>
1515
</dependency>
1616

17-
<!-- utilities -->
18-
<dependency>
19-
<groupId>joda-time</groupId>
20-
<artifactId>joda-time</artifactId>
21-
<version>${joda-time.version}</version>
22-
</dependency>
23-
2417
<!-- Testing -->
2518
<dependency>
2619
<groupId>junit</groupId>
@@ -44,13 +37,24 @@
4437
</dependency>
4538
</dependencies>
4639

40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.5.1</version>
46+
<configuration>
47+
<source>1.8</source>
48+
<target>1.8</target>
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
4754
<properties>
4855
<!-- json-path -->
4956
<json-path.version>2.1.0</json-path.version>
5057

51-
<!-- utilities -->
52-
<joda-time.version>2.9.2</joda-time.version>
53-
5458
<!-- Testing -->
5559
<junit.version>4.12</junit.version>
5660

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[
2+
{
3+
"id": 1,
4+
"title": "Casino Royale",
5+
"director": "Martin Campbell",
6+
"starring":
7+
[
8+
"Daniel Craig",
9+
"Eva Green"
10+
],
11+
12+
"desc": "Twenty-first James Bond movie",
13+
"release date": 1163466000000,
14+
"box office": 594275385
15+
},
16+
17+
{
18+
"id": 2,
19+
"title": "Quantum of Solace",
20+
"director": "Marc Forster",
21+
"starring":
22+
[
23+
"Daniel Craig",
24+
"Olga Kurylenko"
25+
],
26+
27+
"desc": "Twenty-second James Bond movie",
28+
"release date": 1225242000000,
29+
"box office": 591692078
30+
},
31+
32+
{
33+
"id": 3,
34+
"title": "Skyfall",
35+
"director": "Sam Mendes",
36+
"starring":
37+
[
38+
"Daniel Craig",
39+
"Naomie Harris"
40+
],
41+
42+
"desc": "Twenty-third James Bond movie",
43+
"release date": 1350954000000,
44+
"box office": 1110526981
45+
},
46+
47+
{
48+
"id": 4,
49+
"title": "Spectre",
50+
"director": "Sam Mendes",
51+
"starring":
52+
[
53+
"Daniel Craig",
54+
"Lea Seydoux"
55+
],
56+
57+
"desc": "Twenty-fourth James Bond movie",
58+
"release date": 1445821200000,
59+
"box office": 879376275
60+
}
61+
]

json-path/src/test/java/com/baeldung/jsonpath/introduction/ChangingPasswordTest.java

Lines changed: 0 additions & 85 deletions
This file was deleted.

json-path/src/test/java/com/baeldung/jsonpath/introduction/LoggingInTest.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

json-path/src/test/java/com/baeldung/jsonpath/introduction/RegisteringAccountTest.java

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.jsonpath.introduction;
2+
3+
import static org.junit.Assert.assertThat;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.hamcrest.CoreMatchers.containsString;
6+
7+
import org.junit.Test;
8+
9+
import java.io.InputStream;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.Scanner;
15+
16+
import com.jayway.jsonpath.Configuration;
17+
import com.jayway.jsonpath.DocumentContext;
18+
import com.jayway.jsonpath.JsonPath;
19+
import com.jayway.jsonpath.Option;
20+
21+
public class ServiceTest {
22+
InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_service.json");
23+
String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
24+
25+
@Test
26+
public void givenId_whenRequestingRecordData_thenSucceed() {
27+
Object dataObject = JsonPath.parse(jsonString).read("$[?(@.id == 2)]");
28+
String dataString = dataObject.toString();
29+
30+
assertThat(dataString, containsString("2"));
31+
assertThat(dataString, containsString("Quantum of Solace"));
32+
assertThat(dataString, containsString("Twenty-second James Bond movie"));
33+
}
34+
35+
@Test
36+
public void givenStarring_whenRequestingMovieTitle_thenSucceed() {
37+
List<Map<String, Object>> dataList = JsonPath.parse(jsonString).read("$[?('Eva Green' in @['starring'])]");
38+
String title = (String) dataList.get(0).get("title");
39+
40+
assertEquals("Casino Royale", title);
41+
}
42+
43+
@Test
44+
public void givenCompleteStructure_whenCalculatingTotalRevenue_thenSucceed() {
45+
DocumentContext context = JsonPath.parse(jsonString);
46+
int length = context.read("$.length()");
47+
long revenue = 0;
48+
for (int i = 0; i < length; i++) {
49+
revenue += context.read("$[" + i + "]['box office']", Long.class);
50+
}
51+
52+
assertEquals(594275385L + 591692078L + 1110526981L + 879376275L, revenue);
53+
}
54+
55+
@Test
56+
public void givenStructure_whenRequestingHighestRevenueMovieTitle_thenSucceed() {
57+
DocumentContext context = JsonPath.parse(jsonString);
58+
List<Object> revenueList = context.read("$[*]['box office']");
59+
Integer[] revenueArray = revenueList.toArray(new Integer[0]);
60+
Arrays.sort(revenueArray);
61+
62+
int highestRevenue = revenueArray[revenueArray.length - 1];
63+
Configuration pathConfiguration = Configuration.builder().options(Option.AS_PATH_LIST).build();
64+
List<String> pathList = JsonPath.using(pathConfiguration).parse(jsonString).read("$[?(@['box office'] == " + highestRevenue + ")]");
65+
66+
Map<String, String> dataRecord = context.read(pathList.get(0));
67+
String title = dataRecord.get("title");
68+
69+
assertEquals("Skyfall", title);
70+
}
71+
72+
@Test
73+
public void givenDirector_whenRequestingLatestMovieTitle_thenSucceed() {
74+
DocumentContext context = JsonPath.parse(jsonString);
75+
List<Map<String, Object>> dataList = context.read("$[?(@.director == 'Sam Mendes')]");
76+
77+
List<Object> dateList = new ArrayList<>();
78+
for (Map<String, Object> item : dataList) {
79+
Object date = item.get("release date");
80+
dateList.add(date);
81+
}
82+
Long[] dateArray = dateList.toArray(new Long[0]);
83+
Arrays.sort(dateArray);
84+
85+
long latestTime = dateArray[dateArray.length - 1];
86+
List<Map<String, Object>> finalDataList = context.read("$[?(@['director'] == 'Sam Mendes' && @['release date'] == " + latestTime + ")]");
87+
String title = (String) finalDataList.get(0).get("title");
88+
89+
assertEquals("Spectre", title);
90+
}
91+
}

0 commit comments

Comments
 (0)