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