001/*
002 * Copyright 2002-2018 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         * @param expected the expected JSON content
038         * @param actual the actual JSON content
039         * @since 4.1
040         * @see #assertJsonEqual(String, String, boolean)
041         */
042        public void assertJsonEqual(String expected, String actual) throws Exception {
043                assertJsonEqual(expected, actual, false);
044        }
045
046        /**
047         * Parse the expected and actual strings as JSON and assert the two
048         * are "similar" - i.e. they contain the same attribute-value pairs
049         * regardless of formatting.
050         * <p>Can compare in two modes, depending on {@code strict} parameter value:
051         * <ul>
052         * <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li>
053         * <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li>
054         * </ul>
055         * @param expected the expected JSON content
056         * @param actual the actual JSON content
057         * @param strict enables strict checking
058         * @since 4.2
059         */
060        public void assertJsonEqual(String expected, String actual, boolean strict) throws Exception {
061                JSONAssert.assertEquals(expected, actual, strict);
062        }
063
064        /**
065         * Parse the expected and actual strings as JSON and assert the two
066         * are "not similar" - i.e. they contain different attribute-value pairs
067         * regardless of formatting with a lenient checking (extensible, and non-strict
068         * array ordering).
069         * @param expected the expected JSON content
070         * @param actual the actual JSON content
071         * @since 4.1
072         * @see #assertJsonNotEqual(String, String, boolean)
073         */
074        public void assertJsonNotEqual(String expected, String actual) throws Exception {
075                assertJsonNotEqual(expected, actual, false);
076        }
077
078        /**
079         * Parse the expected and actual strings as JSON and assert the two
080         * are "not similar" - i.e. they contain different attribute-value pairs
081         * regardless of formatting.
082         * <p>Can compare in two modes, depending on {@code strict} parameter value:
083         * <ul>
084         * <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li>
085         * <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li>
086         * </ul>
087         * @param expected the expected JSON content
088         * @param actual the actual JSON content
089         * @param strict enables strict checking
090         * @since 4.2
091         */
092        public void assertJsonNotEqual(String expected, String actual, boolean strict) throws Exception {
093                JSONAssert.assertNotEquals(expected, actual, strict);
094        }
095
096}