How to use failIfEmpty method of org.assertj.core.internal.Maps class

Best Assertj code snippet using org.assertj.core.internal.Maps.failIfEmpty

Source:Maps.java Github

copy

Full Screen

...35import static org.assertj.core.error.ShouldNotContain.shouldNotContain;36import static org.assertj.core.error.ShouldNotContainKeys.shouldNotContainKeys;37import static org.assertj.core.error.ShouldNotContainValue.shouldNotContainValue;38import static org.assertj.core.internal.Arrays.assertIsArray;39import static org.assertj.core.internal.CommonValidations.failIfEmptySinceActualIsNotEmpty;40import static org.assertj.core.internal.CommonValidations.hasSameSizeAsCheck;41import static org.assertj.core.util.Objects.areEqual;42import static org.assertj.core.util.Preconditions.checkArgument;43import static org.assertj.core.util.Preconditions.checkNotNull;44import static org.assertj.vavr.api.ShouldNotContainValues.shouldNotContainValues;45public final class Maps {46 private static final Maps INSTANCE = new Maps();47 private Failures failures = Failures.instance();48 private Conditions conditions = Conditions.instance();49 private Maps() {}50 public static Maps instance() {51 return INSTANCE;52 }53 /**54 * Verifies that the given {@code Map} contains the value for given {@code key} that satisfy given {@code valueCondition}.55 *56 * @param <K> key type57 * @param <V> value type58 * @param info contains information about the assertion59 * @param actual the given {@code Map}60 * @param key the given key to check61 * @param valueCondition the given condition for check value62 * @throws NullPointerException if the given values is {@code null}63 * @throws AssertionError if the actual map is {@code null}64 * @throws AssertionError if the actual map does not contain the given {@code key}65 * @throws AssertionError if the actual map contains the given key, but value does not match the given {@code valueCondition}66 */67 @SuppressWarnings("unchecked")68 public <K, V> void assertHasEntrySatisfying(AssertionInfo info, Map<K, V> actual, K key,69 Condition<? super V> valueCondition) {70 conditions.assertIsNotNull(valueCondition);71 assertContainsKeys(info, actual, key);72 Option<V> value = actual.get(key);73 value74 .filter(valueCondition::matches)75 .getOrElseThrow(() -> failures.failure(info, elementsShouldBe(actual, value, valueCondition)));76 }77 public <K, V> void assertContainsAnyOf(AssertionInfo info, Map<K, V> actual,78 Tuple2<K, V>[] entries) {79 doCommonContainsCheck(info, actual, entries);80 if (actual.isEmpty() && entries.length == 0) return;81 failIfEmptySinceActualIsNotEmpty(entries);82 for (Tuple2<? extends K, ? extends V> entry : entries) {83 if (containsEntry(actual, entry)) return;84 }85 throw failures.failure(info, ShouldContainAnyOf.shouldContainAnyOf(actual, entries));86 }87 /**88 * Asserts that the given {@code Map} contains the given entries, in any order.89 *90 * @param <K> key type91 * @param <V> value type92 * @param info contains information about the assertion93 * @param actual the given {@code Map}94 * @param entries the entries that are expected to be in the given {@code Map}95 * @throws NullPointerException if the array of entries is {@code null}96 * @throws IllegalArgumentException if the array of entries is empty97 * @throws NullPointerException if any of the entries in the given array is {@code null}98 * @throws AssertionError if the given {@code Map} is {@code null}99 * @throws AssertionError if the given {@code Map} does not contain the given entries100 */101 public <K, V> void assertContains(AssertionInfo info, Map<K, V> actual,102 Tuple2<K, V>[] entries) {103 doCommonContainsCheck(info, actual, entries);104 if (actual.isEmpty() && entries.length == 0) return;105 failIfEmptySinceActualIsNotEmpty(entries);106 final Set<Tuple2<K, V>> notFound = Array.of(entries).filter(entryNotPresentIn(actual)).toSet();107 if (isNotEmpty(notFound)) {108 throw failures.failure(info, shouldContain(actual, entries, notFound));109 }110 }111 /**112 * Asserts that the given {@code Map} does not contain the given entries.113 *114 * @param <K> key type115 * @param <V> value type116 * @param info contains information about the assertion117 * @param actual the given {@code Map}118 * @param entries the entries that are expected to be in the given {@code Map}119 * @throws NullPointerException if the array of entries is {@code null}120 * @throws IllegalArgumentException if the array of entries is empty121 * @throws NullPointerException if any of the entries in the given array is {@code null}122 * @throws AssertionError if the given {@code Map} is {@code null}123 * @throws AssertionError if the given {@code Map} contains any of the given entries124 */125 public <K, V> void assertDoesNotContain(AssertionInfo info, Map<K, V> actual,126 Tuple2<K, V>[] entries) {127 failIfNullOrEmpty(entries);128 assertNotNull(info, actual);129 failIfEmptySinceActualIsNotEmpty(entries);130 final Set<Tuple2<K, V>> found = Array.of(entries).filter(actual::contains).toSet();131 if (isNotEmpty(found)) {132 throw failures.failure(info, shouldNotContain(actual, entries, found));133 }134 }135 /**136 * Verifies that the actual {@code Map} contains the given keys.137 *138 * @param <K> key type139 * @param <V> value type140 * @param info contains information about the assertion141 * @param actual the given {@code Map}142 * @param keys the given keys143 * @throws NullPointerException if the array of keys is {@code null}144 * @throws IllegalArgumentException if the array of keys is empty145 * @throws AssertionError if the given {@code Map} is {@code null}146 * @throws AssertionError if the given {@code Map} does not contain the given keys147 */148 public <K, V> void assertContainsKeys(AssertionInfo info, Map<K, V> actual,149 @SuppressWarnings("unchecked") K... keys) {150 doCommonContainsCheck(info, actual, keys);151 if (doCommonEmptinessChecks(actual, keys)) return;152 Set<K> expected = HashSet.of(keys);153 Set<K> notFound = expected.filter(keyNotPresentIn(actual.keySet()));154 if (isNotEmpty(notFound)) {155 throw failures.failure(info, shouldContainKeys(actual, notFound.toJavaSet()));156 }157 }158 /**159 * Verifies that the actual {@code Map} does not contain the given keys.160 *161 * @param <K> key type162 * @param <V> value type163 * @param info contains information about the assertion164 * @param actual the given {@code Map}165 * @param keys the given keys166 * @throws NullPointerException if the array of keys is {@code null}167 * @throws IllegalArgumentException if the array of keys is empty168 * @throws AssertionError if the given {@code Map} is {@code null}169 * @throws AssertionError if the given {@code Map} contains the given keys170 */171 public <K, V> void assertDoesNotContainKeys(AssertionInfo info, Map<K, V> actual,172 @SuppressWarnings("unchecked") K... keys) {173 doCommonContainsCheck(info, actual, keys);174 if (doCommonEmptinessChecks(actual, keys)) return;175 Set<K> expected = HashSet.of(keys);176 Set<K> found = expected.filter(keyPresentIn(actual.keySet()));177 if (isNotEmpty(found)) {178 throw failures.failure(info, shouldNotContainKeys(actual, found.toJavaSet()));179 }180 }181 /**182 * Asserts that the given {@code Map} contains the given entries only.183 *184 * @param <K> key type185 * @param <V> value type186 * @param info contains information about the assertion187 * @param actual the given {@code Map}188 * @param entries the entries that are expected to only be in the given {@code Map}189 * @throws AssertionError if the array of entries is {@code null}190 * @throws AssertionError if the array of entries is empty191 * @throws NullPointerException if any of the entries in the given array is {@code null}192 * @throws AssertionError if the given {@code Map} is {@code null}193 * @throws AssertionError if the given {@code Map} contains any of the given entries194 */195 public <K, V> void assertContainsOnly(AssertionInfo info, Map<K, V> actual, Iterable<Tuple2<K, V>> entries) {196 assertNotNull(info, actual);197 failIfNull(entries);198 if (actual.isEmpty() && !entries.iterator().hasNext()) return;199 failIfEmpty(entries);200 Map<K, V> expected = HashMap.ofEntries(entries);201 Map<K, V> notExpected = actual.filter(entryNotPresentIn(expected));202 if (isNotEmpty(notExpected)) {203 Map<K, V> notFound = expected.filter(entryNotPresentIn(actual));204 throw failures.failure(info, shouldContainOnly(actual, expected, notFound, notExpected));205 }206 }207 /**208 * Verifies that the actual map contains only the given entries and nothing else, <b>in order</b>.<br>209 * This assertion should only be used with map that have a consistent iteration order (i.e. don't use it with210 * {@link io.vavr.collection.HashMap}).211 *212 * @param <K> key type213 * @param <V> value type214 * @param info contains information about the assertion215 * @param actual the given {@code Map}216 * @param entries the given entries217 * @throws NullPointerException if the given entries array is {@code null}218 * @throws AssertionError if the actual map is {@code null}219 * @throws IllegalArgumentException if the given entries array is empty220 * @throws AssertionError if the actual map does not contain the given entries with same order, i.e. the actual map221 * contains some or none of the given entries, or the actual map contains more entries than the given ones222 * or entries are the same but the order is not223 */224 public <K, V> void assertContainsExactly(AssertionInfo info, Map<K, V> actual,225 @SuppressWarnings("unchecked") Tuple2<? extends K, ? extends V>... entries) {226 doCommonContainsCheck(info, actual, entries);227 if (actual.isEmpty() && entries.length == 0) return;228 failIfEmpty(entries);229 assertHasSameSizeAs(info, actual, entries);230 final Map<K, V> expectedEntries = asLinkedMap(entries);231 final Map<K, V> notExpected = actual.filter(entry -> !expectedEntries.contains(entry));232 final Map<K, V> notFound = expectedEntries.filter(entry -> !actual.contains(entry));233 if (notExpected.isEmpty() && notFound.isEmpty()) {234 // check entries order235 int index = 0;236 for (K keyFromActual : actual.keySet()) {237 if (areNotEqual(keyFromActual, entries[index]._1)) {238 Tuple2<K, V> actualEntry = Tuple.of(keyFromActual, actual.get(keyFromActual).get());239 throw failures.failure(info, elementsDifferAtIndex(actualEntry, entries[index], index));240 }241 index++;242 }243 // all entries are in the same order.244 return;245 }246 throw failures.failure(info, shouldContainExactly(actual, List.of(entries), notFound, notExpected));247 }248 /**249 * Asserts that the given {@code Map} contains the given keys, in any order.250 *251 * @param <K> key type252 * @param <V> value type253 * @param info contains information about the assertion254 * @param actual the given {@code Map}255 * @param keys the keys that are expected to be in the given {@code Map}256 * @throws NullPointerException if the array of keys is {@code null}257 * @throws IllegalArgumentException if the array of keys is empty258 * @throws AssertionError if the given {@code Map} is {@code null}259 * @throws AssertionError if the given {@code Map} does not contain the given keys260 */261 public <K, V> void assertContainsOnlyKeys(AssertionInfo info, Map<K, V> actual, K[] keys) {262 doCommonContainsCheck(info, actual, keys);263 if (doCommonEmptinessChecks(actual, keys)) return;264 Set<K> expected = HashSet.of(keys);265 Set<K> notExpected = actual.keySet().filter(keyNotPresentIn(expected));266 if (isNotEmpty(notExpected)) {267 Set<K> notFound = expected.filter(keyNotPresentIn(actual.keySet()));268 throw failures.failure(info, shouldContainOnlyKeys(actual, expected, notFound, notExpected));269 }270 }271 /**272 * Verifies that the actual map contains the given values.273 *274 * @param <K> key type275 * @param <V> value type276 * @param info contains information about the assertion277 * @param actual the given {@code Map}278 * @param values the given values279 * @throws AssertionError if the actual map is {@code null}280 * @throws AssertionError if the actual map does not contain the given values281 * @throws NullPointerException if values vararg is {@code null}282 */283 public <K, V> void assertContainsValues(AssertionInfo info, Map<K, V> actual,284 @SuppressWarnings("unchecked") V... values) {285 assertNotNull(info, actual);286 checkNotNull(values, "The array of values to look for should not be null");287 if (actual.isEmpty() && values.length == 0) return;288 Set<V> expected = HashSet.of(values);289 Set<V> notFound = expected.filter(valueNotPresentIn(actual.values()));290 if (isNotEmpty(notFound)) throw failures.failure(info, shouldContainValues(actual, notFound.toJavaSet()));291 }292 /**293 * Verifies that the actual map does not contain the given values.294 *295 * @param <K> key type296 * @param <V> value type297 * @param info contains information about the assertion298 * @param actual the given {@code Map}299 * @param values the given values300 * @throws AssertionError if the actual map is {@code null}301 * @throws AssertionError if the actual map contains the given values302 * @throws NullPointerException if values vararg is {@code null}303 */304 public <K, V> void assertDoesNotContainValues(AssertionInfo info, Map<K, V> actual,305 @SuppressWarnings("unchecked") V... values) {306 assertNotNull(info, actual);307 checkNotNull(values, "The array of values to look for should not be null");308 if (actual.isEmpty() && values.length == 0) return;309 Set<V> expected = HashSet.of(values);310 Set<V> found = expected.filter(valuePresentIn(actual.values()));311 if (isNotEmpty(found)) throw failures.failure(info, shouldNotContainValues(actual, found.toJavaSet()));312 }313 /**314 * Verifies that the actual map contains the given value.315 *316 * @param <K> key type317 * @param <V> value type318 * @param info contains information about the assertion319 * @param actual the given {@code Map}320 * @param value the given value321 * @throws AssertionError if the actual map is {@code null}322 * @throws AssertionError if the actual map does not contain the given value323 */324 public <K, V> void assertContainsValue(AssertionInfo info, Map<K, V> actual, V value) {325 assertNotNull(info, actual);326 if (!actual.containsValue(value)) throw failures.failure(info, shouldContainValue(actual, value));327 }328 /**329 * Verifies that the actual map does not contain the given value.330 *331 * @param <K> key type332 * @param <V> value type333 * @param info contains information about the assertion334 * @param actual the given {@code Map}335 * @param value the given value336 * @throws AssertionError if the actual map is {@code null}337 * @throws AssertionError if the actual map contains the given value338 */339 public <K, V> void assertDoesNotContainValue(AssertionInfo info, Map<K, V> actual, V value) {340 assertNotNull(info, actual);341 if (actual.containsValue(value)) throw failures.failure(info, shouldNotContainValue(actual, value));342 }343 /**344 * Asserts that the number of entries in the given {@code Map} has the same size as the other array.345 *346 * @param info contains information about the assertion347 * @param map the given {@code Map}348 * @param other the group to compare349 * @throws AssertionError if the given {@code Map} is {@code null}350 * @throws AssertionError if the given array is {@code null}351 * @throws AssertionError if the number of entries in the given {@code Map} does not have the same size352 */353 public void assertHasSameSizeAs(AssertionInfo info, Map<?, ?> map, Object other) {354 assertNotNull(info, map);355 assertIsArray(info, other);356 hasSameSizeAsCheck(info, map, other, map.size());357 }358 private <K, V> void doCommonContainsCheck(AssertionInfo info, Map<K, V> actual,359 Tuple2<? extends K, ? extends V>[] entries) {360 assertNotNull(info, actual);361 failIfNull(entries);362 }363 private <K, V> void doCommonContainsCheck(AssertionInfo info, Map<K, V> actual, K[] keys) {364 assertNotNull(info, actual);365 failIfNull(keys);366 }367 private <K, V> boolean doCommonEmptinessChecks(Map<K, V> actual, K[] keys) {368 if (actual.isEmpty() && keys.length == 0) {369 return true;370 }371 failIfEmpty(keys);372 return false;373 }374 private <K, V> boolean containsEntry(Map<K, V> actual, Tuple2<? extends K, ? extends V> entry) {375 checkNotNull(entry, "Entries to look for should not be null");376 return actual.containsKey(entry._1) && areEqual(actual.get(entry._1).get(), entry._2);377 }378 private static <K, V> void failIfEmpty(Tuple2<? extends K, ? extends V>[] entries) {379 checkArgument(entries.length > 0, "The array of entries to look for should not be empty");380 }381 private static <K, V> void failIfEmpty(Iterable<Tuple2<K, V>> entries) {382 checkArgument(entries.iterator().hasNext(), "The entries to look for should not be empty");383 }384 private static <K> void failIfEmpty(K[] keys) {385 checkArgument(keys.length > 0, "The array of keys to look for should not be empty");386 }387 private static <K, V> void failIfNullOrEmpty(Tuple2<? extends K, ? extends V>[] entries) {388 failIfNull(entries);389 failIfEmpty(entries);390 }391 private static <K, V> void failIfNull(Tuple2<? extends K, ? extends V>[] entries) {392 checkNotNull(entries, "The array of entries to look for should not be null");393 }394 private static <K, V> void failIfNull(Iterable<Tuple2<K, V>> entries) {395 checkNotNull(entries, "The entries to look for should not be null");396 }397 private static <K> void failIfNull(K[] keys) {398 checkNotNull(keys, "The array of keys to look for should not be null");399 }400 private static <K> boolean areNotEqual(K actualKey, K expectedKey) {401 return !areEqual(actualKey, expectedKey);402 }403 private static void assertNotNull(AssertionInfo info, Map<?, ?> actual) {...

Full Screen

Full Screen

failIfEmpty

Using AI Code Generation

copy

Full Screen

1package com.baeldung.assertj;2import static org.assertj.core.api.Assertions.assertThat;3import static org.assertj.core.api.Assertions.fail;4import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;5import static org.assertj.core.api.Assertions.failIfEmpty;6import java.util.HashMap;7import java.util.Map;8import org.assertj.core.api.SoftAssertions;9import org.junit.Test;10public class AssertJAssertionTest {11 public void givenMap_whenEmpty_thenThrowException() {12 try {13 Map<String, String> map = new HashMap<>();14 failIfEmpty(map);15 fail("Should have thrown an exception");16 } catch (AssertionError e) {17 assertThat(e).hasMessage("The map should not be empty");18 }19 }20 public void givenMap_whenEmpty_thenThrowExceptionWithAssertJ() {21 Map<String, String> map = new HashMap<>();22 try {23 failIfEmpty(map);24 failBecauseExceptionWasNotThrown(AssertionError.class);25 } catch (AssertionError e) {26 assertThat(e).hasMessage("The map should not be empty");27 }28 }29 public void givenMap_whenEmpty_thenThrowExceptionWithAssertJ2() {30 Map<String, String> map = new HashMap<>();31 try {32 failIfEmpty(map);33 failBecauseExceptionWasNotThrown(AssertionError.class);34 } catch (AssertionError e) {35 assertThat(e).hasMessage("The map should not be empty");36 }37 }38 public void givenMap_whenEmpty_thenThrowExceptionWithAssertJ3() {39 Map<String, String> map = new HashMap<>();40 try {41 failIfEmpty(map);42 failBecauseExceptionWasNotThrown(AssertionError.class);43 } catch (AssertionError e) {44 assertThat(e).hasMessage("The map should not be empty");45 }46 }47 public void givenMap_whenEmpty_thenThrowExceptionWithAssertJ4() {48 Map<String, String> map = new HashMap<>();49 try {50 failIfEmpty(map);51 failBecauseExceptionWasNotThrown(AssertionError.class);52 } catch (AssertionError e) {53 assertThat(e).hasMessage("The map should not be empty");54 }55 }56 public void givenMap_whenEmpty_thenThrowExceptionWithAssertJ5() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful