Skip to content

Commit

Permalink
#535 Failed mapper provider attempt. In AssertJ style, we need to parse
Browse files Browse the repository at this point in the history
the value before the configuration happens
  • Loading branch information
lukas-krecan committed Jul 4, 2024
1 parent 946832f commit 37a66ae
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static ConfigurableJsonAssert assertThatJson(
*/
@NotNull
public static Object json(Object input) {
return new ExpectedNode(JsonUtils.convertToJson(input, "expected", true));
return new ExpectedNode(JsonUtils.convertToJson(input, "expected", true, null));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.javacrumbs.jsonunit.core.internal.DefaultNumberComparator;
import net.javacrumbs.jsonunit.core.internal.PathOption;
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
import net.javacrumbs.jsonunit.providers.MapperProvider;
import org.hamcrest.Matcher;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -49,7 +50,8 @@ public class Configuration {
Collections.emptySet(),
DUMMY_LISTENER,
Collections.emptyList(),
DEFAULT_NUMBER_COMPARATOR);
DEFAULT_NUMBER_COMPARATOR,
null);
private final BigDecimal tolerance;
private final Options options;
private final String ignorePlaceholder;
Expand All @@ -58,6 +60,7 @@ public class Configuration {
private final Set<String> pathsToBeIgnored;
private final DifferenceListener differenceListener;
private final NumberComparator numberComparator;
private final MapperProvider mapperProvider;

private Configuration(
@Nullable BigDecimal tolerance,
Expand All @@ -67,7 +70,8 @@ private Configuration(
Set<String> pathsToBeIgnored,
DifferenceListener differenceListener,
List<PathOption> pathOptions,
NumberComparator numberComparator) {
NumberComparator numberComparator,
MapperProvider mapperProvider) {
this.tolerance = tolerance;
this.options = options;
this.ignorePlaceholder = ignorePlaceholder;
Expand All @@ -76,6 +80,7 @@ private Configuration(
this.pathOptions = pathOptions;
this.differenceListener = differenceListener;
this.numberComparator = numberComparator;
this.mapperProvider = mapperProvider;
}

/**
Expand All @@ -88,9 +93,6 @@ public static Configuration empty() {

/**
* Sets numerical comparison tolerance.
*
* @param tolerance
* @return
*/
@NotNull
public Configuration withTolerance(@Nullable BigDecimal tolerance) {
Expand All @@ -102,7 +104,8 @@ public Configuration withTolerance(@Nullable BigDecimal tolerance) {
pathsToBeIgnored,
differenceListener,
pathOptions,
numberComparator);
numberComparator,
mapperProvider);
}

/**
Expand Down Expand Up @@ -145,7 +148,8 @@ public Configuration withOptions(@NotNull Option first, @NotNull Option... next)
pathsToBeIgnored,
differenceListener,
pathOptions,
numberComparator);
numberComparator,
mapperProvider);
}

/**
Expand All @@ -161,7 +165,8 @@ public Configuration withOptions(@NotNull Collection<Option> optionsToAdd) {
pathsToBeIgnored,
differenceListener,
pathOptions,
numberComparator);
numberComparator,
mapperProvider);
}

@NotNull
Expand All @@ -174,7 +179,8 @@ public Configuration resetOptions() {
pathsToBeIgnored,
differenceListener,
pathOptions,
numberComparator);
numberComparator,
mapperProvider);
}

/**
Expand Down Expand Up @@ -211,7 +217,8 @@ public Configuration withPathOptions(@NotNull List<PathOption> pathOptions) {
pathsToBeIgnored,
differenceListener,
List.copyOf(pathOptions),
numberComparator);
numberComparator,
mapperProvider);
}

@NotNull
Expand All @@ -224,7 +231,8 @@ public Configuration whenIgnoringPaths(@NotNull Collection<String> pathsToBeIgno
Set.copyOf(pathsToBeIgnored),
differenceListener,
pathOptions,
numberComparator);
numberComparator,
mapperProvider);
}

/**
Expand Down Expand Up @@ -257,7 +265,8 @@ public Configuration withIgnorePlaceholder(@NotNull String ignorePlaceholder) {
pathsToBeIgnored,
differenceListener,
pathOptions,
numberComparator);
numberComparator,
mapperProvider);
}

/**
Expand All @@ -277,7 +286,8 @@ public Configuration withMatcher(@NotNull String matcherName, @NotNull Matcher<?
pathsToBeIgnored,
differenceListener,
pathOptions,
numberComparator);
numberComparator,
mapperProvider);
}

/**
Expand All @@ -293,7 +303,8 @@ public Configuration withDifferenceListener(@NotNull DifferenceListener differen
pathsToBeIgnored,
differenceListener,
pathOptions,
numberComparator);
numberComparator,
mapperProvider);
}

/**
Expand All @@ -309,7 +320,22 @@ public Configuration withNumberComparator(@NotNull NumberComparator numberCompar
pathsToBeIgnored,
differenceListener,
pathOptions,
numberComparator);
numberComparator,
mapperProvider);
}

@NotNull
public Configuration withMapperProvider(MapperProvider mapperProvider) {
return new Configuration(
tolerance,
options,
ignorePlaceholder,
matchers,
pathsToBeIgnored,
differenceListener,
pathOptions,
numberComparator,
mapperProvider);
}

@NotNull
Expand Down Expand Up @@ -357,6 +383,11 @@ public NumberComparator getNumberComparator() {
return numberComparator;
}

@Nullable
public MapperProvider getMapperProvider() {
return mapperProvider;
}

public boolean shouldIgnore(String expectedValue) {
if (DEFAULT_IGNORE_PLACEHOLDER.equals(ignorePlaceholder)) {
// special handling of default state. We want to support both # and $ before {json-unit.ignore} but do not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.ArrayList;
import java.util.List;
import net.javacrumbs.jsonunit.providers.Jackson2ObjectMapperProvider;
import net.javacrumbs.jsonunit.providers.MapperProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -39,15 +41,37 @@ record Converter(List<NodeFactory> factories) {

private static final boolean johnzonPresent = isClassPresent("org.apache.johnzon.mapper.Mapper");

private static Converter defaultConverter;

Converter {
if (factories.isEmpty()) {
throw new IllegalStateException("List of factories can not be empty");
}
}

@NotNull
static Converter getConverter(@Nullable MapperProvider mapperProvider) {
if (mapperProvider == null) {
return defaultConverter();
} else {
if (mapperProvider instanceof Jackson2ObjectMapperProvider) {
return new Converter(List.of(new Jackson2NodeFactory((Jackson2ObjectMapperProvider) mapperProvider)));
}
throw new UnsupportedOperationException();
}
}

private static Converter defaultConverter() {
if (defaultConverter == null) {
defaultConverter = createDefaultConverter();
}
return defaultConverter;
}

/**
* Creates converter based on the libraries on the classpath.
*/
@NotNull
static Converter createDefaultConverter() {
List<NodeFactory> factories;
String property = System.getProperty(LIBRARIES_PROPERTY_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public static Diff createInternal(
Configuration configuration,
String differenceString) {
return new Diff(
convertToJson(quoteIfNeeded(expected), "expected", true),
convertToJson(actual, actualName, false),
convertToJson(quoteIfNeeded(expected), "expected", true, configuration.getMapperProvider()),
convertToJson(actual, actualName, false, configuration.getMapperProvider()),
path,
configuration,
DEFAULT_DIFF_LOGGER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@
* Deserializes node using Jackson 2
*/
class Jackson2NodeFactory extends AbstractNodeFactory {
private final ServiceLoader<Jackson2ObjectMapperProvider> serviceLoader =
ServiceLoader.load(Jackson2ObjectMapperProvider.class);

private final Jackson2ObjectMapperProvider mapperProvider;

Jackson2NodeFactory() {
this(findMapperProvider());
}

Jackson2NodeFactory(Jackson2ObjectMapperProvider mapperProvider) {
this.mapperProvider = mapperProvider;
}

@Override
protected Node doConvertValue(Object source) {
Expand Down Expand Up @@ -64,18 +72,7 @@ protected Node readValue(Reader value, String label, boolean lenient) {
}

private ObjectMapper getMapper(boolean lenient) {
return getMapperProvider().getObjectMapper(lenient);
}

private Jackson2ObjectMapperProvider getMapperProvider() {
synchronized (serviceLoader) {
Iterator<Jackson2ObjectMapperProvider> iterator = serviceLoader.iterator();
if (iterator.hasNext()) {
return iterator.next();
} else {
return DefaultObjectMapperProvider.INSTANCE;
}
}
return mapperProvider.getObjectMapper(lenient);
}

private static Node newNode(JsonNode jsonNode) {
Expand Down Expand Up @@ -208,6 +205,19 @@ public String toString() {
}
}

/**
* Tries to find Jackson2ObjectMapperProvider in the ServiceLoader or returns a default one.
*/
private static Jackson2ObjectMapperProvider findMapperProvider() {
ServiceLoader<Jackson2ObjectMapperProvider> serviceLoader = ServiceLoader.load(Jackson2ObjectMapperProvider.class);
Iterator<Jackson2ObjectMapperProvider> iterator = serviceLoader.iterator();
if (iterator.hasNext()) {
return iterator.next();
} else {
return DefaultObjectMapperProvider.INSTANCE;
}
}

private static class DefaultObjectMapperProvider implements Jackson2ObjectMapperProvider {
static final Jackson2ObjectMapperProvider INSTANCE = new DefaultObjectMapperProvider();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@

import static java.util.Collections.emptyList;
import static java.util.Map.Entry.comparingByKey;
import static net.javacrumbs.jsonunit.core.internal.Converter.getConverter;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.Option;
import net.javacrumbs.jsonunit.providers.MapperProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Internal utility class to parse JSON values.
*/
public class JsonUtils {
private static final Converter converter = Converter.createDefaultConverter();

/**
* Converts object to JSON.
Expand All @@ -41,17 +42,17 @@ public class JsonUtils {
* @return
*/
public static Node convertToJson(@Nullable Object source, String label) {
return convertToJson(source, label, false);
return convertToJson(source, label, false, null);
}

/**
* Converts object to JSON.
*/
public static Node convertToJson(@Nullable Object source, String label, boolean lenient) {
public static Node convertToJson(@Nullable Object source, String label, boolean lenient, MapperProvider mapperProvider) {
if (source instanceof JsonSource) {
return converter.convertToNode(((JsonSource) source).getJson(), label, lenient);
return getConverter(mapperProvider).convertToNode(((JsonSource) source).getJson(), label, lenient);
} else {
return converter.convertToNode(source, label, lenient);
return getConverter(mapperProvider).convertToNode(source, label, lenient);
}
}

Expand Down Expand Up @@ -112,9 +113,6 @@ static boolean nodeAbsent(Object json, Path path, boolean treatNullAsAbsent) {

/**
* Add quotes around the object iff it's not a JSON object.
*
* @param source
* @return
*/
static String quoteIfNeeded(String source) {
String trimmed = source.trim();
Expand All @@ -133,9 +131,6 @@ static String quoteIfNeeded(String source) {

/**
* Add quotes around the object iff it's not a JSON object.
*
* @param source
* @return
*/
static Object quoteIfNeeded(Object source) {
if (source instanceof String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.javacrumbs.jsonunit.providers;

import com.google.gson.Gson;

public interface GsonMapperProvider extends MapperProvider {
Gson getGson();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Interface for customizing Jackson 2 ObjectMapper. @see <a href="https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html">SPI intro</a>
*/
public interface Jackson2ObjectMapperProvider {
public interface Jackson2ObjectMapperProvider extends MapperProvider {
/**
* Provides ObjectMapper
* @param lenient Lenient parsing is used for parsing the expected JSON value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package net.javacrumbs.jsonunit.providers;

public interface MapperProvider {}
Loading

0 comments on commit 37a66ae

Please sign in to comment.