com.jayway.jsonassert.impl.JsonAsserterImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-path-assert Show documentation
Show all versions of json-path-assert Show documentation
A library to query and verify JSON
package com.jayway.jsonassert.impl;
import com.jayway.jsonassert.JsonAsserter;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import org.hamcrest.Matcher;
import static java.lang.String.format;
import static org.hamcrest.Matchers.*;
/**
* User: kalle stenflo
* Date: 1/21/11
* Time: 3:43 PM
*/
public class JsonAsserterImpl implements JsonAsserter {
private final Object jsonObject;
/**
* Instantiates a new JSONAsserter
*
* @param jsonObject the object to make asserts on
*/
public JsonAsserterImpl(Object jsonObject) {
this.jsonObject = jsonObject;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public JsonAsserter assertThat(String path, Matcher matcher) {
T obj = JsonPath.read(jsonObject, path);
if (!matcher.matches(obj)) {
throw new AssertionError(String.format("JSON doesn't match.\nExpected:\n%s\nActual:\n%s", matcher.toString(), obj));
}
return this;
}
/**
* {@inheritDoc}
*/
public JsonAsserter assertEquals(String path, T expected) {
return assertThat(path, equalTo(expected));
}
/**
* {@inheritDoc}
*/
public JsonAsserter assertNotDefined(String path) {
try {
Object o = JsonPath.read(jsonObject, path);
throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path));
} catch (InvalidPathException e) {
}
return this;
}
/**
* {@inheritDoc}
*/
public JsonAsserter assertNull(String path) {
return assertThat(path, nullValue());
}
/**
* {@inheritDoc}
*/
public JsonAsserter assertNotNull(String path) {
return assertThat(path, notNullValue());
}
/**
* {@inheritDoc}
*/
public JsonAsserter and() {
return this;
}
}