JsonExpectationsHelper.java 3.5 KB
Newer Older
1
/*
P
Phillip Webb 已提交
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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 org.springframework.test.util;

import org.skyscreamer.jsonassert.JSONAssert;

/**
 * A helper class for assertions on JSON content.
 *
 * <p>Use of this class requires the <a
S
Spring Operator 已提交
25
 * href="https://jsonassert.skyscreamer.org/">JSONassert</a> library.
26 27 28 29 30 31 32 33 34
 *
 * @author Sebastien Deleuze
 * @since 4.1
 */
public class JsonExpectationsHelper {

	/**
	 * Parse the expected and actual strings as JSON and assert the two
	 * are "similar" - i.e. they contain the same attribute-value pairs
H
Hronom 已提交
35 36
	 * regardless of formatting with a lenient checking (extensible, and non-strict
	 * array ordering).
37 38 39
	 * @param expected the expected JSON content
	 * @param actual the actual JSON content
	 * @since 4.1
H
Hronom 已提交
40
	 * @see #assertJsonEqual(String, String, boolean)
41 42
	 */
	public void assertJsonEqual(String expected, String actual) throws Exception {
H
Hronom 已提交
43 44 45 46 47 48 49 50 51
		assertJsonEqual(expected, actual, false);
	}

	/**
	 * Parse the expected and actual strings as JSON and assert the two
	 * are "similar" - i.e. they contain the same attribute-value pairs
	 * regardless of formatting.
	 * <p>Can compare in two modes, depending on {@code strict} parameter value:
	 * <ul>
52 53
	 * <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li>
	 * <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li>
H
Hronom 已提交
54 55 56 57 58 59 60 61
	 * </ul>
	 * @param expected the expected JSON content
	 * @param actual the actual JSON content
	 * @param strict enables strict checking
	 * @since 4.2
	 */
	public void assertJsonEqual(String expected, String actual, boolean strict) throws Exception {
		JSONAssert.assertEquals(expected, actual, strict);
62 63 64 65 66
	}

	/**
	 * Parse the expected and actual strings as JSON and assert the two
	 * are "not similar" - i.e. they contain different attribute-value pairs
H
Hronom 已提交
67 68
	 * regardless of formatting with a lenient checking (extensible, and non-strict
	 * array ordering).
69 70 71
	 * @param expected the expected JSON content
	 * @param actual the actual JSON content
	 * @since 4.1
H
Hronom 已提交
72
	 * @see #assertJsonNotEqual(String, String, boolean)
73 74
	 */
	public void assertJsonNotEqual(String expected, String actual) throws Exception {
H
Hronom 已提交
75
		assertJsonNotEqual(expected, actual, false);
76
	}
H
Hronom 已提交
77 78 79 80 81 82 83

	/**
	 * Parse the expected and actual strings as JSON and assert the two
	 * are "not similar" - i.e. they contain different attribute-value pairs
	 * regardless of formatting.
	 * <p>Can compare in two modes, depending on {@code strict} parameter value:
	 * <ul>
84 85
	 * <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li>
	 * <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li>
H
Hronom 已提交
86 87 88 89 90 91 92 93 94 95
	 * </ul>
	 * @param expected the expected JSON content
	 * @param actual the actual JSON content
	 * @param strict enables strict checking
	 * @since 4.2
	 */
	public void assertJsonNotEqual(String expected, String actual, boolean strict) throws Exception {
		JSONAssert.assertNotEquals(expected, actual, strict);
	}

96
}