/
WithJsonPathTest.java
124 lines (106 loc) · 5.42 KB
/
WithJsonPathTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.jayway.jsonpath.matchers;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.List;
import static com.jayway.jsonpath.JsonPath.compile;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.withJsonPath;
import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class WithJsonPathTest {
private static final ReadContext BOOKS_JSON = JsonPath.parse(resource("books.json"));
@Test
public void shouldMatchExistingCompiledJsonPath() {
assertThat(BOOKS_JSON, withJsonPath(compile("$.expensive")));
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.bicycle")));
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[2].title")));
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author")));
}
@Test
public void shouldMatchExistingStringJsonPath() {
assertThat(BOOKS_JSON, withJsonPath("$.expensive"));
assertThat(BOOKS_JSON, withJsonPath("$.store.bicycle"));
assertThat(BOOKS_JSON, withJsonPath("$.store.book[2].title"));
assertThat(BOOKS_JSON, withJsonPath("$.store.book[*].author"));
}
@Test
public void shouldNotMatchNonExistingJsonPath() {
assertThat(BOOKS_JSON, not(withJsonPath(compile("$.not_there"))));
assertThat(BOOKS_JSON, not(withJsonPath(compile("$.store.book[5].title"))));
assertThat(BOOKS_JSON, not(withJsonPath(compile("$.store.book[1].not_there"))));
}
@Test
public void shouldNotMatchNonExistingStringJsonPath() {
assertThat(BOOKS_JSON, not(withJsonPath("$.not_there")));
assertThat(BOOKS_JSON, not(withJsonPath("$.store.book[5].title")));
assertThat(BOOKS_JSON, not(withJsonPath("$.store.book[1].not_there")));
}
@Test
public void shouldMatchJsonPathEvaluatedToStringValue() {
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.bicycle.color"), equalTo("red")));
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[2].title"), equalTo("Moby Dick")));
assertThat(BOOKS_JSON, withJsonPath("$.store.name", equalTo("Little Shop")));
assertThat(BOOKS_JSON, withJsonPath("$.store.book[2].title", equalTo("Moby Dick")));
}
@Test
public void shouldMatchJsonPathEvaluatedToIntegerValue() {
assertThat(BOOKS_JSON, withJsonPath(compile("$.expensive"), equalTo(10)));
assertThat(BOOKS_JSON, withJsonPath("$.expensive", equalTo(10)));
}
@Test
public void shouldMatchJsonPathEvaluatedToDoubleValue() {
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.bicycle.price"), equalTo(19.95)));
assertThat(BOOKS_JSON, withJsonPath("$.store.bicycle.price", equalTo(19.95)));
}
@Test
public void shouldMatchJsonPathEvaluatedToCollectionValue() {
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), instanceOf(List.class)));
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), hasSize(4)));
assertThat(BOOKS_JSON, withJsonPath(compile("$.store.book[*].author"), hasItem("Evelyn Waugh")));
assertThat(BOOKS_JSON, withJsonPath(compile("$..book[2].title"), hasItem("Moby Dick")));
assertThat(BOOKS_JSON, withJsonPath("$.store.book[*].author", instanceOf(Collection.class)));
assertThat(BOOKS_JSON, withJsonPath("$.store.book[*].author", hasSize(4)));
assertThat(BOOKS_JSON, withJsonPath("$.store.book[*].author", hasItem("Evelyn Waugh")));
assertThat(BOOKS_JSON, withJsonPath("$..book[2].title", hasItem("Moby Dick")));
}
@Test
public void shouldFailOnInvalidJsonPath() {
assertThrows(InvalidPathException.class, () -> withJsonPath("$[}"));
}
@Test
public void shouldNotMatchOnInvalidJson() {
ReadContext invalidJson = JsonPath.parse("invalid-json");
assertThat(invalidJson, not(withJsonPath("$.expensive", equalTo(10))));
}
@Test
public void shouldBeDescriptive() {
Matcher<? super ReadContext> matcher = withJsonPath("path", equalTo(2));
Description description = new StringDescription();
matcher.describeTo(description);
assertThat(description.toString(), containsString("path"));
assertThat(description.toString(), containsString("<2>"));
}
@Test
public void shouldDescribeMismatchOfEvaluation() {
Matcher<? super ReadContext> matcher = withJsonPath("expensive", equalTo(3));
Description description = new StringDescription();
matcher.describeMismatch(BOOKS_JSON, description);
assertThat(description.toString(), containsString("expensive"));
assertThat(description.toString(), containsString("<10>"));
}
@Test
public void shouldDescribeMismatchOfPathNotFound() {
Matcher<? super ReadContext> matcher = withJsonPath("not-here", equalTo(3));
Description description = new StringDescription();
matcher.describeMismatch(BOOKS_JSON, description);
assertThat(description.toString(), containsString("not-here"));
assertThat(description.toString(), containsString("was not found"));
}
}