1 package dev.aherscu.qa.testing.utils.assertions.impl;
2
3 import static java.lang.String.format;
4 import static org.hamcrest.Matchers.*;
5
6 import org.hamcrest.Matcher;
7
8 import com.jayway.jsonpath.Configuration;
9 import com.jayway.jsonpath.JsonPath;
10 import com.jayway.jsonpath.PathNotFoundException;
11
12 import dev.aherscu.qa.testing.utils.assertions.*;
13
14 public class JsonAsserterImpl implements JsonAsserter {
15
16 private final Object jsonObject;
17
18
19
20
21
22
23
24 public JsonAsserterImpl(Object jsonObject) {
25 this.jsonObject = jsonObject;
26 }
27
28
29
30
31 @SuppressWarnings("unchecked")
32 public <T> JsonAsserter assertThat(String path, Matcher<T> matcher) {
33 T obj = null;
34
35 try {
36 obj = JsonPath.read(jsonObject, path);
37 } catch (Exception e) {
38 final AssertionError assertionError = new AssertionError(
39 String.format("Error reading JSON path [%s]", path), e);
40 throw assertionError;
41 }
42
43 if (!matcher.matches(obj)) {
44
45 throw new AssertionError(String.format(
46 "JSON path [%s] doesn't match.\nExpected:\n%s\nActual:\n%s",
47 path, matcher, obj));
48 }
49 return this;
50 }
51
52
53
54
55 @SuppressWarnings("unchecked")
56 public <T> JsonAsserter assertThat(String path, Matcher<T> matcher,
57 String message) {
58 T obj = JsonPath.read(jsonObject, path);
59 if (!matcher.matches(obj)) {
60 throw new AssertionError(String.format(
61 "JSON Assert Error: %s\nExpected:\n%s\nActual:\n%s", message,
62 matcher, obj));
63 }
64 return this;
65 }
66
67
68
69
70 public <T> JsonAsserter assertEquals(String path, T expected) {
71 return assertThat(path, equalTo(expected));
72 }
73
74
75
76
77 public JsonAsserter assertNotDefined(String path) {
78
79 try {
80 Configuration c = Configuration.defaultConfiguration();
81
82 JsonPath.using(c).parse(jsonObject).read(path);
83 throw new AssertionError(format(
84 "Document contains the path <%s> but was expected not to.",
85 path));
86 } catch (PathNotFoundException e) {
87 }
88 return this;
89 }
90
91 @Override
92 public JsonAsserter assertNotDefined(String path, String message) {
93 try {
94 Configuration c = Configuration.defaultConfiguration();
95
96 JsonPath.using(c).parse(jsonObject).read(path);
97
98 throw new AssertionError(format(
99 "Document contains the path <%s> but was expected not to.",
100 path));
101 } catch (PathNotFoundException e) {
102 }
103 return this;
104 }
105
106
107
108
109 public JsonAsserter assertNull(String path) {
110 return assertThat(path, nullValue());
111 }
112
113 @Override
114 public JsonAsserter assertNull(String path, String message) {
115 return assertThat(path, nullValue(), message);
116 }
117
118 @Override
119 public <T> JsonAsserter assertEquals(String path, T expected,
120 String message) {
121 return assertThat(path, equalTo(expected), message);
122 }
123
124
125
126
127 public <T> JsonAsserter assertNotNull(String path) {
128 return assertThat(path, notNullValue());
129 }
130
131 @Override
132 public <T> JsonAsserter assertNotNull(String path, String message) {
133 return assertThat(path, notNullValue(), message);
134 }
135
136
137
138
139 public JsonAsserter and() {
140 return this;
141 }
142
143 }