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