001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.util;
018
019import org.skyscreamer.jsonassert.JSONAssert;
020
021/**
022 * A helper class for assertions on JSON content.
023 *
024 * <p>Use of this class requires the <a
025 * href="https://jsonassert.skyscreamer.org/">JSONassert<a/> library.
026 *
027 * @author Sebastien Deleuze
028 * @since 4.1
029 */
030public class JsonExpectationsHelper {
031
032        /**
033         * Parse the expected and actual strings as JSON and assert the two
034         * are "similar" - i.e. they contain the same attribute-value pairs
035         * regardless of formatting with a lenient checking (extensible, and non-strict
036         * array ordering).
037         *
038         * @param expected the expected JSON content
039         * @param actual the actual JSON content
040         * @since 4.1
041         * @see #assertJsonEqual(String, String, boolean)
042         */
043        public void assertJsonEqual(String expected, String actual) throws Exception {
044                assertJsonEqual(expected, actual, false);
045        }
046
047        /**
048         * Parse the expected and actual strings as JSON and assert the two
049         * are "similar" - i.e. they contain the same attribute-value pairs
050         * regardless of formatting.
051         *
052         * <p>Can compare in two modes, depending on {@code strict} parameter value:
053         * <ul>
054         *     <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li>
055         *     <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li>
056         * </ul>
057         *
058         * @param expected the expected JSON content
059         * @param actual the actual JSON content
060         * @param strict enables strict checking
061         * @since 4.2
062         */
063        public void assertJsonEqual(String expected, String actual, boolean strict) throws Exception {
064                JSONAssert.assertEquals(expected, actual, strict);
065        }
066
067        /**
068         * Parse the expected and actual strings as JSON and assert the two
069         * are "not similar" - i.e. they contain different attribute-value pairs
070         * regardless of formatting with a lenient checking (extensible, and non-strict
071         * array ordering).
072         *
073         * @param expected the expected JSON content
074         * @param actual the actual JSON content
075         * @since 4.1
076         * @see #assertJsonNotEqual(String, String, boolean)
077         */
078        public void assertJsonNotEqual(String expected, String actual) throws Exception {
079                assertJsonNotEqual(expected, actual, false);
080        }
081
082        /**
083         * Parse the expected and actual strings as JSON and assert the two
084         * are "not similar" - i.e. they contain different attribute-value pairs
085         * regardless of formatting.
086         *
087         * <p>Can compare in two modes, depending on {@code strict} parameter value:
088         * <ul>
089         *     <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li>
090         *     <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li>
091         * </ul>
092         *
093         * @param expected the expected JSON content
094         * @param actual the actual JSON content
095         * @param strict enables strict checking
096         * @since 4.2
097         */
098        public void assertJsonNotEqual(String expected, String actual, boolean strict) throws Exception {
099                JSONAssert.assertNotEquals(expected, actual, strict);
100        }
101
102}