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 * Object content usually created from {@link AbstractJsonMarshalTester}. Generally used
026 * only to {@link AssertProvider provide} {@link ObjectContentAssert} to AssertJ
027 * {@code assertThat} calls.
028 *
029 * @param <T> the content type
030 * @author Phillip Webb
031 * @since 1.4.0
032 */
033public final class ObjectContent<T> implements AssertProvider<ObjectContentAssert<T>> {
034
035        private final ResolvableType type;
036
037        private final T object;
038
039        /**
040         * Create a new {@link ObjectContent} instance.
041         * @param type the type under test (or {@code null} if not known)
042         * @param object the actual object content
043         */
044        public ObjectContent(ResolvableType type, T object) {
045                Assert.notNull(object, "Object must not be null");
046                this.type = type;
047                this.object = object;
048        }
049
050        @Override
051        public ObjectContentAssert<T> assertThat() {
052                return new ObjectContentAssert<>(this.object);
053        }
054
055        /**
056         * Return the actual object content.
057         * @return the object content
058         */
059        public T getObject() {
060                return this.object;
061        }
062
063        @Override
064        public String toString() {
065                String createdFrom = (this.type != null) ? " created from " + this.type : "";
066                return "ObjectContent " + this.object + createdFrom;
067        }
068
069}