package com.baeldung.map; import org.apache.commons.collections4.MultiMapUtils; import org.apache.commons.collections4.MultiSet; import org.apache.commons.collections4.MultiValuedMap; import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; import org.apache.commons.collections4.multimap.HashSetValuedHashMap; import org.junit.Test; import java.util.Arrays; import java.util.Collection; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; public class MultiValuedMapUnitTest { @Test public void givenMultiValuesMap_whenPuttingMultipleValuesUsingPutMethod_thenReturningAllValues() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); assertThat((Collection) map.get("fruits")).containsExactly("apple", "orange"); } @Test public void givenMultiValuesMap_whenPuttingMultipleValuesUsingPutAllMethod_thenReturningAllValues() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.putAll("vehicles", Arrays.asList("car", "bike")); assertThat((Collection) map.get("vehicles")).containsExactly("car", "bike"); } @Test public void givenMultiValuesMap_whenGettingValueUsingGetMethod_thenReturningValue() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); assertThat((Collection) map.get("fruits")).containsExactly("apple"); } @Test public void givenMultiValuesMap_whenUsingEntriesMethod_thenReturningMappings() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); Collection> entries = (Collection>) map.entries(); for(Map.Entry entry : entries) { assertThat(entry.getKey()).contains("fruits"); assertTrue(entry.getValue().equals("apple") || entry.getValue().equals("orange") ); } } @Test public void givenMultiValuesMap_whenUsingKeysMethod_thenReturningAllKeys() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); MultiSet keys = map.keys(); assertThat((keys)).contains("fruits", "vehicles"); } @Test public void givenMultiValuesMap_whenUsingKeySetMethod_thenReturningAllKeys() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); Set keys = map.keySet(); assertThat(keys).contains("fruits", "vehicles"); } @Test public void givenMultiValuesMap_whenUsingValuesMethod_thenReturningAllValues() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); assertThat(((Collection) map.values())).contains("apple", "orange", "car", "bike"); } @Test public void givenMultiValuesMap_whenUsingRemoveMethod_thenReturningUpdatedMap() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); assertThat(((Collection) map.values())).contains("apple", "orange", "car", "bike"); map.remove("fruits"); assertThat(((Collection) map.values())).contains("car", "bike"); } @Test public void givenMultiValuesMap_whenUsingRemoveMappingMethod_thenReturningUpdatedMapAfterMappingRemoved() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); assertThat(((Collection) map.values())).contains("apple", "orange", "car", "bike"); map.removeMapping("fruits", "apple"); assertThat(((Collection) map.values())).contains("orange", "car", "bike"); } @Test public void givenMultiValuesMap_whenUsingClearMethod_thenReturningEmptyMap() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); assertThat(((Collection) map.values())).contains("apple", "orange", "car", "bike"); map.clear(); assertTrue(map.isEmpty()); } @Test public void givenMultiValuesMap_whenUsingContainsKeyMethod_thenReturningTrue() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); assertTrue(map.containsKey("fruits")); } @Test public void givenMultiValuesMap_whenUsingContainsValueMethod_thenReturningTrue() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); assertTrue(map.containsValue("orange")); } @Test public void givenMultiValuesMap_whenUsingIsEmptyMethod_thenReturningFalse() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); assertFalse(map.isEmpty()); } @Test public void givenMultiValuesMap_whenUsingSizeMethod_thenReturningElementCount() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("vehicles", "car"); map.put("vehicles", "bike"); assertEquals(4, map.size()); } @Test public void givenArrayListValuedHashMap_whenPuttingDoubleValues_thenReturningAllValues() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); map.put("fruits", "orange"); assertThat((Collection) map.get("fruits")).containsExactly("apple", "orange", "orange"); } @Test public void givenHashSetValuedHashMap_whenPuttingTwiceTheSame_thenReturningOneValue() { MultiValuedMap map = new HashSetValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "apple"); assertThat((Collection) map.get("fruits")).containsExactly("apple"); } @Test(expected = UnsupportedOperationException.class) public void givenUnmodifiableMultiValuedMap_whenInserting_thenThrowingException() { MultiValuedMap map = new ArrayListValuedHashMap<>(); map.put("fruits", "apple"); map.put("fruits", "orange"); MultiValuedMap immutableMap = MultiMapUtils.unmodifiableMultiValuedMap(map); immutableMap.put("fruits", "banana"); } }