├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── Procfile ├── README.md ├── build.gradle ├── changelog.md ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── json-path-assert ├── README.md ├── build.gradle └── src │ ├── main │ └── java │ │ └── com │ │ └── jayway │ │ ├── jsonassert │ │ ├── JsonAssert.java │ │ ├── JsonAsserter.java │ │ └── impl │ │ │ ├── JsonAsserterImpl.java │ │ │ └── matcher │ │ │ ├── CollectionMatcher.java │ │ │ ├── IsCollectionWithSize.java │ │ │ ├── IsEmptyCollection.java │ │ │ ├── IsMapContainingKey.java │ │ │ ├── IsMapContainingValue.java │ │ │ └── MapTypeSafeMatcher.java │ │ └── jsonpath │ │ └── matchers │ │ ├── IsJson.java │ │ ├── JsonPathMatchers.java │ │ ├── WithJsonPath.java │ │ └── WithoutJsonPath.java │ └── test │ ├── java │ └── com │ │ └── jayway │ │ ├── jsonassert │ │ └── JsonAssertTest.java │ │ └── jsonpath │ │ └── matchers │ │ ├── DemoTest.java │ │ ├── HasNoJsonPathTest.java │ │ ├── IsJsonFileTest.java │ │ ├── IsJsonStringTest.java │ │ ├── IsJsonTest.java │ │ ├── JsonPathMatchersTest.java │ │ ├── WithJsonPathTest.java │ │ ├── WithoutJsonPathTest.java │ │ └── helpers │ │ ├── ResourceHelpers.java │ │ ├── StrictParsingConfiguration.java │ │ └── TestingMatchers.java │ └── resources │ ├── books.json │ ├── example.json │ ├── invalid.json │ ├── links.json │ └── lotto.json ├── json-path ├── build.gradle └── src │ ├── main │ └── java │ │ └── com │ │ └── jayway │ │ └── jsonpath │ │ ├── Configuration.java │ │ ├── Criteria.java │ │ ├── DocumentContext.java │ │ ├── EvaluationListener.java │ │ ├── Filter.java │ │ ├── InvalidCriteriaException.java │ │ ├── InvalidJsonException.java │ │ ├── InvalidModificationException.java │ │ ├── InvalidPathException.java │ │ ├── JsonPath.java │ │ ├── JsonPathException.java │ │ ├── MapFunction.java │ │ ├── Option.java │ │ ├── ParseContext.java │ │ ├── PathNotFoundException.java │ │ ├── Predicate.java │ │ ├── ReadContext.java │ │ ├── TypeRef.java │ │ ├── ValueCompareException.java │ │ ├── WriteContext.java │ │ ├── internal │ │ ├── CharacterIndex.java │ │ ├── DefaultsImpl.java │ │ ├── EvaluationAbortException.java │ │ ├── EvaluationContext.java │ │ ├── JsonContext.java │ │ ├── JsonFormatter.java │ │ ├── ParseContextImpl.java │ │ ├── Path.java │ │ ├── PathRef.java │ │ ├── Utils.java │ │ ├── filter │ │ │ ├── Evaluator.java │ │ │ ├── EvaluatorFactory.java │ │ │ ├── ExpressionNode.java │ │ │ ├── FilterCompiler.java │ │ │ ├── LogicalExpressionNode.java │ │ │ ├── LogicalOperator.java │ │ │ ├── PatternFlag.java │ │ │ ├── RelationalExpressionNode.java │ │ │ ├── RelationalOperator.java │ │ │ ├── ValueNode.java │ │ │ └── ValueNodes.java │ │ ├── function │ │ │ ├── ParamType.java │ │ │ ├── Parameter.java │ │ │ ├── PassthruPathFunction.java │ │ │ ├── PathFunction.java │ │ │ ├── PathFunctionFactory.java │ │ │ ├── json │ │ │ │ ├── Append.java │ │ │ │ └── KeySetFunction.java │ │ │ ├── latebinding │ │ │ │ ├── ILateBindingValue.java │ │ │ │ ├── JsonLateBindingValue.java │ │ │ │ └── PathLateBindingValue.java │ │ │ ├── numeric │ │ │ │ ├── AbstractAggregation.java │ │ │ │ ├── Average.java │ │ │ │ ├── Max.java │ │ │ │ ├── Min.java │ │ │ │ ├── StandardDeviation.java │ │ │ │ └── Sum.java │ │ │ ├── sequence │ │ │ │ ├── AbstractSequenceAggregation.java │ │ │ │ ├── First.java │ │ │ │ ├── Index.java │ │ │ │ └── Last.java │ │ │ └── text │ │ │ │ ├── Concatenate.java │ │ │ │ └── Length.java │ │ └── path │ │ │ ├── ArrayIndexOperation.java │ │ │ ├── ArrayIndexToken.java │ │ │ ├── ArrayPathToken.java │ │ │ ├── ArraySliceOperation.java │ │ │ ├── ArraySliceToken.java │ │ │ ├── CompiledPath.java │ │ │ ├── EvaluationContextImpl.java │ │ │ ├── FunctionPathToken.java │ │ │ ├── PathCompiler.java │ │ │ ├── PathToken.java │ │ │ ├── PathTokenAppender.java │ │ │ ├── PathTokenFactory.java │ │ │ ├── PredicateContextImpl.java │ │ │ ├── PredicatePathToken.java │ │ │ ├── PropertyPathToken.java │ │ │ ├── RootPathToken.java │ │ │ ├── ScanPathToken.java │ │ │ └── WildcardPathToken.java │ │ └── spi │ │ ├── cache │ │ ├── Cache.java │ │ ├── CacheProvider.java │ │ ├── LRUCache.java │ │ └── NOOPCache.java │ │ ├── json │ │ ├── AbstractJsonProvider.java │ │ ├── GsonJsonProvider.java │ │ ├── JacksonJsonNodeJsonProvider.java │ │ ├── JacksonJsonProvider.java │ │ ├── JakartaJsonProvider.java │ │ ├── JettisonProvider.java │ │ ├── JsonOrgJsonProvider.java │ │ ├── JsonProvider.java │ │ ├── JsonSmartJsonProvider.java │ │ └── TapestryJsonProvider.java │ │ └── mapper │ │ ├── GsonMappingProvider.java │ │ ├── JacksonMappingProvider.java │ │ ├── JakartaMappingProvider.java │ │ ├── JsonOrgMappingProvider.java │ │ ├── JsonSmartMappingProvider.java │ │ ├── MappingException.java │ │ ├── MappingProvider.java │ │ └── TapestryMappingProvider.java │ └── test │ ├── java │ └── com │ │ └── jayway │ │ └── jsonpath │ │ ├── BaseTest.java │ │ ├── Configurations.java │ │ ├── DeepScanTest.java │ │ ├── EscapeTest.java │ │ ├── EvaluationListenerTest.java │ │ ├── FilterCompilerTest.java │ │ ├── FilterParseTest.java │ │ ├── FilterTest.java │ │ ├── GsonJsonProviderTest.java │ │ ├── InlineFilterTest.java │ │ ├── Issue_487.java │ │ ├── Issue_537.java │ │ ├── Issue_721.java │ │ ├── Issue_762.java │ │ ├── Issue_786.java │ │ ├── Issue_970.java │ │ ├── Issue_973.java │ │ ├── JacksonJsonNodeJsonProviderMapperSupportTest.java │ │ ├── JacksonJsonNodeJsonProviderTest.java │ │ ├── JacksonTest.java │ │ ├── JakartaJsonProviderTest.java │ │ ├── JsonOrgJsonProviderTest.java │ │ ├── JsonProviderTest.java │ │ ├── JsonProviderTestObjectMapping.java │ │ ├── MapperTest.java │ │ ├── MultiPropTest.java │ │ ├── OptionsTest.java │ │ ├── PathCompilerTest.java │ │ ├── PredicateTest.java │ │ ├── ProviderInTest.java │ │ ├── ReadContextTest.java │ │ ├── ReturnTypeTest.java │ │ ├── ScientificNotationTest.java │ │ ├── TapestryJsonProviderTest.java │ │ ├── TestSuppressExceptions.java │ │ ├── TestUtils.java │ │ ├── WriteTest.java │ │ ├── internal │ │ ├── JsonContextTest.java │ │ ├── UtilsTest.java │ │ ├── filter │ │ │ ├── PatternFlagTest.java │ │ │ ├── RegexpEvaluatorTest.java │ │ │ └── RelationalOperatorTest.java │ │ ├── function │ │ │ ├── BaseFunctionTest.java │ │ │ ├── Issue191.java │ │ │ ├── Issue234.java │ │ │ ├── Issue273.java │ │ │ ├── Issue612.java │ │ │ ├── Issue629.java │ │ │ ├── Issue680.java │ │ │ ├── JSONEntityPathFunctionTest.java │ │ │ ├── KeySetFunctionTest.java │ │ │ ├── NestedFunctionTest.java │ │ │ ├── NumericPathFunctionTest.java │ │ │ └── SequentialPathFunctionTest.java │ │ └── path │ │ │ └── PathTokenTest.java │ │ ├── issue_613.java │ │ └── old │ │ ├── ArraySlicingTest.java │ │ ├── ComplianceTest.java │ │ ├── FilterTest.java │ │ ├── IssuesTest.java │ │ ├── JsonPathTest.java │ │ ├── JsonProviderTest.java │ │ ├── NullHandlingTest.java │ │ └── internal │ │ ├── ArrayIndexFilterTest.java │ │ ├── ArrayPathTokenTest.java │ │ ├── PredicatePathTokenTest.java │ │ ├── PropertyPathTokenTest.java │ │ ├── ScanPathTokenTest.java │ │ ├── TestBase.java │ │ └── TestInternal3.java │ └── resources │ ├── issue_191.json │ ├── issue_24.json │ ├── issue_76.json │ ├── issue_76_2.json │ ├── json-test-doc.json │ ├── json_array.json │ ├── json_array_multiple_delete.json │ ├── keyset.json │ └── simplelogger.properties ├── jsonpath.png ├── settings.gradle └── system.properties /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Java CI 3 | 4 | on: 5 | push: 6 | pull_request: 7 | 8 | jobs: 9 | test: 10 | runs-on: ubuntu-latest 11 | strategy: 12 | matrix: 13 | java: [ 8, 11, 17, 21 ] 14 | fail-fast: false 15 | max-parallel: 4 16 | name: JDK ${{ matrix.java }} 17 | 18 | steps: 19 | - uses: actions/checkout@v4 20 | - name: Set up JDK 21 | uses: actions/setup-java@v3 22 | with: 23 | java-version: ${{ matrix.java }} 24 | distribution: temurin 25 | cache: 'gradle' 26 | 27 | - name: Grant execute permission for gradlew 28 | run: chmod +x gradlew 29 | 30 | - name: Build with Gradle 31 | run: ./gradlew build --warning-mode all 32 | 33 | - name: Run Tests 34 | run: ./gradlew check 35 | 36 | - name: Maven Install 37 | run: ./gradlew clean publishToMavenLocal 38 | 39 | ... 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target 3 | *.iws 4 | *.ipr 5 | *.iml 6 | .classpath 7 | .project 8 | .settings 9 | .springBeans 10 | .DS_Store 11 | .gradle 12 | .java-version 13 | TODO 14 | gradle.properties 15 | build 16 | bin/ 17 | out/ 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | sudo: false 4 | 5 | jdk: 6 | - openjdk8 7 | 8 | cache: 9 | directories: 10 | - $HOME/.gradle 11 | 12 | arch: 13 | - amd64 14 | - ppc64le 15 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: java $JAVA_OPTS -Dserver.http.port=$PORT -DresourceBase=json-path-web-test/build/resources/main/webapp/ -jar json-path-web-test/build/libs/json-path-web-test-*-all.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/json-path/JsonPath/45333e0a310af70ad48d34d306da30af1e8e6314/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /json-path-assert/build.gradle: -------------------------------------------------------------------------------- 1 | description = "Assertions on Json using JsonPath" 2 | 3 | jar { 4 | bnd( 5 | 'Implementation-Title': 'json-path-assert', 'Implementation-Version': archiveVersion 6 | ) 7 | } 8 | 9 | dependencies { 10 | implementation project(':json-path') 11 | implementation libs.hamcrest 12 | implementation libs.slf4jApi 13 | 14 | testImplementation libs.jsonSmart 15 | testImplementation libs.test 16 | } 17 | -------------------------------------------------------------------------------- /json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/CollectionMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | BSD License 3 | 4 | Copyright (c) 2000-2006, www.hamcrest.org 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | Redistributions of source code must retain the above copyright notice, this list of 11 | conditions and the following disclaimer. Redistributions in binary form must reproduce 12 | the above copyright notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the distribution. 14 | 15 | Neither the name of Hamcrest nor the names of its contributors may be used to endorse 16 | or promote products derived from this software without specific prior written 17 | permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 22 | SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 27 | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 28 | DAMAGE. 29 | */ 30 | package com.jayway.jsonassert.impl.matcher; 31 | 32 | import org.hamcrest.BaseMatcher; 33 | 34 | import java.util.Collection; 35 | 36 | public abstract class CollectionMatcher> extends BaseMatcher { 37 | @SuppressWarnings("unchecked") 38 | public boolean matches(Object item) { 39 | if (!(item instanceof Collection)) { 40 | return false; 41 | } 42 | return matchesSafely((C)item); 43 | } 44 | 45 | protected abstract boolean matchesSafely(C collection); 46 | } -------------------------------------------------------------------------------- /json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsEmptyCollection.java: -------------------------------------------------------------------------------- 1 | /* 2 | BSD License 3 | 4 | Copyright (c) 2000-2006, www.hamcrest.org 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | Redistributions of source code must retain the above copyright notice, this list of 11 | conditions and the following disclaimer. Redistributions in binary form must reproduce 12 | the above copyright notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the distribution. 14 | 15 | Neither the name of Hamcrest nor the names of its contributors may be used to endorse 16 | or promote products derived from this software without specific prior written 17 | permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 22 | SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 27 | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 28 | DAMAGE. 29 | */ 30 | package com.jayway.jsonassert.impl.matcher; 31 | 32 | import org.hamcrest.Description; 33 | import org.hamcrest.Matcher; 34 | 35 | import java.util.Collection; 36 | 37 | /** 38 | * Tests if collection is empty. 39 | */ 40 | public class IsEmptyCollection extends CollectionMatcher> { 41 | 42 | @Override 43 | public boolean matchesSafely(Collection item) { 44 | return item.isEmpty(); 45 | } 46 | 47 | @Override 48 | public void describeTo(Description description) { 49 | description.appendText("an empty collection"); 50 | } 51 | 52 | /** 53 | * Matches an empty collection. 54 | */ 55 | public static Matcher> empty() { 56 | return new IsEmptyCollection(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | BSD License 3 | 4 | Copyright (c) 2000-2006, www.hamcrest.org 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | Redistributions of source code must retain the above copyright notice, this list of 11 | conditions and the following disclaimer. Redistributions in binary form must reproduce 12 | the above copyright notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the distribution. 14 | 15 | Neither the name of Hamcrest nor the names of its contributors may be used to endorse 16 | or promote products derived from this software without specific prior written 17 | permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 22 | SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 27 | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 28 | DAMAGE. 29 | */ 30 | package com.jayway.jsonassert.impl.matcher; 31 | 32 | import org.hamcrest.Description; 33 | import org.hamcrest.Matcher; 34 | 35 | import java.util.Map; 36 | 37 | import static org.hamcrest.core.IsEqual.equalTo; 38 | 39 | public class IsMapContainingKey extends MapTypeSafeMatcher> { 40 | private final Matcher keyMatcher; 41 | 42 | public IsMapContainingKey(Matcher keyMatcher) { 43 | this.keyMatcher = keyMatcher; 44 | } 45 | 46 | @Override 47 | public boolean matchesSafely(Map item) { 48 | for (K key : item.keySet()) { 49 | if (keyMatcher.matches(key)) { 50 | return true; 51 | } 52 | } 53 | return false; 54 | } 55 | 56 | @Override 57 | public void describeTo(Description description) { 58 | description.appendText("map with key ") 59 | .appendDescriptionOf(keyMatcher); 60 | } 61 | 62 | public static Matcher> hasKey(K key) { 63 | return hasKey(equalTo(key)); 64 | } 65 | 66 | public static Matcher> hasKey(Matcher keyMatcher) { 67 | return new IsMapContainingKey(keyMatcher); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | BSD License 3 | 4 | Copyright (c) 2000-2006, www.hamcrest.org 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | Redistributions of source code must retain the above copyright notice, this list of 11 | conditions and the following disclaimer. Redistributions in binary form must reproduce 12 | the above copyright notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the distribution. 14 | 15 | Neither the name of Hamcrest nor the names of its contributors may be used to endorse 16 | or promote products derived from this software without specific prior written 17 | permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 22 | SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 27 | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 28 | DAMAGE. 29 | */ 30 | package com.jayway.jsonassert.impl.matcher; 31 | 32 | import org.hamcrest.Description; 33 | import org.hamcrest.Matcher; 34 | 35 | import java.util.Map; 36 | 37 | import static org.hamcrest.core.IsEqual.equalTo; 38 | 39 | public class IsMapContainingValue extends MapTypeSafeMatcher>{ 40 | private final Matcher valueMatcher; 41 | 42 | public IsMapContainingValue(Matcher valueMatcher) { 43 | this.valueMatcher = valueMatcher; 44 | } 45 | 46 | @Override 47 | public boolean matchesSafely(Map item) { 48 | for (V value : item.values()) { 49 | if (valueMatcher.matches(value)) { 50 | return true; 51 | } 52 | } 53 | return false; 54 | } 55 | 56 | @Override 57 | public void describeTo(Description description) { 58 | description.appendText("map with value ") 59 | .appendDescriptionOf(valueMatcher); 60 | } 61 | 62 | public static Matcher> hasValue(V value) { 63 | return IsMapContainingValue.hasValue(equalTo(value)); 64 | } 65 | 66 | public static Matcher> hasValue(Matcher valueMatcher) { 67 | return new IsMapContainingValue(valueMatcher); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/MapTypeSafeMatcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | BSD License 3 | 4 | Copyright (c) 2000-2006, www.hamcrest.org 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | Redistributions of source code must retain the above copyright notice, this list of 11 | conditions and the following disclaimer. Redistributions in binary form must reproduce 12 | the above copyright notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the distribution. 14 | 15 | Neither the name of Hamcrest nor the names of its contributors may be used to endorse 16 | or promote products derived from this software without specific prior written 17 | permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 22 | SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 27 | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 28 | DAMAGE. 29 | */ 30 | package com.jayway.jsonassert.impl.matcher; 31 | 32 | import org.hamcrest.BaseMatcher; 33 | 34 | import java.util.Map; 35 | 36 | public abstract class MapTypeSafeMatcher> extends BaseMatcher { 37 | @SuppressWarnings("unchecked") 38 | public boolean matches(Object item) { 39 | return item instanceof Map && matchesSafely((M) item); 40 | } 41 | 42 | protected abstract boolean matchesSafely(M map); 43 | } -------------------------------------------------------------------------------- /json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJson.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers; 2 | 3 | import com.jayway.jsonpath.JsonPath; 4 | import com.jayway.jsonpath.JsonPathException; 5 | import com.jayway.jsonpath.ReadContext; 6 | import org.hamcrest.Description; 7 | import org.hamcrest.Matcher; 8 | import org.hamcrest.TypeSafeMatcher; 9 | 10 | import java.io.File; 11 | import java.io.IOException; 12 | 13 | public class IsJson extends TypeSafeMatcher { 14 | private final Matcher jsonMatcher; 15 | 16 | public IsJson(Matcher jsonMatcher) { 17 | this.jsonMatcher = jsonMatcher; 18 | } 19 | 20 | @Override 21 | protected boolean matchesSafely(T json) { 22 | try { 23 | ReadContext context = parse(json); 24 | return jsonMatcher.matches(context); 25 | } catch (JsonPathException e) { 26 | return false; 27 | } catch (IOException e) { 28 | return false; 29 | } 30 | } 31 | 32 | public void describeTo(Description description) { 33 | description.appendText("is json ").appendDescriptionOf(jsonMatcher); 34 | } 35 | 36 | @Override 37 | protected void describeMismatchSafely(T json, Description mismatchDescription) { 38 | try { 39 | ReadContext context = parse(json); 40 | jsonMatcher.describeMismatch(context, mismatchDescription); 41 | } catch (JsonPathException e) { 42 | buildMismatchDescription(json, mismatchDescription, e); 43 | } catch (IOException e) { 44 | buildMismatchDescription(json, mismatchDescription, e); 45 | } 46 | } 47 | 48 | private static void buildMismatchDescription(Object json, Description mismatchDescription, Exception e) { 49 | mismatchDescription 50 | .appendText("was ") 51 | .appendValue(json) 52 | .appendText(" which failed with ") 53 | .appendValue(e.getMessage()); 54 | } 55 | 56 | private static ReadContext parse(Object object) throws IOException { 57 | if (object instanceof String) { 58 | return JsonPath.parse((String) object); 59 | } else if (object instanceof File) { 60 | return JsonPath.parse((File) object); 61 | } else if (object instanceof ReadContext) { 62 | return (ReadContext) object; 63 | } else { 64 | return JsonPath.parse(object); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers; 2 | 3 | import com.jayway.jsonpath.JsonPath; 4 | import com.jayway.jsonpath.Predicate; 5 | import com.jayway.jsonpath.ReadContext; 6 | import org.hamcrest.Matcher; 7 | 8 | import java.io.File; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | import static org.hamcrest.Matchers.*; 13 | 14 | public class JsonPathMatchers { 15 | 16 | private JsonPathMatchers() { 17 | throw new AssertionError("prevent instantiation"); 18 | } 19 | 20 | public static Matcher hasJsonPath(String jsonPath) { 21 | return describedAs("has json path %0", 22 | isJson(withJsonPath(jsonPath)), 23 | jsonPath); 24 | } 25 | 26 | public static Matcher hasJsonPath(String jsonPath, Matcher resultMatcher) { 27 | return isJson(withJsonPath(jsonPath, resultMatcher)); 28 | } 29 | 30 | public static Matcher hasNoJsonPath(String jsonPath) { 31 | return isJson(withoutJsonPath(jsonPath)); 32 | } 33 | 34 | public static Matcher isJson() { 35 | return isJson(withJsonPath("$", anyOf(instanceOf(Map.class), instanceOf(List.class)))); 36 | } 37 | 38 | public static Matcher isJson(Matcher matcher) { 39 | return new IsJson(matcher); 40 | } 41 | 42 | public static Matcher isJsonString(Matcher matcher) { 43 | return new IsJson(matcher); 44 | } 45 | 46 | public static Matcher isJsonFile(Matcher matcher) { 47 | return new IsJson(matcher); 48 | } 49 | 50 | public static Matcher withJsonPath(String jsonPath, Predicate... filters) { 51 | return withJsonPath(JsonPath.compile(jsonPath, filters)); 52 | } 53 | 54 | public static Matcher withJsonPath(JsonPath jsonPath) { 55 | return describedAs("with json path %0", 56 | withJsonPath(jsonPath, anything()), 57 | jsonPath.getPath()); 58 | } 59 | 60 | public static Matcher withoutJsonPath(String jsonPath, Predicate... filters) { 61 | return withoutJsonPath(JsonPath.compile(jsonPath, filters)); 62 | } 63 | 64 | public static Matcher withoutJsonPath(JsonPath jsonPath) { 65 | return new WithoutJsonPath(jsonPath); 66 | } 67 | 68 | public static Matcher withJsonPath(String jsonPath, Matcher resultMatcher) { 69 | return withJsonPath(JsonPath.compile(jsonPath), resultMatcher); 70 | } 71 | 72 | public static Matcher withJsonPath(JsonPath jsonPath, Matcher resultMatcher) { 73 | return new WithJsonPath(jsonPath, resultMatcher); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /json-path-assert/src/main/java/com/jayway/jsonpath/matchers/WithJsonPath.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers; 2 | 3 | import com.jayway.jsonpath.JsonPath; 4 | import com.jayway.jsonpath.JsonPathException; 5 | import com.jayway.jsonpath.PathNotFoundException; 6 | import com.jayway.jsonpath.ReadContext; 7 | import org.hamcrest.Description; 8 | import org.hamcrest.Matcher; 9 | import org.hamcrest.TypeSafeMatcher; 10 | 11 | public class WithJsonPath extends TypeSafeMatcher { 12 | private final JsonPath jsonPath; 13 | private final Matcher resultMatcher; 14 | 15 | public WithJsonPath(JsonPath jsonPath, Matcher resultMatcher) { 16 | this.jsonPath = jsonPath; 17 | this.resultMatcher = resultMatcher; 18 | } 19 | 20 | @Override 21 | protected boolean matchesSafely(ReadContext context) { 22 | try { 23 | T value = context.read(jsonPath); 24 | return resultMatcher.matches(value); 25 | } catch (JsonPathException e) { 26 | return false; 27 | } 28 | } 29 | 30 | public void describeTo(Description description) { 31 | description 32 | .appendText("with json path ") 33 | .appendValue(jsonPath.getPath()) 34 | .appendText(" evaluated to ") 35 | .appendDescriptionOf(resultMatcher); 36 | } 37 | 38 | @Override 39 | protected void describeMismatchSafely(ReadContext context, Description mismatchDescription) { 40 | try { 41 | T value = jsonPath.read(context.jsonString()); 42 | mismatchDescription 43 | .appendText("json path ") 44 | .appendValue(jsonPath.getPath()) 45 | .appendText(" was evaluated to ") 46 | .appendValue(value); 47 | } catch (PathNotFoundException e) { 48 | mismatchDescription 49 | .appendText("json path ") 50 | .appendValue(jsonPath.getPath()) 51 | .appendText(" was not found in ") 52 | .appendValue(context.json()); 53 | } catch (JsonPathException e) { 54 | mismatchDescription 55 | .appendText("was ") 56 | .appendValue(context.json()); 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /json-path-assert/src/main/java/com/jayway/jsonpath/matchers/WithoutJsonPath.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers; 2 | 3 | import com.jayway.jsonpath.JsonPath; 4 | import com.jayway.jsonpath.JsonPathException; 5 | import com.jayway.jsonpath.ReadContext; 6 | import org.hamcrest.Description; 7 | import org.hamcrest.TypeSafeDiagnosingMatcher; 8 | 9 | public class WithoutJsonPath extends TypeSafeDiagnosingMatcher { 10 | private final JsonPath jsonPath; 11 | 12 | public WithoutJsonPath(JsonPath jsonPath) { 13 | this.jsonPath = jsonPath; 14 | } 15 | 16 | @Override 17 | protected boolean matchesSafely(ReadContext actual, Description mismatchDescription) { 18 | try { 19 | Object value = actual.read(jsonPath); 20 | mismatchDescription 21 | .appendText(jsonPath.getPath()) 22 | .appendText(" was evaluated to ") 23 | .appendValue(value); 24 | return false; 25 | } catch (JsonPathException e) { 26 | return true; 27 | } 28 | } 29 | 30 | @Override 31 | public void describeTo(Description description) { 32 | description.appendText("without json path ").appendValue(jsonPath.getPath()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /json-path-assert/src/test/java/com/jayway/jsonpath/matchers/DemoTest.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers; 2 | 3 | import org.junit.jupiter.api.Disabled; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.io.File; 7 | 8 | import static com.jayway.jsonpath.matchers.JsonPathMatchers.*; 9 | import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource; 10 | import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resourceAsFile; 11 | import static org.hamcrest.MatcherAssert.assertThat; 12 | import static org.hamcrest.Matchers.equalTo; 13 | 14 | @Disabled 15 | public class DemoTest { 16 | @Test 17 | public void shouldFailOnJsonString() { 18 | String json = resource("books.json"); 19 | assertThat(json, isJson(withJsonPath("$.store.name", equalTo("The Shop")))); 20 | } 21 | 22 | @Test 23 | public void shouldFailOnJsonFile() { 24 | File json = resourceAsFile("books.json"); 25 | assertThat(json, isJson(withJsonPath("$.store.name", equalTo("The Shop")))); 26 | } 27 | 28 | @Test 29 | public void shouldFailOnInvalidJsonString() { 30 | String json = resource("invalid.json"); 31 | assertThat(json, isJson(withJsonPath("$.store.name", equalTo("The Shop")))); 32 | } 33 | 34 | @Test 35 | public void shouldFailOnInvalidJsonFile() { 36 | File json = resourceAsFile("invalid.json"); 37 | assertThat(json, isJson(withJsonPath("$.store.name", equalTo("The Shop")))); 38 | } 39 | 40 | @Test 41 | public void shouldFailOnTypedJsonString() { 42 | String json = resource("books.json"); 43 | assertThat(json, isJsonString(withJsonPath("$.store.name", equalTo("The Shop")))); 44 | } 45 | 46 | @Test 47 | public void shouldFailOnTypedJsonFile() { 48 | File json = resourceAsFile("books.json"); 49 | assertThat(json, isJsonFile(withJsonPath("$.store.name", equalTo("The Shop")))); 50 | } 51 | 52 | @Test 53 | public void shouldFailOnTypedInvalidJsonString() { 54 | String json = resource("invalid.json"); 55 | assertThat(json, isJsonString(withJsonPath("$.store.name", equalTo("The Shop")))); 56 | } 57 | 58 | @Test 59 | public void shouldFailOnTypedInvalidJsonFile() { 60 | File json = resourceAsFile("invalid.json"); 61 | assertThat(json, isJsonFile(withJsonPath("$.store.name", equalTo("The Shop")))); 62 | } 63 | 64 | @Test 65 | public void shouldFailOnNonExistingJsonPath() { 66 | String json = resource("books.json"); 67 | assertThat(json, hasJsonPath("$.not-here")); 68 | } 69 | 70 | @Test 71 | public void shouldFailOnExistingJsonPath() { 72 | String json = resource("books.json"); 73 | assertThat(json, hasNoJsonPath("$.store.name")); 74 | } 75 | 76 | @Test 77 | public void shouldFailOnExistingJsonPathAlternative() { 78 | String json = resource("books.json"); 79 | assertThat(json, isJson(withoutJsonPath("$.store.name"))); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /json-path-assert/src/test/java/com/jayway/jsonpath/matchers/HasNoJsonPathTest.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasNoJsonPath; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | import static org.hamcrest.Matchers.*; 8 | 9 | public class HasNoJsonPathTest { 10 | private static final String JSON_STRING = "{" + 11 | "\"none\": null," + 12 | "\"name\": \"Jessie\"" + 13 | "}"; 14 | 15 | @Test 16 | public void shouldMatchMissingJsonPath() { 17 | assertThat(JSON_STRING, hasNoJsonPath("$.not_there")); 18 | } 19 | 20 | @Test 21 | public void shouldNotMatchExistingJsonPath() { 22 | assertThat(JSON_STRING, not(hasNoJsonPath("$.name"))); 23 | } 24 | 25 | @Test 26 | public void shouldNotMatchExplicitNull() { 27 | assertThat(JSON_STRING, not(hasNoJsonPath("$.none"))); 28 | } 29 | 30 | @Test 31 | public void shouldBeDescriptive() { 32 | assertThat(hasNoJsonPath("$.name"), 33 | hasToString(equalTo("is json without json path \"$['name']\""))); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonFileTest.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers; 2 | 3 | import com.jayway.jsonpath.Configuration; 4 | import com.jayway.jsonpath.matchers.helpers.StrictParsingConfiguration; 5 | import org.hamcrest.Description; 6 | import org.hamcrest.Matcher; 7 | import org.hamcrest.StringDescription; 8 | import org.junit.jupiter.api.AfterAll; 9 | import org.junit.jupiter.api.BeforeAll; 10 | import org.junit.jupiter.api.Test; 11 | 12 | import java.io.File; 13 | 14 | import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJsonFile; 15 | import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resourceAsFile; 16 | import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.*; 17 | import static org.hamcrest.MatcherAssert.assertThat; 18 | import static org.hamcrest.Matchers.*; 19 | 20 | public class IsJsonFileTest { 21 | private static final File BOOKS_JSON = resourceAsFile("books.json"); 22 | private static final File INVALID_JSON = resourceAsFile("invalid.json"); 23 | 24 | @BeforeAll 25 | public static void setupStrictJsonParsing() { 26 | Configuration.setDefaults(new StrictParsingConfiguration()); 27 | } 28 | 29 | @AfterAll 30 | public static void setupDefaultJsonParsing() { 31 | Configuration.setDefaults(null); 32 | } 33 | 34 | @Test 35 | public void shouldMatchJsonFileEvaluatedToTrue() { 36 | assertThat(BOOKS_JSON, isJsonFile(withPathEvaluatedTo(true))); 37 | } 38 | 39 | @Test 40 | public void shouldNotMatchJsonFileEvaluatedToFalse() { 41 | assertThat(BOOKS_JSON, not(isJsonFile(withPathEvaluatedTo(false)))); 42 | } 43 | 44 | @Test 45 | public void shouldNotMatchInvalidJsonFile() { 46 | assertThat(INVALID_JSON, not(isJsonFile(withPathEvaluatedTo(true)))); 47 | assertThat(INVALID_JSON, not(isJsonFile(withPathEvaluatedTo(false)))); 48 | } 49 | 50 | @Test 51 | public void shouldBeDescriptive() { 52 | Matcher matcher = isJsonFile(withPathEvaluatedTo(true)); 53 | Description description = new StringDescription(); 54 | matcher.describeTo(description); 55 | assertThat(description.toString(), startsWith("is json")); 56 | assertThat(description.toString(), containsString(MATCH_TRUE_TEXT)); 57 | } 58 | 59 | @Test 60 | public void shouldDescribeMismatchOfValidJson() { 61 | Matcher matcher = isJsonFile(withPathEvaluatedTo(true)); 62 | Description description = new StringDescription(); 63 | matcher.describeMismatch(BOOKS_JSON, description); 64 | assertThat(description.toString(), containsString(MISMATCHED_TEXT)); 65 | } 66 | 67 | @Test 68 | public void shouldDescribeMismatchOfInvalidJson() { 69 | Matcher matcher = isJsonFile(withPathEvaluatedTo(true)); 70 | Description description = new StringDescription(); 71 | matcher.describeMismatch(INVALID_JSON, description); 72 | assertThat(description.toString(), containsString("invalid.json")); 73 | assertThat(description.toString(), containsString("invalid-json")); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonStringTest.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers; 2 | 3 | import com.jayway.jsonpath.Configuration; 4 | import com.jayway.jsonpath.matchers.helpers.StrictParsingConfiguration; 5 | import org.hamcrest.Description; 6 | import org.hamcrest.Matcher; 7 | import org.hamcrest.StringDescription; 8 | import org.junit.jupiter.api.AfterAll; 9 | import org.junit.jupiter.api.BeforeAll; 10 | import org.junit.jupiter.api.Test; 11 | 12 | import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJsonString; 13 | import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource; 14 | import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.*; 15 | import static org.hamcrest.MatcherAssert.assertThat; 16 | import static org.hamcrest.Matchers.*; 17 | 18 | public class IsJsonStringTest { 19 | private static final String BOOKS_JSON = resource("books.json"); 20 | 21 | @BeforeAll 22 | public static void setupStrictJsonParsing() { 23 | Configuration.setDefaults(new StrictParsingConfiguration()); 24 | } 25 | 26 | @AfterAll 27 | public static void setupDefaultJsonParsing() { 28 | Configuration.setDefaults(null); 29 | } 30 | 31 | @Test 32 | public void shouldMatchJsonStringEvaluatedToTrue() { 33 | assertThat(BOOKS_JSON, isJsonString(withPathEvaluatedTo(true))); 34 | } 35 | 36 | @Test 37 | public void shouldNotMatchJsonStringEvaluatedToFalse() { 38 | assertThat(BOOKS_JSON, not(isJsonString(withPathEvaluatedTo(false)))); 39 | } 40 | 41 | @Test 42 | public void shouldNotMatchInvalidJsonString() { 43 | assertThat("invalid-json", not(isJsonString(withPathEvaluatedTo(true)))); 44 | assertThat("invalid-json", not(isJsonString(withPathEvaluatedTo(false)))); 45 | } 46 | 47 | @Test 48 | public void shouldBeDescriptive() { 49 | Matcher matcher = isJsonString(withPathEvaluatedTo(true)); 50 | Description description = new StringDescription(); 51 | matcher.describeTo(description); 52 | assertThat(description.toString(), startsWith("is json")); 53 | assertThat(description.toString(), containsString(MATCH_TRUE_TEXT)); 54 | } 55 | 56 | @Test 57 | public void shouldDescribeMismatchOfValidJson() { 58 | Matcher matcher = isJsonString(withPathEvaluatedTo(true)); 59 | Description description = new StringDescription(); 60 | matcher.describeMismatch(BOOKS_JSON, description); 61 | assertThat(description.toString(), containsString(MISMATCHED_TEXT)); 62 | } 63 | 64 | @Test 65 | public void shouldDescribeMismatchOfInvalidJson() { 66 | Matcher matcher = isJsonString(withPathEvaluatedTo(true)); 67 | Description description = new StringDescription(); 68 | matcher.describeMismatch("invalid-json", description); 69 | assertThat(description.toString(), containsString("\"invalid-json\"")); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /json-path-assert/src/test/java/com/jayway/jsonpath/matchers/WithoutJsonPathTest.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers; 2 | 3 | import com.jayway.jsonpath.JsonPath; 4 | import com.jayway.jsonpath.ReadContext; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import static com.jayway.jsonpath.JsonPath.compile; 8 | import static com.jayway.jsonpath.matchers.JsonPathMatchers.withoutJsonPath; 9 | import static org.hamcrest.MatcherAssert.assertThat; 10 | import static org.hamcrest.Matchers.*; 11 | 12 | public class WithoutJsonPathTest { 13 | private static final String JSON_STRING = "{" + 14 | "\"name\": \"Jessie\"," + 15 | "\"flag\": false," + 16 | "\"empty_array\": []," + 17 | "\"empty_object\": {}," + 18 | "\"none\": null" + 19 | "}"; 20 | private static final ReadContext JSON = JsonPath.parse(JSON_STRING); 21 | 22 | @Test 23 | public void shouldMatchNonExistingJsonPath() { 24 | assertThat(JSON, withoutJsonPath(compile("$.not_there"))); 25 | assertThat(JSON, withoutJsonPath("$.not_there")); 26 | } 27 | 28 | @Test 29 | public void shouldNotMatchExistingJsonPath() { 30 | assertThat(JSON, not(withoutJsonPath(compile("$.name")))); 31 | assertThat(JSON, not(withoutJsonPath("$.name"))); 32 | assertThat(JSON, not(withoutJsonPath("$.flag"))); 33 | assertThat(JSON, not(withoutJsonPath("$.empty_array"))); 34 | assertThat(JSON, not(withoutJsonPath("$.empty_object"))); 35 | assertThat(JSON, not(withoutJsonPath("$.none"))); 36 | } 37 | 38 | @Test 39 | public void shouldBeDescriptive() { 40 | assertThat(withoutJsonPath("$.name"), 41 | hasToString(equalTo("without json path \"$['name']\""))); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /json-path-assert/src/test/java/com/jayway/jsonpath/matchers/helpers/ResourceHelpers.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers.helpers; 2 | 3 | import org.apache.commons.io.IOUtils; 4 | 5 | import java.io.File; 6 | import java.io.IOException; 7 | import java.net.URISyntaxException; 8 | import java.net.URL; 9 | 10 | import static java.lang.ClassLoader.getSystemResource; 11 | import static java.lang.ClassLoader.getSystemResourceAsStream; 12 | 13 | public class ResourceHelpers { 14 | public static String resource(String resource) { 15 | try { 16 | return IOUtils.toString(getSystemResourceAsStream(resource)); 17 | } catch (IOException e) { 18 | throw new AssertionError("Resource not found: " + e.getMessage()); 19 | } 20 | } 21 | 22 | public static File resourceAsFile(String resource) { 23 | try { 24 | URL systemResource = getSystemResource(resource); 25 | return new File(systemResource.toURI()); 26 | } catch (URISyntaxException e) { 27 | throw new AssertionError("URI syntax error:" + e.getMessage()); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /json-path-assert/src/test/java/com/jayway/jsonpath/matchers/helpers/StrictParsingConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.jayway.jsonpath.matchers.helpers; 2 | 3 | import com.jayway.jsonpath.Configuration; 4 | import com.jayway.jsonpath.Option; 5 | import com.jayway.jsonpath.spi.json.JsonProvider; 6 | import com.jayway.jsonpath.spi.json.JsonSmartJsonProvider; 7 | import com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider; 8 | import com.jayway.jsonpath.spi.mapper.MappingProvider; 9 | import net.minidev.json.parser.JSONParser; 10 | 11 | import java.util.EnumSet; 12 | import java.util.Set; 13 | 14 | public class StrictParsingConfiguration implements Configuration.Defaults { 15 | 16 | private final JsonProvider jsonProvider = new JsonSmartJsonProvider(JSONParser.MODE_STRICTEST); 17 | private final MappingProvider mappingProvider = new JsonSmartMappingProvider(); 18 | 19 | public JsonProvider jsonProvider() { 20 | return jsonProvider; 21 | } 22 | 23 | public MappingProvider mappingProvider() { 24 | return mappingProvider; 25 | } 26 | 27 | public Set