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 java.util.Arrays.*;
020import static org.apache.juneau.commons.utils.StringUtils.*;
021
022import java.io.*;
023import java.util.function.*;
024
025import org.apache.juneau.commons.bean.*;
026import org.apache.juneau.commons.function.*;
027
028/**
029 * Used for fluent assertion calls against Java beans.
030 *
031 * <h2 class='section'>Test Methods:</h2>
032 * <ul class='javatree'>
033 *    <li class='jc'>{@link FluentObjectAssertion}
034 *    <ul class='javatreec'>
035 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
036 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
037 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
038 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
039 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
040 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
041 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
042 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
043 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
044 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
045 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
046 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
047 *    </ul>
048 * </ul>
049 *
050 * <h2 class='section'>Transform Methods:</h2>
051 * <ul class='javatree'>
052 *    <li class='jc'>{@link FluentBeanAssertion}
053 *    <ul class='javatreec'>
054 *       <li class='jm'>{@link FluentBeanAssertion#asPropertyMap(String...) asPropertyMap(String...)}
055 *       <li class='jm'>{@link FluentBeanAssertion#asProperty(String) asProperty(String)}
056 *       <li class='jm'>{@link FluentBeanAssertion#asProperties(String...) asProperties(String...)}
057 *    </ul>
058 *    <li class='jc'>{@link FluentObjectAssertion}
059 *    <ul class='javatreec'>
060 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
061 *       <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)}
062 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)}
063 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
064 * </ul>
065 * </ul>
066 *
067 * <h2 class='section'>Configuration Methods:</h2>
068 * <ul class='javatree'>
069 *    <li class='jc'>{@link Assertion}
070 *    <ul class='javatreec'>
071 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
072 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
073 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
074 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
075 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
076*  </ul>
077 * </ul>
078 *
079 * <h2 class='section'>See Also:</h2><ul>
080 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a>
081 * </ul>
082 *
083 * @param <T> The bean type.
084 * @param <R> The return type.
085 */
086public class FluentBeanAssertion<T,R> extends FluentObjectAssertion<T,R> {
087
088   /**
089    * Chained constructor.
090    *
091    * <p>
092    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
093    *
094    * @param creator
095    *    The assertion that created this assertion.
096    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
097    * @param value
098    *    The object being tested.
099    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
100    * @param returns
101    *    The object to return after a test method is called.
102    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
103    * used on the same assertion.
104    */
105   public FluentBeanAssertion(Assertion creator, T value, R returns) {
106      super(creator, value, returns);
107   }
108
109   /**
110    * Constructor.
111    *
112    * @param value
113    *    The object being tested.
114    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
115    * @param returns
116    *    The object to return after a test method is called.
117    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
118    * used on the same assertion.
119    */
120   public FluentBeanAssertion(T value, R returns) {
121      this(null, value, returns);
122   }
123
124   /**
125    * Extracts the specified property as an {@link FluentListAssertion}.
126    *
127    * @param names The names of the properties to extract.  Can also pass in comma-delimited lists.
128    * @return An assertion of the property values.
129    */
130   public FluentListAssertion<Object,R> asProperties(String...names) {
131      BeanMap<T> bm = toBeanMap();
132      return new FluentListAssertion<>(this, stream(names).map(bm::get).toList(), returns());
133   }
134
135   /**
136    * Extracts the specified property as an {@link FluentAnyAssertion}.
137    *
138    * @param name The property to extract.  Can also pass in comma-delimited lists.
139    * @return An assertion of the property value.
140    */
141   public FluentAnyAssertion<Object,R> asProperty(String name) {
142      return new FluentAnyAssertion<>(this, toBeanMap().get(name), returns());
143   }
144
145   /**
146    * Extracts the specified fields of this bean into a simple map of key/value pairs and returns it as
147    * a new {@link MapAssertion}.
148    *
149    * @param names The fields to extract.  Can also pass in comma-delimited lists.
150    * @return This object.
151    */
152   public FluentMapAssertion<String,Object,R> asPropertyMap(String...names) {
153      return new FluentMapAssertion<>(this, toBeanMap().getProperties(splita(names, ',')), returns());
154   }
155
156   @Override /* Overridden from FluentObjectAssertion */
157   public FluentBeanAssertion<T,R> asTransformed(UnaryOperator<T> function) {
158      return new FluentBeanAssertion<>(this, function.apply(orElse(null)), returns());
159   }
160
161   @Override /* Overridden from Assertion */
162   public FluentBeanAssertion<T,R> setMsg(String msg, Object...args) {
163      super.setMsg(msg, args);
164      return this;
165   }
166
167   @Override /* Overridden from Assertion */
168   public FluentBeanAssertion<T,R> setOut(PrintStream value) {
169      super.setOut(value);
170      return this;
171   }
172
173   @Override /* Overridden from Assertion */
174   public FluentBeanAssertion<T,R> setSilent() {
175      super.setSilent();
176      return this;
177   }
178
179   @Override /* Overridden from Assertion */
180   public FluentBeanAssertion<T,R> setStdOut() {
181      super.setStdOut();
182      return this;
183   }
184
185   @Override /* Overridden from Assertion */
186   public FluentBeanAssertion<T,R> setThrowable(Class<? extends RuntimeException> value) {
187      super.setThrowable(value);
188      return this;
189   }
190
191   private BeanMap<T> toBeanMap() {
192      return BeanMap.of(value());
193   }
194}