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.*; 020import static java.util.Collections.*; 021import static java.util.stream.Collectors.*; 022import static org.apache.juneau.commons.utils.Utils.*; 023 024import java.io.*; 025import java.util.*; 026import java.util.function.*; 027 028import org.apache.juneau.commons.utils.*; 029import org.apache.juneau.cp.*; 030import org.apache.juneau.serializer.*; 031 032/** 033 * Used for fluent assertion calls against maps. 034 * 035 * <h5 class='section'>Test Methods:</h5> 036 * <p> 037 * <ul class='javatree'> 038 * <li class='jc'>{@link FluentMapAssertion} 039 * <ul class='javatreec'> 040 * <li class='jm'>{@link FluentMapAssertion#isEmpty() isEmpty()} 041 * <li class='jm'>{@link FluentMapAssertion#isNotEmpty() isNotEmpty()} 042 * <li class='jm'>{@link FluentMapAssertion#isContainsKey(String) isContainsKey(String)} 043 * <li class='jm'>{@link FluentMapAssertion#isNotContainsKey(String) isNotContainsKey(String)} 044 * <li class='jm'>{@link FluentMapAssertion#isSize(int) isSize(int)} 045 * </ul> 046 * <li class='jc'>{@link FluentObjectAssertion} 047 * <ul class='javatreec'> 048 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 049 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 050 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 051 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 052 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 053 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 054 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 055 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 056 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 057 * <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)} 058 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 059 * <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)} 060 * <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)} 061 * <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)} 062 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 063 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 064 * </ul> 065 * </ul> 066 * 067 * <h5 class='section'>Transform Methods:</h5> 068 * <p> 069 * <ul class='javatree'> 070 * <li class='jc'>{@link FluentMapAssertion} 071 * <ul class='javatreec'> 072 * <li class='jm'>{@link FluentMapAssertion#asValue(Object) asValue(Object)} 073 * <li class='jm'>{@link FluentMapAssertion#asValues(Object...) asValues(Object...)} 074 * <li class='jm'>{@link FluentMapAssertion#asValueMap(Object...) asValueMap(Object...)} 075 * <li class='jm'>{@link FluentMapAssertion#asSize() asSize()} 076 * </ul> 077 * <li class='jc'>{@link FluentObjectAssertion} 078 * <ul class='javatreec'> 079 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 080 * <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)} 081 * <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)} 082 * <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()} 083 * <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()} 084 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)} 085 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 086 * </ul> 087 * </ul> 088 * 089 * <h5 class='section'>Configuration Methods:</h5> 090 * <p> 091 * <ul class='javatree'> 092 * <li class='jc'>{@link Assertion} 093 * <ul class='javatreec'> 094 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 095 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 096 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 097 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 098 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 099 * </ul> 100 * </ul> 101 * 102 * <h5 class='section'>See Also:</h5><ul> 103 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem Overview</a> 104 * </ul> 105 * 106 * @param <K> The key type. 107 * @param <V> The value type. 108 * @param <R> The return type. 109 */ 110public class FluentMapAssertion<K,V,R> extends FluentObjectAssertion<Map<K,V>,R> { 111 112 // @formatter:off 113 private static final Messages MESSAGES = Messages.of(FluentMapAssertion.class, "Messages"); 114 private static final String 115 MSG_mapWasNotEmpty = MESSAGES.getString("mapWasNotEmpty"), 116 MSG_mapDidNotContainExpectedKey = MESSAGES.getString("mapDidNotContainExpectedKey"), 117 MSG_mapContainedUnexpectedKey = MESSAGES.getString("mapContainedUnexpectedKey"), 118 MSG_mapWasEmpty = MESSAGES.getString("mapWasEmpty"), 119 MSG_mapDidNotHaveTheExpectedSize = MESSAGES.getString("mapDidNotHaveTheExpectedSize"); 120 // @formatter:on 121 122 /** 123 * Chained constructor. 124 * 125 * <p> 126 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 127 * 128 * @param creator 129 * The assertion that created this assertion. 130 * <br>Should be <jk>null</jk> if this is the top-level assertion. 131 * @param value 132 * The object being tested. 133 * <br>Can be <jk>null</jk>. 134 * @param returns 135 * The object to return after a test method is called. 136 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 137 * used on the same assertion. 138 */ 139 public FluentMapAssertion(Assertion creator, Map<K,V> value, R returns) { 140 super(creator, value, returns); 141 } 142 143 /** 144 * Constructor. 145 * 146 * @param value 147 * The object being tested. 148 * <br>Can be <jk>null</jk>. 149 * @param returns 150 * The object to return after a test method is called. 151 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 152 * used on the same assertion. 153 */ 154 public FluentMapAssertion(Map<K,V> value, R returns) { 155 this(null, value, returns); 156 } 157 158 /** 159 * Returns an integer assertion on the size of this map. 160 * 161 * <p> 162 * If the map is <jk>null</jk>, the returned assertion is a null assertion 163 * (meaning {@link FluentIntegerAssertion#isExists()} returns <jk>false</jk>). 164 * 165 * @return A new assertion. 166 */ 167 public FluentIntegerAssertion<R> asSize() { 168 return new FluentIntegerAssertion<>(this, valueIsNull() ? null : value().size(), returns()); 169 } 170 171 @Override /* Overridden from FluentObjectAssertion */ 172 public FluentMapAssertion<K,V,R> asTransformed(Function<Map<K,V>,Map<K,V>> function) { // NOSONAR - Intentional. 173 return new FluentMapAssertion<>(this, function.apply(orElse(null)), returns()); 174 } 175 176 /** 177 * Returns an object assertion on the value specified at the specified key. 178 * 179 * <p> 180 * If the map is <jk>null</jk> or the map doesn't contain the specified key, the returned assertion is a null assertion 181 * (meaning {@link FluentAnyAssertion#isExists()} returns <jk>false</jk>). 182 * 183 * @param key The key of the item to retrieve from the map. 184 * @return A new assertion. 185 */ 186 public FluentAnyAssertion<V,R> asValue(K key) { 187 return new FluentAnyAssertion<>(this, get(key), returns()); 188 } 189 190 /** 191 * Extracts a subset of this map. 192 * 193 * @param keys The entries to extract. 194 * @return This object. 195 */ 196 @SuppressWarnings("unchecked") 197 public FluentMapAssertion<K,V,R> asValueMap(K...keys) { 198 if (valueIsNull()) 199 return new FluentMapAssertion<>(this, null, returns()); 200 Map<K,V> m1 = value(), m2 = CollectionUtils.map(); 201 if (nn(m1)) 202 for (var k : keys) 203 m2.put(k, m1.get(k)); 204 return new FluentMapAssertion<>(this, m2, returns()); 205 } 206 207 /** 208 * Returns a {@link FluentListAssertion} of the values of the specified keys. 209 * 210 * If the map is <jk>null</jk>, the returned assertion is a null assertion 211 * (meaning {@link FluentObjectAssertion#isExists()} returns <jk>false</jk>). 212 * 213 * @param keys The keys of the values to retrieve from the map. 214 * @return A new assertion. 215 */ 216 @SuppressWarnings("unchecked") 217 public FluentListAssertion<Object,R> asValues(K...keys) { 218 return new FluentListAssertion<>(this, valueIsNull() ? null : stream(keys).map(this::get).collect(toList()), returns()); 219 } 220 221 /** 222 * Asserts that the map contains the expected key. 223 * 224 * @param name The key name to check for. 225 * @return The fluent return object. 226 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 227 */ 228 public R isContainsKey(String name) throws AssertionError { 229 if (value().containsKey(name)) 230 return returns(); 231 throw error(MSG_mapDidNotContainExpectedKey, name, value()); 232 } 233 234 /** 235 * Asserts that the map exists and is empty. 236 * 237 * @return The fluent return object. 238 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 239 */ 240 public R isEmpty() throws AssertionError { 241 if (! value().isEmpty()) 242 throw error(MSG_mapWasNotEmpty); 243 return returns(); 244 } 245 246 /** 247 * Asserts that the map contains the expected key. 248 * 249 * @param name The key name to check for. 250 * @return The fluent return object. 251 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 252 */ 253 public R isNotContainsKey(String name) throws AssertionError { 254 if (! value().containsKey(name)) 255 return returns(); 256 throw error(MSG_mapContainedUnexpectedKey, name, value()); 257 } 258 259 /** 260 * Asserts that the map exists and is not empty. 261 * 262 * @return The fluent return object. 263 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 264 */ 265 public R isNotEmpty() throws AssertionError { 266 if (value().isEmpty()) 267 throw error(MSG_mapWasEmpty); 268 return returns(); 269 } 270 271 /** 272 * Asserts that the map exists and is the specified size. 273 * 274 * @param size The expected size. 275 * @return The fluent return object. 276 * @throws AssertionError If assertion failed or value was <jk>null</jk>. 277 */ 278 public R isSize(int size) throws AssertionError { 279 if (size2() != size) 280 throw error(MSG_mapDidNotHaveTheExpectedSize, size, size2()); 281 return returns(); 282 } 283 284 @Override /* Overridden from Assertion */ 285 public FluentMapAssertion<K,V,R> setMsg(String msg, Object...args) { 286 super.setMsg(msg, args); 287 return this; 288 } 289 290 @Override /* Overridden from Assertion */ 291 public FluentMapAssertion<K,V,R> setOut(PrintStream value) { 292 super.setOut(value); 293 return this; 294 } 295 296 @Override /* Overridden from Assertion */ 297 public FluentMapAssertion<K,V,R> setSilent() { 298 super.setSilent(); 299 return this; 300 } 301 302 @Override /* Overridden from Assertion */ 303 public FluentMapAssertion<K,V,R> setStdOut() { 304 super.setStdOut(); 305 return this; 306 } 307 308 @Override /* Overridden from Assertion */ 309 public FluentMapAssertion<K,V,R> setThrowable(Class<? extends java.lang.RuntimeException> value) { 310 super.setThrowable(value); 311 return this; 312 } 313 314 private V get(K key) { 315 return orElse(emptyMap()).get(key); 316 } 317 318 private int size2() { 319 return value().size(); 320 } 321}