Browse Source
This commit introduces a JsonComparator abstraction, with an implementation using JSONAssert. Previously, JSONAssert was the only choice. Test APIs have been adapted to allow the new abstraction while relying on JSONAssert still for high-level methods. Closes gh-32791 Co-authored-by: Stéphane Nicoll <stephane.nicoll@broadcom.com>pull/32880/head
14 changed files with 539 additions and 118 deletions
@ -0,0 +1,93 @@
@@ -0,0 +1,93 @@
|
||||
/* |
||||
* Copyright 2002-2024 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 |
||||
* |
||||
* https://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.json; |
||||
|
||||
import org.skyscreamer.jsonassert.JSONCompare; |
||||
import org.skyscreamer.jsonassert.JSONCompareMode; |
||||
import org.skyscreamer.jsonassert.JSONCompareResult; |
||||
import org.skyscreamer.jsonassert.comparator.JSONComparator; |
||||
|
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.util.function.ThrowingBiFunction; |
||||
|
||||
/** |
||||
* Useful methods that can be used with {@code org.skyscreamer.jsonassert}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 6.2 |
||||
*/ |
||||
public abstract class JsonAssert { |
||||
|
||||
/** |
||||
* Create a {@link JsonComparator} from the given {@link JsonCompareMode}. |
||||
* @param compareMode the mode to use |
||||
* @return a new {@link JsonComparator} instance |
||||
* @see JSONCompareMode#STRICT |
||||
* @see JSONCompareMode#LENIENT |
||||
*/ |
||||
public static JsonComparator comparator(JsonCompareMode compareMode) { |
||||
return comparator(toJSONCompareMode(compareMode)); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@link JsonComparator} from the given JSONAssert |
||||
* {@link JSONComparator}. |
||||
* @param comparator the JSON Assert {@link JSONComparator} |
||||
* @return a new {@link JsonComparator} instance |
||||
*/ |
||||
public static JsonComparator comparator(JSONComparator comparator) { |
||||
return comparator((expectedJson, actualJson) -> JSONCompare |
||||
.compareJSON(expectedJson, actualJson, comparator)); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@link JsonComparator} from the given JSONAssert |
||||
* {@link JSONCompareMode}. |
||||
* @param mode the JSON Assert {@link JSONCompareMode} |
||||
* @return a new {@link JsonComparator} instance |
||||
*/ |
||||
public static JsonComparator comparator(JSONCompareMode mode) { |
||||
return comparator((expectedJson, actualJson) -> JSONCompare |
||||
.compareJSON(expectedJson, actualJson, mode)); |
||||
} |
||||
|
||||
private static JsonComparator comparator(ThrowingBiFunction<String, String, JSONCompareResult> compareFunction) { |
||||
return (expectedJson, actualJson) -> compare(expectedJson, actualJson, compareFunction); |
||||
} |
||||
|
||||
private static JsonComparison compare(@Nullable String expectedJson, @Nullable String actualJson, |
||||
ThrowingBiFunction<String, String, JSONCompareResult> compareFunction) { |
||||
|
||||
if (actualJson == null) { |
||||
return (expectedJson != null) |
||||
? JsonComparison.mismatch("Expected null JSON") |
||||
: JsonComparison.match(); |
||||
} |
||||
if (expectedJson == null) { |
||||
return JsonComparison.mismatch("Expected non-null JSON"); |
||||
} |
||||
JSONCompareResult result = compareFunction.throwing(IllegalStateException::new).apply(expectedJson, actualJson); |
||||
return (!result.passed()) |
||||
? JsonComparison.mismatch(result.getMessage()) |
||||
: JsonComparison.match(); |
||||
} |
||||
|
||||
private static JSONCompareMode toJSONCompareMode(JsonCompareMode compareMode) { |
||||
return (compareMode != JsonCompareMode.LENIENT ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
/* |
||||
* Copyright 2002-2024 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 |
||||
* |
||||
* https://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.json; |
||||
|
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.test.json.JsonComparison.Result; |
||||
|
||||
/** |
||||
* Strategy interface used to compare JSON strings. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 6.2 |
||||
* @see JsonAssert |
||||
*/ |
||||
@FunctionalInterface |
||||
public interface JsonComparator { |
||||
|
||||
/** |
||||
* Compare the given JSON strings. |
||||
* @param expectedJson the expected JSON |
||||
* @param actualJson the actual JSON |
||||
* @return the JSON comparison |
||||
*/ |
||||
JsonComparison compare(@Nullable String expectedJson, @Nullable String actualJson); |
||||
|
||||
/** |
||||
* Assert that the {@code expectedJson} matches the comparison rules of ths |
||||
* instance against the {@code actualJson}. Throw an {@link AssertionError} |
||||
* if the comparison does not match. |
||||
* @param expectedJson the expected JSON |
||||
* @param actualJson the actual JSON |
||||
*/ |
||||
default void assertIsMatch(@Nullable String expectedJson, @Nullable String actualJson) { |
||||
JsonComparison comparison = compare(expectedJson, actualJson); |
||||
if (comparison.getResult() == Result.MISMATCH) { |
||||
throw new AssertionError(comparison.getMessage()); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
/* |
||||
* Copyright 2002-2024 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 |
||||
* |
||||
* https://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.json; |
||||
|
||||
/** |
||||
* Modes that can be used to compare JSON. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 6.2 |
||||
*/ |
||||
public enum JsonCompareMode { |
||||
|
||||
/** |
||||
* Strict checking. |
||||
*/ |
||||
STRICT, |
||||
|
||||
/** |
||||
* Lenient checking. |
||||
*/ |
||||
LENIENT |
||||
|
||||
} |
||||
@ -0,0 +1,91 @@
@@ -0,0 +1,91 @@
|
||||
/* |
||||
* Copyright 2002-2024 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 |
||||
* |
||||
* https://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.json; |
||||
|
||||
import org.springframework.lang.Nullable; |
||||
|
||||
/** |
||||
* A comparison of two JSON strings as returned from a {@link JsonComparator}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 6.2 |
||||
*/ |
||||
public final class JsonComparison { |
||||
|
||||
private final Result result; |
||||
|
||||
@Nullable |
||||
private final String message; |
||||
|
||||
|
||||
private JsonComparison(Result result, @Nullable String message) { |
||||
this.result = result; |
||||
this.message = message; |
||||
} |
||||
|
||||
/** |
||||
* Factory method to create a new {@link JsonComparison} when the JSON |
||||
* strings match. |
||||
* @return a new {@link JsonComparison} instance |
||||
*/ |
||||
public static JsonComparison match() { |
||||
return new JsonComparison(Result.MATCH, null); |
||||
} |
||||
|
||||
/** |
||||
* Factory method to create a new {@link JsonComparison} when the JSON strings |
||||
* do not match. |
||||
* @param message a message describing the mismatch |
||||
* @return a new {@link JsonComparison} instance |
||||
*/ |
||||
public static JsonComparison mismatch(String message) { |
||||
return new JsonComparison(Result.MISMATCH, message); |
||||
} |
||||
|
||||
/** |
||||
* Return the result of the comparison. |
||||
*/ |
||||
public Result getResult() { |
||||
return this.result; |
||||
} |
||||
|
||||
/** |
||||
* Return a message describing the comparison. |
||||
*/ |
||||
@Nullable |
||||
public String getMessage() { |
||||
return this.message; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Comparison results. |
||||
*/ |
||||
public enum Result { |
||||
|
||||
/** |
||||
* The JSON strings match when considering the comparison rules. |
||||
*/ |
||||
MATCH, |
||||
|
||||
/** |
||||
* The JSON strings do not match when considering the comparison rules. |
||||
*/ |
||||
MISMATCH |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue