001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.web.reactive.server;
018
019import java.util.function.Consumer;
020
021import org.hamcrest.Matcher;
022
023import org.springframework.lang.Nullable;
024import org.springframework.test.util.JsonPathExpectationsHelper;
025
026/**
027 * <a href="https://github.com/jayway/JsonPath">JsonPath</a> assertions.
028 *
029 * @author Rossen Stoyanchev
030 * @since 5.0
031 * @see <a href="https://github.com/jayway/JsonPath">https://github.com/jayway/JsonPath</a>
032 * @see JsonPathExpectationsHelper
033 */
034public class JsonPathAssertions {
035
036        private final WebTestClient.BodyContentSpec bodySpec;
037
038        private final String content;
039
040        private final JsonPathExpectationsHelper pathHelper;
041
042
043        JsonPathAssertions(WebTestClient.BodyContentSpec spec, String content, String expression, Object... args) {
044                this.bodySpec = spec;
045                this.content = content;
046                this.pathHelper = new JsonPathExpectationsHelper(expression, args);
047        }
048
049
050        /**
051         * Applies {@link JsonPathExpectationsHelper#assertValue(String, Object)}.
052         */
053        public WebTestClient.BodyContentSpec isEqualTo(Object expectedValue) {
054                this.pathHelper.assertValue(this.content, expectedValue);
055                return this.bodySpec;
056        }
057
058        /**
059         * Applies {@link JsonPathExpectationsHelper#exists(String)}.
060         */
061        public WebTestClient.BodyContentSpec exists() {
062                this.pathHelper.exists(this.content);
063                return this.bodySpec;
064        }
065
066        /**
067         * Applies {@link JsonPathExpectationsHelper#doesNotExist(String)}.
068         */
069        public WebTestClient.BodyContentSpec doesNotExist() {
070                this.pathHelper.doesNotExist(this.content);
071                return this.bodySpec;
072        }
073
074        /**
075         * Applies {@link JsonPathExpectationsHelper#assertValueIsEmpty(String)}.
076         */
077        public WebTestClient.BodyContentSpec isEmpty() {
078                this.pathHelper.assertValueIsEmpty(this.content);
079                return this.bodySpec;
080        }
081
082        /**
083         * Applies {@link JsonPathExpectationsHelper#assertValueIsNotEmpty(String)}.
084         */
085        public WebTestClient.BodyContentSpec isNotEmpty() {
086                this.pathHelper.assertValueIsNotEmpty(this.content);
087                return this.bodySpec;
088        }
089
090        /**
091         * Applies {@link JsonPathExpectationsHelper#hasJsonPath}.
092         * @since 5.0.3
093         */
094        public WebTestClient.BodyContentSpec hasJsonPath() {
095                this.pathHelper.hasJsonPath(this.content);
096                return this.bodySpec;
097        }
098
099        /**
100         * Applies {@link JsonPathExpectationsHelper#doesNotHaveJsonPath}.
101         * @since 5.0.3
102         */
103        public WebTestClient.BodyContentSpec doesNotHaveJsonPath() {
104                this.pathHelper.doesNotHaveJsonPath(this.content);
105                return this.bodySpec;
106        }
107
108        /**
109         * Applies {@link JsonPathExpectationsHelper#assertValueIsBoolean(String)}.
110         */
111        public WebTestClient.BodyContentSpec isBoolean() {
112                this.pathHelper.assertValueIsBoolean(this.content);
113                return this.bodySpec;
114        }
115
116        /**
117         * Applies {@link JsonPathExpectationsHelper#assertValueIsNumber(String)}.
118         */
119        public WebTestClient.BodyContentSpec isNumber() {
120                this.pathHelper.assertValueIsNumber(this.content);
121                return this.bodySpec;
122        }
123
124        /**
125         * Applies {@link JsonPathExpectationsHelper#assertValueIsArray(String)}.
126         */
127        public WebTestClient.BodyContentSpec isArray() {
128                this.pathHelper.assertValueIsArray(this.content);
129                return this.bodySpec;
130        }
131
132        /**
133         * Applies {@link JsonPathExpectationsHelper#assertValueIsMap(String)}.
134         */
135        public WebTestClient.BodyContentSpec isMap() {
136                this.pathHelper.assertValueIsMap(this.content);
137                return this.bodySpec;
138        }
139
140        /**
141         * Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher)}.
142         * @since 5.1
143         */
144        public <T> WebTestClient.BodyContentSpec value(Matcher<T> matcher) {
145                this.pathHelper.assertValue(this.content, matcher);
146                return this.bodySpec;
147        }
148
149        /**
150         * Delegates to {@link JsonPathExpectationsHelper#assertValue(String, Matcher, Class)}.
151         * @since 5.1
152         */
153        public <T> WebTestClient.BodyContentSpec value(Matcher<T> matcher, Class<T> targetType) {
154                this.pathHelper.assertValue(this.content, matcher, targetType);
155                return this.bodySpec;
156        }
157
158        /**
159         * Consume the result of the JSONPath evaluation.
160         * @since 5.1
161         */
162        @SuppressWarnings("unchecked")
163        public <T> WebTestClient.BodyContentSpec value(Consumer<T> consumer) {
164                Object value = this.pathHelper.evaluateJsonPath(this.content);
165                consumer.accept((T) value);
166                return this.bodySpec;
167        }
168
169        /**
170         * Consume the result of the JSONPath evaluation and provide a target class.
171         * @since 5.1
172         */
173        @SuppressWarnings("unchecked")
174        public <T> WebTestClient.BodyContentSpec value(Consumer<T> consumer, Class<T> targetType) {
175                Object value = this.pathHelper.evaluateJsonPath(this.content, targetType);
176                consumer.accept((T) value);
177                return this.bodySpec;
178        }
179
180
181        @Override
182        public boolean equals(@Nullable Object obj) {
183                throw new AssertionError("Object#equals is disabled " +
184                                "to avoid being used in error instead of JsonPathAssertions#isEqualTo(String).");
185        }
186
187        @Override
188        public int hashCode() {
189                return super.hashCode();
190        }
191
192}