diff --git a/util/internal/test/src/main/java/org/hibernate/search/util/impl/test/JsonHelper.java b/util/internal/test/src/main/java/org/hibernate/search/util/impl/test/JsonHelper.java new file mode 100644 index 00000000000..dc1f6b268e8 --- /dev/null +++ b/util/internal/test/src/main/java/org/hibernate/search/util/impl/test/JsonHelper.java @@ -0,0 +1,47 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.search.util.impl.test; + +import static org.junit.Assert.fail; + +import org.json.JSONException; +import org.skyscreamer.jsonassert.JSONCompare; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.skyscreamer.jsonassert.JSONCompareResult; + +/** + * Helper functionality around JSON documents. + * + * @author Gunnar Morling + */ +public class JsonHelper { + + private JsonHelper() { + } + + public static void assertJsonEquals(String expectedJson, String actualJson) { + assertJsonEquals( expectedJson, actualJson, JSONCompareMode.NON_EXTENSIBLE ); + } + + public static void assertJsonEqualsIgnoringUnknownFields(String expectedJson, String actualJson) { + assertJsonEquals( expectedJson, actualJson, JSONCompareMode.LENIENT ); + } + + private static void assertJsonEquals(String expectedJson, String actualJson, JSONCompareMode mode) { + try { + JSONCompareResult result = JSONCompare.compareJSON( expectedJson, actualJson, mode ); + + if ( result.failed() ) { + fail( result.getMessage() + "; Actual: " + actualJson ); + throw new IllegalStateException( "This should never happen" ); + } + } + catch (JSONException e) { + throw new RuntimeException( e ); + } + } +}