001/*
002 * Copyright 2012-2018 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         * @deprecated in favor of AssertJ's {@link org.assertj.core.api.Assertions#assertThat
059         * assertThat}
060         */
061        @Override
062        @Deprecated
063        public JsonContentAssert assertThat() {
064                return new JsonContentAssert(this.resourceLoadClass, this.json);
065        }
066
067        /**
068         * Return the actual JSON content string.
069         * @return the JSON content
070         */
071        public String getJson() {
072                return this.json;
073        }
074
075        @Override
076        public String toString() {
077                String createdFrom = (this.type != null) ? " created from " + this.type : "";
078                return "JsonContent " + this.json + createdFrom;
079        }
080
081}