View Javadoc
1   package dev.aherscu.qa.testing.utils.assertions;
2   
3   import org.hamcrest.Description;
4   import org.hamcrest.TypeSafeDiagnosingMatcher;
5   
6   import com.jayway.jsonpath.JsonPath;
7   import com.jayway.jsonpath.JsonPathException;
8   import com.jayway.jsonpath.ReadContext;
9   
10  public class WithoutJsonPath extends TypeSafeDiagnosingMatcher<ReadContext> {
11      private final JsonPath jsonPath;
12  
13      public WithoutJsonPath(JsonPath jsonPath) {
14          this.jsonPath = jsonPath;
15      }
16  
17      @Override
18      protected boolean matchesSafely(ReadContext actual,
19          Description mismatchDescription) {
20          try {
21              Object value = actual.read(jsonPath);
22              mismatchDescription
23                  .appendText(jsonPath.getPath())
24                  .appendText(" was evaluated to ")
25                  .appendValue(value);
26              return false;
27          } catch (JsonPathException e) {
28              return true;
29          }
30      }
31  
32      @Override
33      public void describeTo(Description description) {
34          description.appendText("without json path ")
35              .appendValue(jsonPath.getPath());
36      }
37  }