Skip to content

Commit

Permalink
Readme updated
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Jun 19, 2020
1 parent 4c89254 commit fba4c65
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 45 deletions.
84 changes: 41 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,9 @@ assertThat(
Array index placeholder
```java
// standard assert
assertJsonEquals(
"[{\"a\":1, \"b\":0},{\"a\":1, \"b\":0}]",
"[{\"a\":1, \"b\":2},{\"a\":1, \"b\":3}]",
JsonAssert.whenIgnoringPaths("[*].b")
);
assertThatJson("[{\"a\":1, \"b\":2},{\"a\":1, \"b\":3}]")
.whenIgnoringPaths("[*].b")
.isEqualTo("[{\"a\":1, \"b\":0},{\"a\":1, \"b\":0}]");
```
Please note that if you use JsonPath, you should start the path to be ignored by `$`
Also note that `whenIgnoringPaths` method supports full JsonPath syntax only in AssertJ API, all the other flavors support only
Expand All @@ -341,8 +338,8 @@ assertThatJson("{\"fields\":[" +
It is also possible to use regular expressions to compare string values
```java
assertJsonEquals("{\"test\": \"${json-unit.regex}[A-Z]+\"}",
"{\"test\": \"ABCD\"}");
assertThatJson("{\"test\": \"ABCD\"}")
.isEqualTo("{\"test\": \"${json-unit.regex}[A-Z]+\"}");
```
## <a name="typeplc"></a>Type placeholders
Expand Down Expand Up @@ -407,29 +404,29 @@ There are multiple options how you can configure the comparison
**TREATING_NULL_AS_ABSENT** - fields with null values are equivalent to absent fields. For example, this test passes
```java
assertJsonEquals("{\"test\":{\"a\":1}}",
"{\"test\":{\"a\":1, \"b\": null, \"c\": null}}",
when(TREATING_NULL_AS_ABSENT));
assertThatJson("{\"test\":{\"a\":1, \"b\": null}}")
.when(TREATING_NULL_AS_ABSENT)
.isEqualTo("{\"test\":{\"a\":1}}");
```
**IGNORING_ARRAY_ORDER** - ignores order in arrays
```java
assertJsonEquals("{\"test\":[1,2,3]}",
"{\"test\":[3,2,1]}",
when(IGNORING_ARRAY_ORDER));
assertThatJson("{\"test\":[1,2,3]}")
.when(IGNORING_ARRAY_ORDER)
.isEqualTo("{\"test\":[3,2,1]}");
```
**IGNORING_EXTRA_ARRAY_ITEMS** - ignores unexpected array items
```java
assertJsonEquals("{\"test\":[1,2,3]}",
"{\"test\":[1,2,3,4]}",
when(IGNORING_EXTRA_ARRAY_ITEMS));
assertThatJson("{\"test\":[1,2,3,4]}")
.when(IGNORING_EXTRA_ARRAY_ITEMS)
.isEqualTo("{\"test\":[1,2,3]}");
assertJsonEquals("{\"test\":[1,2,3]}",
"{\"test\":[5,5,4,4,3,3,2,2,1,1]}",
when(IGNORING_EXTRA_ARRAY_ITEMS, IGNORING_ARRAY_ORDER));
assertThatJson("{\"test\":[5,5,4,4,3,3,2,2,1,1]}")
.when(IGNORING_EXTRA_ARRAY_ITEMS, IGNORING_ARRAY_ORDER)
.isEqualTo("{\"test\":[1,2,3]}");
```
**IGNORING_EXTRA_FIELDS** - ignores extra fields in the compared value
Expand All @@ -443,17 +440,17 @@ assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}")
**IGNORE_VALUES** - ignores values and compares only types
```java
assertJsonEquals("{\"test\":{\"a\":1,\"b\":2,\"c\":3}}",
"{\"test\":{\"a\":3,\"b\":2,\"c\":1}}",
when(IGNORING_VALUES));
assertThatJson("{\"a\":2,\"b\":\"string2\"}")
.when(paths("a", "b"), then(IGNORING_VALUES))
.isEqualTo("{\"a\":1,\"b\":\"string\"}");
```
It is possible to combine options.
```java
assertJsonEquals("{\"test\":[{\"key\":1},{\"key\":2},{\"key\":3}]}",
"{\"test\":[{\"key\":3},{\"key\":2, \"extraField\":2},{\"key\":1}]}",
when(IGNORING_ARRAY_ORDER, IGNORING_EXTRA_FIELDS));
assertThatJson("{\"test\":[{\"key\":3},{\"key\":2, \"extraField\":2},{\"key\":1}]}")
.when(IGNORING_EXTRA_FIELDS, IGNORING_ARRAY_ORDER)
.isEqualTo("{\"test\":[{\"key\":1},{\"key\":2},{\"key\":3}]}");
```
In Hamcrest assertion you can set the option like this
Expand All @@ -469,18 +466,19 @@ You can define options locally (for specific paths) by using `when(path(...), th
// AssertJ
assertThatJson("{\"test\":{\"a\":1,\"b\":2,\"c\":3}}").when(paths("test.c"), then(IGNORING_VALUES))
.isEqualTo("{\"test\":{\"a\":1,\"b\":2,\"c\":4}}");
// Vintage
assertJsonEquals("[{\"a\": [1,2,3]}, {\"a\": [4,5,6]}]", "[{\"a\": [2,1,3]}, {\"a\": [6,4,5]}]",
when(path("[*].a"), then(IGNORING_ARRAY_ORDER)));
// ignore array order everywhere but [*].a
assertJsonEquals("[{\"a\": [1,2,3]}, {\"a\": [4,5,6]}]", "[{\"a\": [4,5,6]}, {\"a\": [1,2,3]}]",
when(IGNORING_ARRAY_ORDER).when(path("[*].a"), thenNot(IGNORING_ARRAY_ORDER)));
// ignore extra fields in the object obj
assertJsonEquals("{\"obj\":{\"a1\":1}}", "{\"obj\":{\"a1\":1,\"a2\":2}}",
when(path("obj"), then(IGNORING_EXTRA_FIELDS)));
// ignore array order everywhere but [*].b
assertThatJson("[{\"b\":[4,5,6]},{\"b\":[1,2,3]}]")
.when(IGNORING_ARRAY_ORDER)
.when(path("[*].b"), thenNot(IGNORING_ARRAY_ORDER))
.isEqualTo("[{\"b\":[1,2,3]},{\"b\":[4,5,6]}]");
// ignore extra fields in the object "a"
assertThatJson("{\"a\":{\"a1\":1,\"a2\":2},\"b\":{\"b1\":1,\"b2\":2}}")
.when(path("a"), then(IGNORING_EXTRA_FIELDS))
.isEqualTo("{\"a\":{\"a1\":1},\"b\":{\"b1\":1}}"))
// ignore extra array items in the array
assertJsonEquals("{\"array\":[1,2]}", "{\"array\":[1,2,3]}",
when(path("array"), then(IGNORING_EXTRA_ARRAY_ITEMS)));
assertThatJson("{\"a\":[1,2,3]}")
.when(path("a"), then(IGNORING_EXTRA_ARRAY_ITEMS))
.isEqualTo("{\"a\":[1,2]}");
// Hamcrest
assertThat("{\"test\":{\"a\":1,\"b\":2,\"c\":3}}",
jsonEquals("{\"test\":{\"a\":1,\"b\":2,\"c\":4}}").when(path("test.c"), then(IGNORING_VALUES)));
Expand All @@ -489,13 +487,13 @@ assertThat("{\"test\":{\"a\":1,\"b\":2,\"c\":3}}",
Note that **TREATING_NULL_AS_ABSENT** and **IGNORING_VALUES** require exact paths to ignored fields:
```java
// ignoring number and str
assertThatJson("{\"a\":2,\"b\":\"string2\"}")
.when(paths("a", "b"), then(IGNORING_VALUES))
.isEqualTo("{\"a\":1,\"b\":\"string\"}");
assertThatJson("{\"a\":2,\"b\":\"string2\"}")
.when(paths("a", "b"), then(IGNORING_VALUES))
.isEqualTo("{\"a\":1,\"b\":\"string\"}");
// treat null B as absent B
assertThatJson("{\"A\":1,\"B\":null}")
.when(path("B"), then(TREATING_NULL_AS_ABSENT))
.isEqualTo("{\"A\":1}");
assertThatJson("{\"A\":1,\"B\":null}")
.when(path("B"), then(TREATING_NULL_AS_ABSENT))
.isEqualTo("{\"A\":1}");
```
All other options require paths to objects or arrays where values or order should be ignored.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,13 @@ void shouldFindObjectInArrayWithPlaceholder() {
assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}").node("a").isArray().contains(json("{\"c\": \"${json-unit.any-number}\"}"));
}

@Test
void arraySimpleIgnoringOrderComparisonExample() {
assertThatJson("{\"test\":[1,2,3]}")
.when(IGNORING_ARRAY_ORDER)
.isEqualTo("{\"test\":[3,2,1]}");
}

@Test
void arrayIgnoringOrderComparison() {
assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}").node("a").isArray()
Expand Down Expand Up @@ -707,7 +714,8 @@ void testNodeAbsentOk() {

@Test
void shouldTreatNullAsAbsent() {
assertThatJson("{\"a\":1, \"b\": null}").when(Option.TREATING_NULL_AS_ABSENT).node("b").isAbsent();
assertThatJson("{\"a\":1, \"b\": null}")
.when(Option.TREATING_NULL_AS_ABSENT).node("b").isAbsent();
}

@Test
Expand Down Expand Up @@ -845,12 +853,21 @@ void pathShouldBeIgnoredForDifferentValue() {
.isEqualTo("{\"root\":{\"test\":1, \"ignored\": 2}}");
}

@Test
void pathShouldBeIgnoredForArrayExample() {
assertThatJson("[{\"a\":1, \"b\":2},{\"a\":1, \"b\":3}]")
.whenIgnoringPaths("[*].b")
.isEqualTo("[{\"a\":1, \"b\":0},{\"a\":1, \"b\":0}]");
}


private static class TestBean {
final BigDecimal demo;

TestBean(BigDecimal demo) {
this.demo = demo;
}

public BigDecimal getDemo() {
return demo;
}
Expand Down Expand Up @@ -1008,6 +1025,12 @@ void shouldNotParseValueTwice() {
assertThatJson("{\"json\": \"{\\\"a\\\" : 1}\"}").node("json").isString().isEqualTo("{\"a\" : 1}");
}

@Test
void regexExample() {
assertThatJson("{\"test\": \"ABCD\"}")
.isEqualTo("{\"test\": \"${json-unit.regex}[A-Z]+\"}");
}

@Test
void pathEscapingWorks() {
final String json = "{\"C:\\\\path\\\\file.ext\": {\"Status\": \"OK\"}}";
Expand Down Expand Up @@ -1059,14 +1082,23 @@ void testIssue3SpaceStrings() {

@Test
void testTreatNullAsAbsent() {
assertThatJson("{\"test\":{\"a\":1, \"b\": null}}").when(TREATING_NULL_AS_ABSENT).isEqualTo("{\"test\":{\"a\":1}}");
assertThatJson("{\"test\":{\"a\":1, \"b\": null}}")
.when(TREATING_NULL_AS_ABSENT)
.isEqualTo("{\"test\":{\"a\":1}}");
}

@Test
void shouldIgnoreExtraFields() {
assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}").when(IGNORING_EXTRA_FIELDS).isEqualTo("{\"test\":{\"b\":2}}");
}

@Test
void shouldIgnoreExtraFieldsAndorderExample() {
assertThatJson("{\"test\":[{\"key\":3},{\"key\":2, \"extraField\":2},{\"key\":1}]}")
.when(IGNORING_EXTRA_FIELDS, IGNORING_ARRAY_ORDER)
.isEqualTo("{\"test\":[{\"key\":1},{\"key\":2},{\"key\":3}]}");
}

@Test
void andFailure() {
assertThatThrownBy(() -> assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}").and(
Expand Down Expand Up @@ -1379,6 +1411,20 @@ void jsonPathShouldBeAbleToUseArraysFromObject() {
.containsExactlyInAnyOrder("fiction", "reference", "fiction", "fiction");
}

@Test
void ignoreExtraArrayItemsExample() {
assertThatJson("{\"test\":[1,2,3,4]}")
.when(IGNORING_EXTRA_ARRAY_ITEMS)
.isEqualTo("{\"test\":[1,2,3]}");
}

@Test
void ignoreExtraArrayItemsAndOrderExample() {
assertThatJson("{\"test\":[5,5,4,4,3,3,2,2,1,1]}")
.when(IGNORING_EXTRA_ARRAY_ITEMS, IGNORING_ARRAY_ORDER)
.isEqualTo("{\"test\":[1,2,3]}");
}

@Test
void testInnerString() {
String json = "{\"myNode\":{\"inner\":\"foo\"}}";
Expand Down

0 comments on commit fba4c65

Please sign in to comment.