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