Skip to content

Commit

Permalink
#44 Normalizer attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Jul 4, 2024
1 parent 946832f commit 18838a3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public enum Option {
*/
IGNORING_VALUES,

REPORTING_DIFFERENCE_AS_STRING,
/**
* Stops comparison at the first difference. Can bring performance boots to use-cases that do not need the full list of all differences.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static net.javacrumbs.jsonunit.core.Option.IGNORING_EXTRA_ARRAY_ITEMS;
import static net.javacrumbs.jsonunit.core.Option.IGNORING_EXTRA_FIELDS;
import static net.javacrumbs.jsonunit.core.Option.IGNORING_VALUES;
import static net.javacrumbs.jsonunit.core.Option.REPORTING_DIFFERENCE_AS_STRING;
import static net.javacrumbs.jsonunit.core.Option.TREATING_NULL_AS_ABSENT;
import static net.javacrumbs.jsonunit.core.internal.ClassUtils.isClassPresent;
import static net.javacrumbs.jsonunit.core.internal.DifferenceContextImpl.differenceContext;
Expand All @@ -34,6 +35,7 @@
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.quoteIfNeeded;
import static net.javacrumbs.jsonunit.core.internal.Node.KeyValue;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType;
import static net.javacrumbs.jsonunit.core.internal.Normalizer.toNormalizedString;

import java.math.BigDecimal;
import java.util.ArrayList;
Expand All @@ -55,6 +57,7 @@
import net.javacrumbs.jsonunit.core.internal.ArrayComparison.ComparisonResult;
import net.javacrumbs.jsonunit.core.internal.ArrayComparison.NodeWithIndex;
import net.javacrumbs.jsonunit.core.listener.Difference;
import org.opentest4j.AssertionFailedError;

/**
* Compares JSON structures. Mainly for internal use, the API might be more volatile than the rest.
Expand Down Expand Up @@ -720,7 +723,11 @@ public void failIfDifferent() {

public void failIfDifferent(String message) {
if (!similar()) {
throw createException(message, differences);
if (!configuration.getOptions().contains(REPORTING_DIFFERENCE_AS_STRING)) {
throw createException(message, differences);
} else {
throw new AssertionFailedError("JSONs are different", toNormalizedString(expectedRoot), toNormalizedString(actualRoot)); //FIXME: paths
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.javacrumbs.jsonunit.core.internal;

import static java.util.Comparator.comparing;

import java.util.Iterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import net.javacrumbs.jsonunit.core.internal.Node.KeyValue;

class Normalizer {

private static int depth = 2;

static String toNormalizedString(Node node) {
StringBuilder sb = new StringBuilder();
normalize(node, sb, 0);
return sb.toString();
}

private static void normalize(Node node, StringBuilder sb, int indent) {
switch (node.getNodeType()) {
case OBJECT -> {
addIndent(sb, indent);
sb.append("{\n");
stream(node.fields()).sorted(comparing(KeyValue::getKey)).forEach(keyValue -> {
addIndent(sb, indent + depth);
sb.append('"').append(keyValue.getKey()).append("\": ");
normalize(keyValue.getValue(), sb, indent + depth);
sb.append(",\n");
});
addIndent(sb, indent);
sb.append("}");
}
case STRING -> sb.append('\"').append(node).append('"');
default -> sb.append(node);
}
}

private static void addIndent(StringBuilder sb, int indent) {
for (int i = 0; i < indent; i++) {
sb.append(' ');
}
}

private static <T> Stream<T> stream(Iterator<T> iterator) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import net.javacrumbs.jsonunit.test.base.AbstractJsonAssertTest.DivisionMatcher;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;
import org.opentest4j.MultipleFailuresError;
Expand Down Expand Up @@ -2303,6 +2304,16 @@ void shouldFailFast() {
""");
}

@Nested
class ReportAsString {
@Test
void shouldSortMapKeys() {
assertThatJson("{\"a\": {\"c\": 1}, \"b\": 3}")
.when(Option.REPORTING_DIFFERENCE_AS_STRING)
.isEqualTo("{\"b\": 1, \"a\": {\"c\": 2}}");
}
}

private static final String json =
"""
{
Expand Down

0 comments on commit 18838a3

Please sign in to comment.