001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.bct.assertions;
018
019import static org.apache.juneau.commons.reflect.ReflectionUtils.*;
020import static org.apache.juneau.commons.utils.Shorts.*;
021
022import java.io.*;
023import java.time.*;
024import java.util.*;
025import java.util.function.*;
026
027import org.apache.juneau.commons.function.*;
028
029
030/**
031 * Used for assertion calls against generic POJOs.
032 *
033 * <p>
034 * Extends from {@link FluentObjectAssertion} allowing you to perform basic assertions, but adds several transform
035 * methods to convert to more-specific assertion types.
036 *
037 * <h2 class='section'>Example:</h2>
038 * <p class='bjava'>
039 *    <jk>import static</jk> org.apache.juneau.bct.assertions.Assertions.*;
040 *
041 *    List&lt;MyBean&gt; <jv>listOfBeans</jv> = ...;
042 *    <jsm>assertList</jsm>(<jv>listOfBeans</jv>)
043 *       .asItem(1)  <jc>// Returns an AnyAssertion.</jc>
044 *       .asBean()  <jc>// Transforms to BeanAssertion.</jc>
045 *          .asProperty(<js>"foo"</js>)  <jc>// Returns an AnyAssertion.</jc>
046 *          .asString()  <jc>// Transforms to StringAssertion.</jc>
047 *             .is(<js>"bar"</js>);  <jc>// Performs test.</jc>
048 * </p>
049 *
050 * <h2 class='section'>Test Methods:</h2>
051 * <ul class='javatree'>
052 *    <li class='jc'>{@link FluentObjectAssertion}
053 *    <ul class='javatreec'>
054 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
055 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
056 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
057 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
058 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
059 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
060 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
061 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
062 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
063 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
064 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
065 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
066 *    </ul>
067 * </ul>
068 *
069 * <h2 class='section'>Transform Methods:</h2>
070 * <ul class='javatree'>
071 *    <li class='jc'>{@link FluentAnyAssertion}
072 *    <ul class='javatreec'>
073 *       <li class='jm'>{@link FluentAnyAssertion#asArray(Class) asArray(Class)}
074 *       <li class='jm'>{@link FluentAnyAssertion#asIntArray() asIntArray()}
075 *       <li class='jm'>{@link FluentAnyAssertion#asLongArray() asLongArray()}
076 *       <li class='jm'>{@link FluentAnyAssertion#asShortArray() asShortArray()}
077 *       <li class='jm'>{@link FluentAnyAssertion#asFloatArray() asFloatArray()}
078 *       <li class='jm'>{@link FluentAnyAssertion#asDoubleArray() asDoubleArray()}
079 *       <li class='jm'>{@link FluentAnyAssertion#asCharArray() asCharArray()}
080 *       <li class='jm'>{@link FluentAnyAssertion#asByteArray() asByteArray()}
081 *       <li class='jm'>{@link FluentAnyAssertion#asBooleanArray() asBooleanArray()}
082 *       <li class='jm'>{@link FluentAnyAssertion#asBoolean() asBoolean()}
083 *       <li class='jm'>{@link FluentAnyAssertion#asBytes() asBytes()}
084 *       <li class='jm'>{@link FluentAnyAssertion#asCollection() asCollection()}
085 *       <li class='jm'>{@link FluentAnyAssertion#asCollection(Class) asCollection(Class)}
086 *       <li class='jm'>{@link FluentAnyAssertion#asStringList() asStringList()}
087 *       <li class='jm'>{@link FluentAnyAssertion#asComparable() asComparable()}
088 *       <li class='jm'>{@link FluentAnyAssertion#asDate() asDate()}
089 *       <li class='jm'>{@link FluentAnyAssertion#asInteger() asInteger()}
090 *       <li class='jm'>{@link FluentAnyAssertion#asLong() asLong()}
091 *       <li class='jm'>{@link FluentAnyAssertion#asList() asList()}
092 *       <li class='jm'>{@link FluentAnyAssertion#asList(Class) asList(Class)}
093 *       <li class='jm'>{@link FluentAnyAssertion#asMap() asMap()}
094 *       <li class='jm'>{@link FluentAnyAssertion#asMap(Class,Class) asMap(Class,Class)}
095 *       <li class='jm'>{@link FluentAnyAssertion#asBean() asBean()}
096 *       <li class='jm'>{@link FluentAnyAssertion#asBean(Class) asBean(Class)}
097 *       <li class='jm'>{@link FluentAnyAssertion#asBeanList(Class) asBeanList(Class)}
098 *       <li class='jm'>{@link FluentAnyAssertion#asZonedDateTime() asZonedDateTime()}
099 *    </ul>
100 *    <li class='jc'>{@link FluentObjectAssertion}
101 *    <ul class='javatreec'>
102 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
103 *       <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)}
104 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)}
105 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
106 * </ul>
107 * </ul>
108 *
109 * <h2 class='section'>Configuration Methods:</h2>
110 * <ul class='javatree'>
111 *    <li class='jc'>{@link Assertion}
112 *    <ul class='javatreec'>
113 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
114 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
115 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
116 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
117 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
118 *    </ul>
119 * </ul>
120 *
121 * <h2 class='section'>See Also:</h2><ul>
122 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a>
123 * </ul>
124 *
125 * @param <T> The object type.
126 * @param <R> The return type.
127 */
128@SuppressWarnings({
129   "java:S115", // Constants use UPPER_snakeCase convention
130   "java:S1192", // Duplicated literals (argument/property names) read more clearly inline than as constants
131   "unchecked" // The as*() narrowing methods re-type this assertion's value via cast(), which cannot be verified at compile time
132})
133public class FluentAnyAssertion<T,R> extends FluentObjectAssertion<T,R> {
134
135   // @formatter:off
136   private static final Messages MESSAGES = Messages.of(FluentAnyAssertion.class, "Messages");
137   private static final String
138      MSG_objectWasNotType = MESSAGES.getString("objectWasNotType");
139   // @formatter:on
140
141   /**
142    * Chained constructor.
143    *
144    * <p>
145    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
146    *
147    * @param creator
148    *    The assertion that created this assertion.
149    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
150    * @param value
151    *    The object being tested.
152    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
153    * @param returns
154    *    The object to return after a test method is called.
155    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
156    * used on the same assertion.
157    */
158   public FluentAnyAssertion(Assertion creator, T value, R returns) {
159      super(creator, value, returns);
160   }
161
162   /**
163    * Constructor.
164    *
165    * @param value
166    *    The object being tested.
167    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
168    * @param returns
169    *    The object to return after a test method is called.
170    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
171    * used on the same assertion.
172    */
173   public FluentAnyAssertion(T value, R returns) {
174      this(null, value, returns);
175   }
176
177   /**
178    * Converts this object assertion into an array assertion.
179    *
180    * @param <E> The element type of the array.
181    * @param elementType The element type of the array.
182    * @return A new assertion.
183    * @throws AssertionError If object is not an array.
184    */
185   public <E> FluentArrayAssertion<E,R> asArray(Class<E> elementType) throws AssertionError {
186      reqnn("elementType", elementType);
187      return new FluentArrayAssertion<>(this, cast(arrayClass(elementType)), returns());
188   }
189
190   /**
191    * Converts this object assertion into a bean assertion.
192    *
193    * @return A new assertion.
194    * @throws AssertionError If object is not a bean.
195    */
196   public FluentBeanAssertion<T,R> asBean() {
197      return new FluentBeanAssertion<>(this, orElse(null), returns());
198   }
199
200   /**
201    * Converts this object assertion into a bean assertion.
202    *
203    * @param <T2> The bean type.
204    * @param beanType The bean type.
205    * @return A new assertion.
206    * @throws AssertionError If object is not a bean.
207    */
208   public <T2> FluentBeanAssertion<T2,R> asBean(Class<T2> beanType) {
209      reqnn("beanType", beanType);
210      return new FluentBeanAssertion<>(this, cast(beanType), returns());
211   }
212
213   /**
214    * Converts this object assertion into a list-of-beans assertion.
215    *
216    * @param <T2> The bean type.
217    * @param beanType The bean type.
218    * @return A new assertion.
219    * @throws AssertionError If object is not a bean.
220    */
221   public <T2> FluentBeanListAssertion<T2,R> asBeanList(Class<T2> beanType) {
222      reqnn("beanType", beanType);
223      return new FluentBeanListAssertion<>(this, cast(List.class), returns());
224   }
225
226   /**
227    * Converts this object assertion into a boolean assertion.
228    *
229    * @return A new assertion.
230    * @throws AssertionError If object is not a boolean.
231    */
232   public FluentBooleanAssertion<R> asBoolean() {
233      return new FluentBooleanAssertion<>(this, cast(Boolean.class), returns());
234   }
235
236   /**
237    * Converts this object assertion into a primitive boolean array assertion.
238    *
239    * @return A new assertion.
240    * @throws AssertionError If object is not an boolean array.
241    */
242   public FluentPrimitiveArrayAssertion<Boolean,boolean[],R> asBooleanArray() throws AssertionError {
243      return new FluentPrimitiveArrayAssertion<>(this, cast(boolean[].class), returns());
244   }
245
246   /**
247    * Converts this object assertion into a primitive byte array assertion.
248    *
249    * @return A new assertion.
250    * @throws AssertionError If object is not an byte array.
251    */
252   public FluentPrimitiveArrayAssertion<Byte,byte[],R> asByteArray() throws AssertionError {
253      return new FluentPrimitiveArrayAssertion<>(this, cast(byte[].class), returns());
254   }
255
256   /**
257    * Converts this object assertion into a byte array assertion.
258    *
259    * @return A new assertion.
260    * @throws AssertionError If object is not a byte array.
261    */
262   public FluentByteArrayAssertion<R> asBytes() {
263      return new FluentByteArrayAssertion<>(this, cast(byte[].class), returns());
264   }
265
266   /**
267    * Converts this object assertion into a primitive char array assertion.
268    *
269    * @return A new assertion.
270    * @throws AssertionError If object is not an char array.
271    */
272   public FluentPrimitiveArrayAssertion<Character,char[],R> asCharArray() throws AssertionError {
273      return new FluentPrimitiveArrayAssertion<>(this, cast(char[].class), returns());
274   }
275
276   /**
277    * Converts this object assertion into a collection assertion.
278    *
279    * @return A new assertion.
280    * @throws AssertionError If object is not a collection.
281    */
282   public FluentCollectionAssertion<Object,R> asCollection() {
283      return asCollection(Object.class);
284   }
285
286   /**
287    * Converts this object assertion into a collection assertion.
288    *
289    * @param <E> The element type of the collection.
290    * @param elementType The element type of the collection.
291    * @return A new assertion.
292    * @throws AssertionError If object is not a collection.
293    */
294   public <E> FluentCollectionAssertion<E,R> asCollection(Class<E> elementType) {
295      reqnn("elementType", elementType);
296      return new FluentCollectionAssertion<>(this, cast(Collection.class), returns());
297   }
298
299   /**
300    * Converts this object assertion into a comparable object assertion.
301    *
302    * @param <T2> The comparable type.
303    * @return A new assertion.
304    * @throws AssertionError If object is not an instance of {@link Comparable}.
305    */
306   public <T2 extends Comparable<T2>> FluentComparableAssertion<T2,R> asComparable() {
307      return new FluentComparableAssertion<>(this, (T2)cast(Comparable.class), returns());
308   }
309
310   /**
311    * Converts this object assertion into a date assertion.
312    *
313    * @return A new assertion.
314    * @throws AssertionError If object is not a date.
315    */
316   public FluentDateAssertion<R> asDate() {
317      return new FluentDateAssertion<>(this, cast(Date.class), returns());
318   }
319
320   /**
321    * Converts this object assertion into a primitive double array assertion.
322    *
323    * @return A new assertion.
324    * @throws AssertionError If object is not an double array.
325    */
326   public FluentPrimitiveArrayAssertion<Double,double[],R> asDoubleArray() throws AssertionError {
327      return new FluentPrimitiveArrayAssertion<>(this, cast(double[].class), returns());
328   }
329
330   /**
331    * Converts this object assertion into a primitive float array assertion.
332    *
333    * @return A new assertion.
334    * @throws AssertionError If object is not an float array.
335    */
336   public FluentPrimitiveArrayAssertion<Float,float[],R> asFloatArray() throws AssertionError {
337      return new FluentPrimitiveArrayAssertion<>(this, cast(float[].class), returns());
338   }
339
340   /**
341    * Converts this object assertion into a primitive int array assertion.
342    *
343    * @return A new assertion.
344    * @throws AssertionError If object is not an int array.
345    */
346   public FluentPrimitiveArrayAssertion<Integer,int[],R> asIntArray() throws AssertionError {
347      return new FluentPrimitiveArrayAssertion<>(this, cast(int[].class), returns());
348   }
349
350   /**
351    * Converts this object assertion into an integer assertion.
352    *
353    * @return A new assertion.
354    * @throws AssertionError If object is not an integer.
355    */
356   public FluentIntegerAssertion<R> asInteger() {
357      return new FluentIntegerAssertion<>(this, cast(Integer.class), returns());
358   }
359
360   /**
361    * Converts this object assertion into a list assertion.
362    *
363    * @return A new assertion.
364    * @throws AssertionError If object is not a list.
365    */
366   public FluentListAssertion<Object,R> asList() {
367      return asList(Object.class);
368   }
369
370   /**
371    * Converts this object assertion into a list assertion.
372    *
373    * @param <E> The element type.
374    * @param elementType The element type.
375    * @return A new assertion.
376    * @throws AssertionError If object is not a list.
377    */
378   public <E> FluentListAssertion<E,R> asList(Class<E> elementType) {
379      reqnn("elementType", elementType);
380      return new FluentListAssertion<>(this, cast(List.class), returns());
381   }
382
383   /**
384    * Converts this object assertion into a long assertion.
385    *
386    * @return A new assertion.
387    * @throws AssertionError If object is not a long.
388    */
389   public FluentLongAssertion<R> asLong() {
390      return new FluentLongAssertion<>(this, cast(Long.class), returns());
391   }
392
393   /**
394    * Converts this object assertion into a primitive long array assertion.
395    *
396    * @return A new assertion.
397    * @throws AssertionError If object is not an long array.
398    */
399   public FluentPrimitiveArrayAssertion<Long,long[],R> asLongArray() throws AssertionError {
400      return new FluentPrimitiveArrayAssertion<>(this, cast(long[].class), returns());
401   }
402
403   /**
404    * Converts this object assertion into a map assertion.
405    *
406    * @return A new assertion.
407    * @throws AssertionError If object is not a map.
408    */
409   public FluentMapAssertion<String,Object,R> asMap() {
410      return asMap(String.class, Object.class);
411   }
412
413   /**
414    * Converts this object assertion into a map assertion with the specified key and value types.
415    *
416    * @param <K> The key type.
417    * @param <V> The value type.
418    * @param keyType The key type.
419    * @param valueType The value type.
420    * @return A new assertion.
421    * @throws AssertionError If object is not a map.
422    */
423   public <K,V> FluentMapAssertion<K,V,R> asMap(Class<K> keyType, Class<V> valueType) {
424      reqnn("keyType", keyType);
425      reqnn("valueType", valueType);
426      return new FluentMapAssertion<>(this, cast(Map.class), returns());
427   }
428
429   /**
430    * Converts this object assertion into a primitive short array assertion.
431    *
432    * @return A new assertion.
433    * @throws AssertionError If object is not an short array.
434    */
435   public FluentPrimitiveArrayAssertion<Short,short[],R> asShortArray() throws AssertionError {
436      return new FluentPrimitiveArrayAssertion<>(this, cast(short[].class), returns());
437   }
438
439   /**
440    * Converts this object assertion into a collection assertion.
441    *
442    * @return A new assertion.
443    * @throws AssertionError If object is not a collection.
444    */
445   public FluentStringListAssertion<R> asStringList() {
446      return new FluentStringListAssertion<>(this, cast(List.class), returns());
447   }
448
449   /**
450    * Converts this object assertion into a zoned-datetime assertion.
451    *
452    * @return A new assertion.
453    * @throws AssertionError If object is not a zoned-datetime.
454    */
455   public FluentZonedDateTimeAssertion<R> asZonedDateTime() {
456      return new FluentZonedDateTimeAssertion<>(this, cast(ZonedDateTime.class), returns());
457   }
458
459   @Override /* Overridden from Assertion */
460   public FluentAnyAssertion<T,R> setMsg(String msg, Object...args) {
461      super.setMsg(msg, args);
462      return this;
463   }
464
465   @Override /* Overridden from Assertion */
466   public FluentAnyAssertion<T,R> setOut(PrintStream value) {
467      super.setOut(value);
468      return this;
469   }
470
471   @Override /* Overridden from Assertion */
472   public FluentAnyAssertion<T,R> setSilent() {
473      super.setSilent();
474      return this;
475   }
476
477   @Override /* Overridden from Assertion */
478   public FluentAnyAssertion<T,R> setStdOut() {
479      super.setStdOut();
480      return this;
481   }
482
483   @Override /* Overridden from Assertion */
484   public FluentAnyAssertion<T,R> setThrowable(Class<? extends RuntimeException> value) {
485      super.setThrowable(value);
486      return this;
487   }
488
489   private <T2> T2 cast(Class<T2> c) throws AssertionError {
490      Object o = orElse(null);
491      if (o == null || c.isInstance(o))
492         return c.cast(o);
493      throw new BasicAssertionError(MSG_objectWasNotType, info(c).getNameFull(), cn(o.getClass()));
494   }
495}