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 org.apache.juneau.internal.ObjectUtils.*; 016 017import java.io.*; 018import java.util.*; 019import java.util.function.*; 020import java.util.stream.*; 021 022import org.apache.juneau.common.internal.StringUtils; 023import org.apache.juneau.cp.*; 024import org.apache.juneau.internal.*; 025import org.apache.juneau.serializer.*; 026 027/** 028 * Used for fluent assertion calls against collections objects. 029 * 030 * <h5 class='section'>Test Methods:</h5> 031 * <p> 032 * <ul class='javatree'> 033 * <li class='jc'>{@link FluentCollectionAssertion} 034 * <ul class='javatreec'> 035 * <li class='jm'>{@link FluentCollectionAssertion#isEmpty() isEmpty()} 036 * <li class='jm'>{@link FluentCollectionAssertion#isNotEmpty() isNotEmpty()} 037 * <li class='jm'>{@link FluentCollectionAssertion#isContains(Object) isContains(Object)} 038 * <li class='jm'>{@link FluentCollectionAssertion#isNotContains(Object) isNotContains(Object)} 039 * <li class='jm'>{@link FluentCollectionAssertion#isAny(Predicate) isAny(Predicate)} 040 * <li class='jm'>{@link FluentCollectionAssertion#isAll(Predicate) isAll(Predicate)} 041 * <li class='jm'>{@link FluentCollectionAssertion#isSize(int size) isSize(int size)} 042 * </ul> 043 * <li class='jc'>{@link FluentObjectAssertion} 044 * <ul class='javatreec'> 045 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 046 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 047 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 048 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 049 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 050 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 051 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 052 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 053 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 054 * <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)} 055 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 056 * <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)} 057 * <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)} 058 * <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)} 059 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 060 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 061 * </ul> 062 * </ul> 063 * 064 * <h5 class='section'>Transform Methods:</h5> 065 * <p> 066 * <ul class='javatree'> 067 * <li class='jc'>{@link FluentCollectionAssertion} 068 * <ul class='javatreec'> 069 * <li class='jm'>{@link FluentCollectionAssertion#asStrings() asStrings()} 070 * <li class='jm'>{@link FluentCollectionAssertion#asSize() asSize()} 071 * </ul> 072 * <li class='jc'>{@link FluentObjectAssertion} 073 * <ul class='javatreec'> 074 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 075 * <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)} 076 * <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)} 077 * <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()} 078 * <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()} 079 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)} 080 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 081 * </ul> 082 * </ul> 083 * 084 * <h5 class='section'>Configuration Methods:</h5> 085 * <p> 086 * <ul class='javatree'> 087 * <li class='jc'>{@link Assertion} 088 * <ul class='javatreec'> 089 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 090 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 091 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 092 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 093 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 094 * </ul> 095 * </ul> 096 * 097 * <h5 class='section'>See Also:</h5><ul> 098 * <li class='link'><a class="doclink" href="../../../../index.html#ja.Overview">Overview > juneau-assertions > Overview</a> 099 * </ul> 100 * 101 * @param <E> The element type. 102 * @param <R> The return type. 103 */ 104@FluentSetters(returns="FluentCollectionAssertion<E,R>") 105public class FluentCollectionAssertion<E,R> extends FluentObjectAssertion<Collection<E>,R> { 106 107 //----------------------------------------------------------------------------------------------------------------- 108 // Static 109 //----------------------------------------------------------------------------------------------------------------- 110 111 private static final Messages MESSAGES = Messages.of(FluentCollectionAssertion.class, "Messages"); 112 private static final String 113 MSG_collectionWasNotEmpty = MESSAGES.getString("collectionWasNotEmpty"), 114 MSG_collectionDidNotContainExpectedValue = MESSAGES.getString("collectionDidNotContainExpectedValue"), 115 MSG_collectionDidNotContainTestedValue = MESSAGES.getString("collectionDidNotContainTestedValue"), 116 MSG_collectionContainedUnexpectedValue = MESSAGES.getString("collectionContainedUnexpectedValue"), 117 MSG_collectionWasEmpty = MESSAGES.getString("collectionWasEmpty"), 118 MSG_collectionDidNotHaveExpectedSize = MESSAGES.getString("collectionDidNotHaveExpectedSize"); 119 120 //----------------------------------------------------------------------------------------------------------------- 121 // Instance 122 //----------------------------------------------------------------------------------------------------------------- 123 124 /** 125 * Constructor. 126 * 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 FluentCollectionAssertion(Collection<E> value, R returns) { 136 this(null, value, returns); 137 } 138 139 /** 140 * Chained constructor. 141 * 142 * <p> 143 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 144 * 145 * @param creator 146 * The assertion that created this assertion. 147 * <br>Should be <jk>null</jk> if this is the top-level assertion. 148 * @param value 149 * The object being tested. 150 * <br>Can be <jk>null</jk>. 151 * @param returns 152 * The object to return after a test method is called. 153 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 154 * used on the same assertion. 155 */ 156 public FluentCollectionAssertion(Assertion creator, Collection<E> value, R returns) { 157 super(creator, value, returns); 158 } 159 160 //----------------------------------------------------------------------------------------------------------------- 161 // Transform methods 162 //----------------------------------------------------------------------------------------------------------------- 163 164 @Override /* FluentObjectAssertion */ 165 public FluentCollectionAssertion<E,R> asTransformed(Function<Collection<E>,Collection<E>> function) { 166 return new FluentCollectionAssertion<>(this, function.apply(orElse(null)), returns()); 167 } 168 169 /** 170 * Converts this assertion into a {@link FluentListAssertion} of strings. 171 * 172 * @return A new fluent string assertion. 173 */ 174 public FluentStringListAssertion<R> asStrings() { 175 return new FluentStringListAssertion<>(this, valueIsNull() ? null : value().stream().map(StringUtils::stringify).collect(Collectors.toList()), returns()); 176 } 177 178 179 /** 180 * Returns an integer assertion on the size of this collection. 181 * 182 * <p> 183 * If the collection is <jk>null</jk>, the returned assertion is a null assertion 184 * (meaning {@link FluentIntegerAssertion#isExists()} returns <jk>false</jk>). 185 * 186 * @return A new assertion. 187 */ 188 public FluentIntegerAssertion<R> asSize() { 189 return new FluentIntegerAssertion<>(this, valueIsNull() ? null : value().size(), returns()); 190 } 191 192 //----------------------------------------------------------------------------------------------------------------- 193 // Test methods 194 //----------------------------------------------------------------------------------------------------------------- 195 196 /** 197 * Asserts that the collection exists and is empty. 198 * 199 * @return The fluent return object. 200 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 201 */ 202 public R isEmpty() throws AssertionError { 203 if (! value().isEmpty()) 204 throw error(MSG_collectionWasNotEmpty); 205 return returns(); 206 } 207 208 /** 209 * Asserts that the collection exists and is not empty. 210 * 211 * @return The fluent return object. 212 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 213 */ 214 public R isNotEmpty() throws AssertionError { 215 if (value().isEmpty()) 216 throw error(MSG_collectionWasEmpty); 217 return returns(); 218 } 219 220 /** 221 * Asserts that the collection contains the expected value. 222 * 223 * @param entry The value to check for. 224 * @return The fluent return object. 225 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 226 */ 227 public R isContains(E entry) throws AssertionError { 228 for (Object v : value()) 229 if (eq(v, entry)) 230 return returns(); 231 throw error(MSG_collectionDidNotContainExpectedValue, entry, value()); 232 } 233 234 /** 235 * Asserts that the collection contains the expected value. 236 * 237 * @param entry The value to check for. 238 * @return The fluent return object. 239 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 240 */ 241 public R isNotContains(E entry) throws AssertionError { 242 value().forEach(x -> { 243 if (eq(x, entry)) 244 throw error(MSG_collectionContainedUnexpectedValue, entry, value()); 245 }); 246 return returns(); 247 } 248 249 /** 250 * Asserts that at least one value in the collection passes the specified test. 251 * 252 * @param test The predicate test. 253 * @return The fluent return object. 254 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 255 */ 256 public R isAny(Predicate<E> test) throws AssertionError { 257 if (test == null) 258 return returns(); 259 for (E v : value()) 260 if (test.test(v)) 261 return returns(); 262 throw error(MSG_collectionDidNotContainTestedValue, value()); 263 } 264 265 /** 266 * Asserts that all values in the collection pass the specified test. 267 * 268 * @param test The predicate test. 269 * @return The fluent return object. 270 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 271 */ 272 public R isAll(Predicate<E> test) throws AssertionError { 273 if (test == null) 274 return returns(); 275 value().forEach(x -> { 276 if (! test.test(x)) 277 throw error(MSG_collectionDidNotContainTestedValue, value()); 278 }); 279 return returns(); 280 } 281 282 /** 283 * Asserts that the collection exists and is the specified size. 284 * 285 * @param size The expected size. 286 * @return The fluent return object. 287 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 288 */ 289 public R isSize(int size) throws AssertionError { 290 if (getSize() != size) 291 throw error(MSG_collectionDidNotHaveExpectedSize, size, getSize()); 292 return returns(); 293 } 294 295 //----------------------------------------------------------------------------------------------------------------- 296 // Fluent setters 297 //----------------------------------------------------------------------------------------------------------------- 298 299 // <FluentSetters> 300 301 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 302 public FluentCollectionAssertion<E,R> setMsg(String msg, Object...args) { 303 super.setMsg(msg, args); 304 return this; 305 } 306 307 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 308 public FluentCollectionAssertion<E,R> setOut(PrintStream value) { 309 super.setOut(value); 310 return this; 311 } 312 313 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 314 public FluentCollectionAssertion<E,R> setSilent() { 315 super.setSilent(); 316 return this; 317 } 318 319 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 320 public FluentCollectionAssertion<E,R> setStdOut() { 321 super.setStdOut(); 322 return this; 323 } 324 325 @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ 326 public FluentCollectionAssertion<E,R> setThrowable(Class<? extends java.lang.RuntimeException> value) { 327 super.setThrowable(value); 328 return this; 329 } 330 331 // </FluentSetters> 332 333 //----------------------------------------------------------------------------------------------------------------- 334 // Utility methods 335 //----------------------------------------------------------------------------------------------------------------- 336 337 /** 338 * Returns the size of this collection if it is not <jk>null</jk>. 339 * 340 * @return the size of this collection if it is not <jk>null</jk>. 341 * @throws AssertionError If value was <jk>null</jk>. 342 */ 343 protected int getSize() throws AssertionError { 344 return value().size(); 345 } 346}