1 package dev.aherscu.qa.testing.utils.assertions;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.Reader;
7 import java.io.StringWriter;
8 import java.io.Writer;
9 import java.text.ParseException;
10 import java.util.Collection;
11 import java.util.Map;
12
13 import org.hamcrest.Matcher;
14
15 import com.jayway.jsonpath.JsonPath;
16
17 import dev.aherscu.qa.testing.utils.assertions.impl.*;
18 import dev.aherscu.qa.testing.utils.assertions.impl.matcher.*;
19
20 public class JsonAssert {
21
22
23
24
25
26
27
28
29
30
31 public static JsonAsserter with(String json) {
32 return new JsonAsserterImpl(JsonPath.parse(json).json());
33 }
34
35
36
37
38
39
40
41
42
43
44 public static JsonAsserter with(Reader reader) throws IOException {
45 return new JsonAsserterImpl(
46 JsonPath.parse(convertReaderToString(reader)).json());
47
48 }
49
50
51
52
53
54
55
56
57
58
59 public static JsonAsserter with(InputStream is) throws IOException {
60 Reader reader = new InputStreamReader(is);
61 return with(reader);
62 }
63
64
65
66 public static CollectionMatcher collectionWithSize(
67 Matcher<? super Integer> sizeMatcher) {
68 return new IsCollectionWithSize(sizeMatcher);
69 }
70
71 public static Matcher<Map<String, ?>> mapContainingKey(
72 Matcher<String> keyMatcher) {
73 return new IsMapContainingKey(keyMatcher);
74 }
75
76 public static <V> Matcher<? super Map<?, V>> mapContainingValue(
77 Matcher<? super V> valueMatcher) {
78 return new IsMapContainingValue<V>(valueMatcher);
79 }
80
81 public static Matcher<Collection<Object>> emptyCollection() {
82 return new IsEmptyCollection<Object>();
83 }
84
85 private static String convertReaderToString(Reader reader)
86 throws IOException {
87
88 if (reader != null) {
89 Writer writer = new StringWriter();
90
91 char[] buffer = new char[1024];
92 try {
93 int n;
94 while ((n = reader.read(buffer)) != -1) {
95 writer.write(buffer, 0, n);
96 }
97 } finally {
98 reader.close();
99 }
100 return writer.toString();
101 } else {
102 return "";
103 }
104 }
105
106 }