Skip to content

Commit

Permalink
#157 Marking unsupported operations as unsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Jan 11, 2019
1 parent 8d49d8a commit 3f1c773
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import java.util.Map;

import static net.javacrumbs.jsonunit.core.Option.IGNORING_EXTRA_FIELDS;
import static net.javacrumbs.jsonunit.core.Option.TREATING_NULL_AS_ABSENT;
import static org.assertj.core.error.ShouldContainValue.shouldContainValue;
import static org.assertj.core.error.ShouldNotContainValue.shouldNotContainValue;

Expand All @@ -41,10 +43,7 @@ class JsonMapAssert extends MapAssert<String, Object> {

@Override
public JsonMapAssert isEqualTo(Object expected) {
describedAs(null);
Diff diff = Diff.create(expected, actual, "fullJson", path, configuration);
diff.failIfDifferent();
return this;
return compare(expected, configuration);
}

@Override
Expand All @@ -71,6 +70,42 @@ public MapAssert<String, Object> doesNotContainValue(Object expected) {
}
}

@Override
public JsonMapAssert isEqualToIgnoringGivenFields(Object other, String... propertiesOrFieldsToIgnore) {
return compare(other, configuration.whenIgnoringPaths(propertiesOrFieldsToIgnore));
}

@Override
public MapAssert<String, Object> isEqualToComparingOnlyGivenFields(Object other, String... propertiesOrFieldsUsedInComparison) {
throw unsupportedOperation();
}

@Override
public MapAssert<String, Object> isEqualToIgnoringNullFields(Object other) {
throw unsupportedOperation();
}

@Override
public MapAssert<String, Object> isEqualToComparingFieldByField(Object other) {
throw unsupportedOperation();
}

@Override
public MapAssert<String, Object> isEqualToComparingFieldByFieldRecursively(Object other) {
throw unsupportedOperation();
}

private UnsupportedOperationException unsupportedOperation() {
return new UnsupportedOperationException("Operation not supported for JSON documents");
}

private JsonMapAssert compare(Object other, Configuration configuration) {
describedAs(null);
Diff diff = Diff.create(other, actual, "fullJson", path, configuration);
diff.failIfDifferent();
return this;
}

private boolean contains(Object expected) {
return actual.entrySet().stream().anyMatch(kv -> Diff.create(expected, kv.getValue(), "fullJson", path.asPrefix(), configuration).similar());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
*/
package net.javacrumbs.jsonunit.test.base;

import net.javacrumbs.jsonunit.ConfigurableJsonMatcher;
import net.javacrumbs.jsonunit.JsonMatchers;
import net.javacrumbs.jsonunit.assertj.JsonAssert.ConfigurableJsonAssert;
import net.javacrumbs.jsonunit.core.Option;
import net.javacrumbs.jsonunit.fluent.JsonFluentAssert;
import net.javacrumbs.jsonunit.core.ParametrizedMatcher;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;

import static java.math.BigDecimal.valueOf;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json;
import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
Expand Down Expand Up @@ -866,6 +872,14 @@ void shouldWorkWithPercentSign() {
"Different keys found in node \"\", missing: \"%\", extra: \"a\", expected: <{\"%\":\"2\"}> but was: <{\"a\":\"1\"}>\n");
}

@Test
void shouldIgnoreFields() {
assertThatJson("{\"root\":{\"test\":1, \"ignored\": 1}}")
.isObject()
.isEqualToIgnoringGivenFields("{\"root\":{\"test\":1, \"ignored\": 2}}", "root.ignored");

}

// *****************************************************************************************************************
// ********************************************** JSON Path ********************************************************
// *****************************************************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,64 @@
*/
package net.javacrumbs.jsonunit.test.all;

import net.javacrumbs.jsonunit.ConfigurableJsonMatcher;
import net.javacrumbs.jsonunit.core.ParametrizedMatcher;
import net.javacrumbs.jsonunit.test.base.AbstractAssertJTest;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.jupiter.api.Test;

import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static net.javacrumbs.jsonunit.test.base.JsonTestUtils.readByJackson2;

public class AllAssertJTest extends AbstractAssertJTest {
@Override
protected Object readValue(String value) {
return readByJackson2(value);
}

@Test
void embeddedJsonShouldBeParsed() {
String actual = "{" +
"\"response\": {" +
"\"body\": \"{'message':'Success','time-sent':'2018-12-10T14:49:11.537Z[GMT]'}\"" +
"}" +
"}";

String expected = "{" +
"\"response\": {" +
"\"body\": \"${json-unit.matches:embeddedJson}{'message':'Success','time-sent':'${json-unit.ignore}'}\"" +
"}" +
"}";

assertThatJson(actual).withConfiguration(c->c.withMatcher("embeddedJson", new EmbeddedJsonMatcher())).isEqualTo(expected);
}


static private class EmbeddedJsonMatcher extends BaseMatcher<Object> implements ParametrizedMatcher {
private ConfigurableJsonMatcher<Object> embeddedMatcher;


@Override
public void setParameter(String parameter) {
embeddedMatcher = jsonEquals(parameter);
}

@Override
public boolean matches(Object item) {
// leniently parse
return embeddedMatcher.matches(new org.json.JSONObject((String)item));
}

@Override
public void describeTo(Description description) {
embeddedMatcher.describeTo(description);
}

@Override
public void describeMismatch(Object item, Description description) {
embeddedMatcher.describeMismatch(item, description);
}
}
}

0 comments on commit 3f1c773

Please sign in to comment.