All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.javacrumbs.jsonunit.JsonMatchers Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
/**
 * Copyright 2009-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.javacrumbs.jsonunit;

import static net.javacrumbs.jsonunit.core.internal.Diff.createInternal;
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.JsonUtils.nodeAbsent;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Map;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.ConfigurationWhen.ApplicableForPath;
import net.javacrumbs.jsonunit.core.ConfigurationWhen.PathsParam;
import net.javacrumbs.jsonunit.core.Option;
import net.javacrumbs.jsonunit.core.internal.Diff;
import net.javacrumbs.jsonunit.core.internal.Node;
import net.javacrumbs.jsonunit.core.internal.Path;
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

/**
 * Contains Hamcrest matchers to be used with Hamcrest assertThat and other tools.
 * 

* All the methods accept Objects as parameters. The supported types are: *

    *
  1. Jackson JsonNode
  2. *
  3. Numbers, booleans and any other type parseable by Jackson's ObjectMapper.convertValue
  4. *
  5. String is parsed as JSON. For expected values the string is quoted if it contains obviously invalid JSON.
  6. *
  7. {@link java.io.Reader} similarly to String
  8. *
  9. null as null Node
  10. *
*/ public class JsonMatchers { private static final String EMPTY_PATH = ""; private static final String FULL_JSON = "fullJson"; /** * Are the JSONs equivalent? */ public static ConfigurableJsonMatcher jsonEquals(Object expected) { return new JsonPartMatcher<>(EMPTY_PATH, expected); } /** * Is the part of the JSON equivalent? */ public static ConfigurableJsonMatcher jsonPartEquals(String path, Object expected) { return new JsonPartMatcher<>(path, expected); } /** * Applies matcher to the part of the JSON. */ public static Matcher jsonPartMatches(String path, Matcher matcher) { return new MatcherApplyingMatcher<>(path, matcher); } /** * Are the JSONs equivalent? *

* This method exist only for those cases, when you need to use it as Matcher<String> and Java refuses to * do the type inference correctly. */ public static ConfigurableJsonMatcher jsonStringEquals(Object expected) { return jsonEquals(expected); } /** * Is the part of the JSON equivalent? *

* This method exist only for those cases, when you need to use it as Matcher<String> and Java refuses to * do the type inference correctly. */ public static ConfigurableJsonMatcher jsonStringPartEquals(String path, Object expected) { return jsonPartEquals(path, expected); } /** * Is the node in path absent? */ public static ConfigurableJsonMatcher jsonNodeAbsent(String path) { return new JsonNodeAbsenceMatcher<>(path); } /** * Is the node in path present? */ public static ConfigurableJsonMatcher jsonNodePresent(String path) { return new JsonNodePresenceMatcher<>(path); } private abstract static class AbstractMatcher extends BaseMatcher { final String path; Object actual; AbstractMatcher(String path) { this.path = path; } @Override public final boolean matches(Object item) { actual = item; return doMatch(item); } abstract boolean doMatch(Object item); Path getPath() { return Path.create(path, getPathPrefix(actual)); } } private abstract static class AbstractJsonMatcher extends AbstractMatcher implements ConfigurableJsonMatcher { Configuration configuration = JsonAssert.getConfiguration(); AbstractJsonMatcher(String path) { super(path); } @Override public ConfigurableJsonMatcher withTolerance(BigDecimal tolerance) { configuration = configuration.withTolerance(tolerance); return this; } @Override public ConfigurableJsonMatcher withTolerance(double tolerance) { configuration = configuration.withTolerance(tolerance); return this; } @Override public ConfigurableJsonMatcher when(Option first, Option... next) { configuration = configuration.withOptions(first, next); return this; } @Override public ConfigurableJsonMatcher withOptions(Collection





© 2015 - 2024 Weber Informatics LLC | Privacy Policy