Skip to content

Commit

Permalink
Ass spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Nov 1, 2023
1 parent b875c5d commit 4bfd0d1
Show file tree
Hide file tree
Showing 102 changed files with 2,734 additions and 1,975 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package net.javacrumbs.jsonunit.assertj;


import net.javacrumbs.jsonunit.core.internal.Node;

import java.math.BigDecimal;
import java.util.Iterator;
import net.javacrumbs.jsonunit.core.internal.Node;

/**
* In AssertJ we need to know which node is expected and which is actual. This class marks expected node.
Expand Down Expand Up @@ -93,8 +91,7 @@ public Object getValue() {
}

@Override
public void ___do_not_implement_this_interface_seriously() {
}
public void ___do_not_implement_this_interface_seriously() {}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@
*/
package net.javacrumbs.jsonunit.assertj;

import static net.javacrumbs.jsonunit.core.internal.Diff.quoteTextValue;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getNode;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getPathPrefix;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.ARRAY;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.BOOLEAN;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.NULL;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.NUMBER;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.OBJECT;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.STRING;
import static net.javacrumbs.jsonunit.jsonpath.InternalJsonPathUtils.resolveJsonPaths;
import static org.assertj.core.description.Description.mostRelevantDescription;
import static org.assertj.core.util.Strings.isNullOrEmpty;

import java.math.BigDecimal;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.ConfigurationWhen.ApplicableForPath;
import net.javacrumbs.jsonunit.core.ConfigurationWhen.PathsParam;
Expand All @@ -39,34 +59,15 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.math.BigDecimal;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static net.javacrumbs.jsonunit.core.internal.Diff.quoteTextValue;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getNode;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getPathPrefix;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.ARRAY;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.BOOLEAN;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.NULL;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.NUMBER;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.OBJECT;
import static net.javacrumbs.jsonunit.core.internal.Node.NodeType.STRING;
import static net.javacrumbs.jsonunit.jsonpath.InternalJsonPathUtils.resolveJsonPaths;
import static org.assertj.core.description.Description.mostRelevantDescription;
import static org.assertj.core.util.Strings.isNullOrEmpty;

public class JsonAssert extends AbstractAssert<JsonAssert, Object> {
final Path path;
final Configuration configuration;
private final Object actualForMatcher;

JsonAssert(Path path, Configuration configuration, Object actual, boolean alreadyParsed) {
super(alreadyParsed ? JsonUtils.wrapDeserializedObject(actual) : JsonUtils.convertToJson(actual, "actual"), JsonAssert.class);
super(
alreadyParsed ? JsonUtils.wrapDeserializedObject(actual) : JsonUtils.convertToJson(actual, "actual"),
JsonAssert.class);
this.path = path;
this.configuration = configuration;
this.actualForMatcher = alreadyParsed ? JsonUtils.wrapDeserializedObject(actual) : actual;
Expand All @@ -89,7 +90,6 @@ public JsonAssert node(@NotNull String node) {
return new JsonAssert(path.to(node), configuration, getNode(actual, node));
}


/**
* Allows to do multiple comparisons on a document like
*
Expand Down Expand Up @@ -133,7 +133,7 @@ public JsonAssert isEqualTo(@Nullable Object expected) {
public JsonMapAssert isObject() {
Node node = assertType(OBJECT);
return new JsonMapAssert((Map<String, Object>) node.getValue(), path.asPrefix(), configuration)
.as("Different value found in node \"%s\"", path);
.as("Different value found in node \"%s\"", path);
}

/**
Expand All @@ -151,7 +151,7 @@ public BigDecimalAssert isNumber() {
public BigIntegerAssert isIntegralNumber() {
Node node = internalMatcher().assertIntegralNumber();
return new BigIntegerAssert(node.decimalValue().toBigIntegerExact())
.as("Different value found in node \"%s\"", path);
.as("Different value found in node \"%s\"", path);
}

/**
Expand All @@ -167,7 +167,8 @@ public BigDecimalAssert asNumber() {
try {
return createBigDecimalAssert(new BigDecimal(node.asText()));
} catch (NumberFormatException e) {
failWithMessage("Node \"" + path + "\" can not be converted to number expected: <a number> but was: <" + quoteTextValue(node.getValue()) + ">.");
failWithMessage("Node \"" + path + "\" can not be converted to number expected: <a number> but was: <"
+ quoteTextValue(node.getValue()) + ">.");
}
} else {
internalMatcher().failOnType(node, "number or string");
Expand All @@ -182,13 +183,7 @@ private BigDecimalAssert createBigDecimalAssert(BigDecimal value) {

private InternalMatcher internalMatcher() {
String description = mostRelevantDescription(info.description(), "Node \"" + path + "\"");
return new InternalMatcher(
actualForMatcher,
path.asPrefix(),
"",
configuration,
description
);
return new InternalMatcher(actualForMatcher, path.asPrefix(), "", configuration, description);
}

/**
Expand All @@ -199,8 +194,7 @@ private InternalMatcher internalMatcher() {
@NotNull
public JsonListAssert isArray() {
Node node = assertType(ARRAY);
return new JsonListAssert((List<?>)node.getValue(), path.asPrefix(), configuration)
.as( "Node \"%s\"", path);
return new JsonListAssert((List<?>) node.getValue(), path.asPrefix(), configuration).as("Node \"%s\"", path);
}

/**
Expand Down Expand Up @@ -345,7 +339,8 @@ public ConfigurableJsonAssert withOptions(@NotNull Collection<Option> optionsToA
* </code>
*/
@NotNull
public ConfigurableJsonAssert withConfiguration(@NotNull Function<Configuration, Configuration> configurationFunction) {
public ConfigurableJsonAssert withConfiguration(
@NotNull Function<Configuration, Configuration> configurationFunction) {
Configuration newConfiguration = configurationFunction.apply(configuration);
newConfiguration = resolveJsonPaths(originalActual, newConfiguration);
return new ConfigurableJsonAssert(path, newConfiguration, actual);
Expand Down Expand Up @@ -408,7 +403,6 @@ public ConfigurableJsonAssert withMatcher(@NotNull String matcherName, @NotNull
return withConfiguration(c -> c.withMatcher(matcherName, matcher));
}


/**
* Sets difference listener
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public final class JsonAssertions {
Assertions.useRepresentation(new JsonRepresentation());
}

private JsonAssertions() {

}
private JsonAssertions() {}

@NotNull
public static ConfigurableJsonAssert assertThatJson(@Nullable Object actual) {
Expand All @@ -49,9 +47,10 @@ public static ConfigurableJsonAssert assertThatJson(@Nullable Object actual) {
* }</pre>
*/
@NotNull
public static ConfigurableJsonAssert assertThatJson(@NotNull Object actual, @NotNull JsonAssertionCallback... callbacks) {
public static ConfigurableJsonAssert assertThatJson(
@NotNull Object actual, @NotNull JsonAssertionCallback... callbacks) {
ConfigurableJsonAssert a = assertThatJson(actual);
for (JsonAssertionCallback callback: callbacks) {
for (JsonAssertionCallback callback : callbacks) {
callback.doAssert(a);
}
return a;
Expand All @@ -68,7 +67,6 @@ public static Object json(Object input) {
return new ExpectedNode(JsonUtils.convertToJson(input, "expected", true));
}


/**
* Value passed here is not parsed as JSON but used as it is
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
package net.javacrumbs.jsonunit.assertj;

import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject;

import java.util.Comparator;
import java.util.List;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.internal.Diff;
import net.javacrumbs.jsonunit.core.internal.Node;
import net.javacrumbs.jsonunit.core.internal.Path;
import org.assertj.core.groups.Tuple;

import java.util.Comparator;
import java.util.List;

import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject;

class JsonComparator implements Comparator<Object> {
private final Configuration configuration;
private final Path path;
Expand All @@ -44,10 +43,11 @@ public int compare(Object actual, Object expected) {
}

// this comparator is not transitive, `expected` is usually a Node and `actual` is usually a Map, or primitive
if (
(actualParsed && !(actual instanceof Node) && (expected instanceof Node) && !(expected instanceof ExpectedNode)) ||
(actual instanceof ExpectedNode)
) {
if ((actualParsed
&& !(actual instanceof Node)
&& (expected instanceof Node)
&& !(expected instanceof ExpectedNode))
|| (actual instanceof ExpectedNode)) {
Object tmp = actual;
actual = expected;
expected = tmp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/
package net.javacrumbs.jsonunit.assertj;

import java.util.Set;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.Option;

import java.util.Set;

class JsonComparisonStrategy {
private final Configuration configuration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package net.javacrumbs.jsonunit.assertj;

import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject;
import static org.assertj.core.util.Lists.newArrayList;

import java.util.List;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.internal.Diff;
import net.javacrumbs.jsonunit.core.internal.Path;
Expand All @@ -25,11 +29,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject;
import static org.assertj.core.util.Lists.newArrayList;

public class JsonListAssert extends FactoryBasedNavigableListAssert<JsonListAssert, List<?>, Object, JsonAssert> {
private final Configuration configuration;
private final Path path;
Expand Down Expand Up @@ -57,7 +56,8 @@ public JsonListAssert isNotEqualTo(@Nullable Object other) {
Diff diff = createDiff(other);
if (diff.similar()) {
JsonComparisonStrategy strategy = new JsonComparisonStrategy(configuration);
throwAssertionError(new BasicErrorMessageFactory("%nExpecting:%n <%s>%nnot to be equal to:%n <%s>%n%s", actual, other, strategy));
throwAssertionError(new BasicErrorMessageFactory(
"%nExpecting:%n <%s>%nnot to be equal to:%n <%s>%n%s", actual, other, strategy));
}
return this;
}
Expand All @@ -72,7 +72,8 @@ protected JsonListAssert newAbstractIterableAssert(Iterable<?> iterable) {
*/
@Override
@Deprecated
public <ASSERT extends AbstractAssert<?, ?>> ASSERT element(int index, InstanceOfAssertFactory<?, ASSERT> assertFactory) {
public <ASSERT extends AbstractAssert<?, ?>> ASSERT element(
int index, InstanceOfAssertFactory<?, ASSERT> assertFactory) {
return super.element(index, assertFactory);
}

Expand All @@ -81,14 +82,16 @@ protected JsonListAssert newAbstractIterableAssert(Iterable<?> iterable) {
*/
@Override
@Deprecated
public <ASSERT extends AbstractAssert<?, ?>> ASSERT singleElement(InstanceOfAssertFactory<?, ASSERT> assertFactory) {
public <ASSERT extends AbstractAssert<?, ?>> ASSERT singleElement(
InstanceOfAssertFactory<?, ASSERT> assertFactory) {
return super.singleElement(assertFactory);
}

@Override
public <ASSERT extends AbstractAssert<?, ?>> ASSERT asInstanceOf(InstanceOfAssertFactory<?, ASSERT> instanceOfAssertFactory) {
public <ASSERT extends AbstractAssert<?, ?>> ASSERT asInstanceOf(
InstanceOfAssertFactory<?, ASSERT> instanceOfAssertFactory) {
throw failure("Please use isString(), isNumber(), isBoolean(), isNull(), isUri(), isArray() or isObject().%n"
+ "This method will most likely not provide the result you expect it to.");
+ "This method will most likely not provide the result you expect it to.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@
*/
package net.javacrumbs.jsonunit.assertj;

import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.internal.Diff;
import net.javacrumbs.jsonunit.core.internal.Node;
import net.javacrumbs.jsonunit.core.internal.Path;
import org.assertj.core.api.AbstractMapAssert;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import static java.util.Arrays.stream;
import static java.util.Objects.deepEquals;
import static java.util.stream.Collectors.toList;
Expand All @@ -41,6 +28,18 @@
import static org.assertj.core.error.ShouldNotContainValue.shouldNotContainValue;
import static org.assertj.core.util.Arrays.array;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.internal.Diff;
import net.javacrumbs.jsonunit.core.internal.Node;
import net.javacrumbs.jsonunit.core.internal.Path;
import org.assertj.core.api.AbstractMapAssert;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class JsonMapAssert extends AbstractMapAssert<JsonMapAssert, Map<String, Object>, String, Object> {
private final Configuration configuration;
private final Path path;
Expand Down Expand Up @@ -95,14 +94,16 @@ public JsonMapAssert doesNotContainValue(@Nullable Object expected) {
@Override
@NotNull
@Deprecated
public JsonMapAssert isEqualToIgnoringGivenFields(@Nullable Object other, @NotNull String... propertiesOrFieldsToIgnore) {
public JsonMapAssert isEqualToIgnoringGivenFields(
@Nullable Object other, @NotNull String... propertiesOrFieldsToIgnore) {
return compare(other, configuration.whenIgnoringPaths(propertiesOrFieldsToIgnore));
}

@Override
@NotNull
@Deprecated
public JsonMapAssert isEqualToComparingOnlyGivenFields(@Nullable Object other, @NotNull String... propertiesOrFieldsUsedInComparison) {
public JsonMapAssert isEqualToComparingOnlyGivenFields(
@Nullable Object other, @NotNull String... propertiesOrFieldsUsedInComparison) {
throw unsupportedOperation();
}

Expand Down Expand Up @@ -169,7 +170,8 @@ public JsonMapAssert containsExactlyEntriesOf(Map<? extends String, ?> map) {
@SafeVarargs
@Override
protected final JsonMapAssert containsOnlyForProxy(Entry<? extends String, ?>... expected) {
Map<? extends String, ?> expectedAsMap = stream(expected).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
Map<? extends String, ?> expectedAsMap =
stream(expected).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
return isEqualTo(wrapDeserializedObject(expectedAsMap));
}

Expand Down Expand Up @@ -290,6 +292,7 @@ private boolean contains(Object expected) {
}

private boolean isSimilar(Object actual, Object expected) {
return Diff.create(expected, actual, "fullJson", path.asPrefix(), configuration).similar();
return Diff.create(expected, actual, "fullJson", path.asPrefix(), configuration)
.similar();
}
}
Loading

0 comments on commit 4bfd0d1

Please sign in to comment.