001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.assertions;
014
015import static java.util.stream.Collectors.*;
016import static org.apache.juneau.common.internal.StringUtils.*;
017import static java.util.Arrays.*;
018
019import java.io.*;
020import java.util.function.*;
021
022import org.apache.juneau.*;
023import org.apache.juneau.internal.*;
024import org.apache.juneau.serializer.*;
025
026/**
027 * Used for fluent assertion calls against Java beans.
028 *
029 * <h5 class='section'>Test Methods:</h5>
030 * <p>
031 * <ul class='javatree'>
032 *    <li class='jc'>{@link FluentObjectAssertion}
033 *    <ul class='javatreec'>
034 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
035 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
036 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
037 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
038 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
039 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
040 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
041 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
042 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
043 *       <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)}
044 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
045 *       <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
046 *       <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
047 *       <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
048 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
049 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
050 *    </ul>
051 * </ul>
052 *
053 * <h5 class='section'>Transform Methods:</h5>
054 * <p>
055 * <ul class='javatree'>
056 *    <li class='jc'>{@link FluentBeanAssertion}
057 *    <ul class='javatreec'>
058 *       <li class='jm'>{@link FluentBeanAssertion#asPropertyMap(String...) asPropertyMap(String...)}
059 *       <li class='jm'>{@link FluentBeanAssertion#asProperty(String) asProperty(String)}
060 *       <li class='jm'>{@link FluentBeanAssertion#asProperties(String...) asProperties(String...)}
061 *    </ul>
062 *    <li class='jc'>{@link FluentObjectAssertion}
063 *    <ul class='javatreec'>
064 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
065 *       <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)}
066 *       <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)}
067 *       <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()}
068 *       <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
069 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
070 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
071 * </ul>
072 * </ul>
073 *
074 * <h5 class='section'>Configuration Methods:</h5>
075 * <p>
076 * <ul class='javatree'>
077 *    <li class='jc'>{@link Assertion}
078 *    <ul class='javatreec'>
079 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
080 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
081 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
082 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
083 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
084*  </ul>
085 * </ul>
086 *
087 * <h5 class='section'>See Also:</h5><ul>
088 *    <li class='link'><a class="doclink" href="../../../../index.html#ja.Overview">Overview &gt; juneau-assertions &gt; Overview</a>
089 * </ul>
090 *
091 * @param <T> The bean type.
092 * @param <R> The return type.
093 */
094@FluentSetters(returns="FluentBeanAssertion<T,R>")
095public class FluentBeanAssertion<T,R> extends FluentObjectAssertion<T,R> {
096
097   //-----------------------------------------------------------------------------------------------------------------
098   // Instance
099   //-----------------------------------------------------------------------------------------------------------------
100
101   /**
102    * Constructor.
103    *
104    * @param value
105    *    The object being tested.
106    *    <br>Can be <jk>null</jk>.
107    * @param returns
108    *    The object to return after a test method is called.
109    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
110    * used on the same assertion.
111    */
112   public FluentBeanAssertion(T value, R returns) {
113      this(null, value, returns);
114   }
115
116   /**
117    * Chained constructor.
118    *
119    * <p>
120    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
121    *
122    * @param creator
123    *    The assertion that created this assertion.
124    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
125    * @param value
126    *    The object being tested.
127    *    <br>Can be <jk>null</jk>.
128    * @param returns
129    *    The object to return after a test method is called.
130    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
131    * used on the same assertion.
132    */
133   public FluentBeanAssertion(Assertion creator, T value, R returns) {
134      super(creator, value, returns);
135   }
136
137   //-----------------------------------------------------------------------------------------------------------------
138   // Transform methods
139   //-----------------------------------------------------------------------------------------------------------------
140
141   @Override /* FluentObjectAssertion */
142   public FluentBeanAssertion<T,R> asTransformed(Function<T,T> function) {
143      return new FluentBeanAssertion<>(this, function.apply(orElse(null)), returns());
144   }
145
146   /**
147    * Extracts the specified fields of this bean into a simple map of key/value pairs and returns it as
148    * a new {@link MapAssertion}.
149    *
150    * @param names The fields to extract.  Can also pass in comma-delimited lists.
151    * @return This object.
152    */
153   public FluentMapAssertion<String,Object,R> asPropertyMap(String...names) {
154      return new FluentMapAssertion<>(this, toBeanMap().getProperties(split(names, ',')), returns());
155   }
156
157   /**
158    * Extracts the specified property as an {@link FluentAnyAssertion}.
159    *
160    * @param name The property to extract.  Can also pass in comma-delimited lists.
161    * @return An assertion of the property value.
162    */
163   public FluentAnyAssertion<Object,R> asProperty(String name) {
164      return new FluentAnyAssertion<>(this, toBeanMap().get(name), returns());
165   }
166
167   /**
168    * Extracts the specified property as an {@link FluentListAssertion}.
169    *
170    * @param names The names of the properties to extract.  Can also pass in comma-delimited lists.
171    * @return An assertion of the property values.
172    */
173   public FluentListAssertion<Object,R> asProperties(String...names) {
174      BeanMap<T> bm = toBeanMap();
175      return new FluentListAssertion<>(this, stream(names).map(x -> bm.get(x)).collect(toList()), returns());
176   }
177
178   //-----------------------------------------------------------------------------------------------------------------
179   // Fluent setters
180   //-----------------------------------------------------------------------------------------------------------------
181
182   // <FluentSetters>
183
184   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
185   public FluentBeanAssertion<T,R> setMsg(String msg, Object...args) {
186      super.setMsg(msg, args);
187      return this;
188   }
189
190   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
191   public FluentBeanAssertion<T,R> setOut(PrintStream value) {
192      super.setOut(value);
193      return this;
194   }
195
196   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
197   public FluentBeanAssertion<T,R> setSilent() {
198      super.setSilent();
199      return this;
200   }
201
202   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
203   public FluentBeanAssertion<T,R> setStdOut() {
204      super.setStdOut();
205      return this;
206   }
207
208   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
209   public FluentBeanAssertion<T,R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
210      super.setThrowable(value);
211      return this;
212   }
213
214   // </FluentSetters>
215
216   //-----------------------------------------------------------------------------------------------------------------
217   // Utility methods
218   //-----------------------------------------------------------------------------------------------------------------
219
220   private BeanMap<T> toBeanMap() {
221      return BeanMap.of(value());
222   }
223}