/
IsJsonStringTest.java
71 lines (60 loc) · 2.58 KB
/
IsJsonStringTest.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
package com.jayway.jsonpath.matchers;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.matchers.helpers.StrictParsingConfiguration;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJsonString;
import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource;
import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class IsJsonStringTest {
private static final String BOOKS_JSON = resource("books.json");
@BeforeAll
public static void setupStrictJsonParsing() {
Configuration.setDefaults(new StrictParsingConfiguration());
}
@AfterAll
public static void setupDefaultJsonParsing() {
Configuration.setDefaults(null);
}
@Test
public void shouldMatchJsonStringEvaluatedToTrue() {
assertThat(BOOKS_JSON, isJsonString(withPathEvaluatedTo(true)));
}
@Test
public void shouldNotMatchJsonStringEvaluatedToFalse() {
assertThat(BOOKS_JSON, not(isJsonString(withPathEvaluatedTo(false))));
}
@Test
public void shouldNotMatchInvalidJsonString() {
assertThat("invalid-json", not(isJsonString(withPathEvaluatedTo(true))));
assertThat("invalid-json", not(isJsonString(withPathEvaluatedTo(false))));
}
@Test
public void shouldBeDescriptive() {
Matcher<String> matcher = isJsonString(withPathEvaluatedTo(true));
Description description = new StringDescription();
matcher.describeTo(description);
assertThat(description.toString(), startsWith("is json"));
assertThat(description.toString(), containsString(MATCH_TRUE_TEXT));
}
@Test
public void shouldDescribeMismatchOfValidJson() {
Matcher<String> matcher = isJsonString(withPathEvaluatedTo(true));
Description description = new StringDescription();
matcher.describeMismatch(BOOKS_JSON, description);
assertThat(description.toString(), containsString(MISMATCHED_TEXT));
}
@Test
public void shouldDescribeMismatchOfInvalidJson() {
Matcher<String> matcher = isJsonString(withPathEvaluatedTo(true));
Description description = new StringDescription();
matcher.describeMismatch("invalid-json", description);
assertThat(description.toString(), containsString("\"invalid-json\""));
}
}