1 package dev.aherscu.qa.testing.utils.assertions; 2 3 import org.hamcrest.Matcher; 4 5 public interface JsonAsserter { 6 7 /** 8 * Asserts that object specified by path satisfies the condition specified 9 * by matcher. If not, an AssertionError is thrown with information about 10 * the matcher and failing value. Example: 11 * <p/> 12 * <code> 13 * with(json).assertThat("items[0].name", equalTo("Bobby")) 14 * .assertThat("items[0].age" , equalTo(24L)) 15 * </code> 16 * 17 * @param path 18 * the json path specifying the value being compared 19 * @param matcher 20 * an expression, built of Matchers, specifying allowed values 21 * @param <T> 22 * the static type accepted by the matcher 23 * @return this to allow fluent assertion chains 24 */ 25 <T> JsonAsserter assertThat(String path, Matcher<T> matcher); 26 27 /** 28 * @param path 29 * the json path specifying the value being compared 30 * @param matcher 31 * an expression, built of Matchers, specifying allowed values 32 * @param message 33 * the explanation message 34 * @param <T> 35 * the static type that should be returned by the path 36 * @return this to allow fluent assertion chains 37 */ 38 <T> JsonAsserter assertThat(String path, Matcher<T> matcher, 39 String message); 40 41 /** 42 * Asserts that object specified by path is equal to the expected value. If 43 * they are not, an AssertionError is thrown with the given message. 44 * 45 * @param path 46 * the json path specifying the value being compared 47 * @param expected 48 * the expected value 49 * @param <T> 50 * the static type that should be returned by the path 51 * @return this to allow fluent assertion chains 52 */ 53 <T> JsonAsserter assertEquals(String path, T expected); 54 55 <T> JsonAsserter assertEquals(String path, T expected, String message); 56 57 /** 58 * Checks that a path is not defined within a document. If the document 59 * contains the given path, an AssertionError is thrown 60 * 61 * @param path 62 * the path to make sure not exists 63 * @return this 64 */ 65 JsonAsserter assertNotDefined(String path); 66 67 JsonAsserter assertNotDefined(String path, String message); 68 69 /** 70 * Asserts that object specified by path is null. If it is not, an 71 * AssertionError is thrown with the given message. 72 * 73 * @param path 74 * the json path specifying the value that should be null 75 * @return this to allow fluent assertion chains 76 */ 77 JsonAsserter assertNull(String path); 78 79 JsonAsserter assertNull(String path, String message); 80 81 /** 82 * Asserts that object specified by path is NOT null. If it is, an 83 * AssertionError is thrown with the given message. 84 * 85 * @param path 86 * the json path specifying the value that should be NOT null 87 * @return this to allow fluent assertion chains 88 */ 89 <T> JsonAsserter assertNotNull(String path); 90 91 <T> JsonAsserter assertNotNull(String path, String message); 92 93 /** 94 * Syntactic sugar to allow chaining assertions with a separating and() 95 * statement 96 * <p/> 97 * <p/> 98 * <code> 99 * with(json).assertThat("firstName", is(equalTo("Bobby"))).and().assertThat("lastName", is(equalTo("Ewing"))) 100 * </code> 101 * 102 * @return this to allow fluent assertion chains 103 */ 104 JsonAsserter and(); 105 }