001/*
002 * Copyright 2012-2016 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 *      http://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.boot.test.json;
018
019import org.assertj.core.api.AssertProvider;
020
021import org.springframework.core.ResolvableType;
022import org.springframework.util.Assert;
023
024/**
025 * JSON content created usually from a JSON tester. Generally used only to
026 * {@link AssertProvider provide} {@link JsonContentAssert} to AssertJ {@code assertThat}
027 * calls.
028 *
029 * @param <T> the source type that created the content
030 * @author Phillip Webb
031 * @since 1.4.0
032 */
033public final class JsonContent<T> implements AssertProvider<JsonContentAssert> {
034
035        private final Class<?> resourceLoadClass;
036
037        private final ResolvableType type;
038
039        private final String json;
040
041        /**
042         * Create a new {@link JsonContent} instance.
043         * @param resourceLoadClass the source class used to load resources
044         * @param type the type under test (or {@code null} if not known)
045         * @param json the actual JSON content
046         */
047        public JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json) {
048                Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
049                Assert.notNull(json, "JSON must not be null");
050                this.resourceLoadClass = resourceLoadClass;
051                this.type = type;
052                this.json = json;
053        }
054
055        /**
056         * Use AssertJ's {@link org.assertj.core.api.Assertions#assertThat assertThat}
057         * instead.
058         *
059         * @deprecated in favor of AssertJ's {@link org.assertj.core.api.Assertions#assertThat
060         * assertThat}
061         */
062        @Override
063        @Deprecated
064        public JsonContentAssert assertThat() {
065                return new JsonContentAssert(this.resourceLoadClass, this.json);
066        }
067
068        /**
069         * Return the actual JSON content string.
070         * @return the JSON content
071         */
072        public String getJson() {
073                return this.json;
074        }
075
076        @Override
077        public String toString() {
078                return "JsonContent " + this.json
079                                + (this.type == null ? "" : " created from " + this.type);
080        }
081
082}