forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
357 additions
and
94 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
88 changes: 88 additions & 0 deletions
88
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,88 @@ | ||
/* | ||
* 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.util.function.ThrowingBiFunction; | ||
|
||
/** | ||
* Useful methods that can be used with {@code org.skyscreamer.jsonassert}. | ||
* | ||
* @author Phillip Webb | ||
* @since 6.2 | ||
*/ | ||
public class JsonAssert { | ||
|
||
static JsonComparator comparator(JsonCompareMode compareMode) { | ||
return comparator(compareMode != JsonCompareMode.LENIENT ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); | ||
} | ||
|
||
/** | ||
* Create a new {@link JsonComparator} from the given JSON Assert | ||
* {@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 JSON Assert | ||
* {@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)); | ||
} | ||
|
||
/** | ||
* Create a new {@link JsonComparator} from the given JSON Assert | ||
* {@link Function}. | ||
* @param compareFunction the JSON Assert {@link Function} that returns a | ||
* {@link JSONCompareResult}. | ||
* @return a new {@link JsonComparator} instance | ||
*/ | ||
public static JsonComparator comparator(ThrowingBiFunction<String, String, JSONCompareResult> compareFunction) { | ||
return (expectedJson, actualJson) -> compare(expectedJson, actualJson, compareFunction); | ||
} | ||
|
||
private static JsonComparison compare(String expectedJson, String actualJson, | ||
ThrowingBiFunction<String, String, JSONCompareResult> compareFunction) { | ||
|
||
if (actualJson == null) { | ||
return (expectedJson != null) | ||
? JsonComparison.mismatch(expectedJson, actualJson,"Expected null JSON") | ||
: JsonComparison.match(expectedJson, actualJson); | ||
} | ||
if (expectedJson == null) { | ||
return JsonComparison.mismatch(expectedJson, actualJson, "Expected non-null JSON"); | ||
} | ||
JSONCompareResult result = compareFunction.throwing(IllegalStateException::new).apply(expectedJson, actualJson); | ||
return (!result.passed()) | ||
? JsonComparison.mismatch(expectedJson, actualJson, result.getMessage()) | ||
: JsonComparison.match(expectedJson, actualJson); | ||
} | ||
|
||
} |
Oops, something went wrong.