View Javadoc
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       * Creates a JSONAsserter
24       *
25       * @param json
26       *            the JSON document to create a JSONAsserter for
27       * @return a JSON asserter initialized with the provided document
28       * @throws ParseException
29       *             when the given JSON could not be parsed
30       */
31      public static JsonAsserter with(String json) {
32          return new JsonAsserterImpl(JsonPath.parse(json).json());
33      }
34  
35      /**
36       * Creates a JSONAsserter
37       *
38       * @param reader
39       *            the reader of the json document
40       * @return a JSON asserter initialized with the provided document
41       * @throws ParseException
42       *             when the given JSON could not be parsed
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       * Creates a JSONAsserter
52       *
53       * @param is
54       *            the input stream
55       * @return a JSON asserter initialized with the provided document
56       * @throws ParseException
57       *             when the given JSON could not be parsed
58       */
59      public static JsonAsserter with(InputStream is) throws IOException {
60          Reader reader = new InputStreamReader(is);
61          return with(reader);
62      }
63  
64      // Matchers
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 }