net.javacrumbs.jsonunit.core.ConfigurationWhen Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-unit-core Show documentation
Show all versions of json-unit-core Show documentation
Core classes for JSON unit
package net.javacrumbs.jsonunit.core;
import net.javacrumbs.jsonunit.core.internal.PathOption;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Everything for {@link Configuration#when(PathsParam, ApplicableForPath...)}
*/
public class ConfigurationWhen {
private ConfigurationWhen() {}
/**
* Adds path conditions. Examples:
*
*
* - Ignore order for [*].a:
when(path("[*].a"), then(IGNORING_ARRAY_ORDER))`
* - Fully ignore multiple paths:
when(paths("[*].b", "[*].c"), thenIgnore())`
* - Ignore array order for every path except [*].b:
when(IGNORING_ARRAY_ORDER).when(path("[*].b"), thenNot(IGNORING_ARRAY_ORDER))
*
*
* @see #then
* @see #thenNot
* @see #thenIgnore
*/
public static PathsParam path(String path) {
return new PathsParam(path);
}
public static PathsParam paths(String... paths) {
return new PathsParam(paths);
}
public static PathsParam rootPath() {
return path("");
}
/**
* Applies specified options to the object. Use {@link #thenNot} to explicitly remove options from the path.
*
* When passing a path, options {@link Option#TREATING_NULL_AS_ABSENT} and {@link Option#IGNORING_VALUES} expect
* paths to the field which supposed to be treated as absent or value-ignored.
* Example: when(path("a.b"), then(TREATING_NULL_AS_ABSENT))
for JSON of {"a": 1, "b": null}
.
*
* For any other option specify the path to an object with ignorable child fields, or path to an array:
* when(path("a.array"), then(IGNORING_ARRAY_ORDER))
for {"a": {"array": [1, 2, 3]}}
*
*/
public static ApplicableForPath then(Option first, Option... next) {
return new OptionsParam(true, first, next);
}
/**
* Explicitly remove options from the objects.
* @see #then
*/
public static ApplicableForPath thenNot(Option first, Option... next) {
return new OptionsParam(false, first, next);
}
/**
* Marks that the object should be ignored.
*/
public static ApplicableForPath thenIgnore() {
return new IgnoredParam();
}
public static class PathsParam {
private final List paths;
private PathsParam(String path) {
this.paths = Collections.singletonList(path);
}
private PathsParam(String... paths) {
this.paths = Arrays.asList(paths);
}
List getPaths() {
return paths;
}
Configuration apply(Configuration configuration, ApplicableForPath action) {
return action.applyForPaths(configuration, this);
}
}
public interface ApplicableForPath {
@NotNull
Configuration applyForPaths(@NotNull Configuration configuration, @NotNull PathsParam pathsParam);
}
static class OptionsParam implements ApplicableForPath {
private final EnumSet