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