forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce JSON comparison abstraction for tests
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 spring-projectsgh-32791 Co-authored-by: Stéphane Nicoll <stephane.nicoll@broadcom.com>
- Loading branch information
Showing
7 changed files
with
401 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
spring-test/src/main/java/org/springframework/test/json/JsonAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
Oops, something went wrong.